0

I am getting confused in how to use placeholder for batch training. In my code, input image is of size 3 x 3. In order to do batch training, I am setting tf.placeholder(tf.float32,shape=[None,3,3]).

When I try to give batches of 3x3 as an input, TensorFlow gives an error that

Cannot feed value of shape (3, 3) for Tensor u'Placeholder_1:0', which has shape '(?, 3, 3).

Below is the code

input = np.array([[1,1,1],[1,1,1],[1,1,1]])
placeholder = tf.placeholder(tf.float32,shape=[None,3,3])
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    sess.run(placeholder, feed_dict{placeholder:input})
4

1 回答 1

1

您的占位符具有形状None x 3 x 3,因此您需要输入具有3 个维度的数据,即使第一个维度的大小为 1(即1 x 3 x 3在您的情况下是 a 而不是 a 3 x 3)。向数组添加额外维度(大小为 1)的一种简单方法是执行array[None]. 有形则有形。array_ 因此,您可以将代码更新为3 x 3array[None]1 x 3 x 3

inputs = np.array([[1, 1 ,1], [1, 1, 1], [1, 1, 1]])
placeholder = tf.placeholder(tf.float32,shape=[None, 3, 3])
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    sess.run(placeholder, feed_dict{placeholder: inputs[None]})

(我改成因为是Pythoninput中的关键字,不应该用作变量名)inputsinput

inputs[None]请注意,如果inputs已经是 3D ,您将不想这样做。如果它可能是 2D 或 3D,您将需要像inputs[None] if inputs.ndim == 2 else inputs.

于 2019-04-12T16:16:05.290 回答