TypeError: Не удалось преобразовать объект типа <class 'generator'> в Tensor - PullRequest
0 голосов
/ 02 мая 2018

Я использую tensorflow 1.8 для программирования механизма машинного обучения. Есть два файла с данными в формате indices.csv и wordvecs.csv соответственно, выглядит так

1
4
2,5
3
5,2
0,4
2
3,0,5

и

-0.12345059557113995,0.03371361228990585,0.07759442044713848,-0.09351239346779948,-0.1007286056888656,-0.140748735915854
0.014213571292471555,-0.06387983193416157,0.05330171478705643,0.04305347066760018,0.17353564121812493,-0.08545822424701655
0.1115924103860661,0.028647204721581814,-0.014493976391447934,0.05366135474527653,0.038774950757834666,-0.003819841629195327
-0.10699382939890856,0.0757988325252639,0.0664057529693505,0.16989582016940627,0.01006690468518734,-0.06258552603067982
0.17102787491848018,-0.030614539321636897,-0.15359844987724858,0.0011099140388563188,0.10147370595637244,0.1199556215310892
-0.03879720602129771,-0.013373188250013131,-0.1825945755595128,0.1323576505819509,0.05330472471776107,-0.007759040777900835
-0.0014810406798866748,-0.06939957708730923,-0.0811730614526404,0.10849800927723016,0.08195334952551664,-0.03027921767992948
0.10481953484731303,0.05268307571788979,0.06041322422781497,0.026406965699539774,0.10850691186246961,-0.08561693977032292

Я загружаю все в память, используя эти два блока кода

# loading indices
indices = []
with open('../data/indices.csv', 'r', newline='') as f:
    content = csv.reader(f)
    indices = [list(map(int, row)) for row in content]
print('Finish loading indices.csv\n')

# loading wordvecs
wordvecs = []
with open('../data/wordvecs.csv', 'r', newline='') as f:
    content = csv.reader(f)
    wordvecs = [list(map(float, row)) for row in content]
print('Finish loading wordvecs.csv\n')

Затем я определяю computation graph

print('Defining graph')
ten_variables = tf.Variable(wordvecs, name='my_variable') # create variable with initial values from wordvecs
ten_indices = [tf.constant(e) for e in indices] # convert indices into tf.constant
ten_wordvecs = tf.stack([tf.reduce_mean(tf.gather(ten_variables, index) for index in ten_indices)]) # reduce the data inside ten_variables and stack it

Наконец, я определяю section для выполнения графика. Но, прежде чем дойти до section, я получил следующую ошибку в строке ten_wordvecs = tf.stack([tf.reduce_mean(tf.gather(ten_variables, index) for index in ten_indices)])

TypeError: Failed to convert object of type <class 'generator'> to Tensor. Contents: <generator object <genexpr> at 0x00000149E2C414C0>. Consider casting elements to a supported type.

Что не так с моим кодом?

1 Ответ

0 голосов
/ 02 мая 2018

Вы, вероятно, пропустили один ")"

Попытка:

tf.stack([tf.reduce_mean(tf.gather(ten_variables, index)) for index in ten_indices])

вместо

tf.stack([tf.reduce_mean(tf.gather(ten_variables, index) for index in ten_indices)])

Обычно полезно разбить эти сложные строки на отдельные инструкции и отладить их по одной.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...