Reputation: 21
I'm new to Tensorflow, and sorry if I'm asking a silly question. Here is my code. And it always give an error:
ValueError: Cannot feed value of shape (3,) for Tensor
'Placeholder:0', which has shape '(3, ?)'
My problem is what does it mean by shape (3,)? Why cannot I feed a value of shape (3,) to a shape of (3,?) placeholder? As I feed a single raw matrix(i.e. [1,3,8]) why does tensorflow recognize it as a shape of (3,), which seems to be a matrix with 3 raws?
Code:
import tensorflow as tf
x = tf.placeholder(tf.int32, [3,None])
y = x-2
with tf.Session() as session:
result = session.run(y, feed_dict={x: [1,3,8]})
print(result)
Upvotes: 1
Views: 1594
Reputation: 222511
Before starting any framework, it is very beneficial to read the basics. TF already has them. It will take less than an hour to read and will save you days. Enough of the rant.
Reading about the terminology you can see that shape (3, ) means you have a vector of 3 elements. And this is exactly what you provide [1, 3, 8]
.
Reading about shapes you would see that you want your placeholder to be a matrix of size (3 x something). So adjust either a placeholder or a feeding value.
Upvotes: 1
Reputation: 17191
Your x
is a 2-D array, but you are feeding a 1-D input to it.
This change will work:
import tensorflow as tf
import numpy as np
x = tf.placeholder(tf.int32, [3,None])
y = x-2
with tf.Session() as session:
result = session.run(y, feed_dict={x: np.reshape([1,3,8], (3,-1))})
print(result)
Upvotes: 0