张量流:"TypeError: Expected int32, got list containing Tensors of type '_Message' instead"



当我运行以下代码时:

import tensorflow as tf
pivot = tf.constant([1, 2])
my_ones = tf.ones([2, 3])
padded = tf.pad(my_ones, [[pivot[0], 0], [1,1]])
sess = tf.Session()
init_op = tf.initialize_all_variables()
sess.run(init_op)
my_ones_var = sess.run(padded)
print("my_ones, ", my_ones_var)

我在包含tf.pad():的线路上收到一个错误

TypeError: Expected int32, got list containing Tensors of type '_Message' instead

我怎样才能做到这一点?

TL;DR:您目前无法将张量参数定义为包含张量的列表,因此您必须手动构建填充。

此错误发生在以下行:

padded = tf.pad(my_ones, [[pivot[0], 0], [1,1]])

失败的原因是pivot[0]tf.Tensortf.pad()的第二个参数需要tf.Tensor,而TensorFlow当前没有将包含tf.Tensor对象的列表转换为新的张量。解决方法是使用tf.pack()手动构建填充张量:

paddings = tf.pack([tf.pack([pivot[0], 0]), [1, 1]])
padded = tf.pad(my_ones, paddings)

我们正在研究自动发生这种情况的方法,这样你的原创作品就可以发挥作用。


编辑:现在支持自动打包(自TensorFlow 0.9以来),因此以下代码现在可以工作:

padded = tf.pad(my_ones, [[pivot[0], 0], [1, 1]])

hi所有问题都是由于Keras版本引起的。最重要的是,我尝试了,但没有成功。卸载Keras并通过pip进行安装。它对我有效。

我在Keras 1.0.2&使用Keras 1.2.0 解决

希望这会有所帮助。感谢

您只需要使用tf.concat 更新所有行

例如

means=范围(k)中c的tf.concat(0,[tf.reducede_mean(tf.gate(vectors,tf.reforme(tf.where(tf.equal(assignments,c)),[1,-1]),reduction_indexs=[1])

应该改为

means=tf.concat([tf.reducede_mean(tf.gate(vectors,tf.reform(tf.where(tf.equal(assignments,c)),[1,-1]),reduction_indexs=[1]),范围(k)中的c,0)

相关内容

最新更新