在TensorFlow中,如何在定義變量時的同時定義它自己的collection名稱呢?

方法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>]

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章