Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow: How to feed a placeholder variable with a tensor?

Tags:

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?

like image 383
mcExchange Avatar asked Mar 02 '17 16:03

mcExchange


People also ask

How do you feed a placeholder tensor value?

In order to feed the placeholder tf_a we can make use of the feed_dict argument in the session. run() function. The argument takes a dictionary. In this dictionary, you specify which placeholders you need to pass values to, along with the value you want to pass to them.

What is placeholder tensor?

A placeholder is simply a variable that we will assign data to at a later date. It allows us to create our operations and build our computation graph, without needing the data. In TensorFlow terminology, we then feed data into the graph through these placeholders.

What is the difference between placeholder and variable in TensorFlow?

placeholder is used for inputs that will be provided externally to the computation at run-time (e.g. training data). tf. Variable is used for inputs that are part of the computation and are going to be modified by the computation (e.g. weights of a neural network).

What is Feed_dict in TensorFlow?

TensorFlow feed_dict example: Use feed_dict to feed values to TensorFlow placeholders so that you don't run into the error that says you must feed a value for placeholder tensors.


2 Answers

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.

like image 130
Lerner Zhang Avatar answered Jan 23 '23 06:01

Lerner Zhang


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.]

like image 33
fred271828 Avatar answered Jan 23 '23 05:01

fred271828