Python 是否将每个张量作为回迁传递给会话的run方法?

Python 是否将每个张量作为回迁传递给会话的run方法?,python,tensorflow,Python,Tensorflow,假设我们有以下代码: import tensorflow as tf temp = {"first": tf.Variable(1.0), "second": tf.Variable(2.0)} with tf.Session() as sess: sess.run(temp) print(temp["first"]) print(temp["second"]) 如果在包含两个TensorFlow变量的字典(例如,temp)上执行sess.run(),将对这两个变

假设我们有以下代码:

import tensorflow as tf


temp = {"first": tf.Variable(1.0), "second": tf.Variable(2.0)}

with tf.Session() as sess:
    sess.run(temp)
    print(temp["first"])
    print(temp["second"])

如果在包含两个TensorFlow变量的字典(例如,
temp
)上执行
sess.run()
,将对这两个变量求值吗?

我相信正确的语法如下所示

results = sess.run(temp.values())
for name, val in zip(temp, results):
  print name, val
会话
类的
运行
方法的

run(fetches,feed\u dict=None,options=None,run\u metadata=None)

获取
中运行操作并计算张量

此方法运行TensorFlow计算的一个“步骤”,通过运行必要的图形片段来执行每个操作并计算
获取
中的每个张量,用
提要_dict
中的值替换相应的输入值

fetches参数可以是单个图形元素,也可以是任意嵌套的列表、元组、namedtuple、dict或OrderedDict,在其叶子处包含图形元素

在您的情况下,
temp
(a
dict
)是传递给
run
的“
获取
”。因此,
temp
中的每个操作或张量都会被计算

如果我没有遗漏任何内容,那么文档中没有提到任何关于求值顺序的内容,我认为,您不应该假设操作或张量是按照特定顺序求值的,因为这可能会发生更改(例如,由于优化)

因此,为了直接回答您的问题:是的,每个变量/张量(作为
获取
到会话的
运行
方法传递)都会被计算

此外,请注意,您的代码也有问题,例如

import tensorflow as tf


temp = {"first": tf.Variable(1.0), "second": tf.Variable(2.0)}

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    sess.run(temp)
    print(temp["first"])
    print(temp["second"])
像美国一样

启动图形时,必须显式初始化变量,然后才能运行使用其值的操作


这根本不能回答问题!不,OP在字典中打印值的方式也是有效的。谢谢!是的,我只是写了一些小代码,这样你就可以得到要点,我碰巧错过了。