tensorflow tf.name_scope tf.variable_scope 配合get_collection使用時的區別

name_scope和variable_scope 在定義tensor的時候都會在name前面加上前綴,但是在使用

tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=scope)

的使用之後配合variable_scope才能獲得對應的tensor list。

例如

with tf.variable_scope(scope,reuse=reuse):
    flat_state = tf.layers.flatten(state)
    out = tf.layers.dense(flat_state, num_actions)
tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=scope)

可以獲得這兩個tensor的list

但是

with tf.name_scope(scope,reuse=reuse):
    flat_state = tf.layers.flatten(state)
    out = tf.layers.dense(flat_state, num_actions)
tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=scope)

不行,返回[]

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