Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:未能将类型为的对象转换为张量_Python_Tensorflow - Fatal编程技术网

Python TypeError:未能将类型为的对象转换为张量

Python TypeError:未能将类型为的对象转换为张量,python,tensorflow,Python,Tensorflow,我使用tensorflow 1.8编写机器学习框架。有两个文件包含的数据格式分别为index.csv和wordvecs.csv,如下所示 1 4 2,5 3 5,2 0,4 2 3,0,5 及 我使用这两块代码将所有内容加载到内存中 # loading indices indices = [] with open('../data/indices.csv', 'r', newline='') as f: content = csv.reader(f) indices = [li

我使用tensorflow 1.8编写机器学习框架。有两个文件包含的数据格式分别为index.csv和wordvecs.csv,如下所示

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

我使用这两块代码将所有内容加载到内存中

# 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')
然后,我定义了计算图

最后,我定义了一个部分来执行该图。但是,在到达该部分之前,我在第十行ten_wordvecs=tf.stack[tf.reduce_meanstf.gatherten_变量,十个索引中的索引]中得到了以下错误


我的代码有什么问题?

您可能错过了一个

尝试:

而不是

tf.stack([tf.reduce_mean(tf.gather(ten_variables, index) for index in ten_indices)])
将这些复杂的行拆分为单个指令并逐个调试通常是有帮助的

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
TypeError: Failed to convert object of type <class 'generator'> to Tensor. Contents: <generator object <genexpr> at 0x00000149E2C414C0>. Consider casting elements to a supported type.
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)])