我正在尝试获取变量范围内的所有变量,如所解释的here https://stackoverflow.com/questions/36533723/tensorflow-get-all-variables-in-scope。然而,该行tf.get_collection(tf.GraphKeys.VARIABLES, scope='my_scope')
即使该范围内有变量,也会返回一个空列表。
这是一些示例代码:
import tensorflow as tf
with tf.variable_scope('my_scope'):
a = tf.Variable(0)
print tf.get_collection(tf.GraphKeys.VARIABLES, scope='my_scope')
打印[]
.
如何获取其中声明的变量'my_scope'
?
The tf.GraphKeys.VARIABLES
自 TensorFlow 0.12 起,集合名称已被弃用。使用tf.GraphKeys.GLOBAL_VARIABLES
将给出预期结果:
with tf.variable_scope('my_scope'):
a = tf.Variable(0)
print tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='my_scope')
# ==> '[<tensorflow.python.ops.variables.Variable object at 0x7f33f67ebbd0>]'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)