问题描述
我使用操作assign"来更改变量的值而不是=",但我发现我得到的渐变完全不同.谁能告诉我有什么区别,为什么?谢谢!比如将 w = w1 改为 op1 = tf.assign(w, w1) sess.run(op1)
I use the op "assign" to change the value of variables instead of "=", but I found the gradient I got is quite different. Could anyone tell me the difference and why? thanks!Like change w = w1 to op1 = tf.assign(w, w1) sess.run(op1)
推荐答案
=
和 tf.assign
是不同的操作.
=
and tf.assign
are different operations.
=
是一个 python 操作,在该操作中,您将一个 python 值分配给一个 python 变量
=
is a python operation, in which you assign a python value to a python variable
tf.assign
是一个 Tensorflow 操作,将值赋给变量 ref 并返回 assign 操作.
tf.assign
is a Tensorflow operation that assigns the value to the variable ref and returns the assign operation.
=
在python中执行,不影响计算图.tf.assign
是计算图中的一个节点.
=
is executed in python and doesn't affect the computation graph.tf.assign
is a node in the computational graph.
为了理解,让我们运行这个简单的脚本
To understand, let's run this simple script
import tensorflow as tf
x = tf.Variable(1)
y = tf.Variable(2)
x = y
print(x.name, y.name)
a = tf.Variable(1)
b = tf.Variable(2)
# override a, otherwise a content is 1
a = a.assign(b)
print(a.name, b.name)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print(sess.run([x, y, a, b]))
print(x.name, y.name)
输出 Variable_1:0 Variable_1:0
因为 =
在 python 中执行并且你已经覆盖了变量 x
.
because =
is executed in python and you've overwritten the variable x
.
print(a.name, b.name)
输出 Assign:0 Variable_3:0
因为你在计算图中定义了一个赋值操作,现在 a
是一个赋值操作.
print(a.name, b.name)
outputs Assign:0 Variable_3:0
because you defined an assign op in the computational graph, now a
is an assign op.
运行定义的图形时,您会得到:
When you run the defined graph, you get:
[2, 2, 2, 2]
但是这些值的计算方式不同:一个是图中的计算,其他的不是.
But these values are computed differently: one is a computation in the graph, the others no.
如果您忘记将 a
分配给使用 tf.assign
创建的 assign op(因此您更改了行 a =a.assign(b)
到 a.assign(b)
),然后当你评估图表时,你会得到:
If you forgot to assign a
to the assign op created with tf.assign
(thus you change the line a = a.assign(b)
to a.assign(b)
), then when you evaluate the graph, you'll get:
[2, 2, 1, 2]
这篇关于(Tensorflow) opassign 是否改变了梯度计算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!