InvalidArgumentError: you must specify the value of the Placeholder placeholder tensor

I started to study tensor flow and am having difficulty understanding problems with placeholders / variables.

I am trying to write a function for matrix multiplication. It works when using tf.constant, but it's hard for me to figure out how to use variables

here is my code

import tensorflow as tf import numpy as np mat_1 = np.array([[0,1,1,0], [1,0,1,0], [1,0,0,1], [0,1,1,0]]).astype('int32') mat_2 = np.array([[0,1,1,0], [1,0,1,0], [1,0,0,1], [0,1,1,0]]).astype('int32') def my_matmult1(mat_1, mat_2): #define session x_sess = tf.Session() with x_sess: xmat_1 = tf.constant(mat_1) xmat_2 = tf.constant(mat_2) r1 = tf.matmul(xmat_1, xmat_2) qq1 = x_sess.run(r1) return qq1 def my_matmult2(mat_1, mat_2): #define session x_sess1 = tf.Session() with x_sess1: #initialize placeholders xmat_1_plh = tf.placeholder(dtype=mat_1.dtype, shape=mat_1.shape) xmat_2_plh = tf.placeholder(dtype=mat_2.dtype, shape=mat_2.shape) #create variables x_mat_1 = tf.Variable(xmat_1_plh, trainable = False) x_mat_2 = tf.Variable(xmat_2_plh, trainable = False) x_sess1.run(tf.initialize_all_variables()) # r1 = tf.matmul(xmat_1, xmat_2) qq1 = x_sess1.run(r1, feed_dic={mat_1, mat_2}) return qq1 

This works as expected:

 my_matmult1(mat_1, mat_1) 

However, the following fails:

 my_matmult2(mat_1, mat_1) 

with the following error

InvalidArgumentError

You must submit a value for the 'Placeholder' placeholder with dtype int32 and form [4,4]

Even after changing the last line in

 qq1 = x_sess1.run(r1, feed_dic={tf.convert_to_tensor(mat_1), tf.convert_to_tensor(mat_2)}) 

What am I doing wrong?

0
source share
3 answers

Your code should work if you delete the tf.Variable() lines after creating placeholders (and change the name of the root variables accordingly).

Placeholders are for variables that you want to pass to your model. Variables refer to the parameters of your model (e.g. weights).

Therefore, you created two placeholders correctly, but then you created additional variables for no reason, which probably messed up something in the Tensorflow column.

The function will look like this:

 import tensorflow as tf import numpy as np def my_matmult2(mat_1, mat_2): #define session x_sess1=tf.Session() with x_sess1: #initialize placeholders xmat_1_plh = tf.placeholder(dtype=mat_1.dtype, shape=mat_1.shape) xmat_2_plh = tf.placeholder(dtype=mat_2.dtype, shape=mat_2.shape) r1 = tf.matmul(xmat_1_plh, xmat_2_plh) x_sess1.run(tf.initialize_all_variables()) # qq1 = x_sess1.run(r1, feed_dict={xmat_1_plh: mat_1 , xmat_2_plh: mat_2}) return qq1 mat_1=np.ones((5,5)) mat_2=np.ones((5,5)) b=my_matmult2(mat_1,mat_2) print b 
+3
source

Do not feed the dictionary correctly. You need to install the dictionary in the name of Placeholder. I also added a name, you can use "xmat_1_plh" as a name, but I prefer to add my own name. I also think that you have extra lines in the my_matmult2 () function. x_mat_1 / 2 I don’t think there is much to add, but it probably won’t hurt (maybe a bit of performance by adding another OP to the chart.

 def my_matmult2(mat_1, mat_2): #define session x_sess1 = tf.Session() with x_sess1: #initialize placeholders xmat_1_plh = tf.placeholder(dtype=mat_1.dtype, shape=mat_1.shape, name ="xmat1") xmat_2_plh = tf.placeholder(dtype=mat_2.dtype, shape=mat_2.shape, name ="xmat2") #create variables x_mat_1 = tf.Variable(xmat_1_plh, trainable = False) x_mat_2 = tf.Variable(xmat_2_plh, trainable = False) x_sess1.run(tf.initialize_all_variables()) # r1 = tf.matmul(xmat_1, xmat_2) qq1 = x_sess1.run(r1, feed_dic={xmat1: mat_1, xmat2: mat_2}) return qq1 

I'm not sure what the ultimate goal of this function is, but you are creating nodes in the graph. Because of this, you probably want to transfer the ".run ()" instruction from this function (to where you want to actively multiply matrix 2), since you shouldn't call it in a loop if you're just looking for a way to multiply 2 matrices.

If this is a single test / call to my_matmult2 (), then you should work with the fix in the dictionary.

+1
source

In order to meaningfully answer this question, I must return to how shadoworflow is designed to work

Counts
The graphs in Tensorflow are just a map / path that will perform the calculation. It does not contain any values ​​and does nothing.

Session
On the other hand, a session requires a schedule, data, and runtime. This concept of graphs and sessions allows TensorFolow to separate flow or model definitions from actual computation time.

Split runtime from thread schedule
This was most likely done to isolate the definition of the schedule from the run-time configurations and the actual execution with the data. For example, the runtime may be in a cluster. Thus, each of the execution steps in the cluster must have the same schedule definition. But each of the run-time may locally have a different data set during the execution process. Therefore, it is important that input and output data can be provided during distributed execution in the cluster.

Why placeholders, not variables
Placeholders act as input / output channels for Charts. If you visualize your graph as the number of nodes, then placeholders are input or output nodes.

The real question is: why doesn't TensorFlow use a normal variable for I / O nodes? Why is there another type?

During the training process (when the program is running in a session), you need to make sure that the actual values ​​are used to train the model. Basically feed_dict inside the learning process will only take actual values, for example. Nudism. These actual values ​​cannot be provided by the TensorFlow variable, since Tensorflow variables have no data unless eval () or session.run () are used. However, the training report itself is part of the session.run () function - therefore, it cannot use another session.run () inside it to allow the tensor variable for the data. By this time, session.run () should already be bound to a specific run-time configuration and data.

+1
source

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


All Articles