如何在 Tensorflow 中增加一个变量? [英] How to Increment a Variable in Tensorflow?
问题描述
当我尝试在 Tensorflow 中使用主管时,我意识到:
When trying to use the supervisor in Tensorflow I was made aware that :
您的训练操作负责增加全局步长值.
your training op is responsible for incrementing the global step value.
(参考)
那么如何在 Tensorflow 中的图表中增加一个变量?
So how do you increment a variable in a graph in Tensorflow?
推荐答案
非常简单的解决方案:
global_step = tf.Variable(1, name='global_step', trainable=False, dtype=tf.int32)
increment_global_step_op = tf.assign(global_step, global_step+1)
然后当你想增加它时,只需在当前 tf.Session
sess
下运行该操作.
Then when you want to increment it, just run that op under the current tf.Session
sess
.
step = sess.run(increment_global_step_op)
放在step
中的结果是自增后的自增变量的值.在这种情况下,global_step 的值增加后.所以2
.
The result placed in step
is the value of the incremented variable after the increment. In this case, the value of global_step after being incremented. So 2
.
如果您像我一样将其用于 global_step,请将其与您的 training_op
一起运行.
If you're using this for global_step like me, run it along with your training_op
.
result = sess.run([out, increment_global_step_op], {x: [i]})
这篇关于如何在 Tensorflow 中增加一个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!