问题描述
假设我们有一个变量:
x = tf.Variable(...)
可以在训练过程中使用 assign()
方法更新此变量.
This variable can be updated during the training process using the assign()
method.
获取变量当前值的最佳方法是什么?
我知道我们可以使用这个:
I know we could use this:
session.run(x)
但我担心这会触发整个操作链.
But I'm afraid this would trigger a whole chain of operations.
在 Theano 中,你可以做到
In Theano, you could just do
y = theano.shared(...)
y_vals = y.get_value()
我正在 TensorFlow 中寻找等效的东西.
I'm looking for the equivalent thing in TensorFlow.
推荐答案
一般来说,session.run(x)
只会评估计算 x
没有别的,所以如果你想检查变量的值应该相对便宜.
In general, session.run(x)
will evaluate only the nodes that are necessary to compute x
and nothing else, so it should be relatively cheap if you want to inspect the value of the variable.
看看这个很棒的答案https://stackoverflow.com/a/33610914/5543198了解更多背景信息.
Take a look at this great answer https://stackoverflow.com/a/33610914/5543198 for more context.
这篇关于如何获取变量的当前值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!