我是tensorflow的新手。我有一些我想理解的代码。有没有一种方法可以获取sess.run中“ feed_dict”的所有可能输入的列表? feed_dict的结构是否始终相同,还是取决于会话?
代码:
sess.run([input,input2],feed_dict={is_train:False,y:stuff,user:[_user]})
更新:
以下注释中的代码描述了如何获取feed_dict的输入
代码:
# 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)
输出:
(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')
代码:
# get all placeholders in graph
placeholders = [ op for op in sess.graph.get_operations() if op.type == "Placeholder"]
placeholders
输出:
[<tf.Operation 'Placeholder' type=Placeholder>,
<tf.Operation 'Placeholder_1' type=Placeholder>,
<tf.Operation 'Placeholder_2' type=Placeholder>,
<tf.Operation 'Placeholder_3' type=Placeholder>]
更新2:
来自https://github.com/kang205/DVBPR的DVBPR文件夹中的main.py。
#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])
答案 0 :(得分:3)
好问题。首先,feed_dict
只是一个Python dictionary,其中每个键是一个tf.placeholder
,每个对应的值是一个python对象。该对象的形状必须等于相应占位符的形状,并且必须具有可强制进入占位符dtype
的数据类型。 feed_dict
的结构由图的结构决定,因为图中的每个占位符必须有一个字典键值元组。
要获取图形中的所有占位符,请执行以下单行代码:
placeholders = [ op for op in graph.get_operations() if op.type == "Placeholder"]
该解决方案的信用额在相关的TensorFlow问题上获得this comment的奖励。如果操作类型为placeholders
,则这种单线工作方式是查看每个操作并将其附加到"Placeholder"
。
答案 1 :(得分:1)
feed_dict只是一个字典,其中的键是包含张量信息的变量,而值是要馈送到网络的数据。通常,您可以填充会话图并找到占位符,因为它们只能作为图的输入。您可以通过以下方式填充图形:
for op in sess.graph.get_operations():
print(op.name, op.type)
由于每个会话都可以依赖于不同的图,因此feed_dict可以具有不同的输入。如果要定义自己的图形,则最好将输入占位符保留为不同的变量。
您的sess.run
命令也在从会话中提取输入。