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)

不行,返回[]

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