使用tf.Variable()和tf.get_variable()时的结果不同 [英] Different outcomes when using tf.Variable() and tf.get_variable()
问题描述
我正在尝试通过此网站熟悉TensorFlow框架通过玩线性回归(LR).可以在此处中找到LR的源代码,名称03_linear_regression_sol.py
.
I'm trying to get familiar with TensorFlow framework from this site by playing around with Linear Regression (LR). The source code for LR can be found here, with the name 03_linear_regression_sol.py
.
通常,LR的定义模型为Y_predicted = X * w + b
,其中
Generally, the defined model for LR is Y_predicted = X * w + b
, where
-
w
和b
是参数(tf.Variable
) -
Y_predicted
和X
是训练数据(placeholder
)
w
andb
are parameters (tf.Variable
)Y_predicted
andX
are training data (placeholder
)
对于w
和b
,在示例代码中,它们的定义如下
For w
and b
, in the sample code, they are defined as following
w = tf.Variable(0.0, name='weights')
b = tf.Variable(0.0, name='bias')
我如下更改了这两行代码
And I changed these two lines of code a little bit as following
w = tf.get_variable('weights', [], dtype=tf.float32)
b = tf.get_variable('bias', [], dtype=tf.float32)
对于本实验,对于这两个版本,我得到了两个不同的total_loss/n_samples
.更具体地说,在原始版本中,我随时可以得到确定性的结果,1539.0050282141283
.但是,在修改后的版本中,我在不同的运行时间得到了不确定的结果,例如,total_loss/n_samples
可能是1531.3039793868859
,1526.3752814714044
,...等.
For this experiment, I got two different total_loss/n_samples
for those two versions. More specifically, in the original version, I got a deterministic result at anytime, 1539.0050282141283
. But, in the modified version, I got undeterministic results at different running time, for example, total_loss/n_samples
could be 1531.3039793868859
, 1526.3752814714044
, ... etc.
tf.Variable()
和tf.get_variable()
有什么区别?
推荐答案
tf.Variable
在创建时接受一个初始值(一个常数),这解释了使用时的确定性结果.
tf.Variable
accepts an initial value upon creation (a constant), this explains deterministic results when you use it.
tf.get_variable
略有不同:它具有initializer
参数,默认情况下为None
,其解释如下:
tf.get_variable
is slightly different: it has an initializer
argument, by default None
, which is interpreted like this:
如果
initializer
是None
(默认值),则将使用在变量范围中传递的默认初始化程序.如果该名称也是None
,则将使用glorot_uniform_initializer
.
If
initializer
isNone
(the default), the default initializer passed in the variable scope will be used. If that one isNone
too, aglorot_uniform_initializer
will be used.
由于您没有传递初始化程序,因此该变量具有统一的随机初始值.
Since you didn't pass an initializer, the variable got uniform random initial value.
这篇关于使用tf.Variable()和tf.get_variable()时的结果不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!