张量流形状问题在一个非常简单的全标量情况下



尽管所有内容都符合标量,但此SSCCE仍会生成形状错误:

import tensorflow as tf
x = tf.placeholder(tf.float32, shape=[1], name='x')
y = tf.add(x, 1.0)
feed = dict()
feed[x] = 0.0
with tf.Session() as sess:
print('Simple:', sess.run(y, feed_dict=feed))
# ValueError: Cannot feed value of shape () for Tensor 'x:0', which has shape '(1,)'

此代码中的缺陷是什么?将 1.0 包装在 tf.convert_to_tensor 中不起作用。

我认为您可能希望使占位符具有 (( 的形状,即tf.placeholder(dtype=tf.float32, shape=(), name='x').

最新更新