Tensorflow:tf.get_collection不返回范围

时间:2016-12-01 18:21:01

标签: python tensorflow

我试图将所有变量都放在变量范围内,如here所述。但是,即使该范围内存在变量,行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'中声明的变量?

1 个答案:

答案 0 :(得分:11)

自TensorFlow 0.12以来,已弃用tf.GraphKeys.VARIABLES集合名称。使用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>]'