24

I have a placeholder variable that expects a batch of input images:

input_placeholder = tf.placeholder(tf.float32, [None] + image_shape, name='input_images')

Now I have 2 sources for the input data:
1) a tensor and
2) some numpy data.

For the numpy input data, I know how to feed data to the placeholder variable:

sess = tf.Session()
mLoss, = sess.run([loss], feed_dict = {input_placeholder: myNumpyData})

How can I feed a tensor to that placeholder variable?

mLoss, = sess.run([loss], feed_dict = {input_placeholder: myInputTensor})

gives me an error:

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

I don't want to convert the tensor into a numpy array using .eval(), since that would slow my program down, is there any other way?

mcExchange
  • 6,154
  • 12
  • 57
  • 103

4 Answers4

4

This has been discussed on GitHub in 2016, and please check here. Here is the key point by concretevitamin:

One key thing to note is that Tensor is simply a symbolic object. The values of your feed_dict are the actual values, e.g. a Numpy ndarry.

The tensor as a symbolic object is flowing in the graph while the actual values are outside of it, then we can only pass the actual values into the graph and the symbolic object can not exist outside the graph.

Lerner Zhang
  • 6,184
  • 2
  • 49
  • 66
3

You can use feed_dict to feed data into non-placeholders. So, first, wire up your dataflow graph directly to your myInputTensor tensor data source (i.e. don't use a placeholder). Then when you want to run with your numpy data you can effectively mask myImportTensor with myNumpyData, like this:

mLoss, = sess.run([loss], feed_dict={myImportTensor: myNumpyData})

[I'm still trying to figure out how to do this with multiple tensor data sources however.]

fred271828
  • 959
  • 1
  • 11
  • 18
0

One way of solving the problem is to actually remove the Placeholder tensor and replace it by your "myInputTensor".

You will use the myInputTensor as the source for the other operations in the graph and when you want to infer the graph with your np array as input data, you will feed a value to this tensor directly.

Here is a quick example:

import tensorflow as tf 
import numpy as np

# Input Tensor
myInputTensor = tf.ones(dtype=tf.float32, shape=1) # In your case, this would be the results of some ops

output = myInputTensor * 5.0

with tf.Session() as sess:
    print(sess.run(output)) # == 5.0, using the Tensor value
    myNumpyData = np.zeros(1)
    print(sess.run(output, {myInputTensor: myNumpyData}) # == 0.0 * 5.0 = 0.0, using the np value
Olivier Dehaene
  • 1,620
  • 11
  • 15
-1

This works for me in latest version...maybe you have older version of TF?

a = tf.Variable(1)
sess.run(2*a, feed_dict={a:5}) # prints 10
Yaroslav Bulatov
  • 57,332
  • 22
  • 139
  • 197
  • well this would be feeding it with data. What I would need is `feed_dict={a: someTensorObject}` – mcExchange Mar 02 '17 at 18:08
  • 3
    Correct, `feed_dict` is only for feeding data. If you want to feed a tensor, use regular TensorFlow API. IE, `tf.add(a,b)` "feeds" `a` and `b` into `tf.add`. You probably want `sess.run(a.assign(someTensorObject))` – Yaroslav Bulatov Mar 02 '17 at 19:00