方法1:一步法
x0 = tf.get_variable('x0',[2,2], collections=['my_scope',tf.GraphKeys.GLOBAL_VARIABLES])
#或者直接省去tf.GraphKeys.GLOBAL_VARIABLES,直接爲:x0 = tf.get_variable('x0',[2,2], collections=['my_scope'])
方法2:兩步法
x1 = tf.get_variable('x1',[2,2])
tf.add_to_collection('my_scope',x1)
測試(tf.get_collection)
x0 = tf.get_variable('x0',[2,2], collections=['my_scope',tf.GraphKeys.GLOBAL_VARIABLES])
#或者直接省去tf.GraphKeys.GLOBAL_VARIABLES,直接爲:x0 = tf.get_variable('x0',[2,2], collections=['my_scope'])
x1 = tf.get_variable('x1',[2,2])
tf.add_to_collection('my_scope',x1)
tf.get_collection('my_scope')
輸出爲:
[<tf.Variable 'x0:0' shape=(2, 2) dtype=float32_ref>, <tf.Variable 'x1:0' shape=(2, 2) dtype=float32_ref>]