subbie
subbie

Reputation: 93

Feeding dtype np.float32 to TensorFlow placeholder

I am trying to feed an numpy ndarray of type : float32 to a TensorFlow placeholder, but it's giving me the following error:

You must feed a value for placeholder tensor 'Placeholder' with dtype float

My place holders are defined as:

n_steps = 10
n_input = 13
n_classes = 1201

x = tf.placeholder("float", [None, n_steps, n_input])
y = tf.placeholder("float", [None, n_classes])

And the line it's giving me the above error is:

sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})

where my batch_x and batch_y are numpy ndarrays of dtype('float32'). The following are the types that I printed using pdb:

(Pdb)batch_x.dtype
dtype('float32')
(Pdb)x.dtype
tf.float32

I have also tried type-casting batch_x and batch_y to tf.float32 as it seems like x is of dtype tf.float32 but running the code with type-casting:

sess.run(optimizer, feed_dict={x: tf.to_float(batch_x), y: tf.to_float(batch_y)})

gives the following error:

TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, or numpy ndarrays.

How should I feed the placeholders? of what type should I use? Any help/advice will be much appreciated!

Upvotes: 2

Views: 4726

Answers (1)

Ishamael
Ishamael

Reputation: 12795

For your first problem, are you sure that batch_y is also float32? You only provide the trace of the batch_x type, and batch_y is more likely to be integer, since it appears to be a one-hot encoding of your classes.

For the second problem, what you do wrong is you use tf.to_float, which is a tensor operation, on a regular numpy array. You should use numpy cast intstead:

sess.run(optimizer, feed_dict={x: batch_x.astype(np.float32), y: batch_y.astype(np.float32)})

Upvotes: 2

Related Questions