假设我们有一个变量:
x = tf.Variable(...)
可以在训练过程中使用 assign()
方法更新此变量。
获取变量当前值的最佳方法是什么?
我知道我们可以使用这个:
session.run(x)
但我担心这会引发一整套操作。
在 Theano 中,你可以这样做
y = theano.shared(...)
y_vals = y.get_value()
我正在 TensorFlow 中寻找等价的东西。
最佳答案
获取变量值的唯一方法是在 session
中运行它。在 FAQ it is written那:
A Tensor object is a symbolic handle to the result of an operation, but does not actually hold the values of the operation's output.
所以 TF 等效项是:
import tensorflow as tf
x = tf.Variable([1.0, 2.0])
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
v = sess.run(x)
print(v) # will show you your variable.
带有 init = global_variables_initializer()
的部分很重要,应该这样做以初始化变量。
另外,看看 InteractiveSession如果你在 IPython 中工作。
关于python - 如何获取变量的当前值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33679382/