简体   繁体   English

在TensorFlow中可以训练tf.variable是什么意思

[英]What does it mean that a tf.variable is trainable in TensorFlow

This question came to me when I read the documentation of global_step . 当我阅读global_step的文档时,这个问题出现了。 Here it explicitly declares global_step is not trainable. 这里明确声明global_step不可训练。

global_step_tensor = tf.Variable(10, trainable=False, name='global_step') 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))

From my understanding, trainable means that the value could be changed during sess.run(). 根据我的理解,可训练意味着可以在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. 所以我不明白为什么我们需要声明它不可训练。

I read the documentation of trainable but didn't quite get it. 我阅读了可训练的文件,但并没有得到它。

So my question is: 所以我的问题是:

  1. Can non-trainable variable value be changed during sess.run() and vice versa? 可以在sess.run()期间更改不可训练的变量值,反之亦然?
  2. What is the point that make a variable not trainable? 使变量无法训练的重点是什么?

From my understanding, trainable means that the value could be changed during 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). sess.run()期间可以修改任何变量(这就是为什么它们是变量而不是常量)。

The distinction between trainable variables and non-trainable variables is used to let Optimizer s know which variables they can act upon. 可训练变量和非训练变量之间的区别用于让Optimizer知道它们可以对哪些变量起作用。 When defining a tf.Variable() , setting trainable=True (the default) automatically adds the variable to the GraphKeys.TRAINABLE_VARIABLES collection. 定义tf.Variable() ,设置tf.Variable() trainable=True (默认值)会自动将变量添加到GraphKeys.TRAINABLE_VARIABLES集合中。 During training, an optimizer gets the content of that collection via tf.trainable_variables() and applies the training to all of them. 在训练期间,优化器通过tf.trainable_variables()获取该集合的内容,并将训练应用于所有这些内容。

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. 不可训练变量的典型示例是global_step ,因为它的值确实随时间变化(通常在每次训练迭代时为+1),但您不希望对其应用优化算法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM