Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I feed Tensorflow placeholders with numpy arrays?

I'm working on creating a simple toy example in TensorFlow and I've run into a strange error. I have two placeholders defined as follows:

x = tf.placeholder(tf.float32, shape=[None,2]) [two-parameter input]

y_ = tf.placeholder(tf.float32, shape=[None,2]) [one-hot labels]

I later attempt to feed these placeholders with a feed_dict defined as:

feed_dict={x: batch[0].astype(np.float32), y_: batch[1].astype(np.float32)}

Where batch[0] and batch[1] are both numpy ndarrays of size (100,2) [verified by printing out their respective sizes]

When I attempt to run the model, I get the error:

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

caused by my Placeholder "x" defined above

Can anyone tell what I'm doing wrong? I've looked through several examples online and it seems like this should work... Is there another way to feed placeholders with values from numpy arrays?

If it helps, I'm working in Ubuntu, SCL, and Python 2.7, and I have all the standard numpy and tensorflow packages installed.

like image 975
Patrick Menninger Avatar asked Nov 07 '16 16:11

Patrick Menninger


1 Answers

Without your entire code, it is hard to answer precisely. I tried to reproduce what you described in a toy example and it worked.

import tensorflow as tf
import numpy as np

x = tf.placeholder(tf.float32, shape=[None, 2])
y_ = tf.placeholder(tf.float32, shape=[None, 2])
loss = tf.reduce_sum(tf.abs(tf.sub(x, y_)))#Function chosen arbitrarily
input_x=np.random.randn(100, 2)#Random generation of variable x
input_y=np.random.randn(100, 2)#Random generation of variable y

with tf.Session() as sess:
    print(sess.run(loss, feed_dict={x: input_x, y_: input_y}))
like image 82
Thibaut Loiseleur Avatar answered Sep 22 '22 17:09

Thibaut Loiseleur