我使用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.
我的代码出了什么问题?
答案 0 :(得分:1)
你可能错过了一个&#34;)&#34;
尝试:
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)])
将这些复杂的行拆分为单个指令并逐个进行调试通常很有帮助。