问题描述
当我阅读 global_step 的文档时,我想到了这个问题.这里明确声明 global_step 不可训练.
This question came to me when I read the documentation of global_step.Here it explicitly declares global_step is not trainable.
global_step_tensor = tf.Variable(10, trainable=False, name='global_step')
sess = tf.Session()
sess = tf.Session()
print('global_step: %s' % tf.train.global_step(sess, global_step_tensor))
print('global_step: %s' % tf.train.global_step(sess, global_step_tensor))
根据我的理解,可训练意味着可以在 sess.run() 期间更改该值.我试图将它声明为可训练和不可训练并得到相同的结果.所以我不明白为什么我们需要宣布它不可训练.
From my understanding, trainable means that the value could be changed during sess.run(). I have tried to declare it both trainable and non-trainable and got the same results. So I didn't understand why we need to declare it not trainable.
我阅读了 trainable 的文档,但不太明白.
I read the documentation of trainable but didn't quite get it.
所以我的问题是:
- 在 sess.run() 期间是否可以更改不可训练的变量值,反之亦然?
- 使变量不可训练有什么意义?
推荐答案
那不是可训练变量的定义.任何变量都可以在 sess.run()
期间修改(这就是为什么它们是变量而不是常量).
That is not the definition of a trainable variable. Any variable can be modified during a sess.run()
(That's why they are variables and not constants).
可训练变量和不可训练变量之间的区别用于让 Optimizer
知道他们可以对哪些变量起作用.定义 tf.Variable()
时,设置 trainable=True
(默认值)会自动将变量添加到 GraphKeys.TRAINABLE_VARIABLES
集合.在训练期间,优化器通过 tf.trainable_variables()
获取该集合的内容,并将训练应用于所有这些.
The distinction between trainable variables and non-trainable variables is used to let Optimizer
s know which variables they can act upon.When defining a tf.Variable()
, setting trainable=True
(the default) automatically adds the variable to the GraphKeys.TRAINABLE_VARIABLES
collection.During training, an optimizer gets the content of that collection via tf.trainable_variables()
and applies the training to all of them.
不可训练变量的典型示例是 global_step
,因为它的值确实会随时间变化(通常在每次训练迭代时 +1),但您不想应用优化算法.
The typical example of a non-trainable variable is global_step
, because its value does change over time (+1 at each training iteration, typically), but you don't want to apply an optimization algorithm to it.
这篇关于tf.variable 在 TensorFlow 中是可训练的是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!