Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undestanding feed_dict in sess.run

I'm new to tensorflow. I have some code I'm trying to understand. Is there a way to get a list of all possible inputs for the "feed_dict" in sess.run? Is the structure of feed_dict always the same or does it depend on the session?

code:

sess.run([input,input2],feed_dict={is_train:False,y:stuff,user:[_user]})

Update:

Code below from comments describing how to get input for feed_dict

Code:

# populate session graph to look at place holders
# place holders are possible inputs to sess.run()

for op in sess.graph.get_operations():
     print(op.name, op.type)

Output:

(u'Placeholder', u'Placeholder')
(u'ToFloat', u'Cast')
(u'sub/y', u'Const')
(u'sub', u'Sub')
(u'div/y', u'Const')
(u'div', u'RealDiv')
(u'Placeholder_1', u'Placeholder')
(u'DVBPR/Reshape/shape', u'Const')
(u'DVBPR/Reshape', u'Reshape')
(u'DVBPR/wc1/Initializer/random_uniform/shape', u'Const')
(u'DVBPR/wc1/Initializer/random_uniform/min', u'Const')
(u'DVBPR/wc1/Initializer/random_uniform/max', u'Const')
(u'DVBPR/wc1/Initializer/random_uniform/RandomUniform', u'RandomUniform')
(u'DVBPR/wc1/Initializer/random_uniform/sub', u'Sub')
(u'DVBPR/wc1/Initializer/random_uniform/mul', u'Mul')
(u'DVBPR/wc1/Initializer/random_uniform', u'Add')
(u'DVBPR/wc1', u'VariableV2')
(u'DVBPR/wc1/Assign', u'Assign')
(u'DVBPR/wc1/read', u'Identity')
(u'DVBPR/zeros', u'Const')
(u'DVBPR/bc1', u'VariableV2')
(u'DVBPR/bc1/Assign', u'Assign')
(u'DVBPR/bc1/read', u'Identity')
(u'DVBPR/Conv2D', u'Conv2D')
(u'DVBPR/BiasAdd', u'BiasAdd')
(u'DVBPR/Relu', u'Relu')
(u'DVBPR/Relu_1', u'Relu')
(u'DVBPR/MaxPool', u'MaxPool')
(u'DVBPR/wc2/Initializer/random_uniform/shape', u'Const')
(u'DVBPR/wc2/Initializer/random_uniform/min', u'Const')
(u'DVBPR/wc2/Initializer/random_uniform/max', u'Const')
(u'DVBPR/wc2/Initializer/random_uniform/RandomUniform', u'RandomUniform')
(u'DVBPR/wc2/Initializer/random_uniform/sub', u'Sub')
(u'DVBPR/wc2/Initializer/random_uniform/mul', u'Mul')
(u'DVBPR/wc2/Initializer/random_uniform', u'Add')
(u'DVBPR/wc2', u'VariableV2')
(u'DVBPR/wc2/Assign', u'Assign')
(u'DVBPR/wc2/read', u'Identity')
(u'DVBPR/zeros_1', u'Const')
(u'DVBPR/bc2', u'VariableV2')
(u'DVBPR/bc2/Assign', u'Assign')
(u'DVBPR/bc2/read', u'Identity')
(u'DVBPR/Conv2D_1', u'Conv2D')
(u'DVBPR/BiasAdd_1', u'BiasAdd')
(u'DVBPR/Relu_2', u'Relu')
(u'DVBPR/Relu_3', u'Relu')
(u'DVBPR/MaxPool_1', u'MaxPool')
(u'DVBPR/wc3/Initializer/random_uniform/shape', u'Const')
(u'DVBPR/wc3/Initializer/random_uniform/min', u'Const')
(u'DVBPR/wc3/Initializer/random_uniform/max', u'Const')
(u'DVBPR/wc3/Initializer/random_uniform/RandomUniform', u'RandomUniform')
(u'DVBPR/wc3/Initializer/random_uniform/sub', u'Sub')
(u'DVBPR/wc3/Initializer/random_uniform/mul', u'Mul')
(u'DVBPR/wc3/Initializer/random_uniform', u'Add')
(u'DVBPR/wc3', u'VariableV2')
(u'DVBPR/wc3/Assign', u'Assign')
(u'DVBPR/wc3/read', u'Identity')
(u'DVBPR/zeros_2', u'Const')
(u'DVBPR/bc3', u'VariableV2')
(u'DVBPR/bc3/Assign', u'Assign')
(u'DVBPR/bc3/read', u'Identity')
(u'DVBPR/Conv2D_2', u'Conv2D')
(u'DVBPR/BiasAdd_2', u'BiasAdd')
(u'DVBPR/Relu_4', u'Relu')
(u'DVBPR/Relu_5', u'Relu')
(u'DVBPR/wc4/Initializer/random_uniform/shape', u'Const')
(u'DVBPR/wc4/Initializer/random_uniform/min', u'Const')
(u'DVBPR/wc4/Initializer/random_uniform/max', u'Const')
(u'DVBPR/wc4/Initializer/random_uniform/RandomUniform', u'RandomUniform')
(u'DVBPR/wc4/Initializer/random_uniform/sub', u'Sub')
(u'DVBPR/wc4/Initializer/random_uniform/mul', u'Mul')
(u'DVBPR/wc4/Initializer/random_uniform', u'Add')
(u'DVBPR/wc4', u'VariableV2')
(u'DVBPR/wc4/Assign', u'Assign')
(u'DVBPR/wc4/read', u'Identity')
(u'DVBPR/zeros_3', u'Const')
(u'DVBPR/bc4', u'VariableV2')
(u'DVBPR/bc4/Assign', u'Assign')
(u'DVBPR/bc4/read', u'Identity')
(u'DVBPR/Conv2D_3', u'Conv2D')
(u'DVBPR/BiasAdd_3', u'BiasAdd')
(u'DVBPR/Relu_6', u'Relu')
(u'DVBPR/Relu_7', u'Relu')
(u'DVBPR/wc5/Initializer/random_uniform/shape', u'Const')
(u'DVBPR/wc5/Initializer/random_uniform/min', u'Const')
(u'DVBPR/wc5/Initializer/random_uniform/max', u'Const')
(u'DVBPR/wc5/Initializer/random_uniform/RandomUniform', u'RandomUniform')
(u'DVBPR/wc5/Initializer/random_uniform/sub', u'Sub')
(u'DVBPR/wc5/Initializer/random_uniform/mul', u'Mul')
(u'DVBPR/wc5/Initializer/random_uniform', u'Add')
(u'DVBPR/wc5', u'VariableV2')
(u'DVBPR/wc5/Assign', u'Assign')
(u'DVBPR/wc5/read', u'Identity')
(u'DVBPR/zeros_4', u'Const')
(u'DVBPR/bc5', u'VariableV2')
(u'DVBPR/bc5/Assign', u'Assign')
(u'DVBPR/bc5/read', u'Identity')
(u'DVBPR/Conv2D_4', u'Conv2D')
(u'DVBPR/BiasAdd_4', u'BiasAdd')
(u'DVBPR/Relu_8', u'Relu')
(u'DVBPR/Relu_9', u'Relu')
(u'DVBPR/MaxPool_2', u'MaxPool')
(u'DVBPR/Reshape_1/shape', u'Const')
(u'DVBPR/Reshape_1', u'Reshape')
(u'DVBPR/wd1/Initializer/random_uniform/shape', u'Const')
(u'DVBPR/wd1/Initializer/random_uniform/min', u'Const')
(u'DVBPR/wd1/Initializer/random_uniform/max', u'Const')
(u'DVBPR/wd1/Initializer/random_uniform/RandomUniform', u'RandomUniform')
(u'DVBPR/wd1/Initializer/random_uniform/sub', u'Sub')
(u'DVBPR/wd1/Initializer/random_uniform/mul', u'Mul')
(u'DVBPR/wd1/Initializer/random_uniform', u'Add')
(u'DVBPR/wd1', u'VariableV2')
(u'DVBPR/wd1/Assign', u'Assign')
(u'DVBPR/wd1/read', u'Identity')
(u'DVBPR/MatMul', u'MatMul')
(u'DVBPR/zeros_5/shape_as_tensor', u'Const')
(u'DVBPR/zeros_5/Const', u'Const')
(u'DVBPR/zeros_5', u'Fill')
(u'DVBPR/bd1', u'VariableV2')
(u'DVBPR/bd1/Assign', u'Assign')
(u'DVBPR/bd1/read', u'Identity')
(u'DVBPR/Add', u'Add')
(u'DVBPR/Relu_10', u'Relu')
(u'DVBPR/dropout/keep_prob', u'Const')
(u'DVBPR/wd2/Initializer/random_uniform/shape', u'Const')
(u'DVBPR/wd2/Initializer/random_uniform/min', u'Const')
(u'DVBPR/wd2/Initializer/random_uniform/max', u'Const')
(u'DVBPR/wd2/Initializer/random_uniform/RandomUniform', u'RandomUniform')
(u'DVBPR/wd2/Initializer/random_uniform/sub', u'Sub')
(u'DVBPR/wd2/Initializer/random_uniform/mul', u'Mul')
(u'DVBPR/wd2/Initializer/random_uniform', u'Add')
(u'DVBPR/wd2', u'VariableV2')
(u'DVBPR/wd2/Assign', u'Assign')
(u'DVBPR/wd2/read', u'Identity')
(u'DVBPR/MatMul_1', u'MatMul')
(u'DVBPR/zeros_6/shape_as_tensor', u'Const')
(u'DVBPR/zeros_6/Const', u'Const')
(u'DVBPR/zeros_6', u'Fill')
(u'DVBPR/bd2', u'VariableV2')
(u'DVBPR/bd2/Assign', u'Assign')
(u'DVBPR/bd2/read', u'Identity')
(u'DVBPR/Add_1', u'Add')
(u'DVBPR/Relu_11', u'Relu')
(u'DVBPR/dropout_1/keep_prob', u'Const')
(u'DVBPR/wd3/Initializer/random_uniform/shape', u'Const')
(u'DVBPR/wd3/Initializer/random_uniform/min', u'Const')
(u'DVBPR/wd3/Initializer/random_uniform/max', u'Const')
(u'DVBPR/wd3/Initializer/random_uniform/RandomUniform', u'RandomUniform')
(u'DVBPR/wd3/Initializer/random_uniform/sub', u'Sub')
(u'DVBPR/wd3/Initializer/random_uniform/mul', u'Mul')
(u'DVBPR/wd3/Initializer/random_uniform', u'Add')
(u'DVBPR/wd3', u'VariableV2')
(u'DVBPR/wd3/Assign', u'Assign')
(u'DVBPR/wd3/read', u'Identity')
(u'DVBPR/MatMul_2', u'MatMul')
(u'DVBPR/zeros_7', u'Const')
(u'DVBPR/bd3', u'VariableV2')
(u'DVBPR/bd3/Assign', u'Assign')
(u'DVBPR/bd3/read', u'Identity')
(u'DVBPR/Add_2', u'Add')
(u'DVBPR/random_uniform/shape', u'Const')
(u'DVBPR/random_uniform/min', u'Const')
(u'DVBPR/random_uniform/max', u'Const')
(u'DVBPR/random_uniform/RandomUniform', u'RandomUniform')
(u'DVBPR/random_uniform/sub', u'Sub')
(u'DVBPR/random_uniform/mul', u'Mul')
(u'DVBPR/random_uniform', u'Add')
(u'DVBPR/div/y', u'Const')
(u'DVBPR/div', u'RealDiv')
(u'DVBPR/Variable', u'VariableV2')
(u'DVBPR/Variable/Assign', u'Assign')
(u'DVBPR/Variable/read', u'Identity')
(u'init', u'NoOp')
(u'save/Const', u'Const')
(u'save/SaveV2/tensor_names', u'Const')
(u'save/SaveV2/shape_and_slices', u'Const')
(u'save/SaveV2', u'SaveV2')
(u'save/control_dependency', u'Identity')
(u'save/RestoreV2/tensor_names', u'Const')
(u'save/RestoreV2/shape_and_slices', u'Const')
(u'save/RestoreV2', u'RestoreV2')
(u'save/Assign', u'Assign')
(u'save/Assign_1', u'Assign')
(u'save/Assign_2', u'Assign')
(u'save/Assign_3', u'Assign')
(u'save/Assign_4', u'Assign')
(u'save/Assign_5', u'Assign')
(u'save/Assign_6', u'Assign')
(u'save/Assign_7', u'Assign')
(u'save/Assign_8', u'Assign')
(u'save/Assign_9', u'Assign')
(u'save/Assign_10', u'Assign')
(u'save/Assign_11', u'Assign')
(u'save/Assign_12', u'Assign')
(u'save/Assign_13', u'Assign')
(u'save/Assign_14', u'Assign')
(u'save/Assign_15', u'Assign')
(u'save/Assign_16', u'Assign')
(u'save/restore_all', u'NoOp')
(u'Reshape/tensor', u'Const')
(u'Reshape/shape', u'Const')
(u'Reshape', u'Reshape')
(u'input_code/initial_value', u'Const')
(u'input_code', u'VariableV2')
(u'input_code/Assign', u'Assign')
(u'input_code/read', u'Identity')
(u'Placeholder_2', u'Placeholder')
(u'ResizeNearestNeighbor/size', u'Const')
(u'ResizeNearestNeighbor', u'ResizeNearestNeighbor')
(u'DVBPR_1/Reshape/shape', u'Const')
(u'DVBPR_1/Reshape', u'Reshape')
(u'DVBPR_1/zeros', u'Const')
(u'DVBPR_1/Conv2D', u'Conv2D')
(u'DVBPR_1/BiasAdd', u'BiasAdd')
(u'DVBPR_1/Relu', u'Relu')
(u'DVBPR_1/Relu_1', u'Relu')
(u'DVBPR_1/MaxPool', u'MaxPool')
(u'DVBPR_1/zeros_1', u'Const')
(u'DVBPR_1/Conv2D_1', u'Conv2D')
(u'DVBPR_1/BiasAdd_1', u'BiasAdd')
(u'DVBPR_1/Relu_2', u'Relu')
(u'DVBPR_1/Relu_3', u'Relu')
(u'DVBPR_1/MaxPool_1', u'MaxPool')
(u'DVBPR_1/zeros_2', u'Const')
(u'DVBPR_1/Conv2D_2', u'Conv2D')
(u'DVBPR_1/BiasAdd_2', u'BiasAdd')
(u'DVBPR_1/Relu_4', u'Relu')
(u'DVBPR_1/Relu_5', u'Relu')
(u'DVBPR_1/zeros_3', u'Const')
(u'DVBPR_1/Conv2D_3', u'Conv2D')
(u'DVBPR_1/BiasAdd_3', u'BiasAdd')
(u'DVBPR_1/Relu_6', u'Relu')
(u'DVBPR_1/Relu_7', u'Relu')
(u'DVBPR_1/zeros_4', u'Const')
(u'DVBPR_1/Conv2D_4', u'Conv2D')
(u'DVBPR_1/BiasAdd_4', u'BiasAdd')
(u'DVBPR_1/Relu_8', u'Relu')
(u'DVBPR_1/Relu_9', u'Relu')
(u'DVBPR_1/MaxPool_2', u'MaxPool')
(u'DVBPR_1/Reshape_1/shape', u'Const')
(u'DVBPR_1/Reshape_1', u'Reshape')
(u'DVBPR_1/MatMul', u'MatMul')
(u'DVBPR_1/zeros_5/shape_as_tensor', u'Const')
(u'DVBPR_1/zeros_5/Const', u'Const')
(u'DVBPR_1/zeros_5', u'Fill')
(u'DVBPR_1/Add', u'Add')
(u'DVBPR_1/Relu_10', u'Relu')
(u'DVBPR_1/dropout/keep_prob', u'Const')
(u'DVBPR_1/MatMul_1', u'MatMul')
(u'DVBPR_1/zeros_6/shape_as_tensor', u'Const')
(u'DVBPR_1/zeros_6/Const', u'Const')
(u'DVBPR_1/zeros_6', u'Fill')
(u'DVBPR_1/Add_1', u'Add')
(u'DVBPR_1/Relu_11', u'Relu')
(u'DVBPR_1/dropout_1/keep_prob', u'Const')
(u'DVBPR_1/MatMul_2', u'MatMul')
(u'DVBPR_1/zeros_7', u'Const')
(u'DVBPR_1/Add_2', u'Add')
(u'Placeholder_3', u'Placeholder')
(u'GatherV2/axis', u'Const')
(u'GatherV2', u'GatherV2')
(u'transpose/Rank', u'Rank')
(u'transpose/sub/y', u'Const')
(u'transpose/sub', u'Sub')
(u'transpose/Range/start', u'Const')
(u'transpose/Range/delta', u'Const')
(u'transpose/Range', u'Range')
(u'transpose/sub_1', u'Sub')
(u'transpose', u'Transpose')
(u'MatMul', u'MatMul')
(u'Sum/reduction_indices', u'Const')
(u'Sum', u'Sum')

Code:

# get all placeholders in graph

placeholders = [ op for op in sess.graph.get_operations() if op.type == "Placeholder"]

placeholders

Output:

[<tf.Operation 'Placeholder' type=Placeholder>,
 <tf.Operation 'Placeholder_1' type=Placeholder>,
 <tf.Operation 'Placeholder_2' type=Placeholder>,
 <tf.Operation 'Placeholder_3' type=Placeholder>]

Update 2:

from main.py in DVBPR folder at https://github.com/kang205/DVBPR

#define model
with tf.device('/gpu:0'):
    #training sample
    queueu = tf.placeholder(dtype=tf.int32,shape=[1])
    queuei = tf.placeholder(dtype=tf.int32,shape=[1])
    queuej = tf.placeholder(dtype=tf.int32,shape=[1])
    queueimage1 = tf.placeholder(dtype=tf.uint8,shape=[224,224,3])
    queueimage2 = tf.placeholder(dtype=tf.uint8,shape=[224,224,3])
    batch_train_queue = tf.FIFOQueue(batch_size*5, dtypes=[tf.int32,tf.int32,tf.int32,tf.uint8,tf.uint8], shapes=[[1],[1],[1],[224,224,3],[224,224,3]])
    batch_train_queue_op = batch_train_queue.enqueue([queueu,queuei,queuej,queueimage1,queueimage2]);
    u,i,j,image1,image2 = batch_train_queue.dequeue_many(batch_size)

    image_test=tf.placeholder(dtype=tf.uint8,shape=[batch_size,224,224,3])
like image 396
user3476463 Avatar asked Jul 09 '18 20:07

user3476463


People also ask

What is Feed_dict in TensorFlow?

TensorFlow feed_dict: Use feed_dict To Feed Values To TensorFlow Placeholders. 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.

How does TF session work?

TensorFlow Session is a session object which encapsulates the environment in which Operation objects are executed, and data objects are evaluated. TensorFlow requires a session to execute an operation and retrieve its calculated value. A session may own several resources, for example, tf. QueueBase, tf.

What's the difference between TF placeholder and TF variable?

tf. 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 placeholder session?

As a general rule, if someone is reserving time on your calendar for a meeting that has yet to be finalized for attendees, subject matter experts, etc, the organizer will likely use the term "Placeholder" to hold that place on your calendar and not lose the free space to other meetings.


2 Answers

Good questions. First, feed_dict is simply a python dictionary in which each key is a tf.placeholder and each corresponding value is a python object. This object must have a shape equal to that of the corresponding placeholder, and must have a datatype which can be coerced into the placeholders dtype. The structure of feed_dict is dictated by the structure of the graph, because there must be one dictionary key-value tuple for each placeholder in the graph.

To get all of the placeholders in the graph, the following one-liner will do:

placeholders = [ op for op in graph.get_operations() if op.type == "Placeholder"]

Credit for that solution goes to this comment on a related TensorFlow issue. This one-liner works by reviewing each operation and appending it to placeholders if the operation type is "Placeholder".

like image 66
Justin Fletcher Avatar answered Nov 15 '22 08:11

Justin Fletcher


feed_dict is just a dictionary where the key is the variable containing tensor information, and the value is the data to be fed to the network. Usually you can populate the session graph and find the placeholders, as they can only be inputs to a graph. You can populate the graph by:

for op in sess.graph.get_operations(): print(op.name, op.type)

As each session can depend on a different graph, feed_dict can have different inputs. If you are defining your own graph, it is good practice to keep the input placeholders as different variables.

Also your sess.run command is extracting inputs from the session.

like image 41
Abhishek Sehgal Avatar answered Nov 15 '22 06:11

Abhishek Sehgal