How to feed Tensorflow placeholder using numpy arrays?

I am working on creating a simple toy example in TensorFlow, and I came across a strange error. I have two placeholders that are 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] 

Later I will try to pass these feed_dict placeholders, which are defined as:

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

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

When I try to run the model, I get an error message:

"InvalidArgumentError: you must pass the value of the 'Placeholder' placeholder tensor with a dtype float

caused by my Alternate "x" as defined above

Can anyone tell me what I'm doing wrong? I looked through a few examples on the Internet and it looks like this should work ... Is there any other way to populate placeholders with values ​​from numpy arrays?

If this helps, I work in Ubuntu, SCL, and Python 2.7, and I have all the standard numpy and tensorflow packages installed.

+9
source share
2 answers

Without your entire code, an exact answer is accurate. I tried to reproduce what you described in the 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})) 
+7
source

Do you meet a new problem? When the array of supplied arrays is huge, somewhere in the official document it says that the tmp size limit exceeds 2 GB, the channel code just hangs.

0
source

Source: https://habr.com/ru/post/1012113/


All Articles