TensorFlow 变量和常量 [英] TensorFlow Variables and Constants
问题描述
我是 tensorflow 的新手,我无法理解变量和常量的区别,我的想法是我们将变量用于方程,常量用于直接值,但是为什么代码 #1 只有效,为什么代码不可用#2 和 #3,请解释在哪些情况下我们必须先运行我们的图 (a) 然后我们的变量 (b) 即
(a) session.run(model)(b) 打印(session.run(y))
在这种情况下我可以直接执行这个命令即
print(session.run(y))
代码#1:
x = tf.constant(35, name='x')y = tf.Variable(x + 5, name='y')模型 = tf.global_variables_initializer()使用 tf.Session() 作为会话:session.run(模型)打印(会话.运行(y))
代码#2:
x = tf.Variable(35, name='x')y = tf.Variable(x + 5, name='y')模型 = tf.global_variables_initializer()使用 tf.Session() 作为会话:session.run(模型)打印(会话.运行(y))
代码#3:
x = tf.constant(35, name='x')y = tf.constant(x + 5, name='y')模型 = tf.global_variables_initializer()使用 tf.Session() 作为会话:session.run(模型)打印(会话.运行(y))
在 TensorFlow 中,常量和变量之间的区别在于,当您声明一些 constant,它的值在以后不能改变(同样初始化应该带值,不带操作).
然而,当您声明一个变量时,您可以在tf.assign() 方法的未来(初始化可以通过值或操作).
函数 tf.global_variables_initializer() 用作为参数传递的值,但它在异步模式下工作,因此当变量之间存在依赖关系时无法正常工作.
您的第一个代码 (#1) 工作正常,因为不依赖于变量初始化,并且常量是用一个值构造的.
由于 tf.global_variables_initializer()
的异步行为,第二个代码 (#2) 不起作用.您可以使用 tf.variables_initializer() 修复它,如下所示:
x = tf.Variable(35, name='x')model_x = tf.variables_initializer([x])y = tf.Variable(x + 5, name='y')model_y = tf.variables_initializer([y])使用 tf.Session() 作为会话:session.run(model_x)session.run(model_y)打印(会话.运行(y))
第三个代码 (#3) 不能正常工作,因为您试图用一个操作初始化一个常量,这是不可能的.要解决它,适当的策略是 (#1).
关于你的最后一个问题.当你的计算图中有变量(b) print(session.run(y))
时,你需要运行(a) session.run(model)
.>
I am new to tensorflow , I am not able to understand the difference of variable and constant, I get the idea that we use variables for equations and constants for direct values , but why code #1 works only and why not code#2 and #3, and please explain in which cases we have to run our graph first(a) and then our variable(b) i.e
(a) session.run(model)
(b) print(session.run(y))
and in which case I can directly execute this command i.e
print(session.run(y))
Code #1 :
x = tf.constant(35, name='x')
y = tf.Variable(x + 5, name='y')
model = tf.global_variables_initializer()
with tf.Session() as session:
session.run(model)
print(session.run(y))
Code #2 :
x = tf.Variable(35, name='x')
y = tf.Variable(x + 5, name='y')
model = tf.global_variables_initializer()
with tf.Session() as session:
session.run(model)
print(session.run(y))
Code #3 :
x = tf.constant(35, name='x')
y = tf.constant(x + 5, name='y')
model = tf.global_variables_initializer()
with tf.Session() as session:
session.run(model)
print(session.run(y))
In TensorFlow the differences between constants and variables are that when you declare some constant, its value can't be changed in the future (also the initialization should be with a value, not with operation).
Nevertheless, when you declare a Variable, you can change its value in the future with tf.assign() method (and the initialization can be achieved with a value or operation).
The function tf.global_variables_initializer() initialises all variables in your code with the value passed as parameter, but it works in async mode, so doesn't work properly when dependencies exists between variables.
Your first code (#1) works properly because there is no dependencies on variable initialization and the constant is constructed with a value.
The second code (#2) doesn't work because of the async behavior of tf.global_variables_initializer()
. You can fix it using tf.variables_initializer() as follows:
x = tf.Variable(35, name='x')
model_x = tf.variables_initializer([x])
y = tf.Variable(x + 5, name='y')
model_y = tf.variables_initializer([y])
with tf.Session() as session:
session.run(model_x)
session.run(model_y)
print(session.run(y))
The third code (#3) doesn't work properly because you are trying to initialize a constant with an operation, that isn't possible. To solve it, an appropriate strategy is (#1).
Regarding to your last question. You need to run (a) session.run(model)
when there are variables in your calculation graph (b) print(session.run(y))
.
这篇关于TensorFlow 变量和常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!