Python 如何获取变量的当前值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33679382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:45:31  来源:igfitidea点击:

How do I get the current value of a Variable?

pythontensorflow

提问by Denis L

Suppose we have a variable:

假设我们有一个变量:

x = tf.Variable(...)

This variable can be updated during the training process using the assign()method.

该变量可以在训练过程中使用该assign()方法进行更新。

What is the best way to get the current value of a variable?

获取变量当前值的最佳方法是什么?

I know we could use this:

我知道我们可以使用这个:

session.run(x)

But I'm afraid this would trigger a whole chain of operations.

但恐怕这会引发一整套操作。

In Theano, you could just do

在 Theano 中,你可以这样做

y = theano.shared(...)
y_vals = y.get_value()

I'm looking for the equivalent thing in TensorFlow.

我正在 TensorFlow 中寻找等效的东西。

采纳答案by Rafa? Józefowicz

In general, session.run(x)will evaluate only the nodes that are necessary to compute xand nothing else, so it should be relatively cheap if you want to inspect the value of the variable.

一般情况下,session.run(x)只会评估计算所需的节点,而不会评估x其他任何节点,因此如果您想检查变量的值,它应该相对便宜。

Take a look at this great answer https://stackoverflow.com/a/33610914/5543198for more context.

看看这个很棒的答案https://stackoverflow.com/a/33610914/5543198了解更多上下文。

回答by Salvador Dali

The only way to get the value of the variable is by running it in a session. In the FAQ it is writtenthat:

获取变量值的唯一方法是在session. 在常见问题解答中写道

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.

Tensor 对象是操作结果的符号句柄,但实际上并不保存操作输出的值。

So TF equivalent would be:

所以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.

The part with init = global_variables_initializer()is important and should be done in order to initialize variables.

with 的部分init = global_variables_initializer()很重要,应该完成以初始化变量。

Also, take a look at InteractiveSessionif you work in IPython.

另外,如果您使用 IPython ,请查看InteractiveSession

回答by patapouf_ai

tf.Printcan simplify your life!

tf.Print可以简化你的生活!

tf.Printwill print the value of the tensor(s) you tell it to print at the moment where the tf.Printline is called in your code when your code is evaluated.

tf.Print将打印您告诉它在tf.Print评估代码时在代码中调用该行的那一刻打印的张量的值。

So for example:

例如:

import tensorflow as tf
x = tf.Variable([1.0, 2.0])
x = tf.Print(x,[x])
x = 2* x

tf.initialize_all_variables()

sess = tf.Session()
sess.run()

[1.0 2.0 ]

[1.0 2.0]

because it prints the value of xat the moment when the tf.Printline is. If instead you do

因为它会打印xtf.Print行所在时刻的值。如果你这样做

v = x.eval()
print(v)

you will get:

你会得到:

[2.0 4.0 ]

[2.0 4.0]

because it will give you the final value of x.

因为它会给你最终的 x 值。

回答by Jimin Bao

As they cancelled tf.Variable()in tensorflow 2.0.0,

当他们取消tf.Variable()tensorflow 2.0.0

If you want to extract values from a tensor(ie "net"), you can use this,

如果你想从 a 中提取值tensor(ie "net"),你可以使用这个,

net.[tf.newaxis,:,:].numpy().