为了研究一下tensorflow的name_scope
和variable_scope
到底有啥区别,我对Variable和Summary对象分别试验了这两种scope。直接上代码:
1.对Variable先加name_scope
,再加variable_scope
import tensorflow as tf
with tf.name_scope('ns1'): #name_scope
v1 = tf.get_variable('v1', shape=(1,))
with tf.variable_scope('vs1'): #variable_scope
v2 = tf.get_variable('v2', shape=(1,))
v1v2_coll = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,scope='ns1') #[]
v2_coll = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,scope='vs1') #'vs1/v2:0'
#name_scope对变量无效, 'vs1/v2:0'的prefix里没有'ns1/'
2.对Variable先加variable_scope
,再加name_scope
with tf.variable_scope('vs2'): #variable_scope
v1 = tf.get_variable('v1', shape=(1,))
with tf.name_scope('ns2'): #name_scope
v2 = tf.get_variable('v2', shape=(1,))
v1v2_coll = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,scope='vs2') #'vs2/v1:0'
v2_coll = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,scope='ns2') #[]
#name_scope对变量无效, 'vs2/v1:0'的prefix里没有'ns2/'
3.对Summary先加name_scope
,再加variable_scope
with tf.name_scope('ns3'): #name_scope
tf.summary.histogram('sum_ns', tf.convert_to_tensor([1]))
with tf.variable_scope('vs3'): #variable_scope
tf.summary.histogram('sum_nsvs', tf.convert_to_tensor([1]))
sum_ns_coll = tf.get_collection(tf.GraphKeys.SUMMARIES,scope='ns3')
#'ns3/sum_ns:0', 'ns3/vs3/sum_vs:0'
sum_nsvs_coll = tf.get_collection(tf.GraphKeys.SUMMARIES,scope='ns3/vs3')
#'ns3/vs3/sum_nsvs:0'
4.对Summary先加variable_scope
,再加name_scope
with tf.variable_scope('vs4'): #variable_scope
tf.summary.histogram('sum_vs', tf.convert_to_tensor([1]))
with tf.name_scope('ns4'): #name_scope
tf.summary.histogram('sum_ns', tf.convert_to_tensor([1]))
sum_vs_coll = tf.get_collection(tf.GraphKeys.SUMMARIES,scope='vs4')
#'vs4/sum_vs:0', 'vs4/ns4/sum_ns:0'
sum_vsns_coll = tf.get_collection(tf.GraphKeys.SUMMARIES,scope='vs4/ns4')
#'vs4/ns4/sum_ns:0'