gpt4 book ai didi

python-2.7 - 理解 sess.run 中的 feed_dict

转载 作者:行者123 更新时间:2023-12-01 10:21:34 28 4
gpt4 key购买 nike

我是 tensorflow 的新手。我有一些我想理解的代码。有没有办法在 sess.run 中获取“feed_dict”的所有可能输入的列表? feed_dict 的结构是始终相同还是取决于 session ?

代码:

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

最佳答案

好问题。一、feed_dict只是一条 python dictionary其中每个键都是一个 tf.placeholder 每个对应的值都是一个 python 对象。该对象的形状必须与相应占位符的形状相同,并且必须具有可以强制转换为占位符 dtype 的数据类型. feed_dict的结构由图的结构决定,因为图中的每个占位符都必须有一个字典键值元组。

要获取图表中的所有占位符,可以使用以下单行:

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

该解决方案的功劳归于 this comment在相关的 TensorFlow 问题上。这个单行程序通过检查每个操作并将其附加到 placeholders 来工作。如果操作类型为 "Placeholder" .

关于python-2.7 - 理解 sess.run 中的 feed_dict,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51253483/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com