TF 学习之命名坑---【如何在 tensorboard 中正确命名】

我看了视频刚学习到 tensorboard,感觉是视频所用的 TF 版本过低的原因,我用新版本的 TF,命名之后的权重、偏置值之类的怎么看着这么啰嗦(命名都重复了,然后我去掉了命名代码,这些图又出不来了,蛋疼)…代码如下:

def add_layer (inputs, in_size, out_size, n_layer, actvation_faction=None):
    layer_name = 'n_layer%s' % n_layer
    with tf.name_scope (layer_name):
        with tf.name_scope ('weights'):
              Weights = tf.Variable (tf.random_normal ([in_size, out_size]), name='w')
              tf.summary.histogram (layer_name+'/weights', Weights)
        with tf.name_scope ('biases'):
              biases = tf.Variable (tf.zeros ([1, out_size])+0.1, name='b')
              tf.summary.histogram (layer_name+'/biases', biases)

        with tf.name_scope ('Wx_plus_b'):
              Wx_plus_b = tf.add (tf.matmul (inputs, Weights), biases)
        if actvation_faction is None:
              outputs = Wx_plus_b
        else:
              outputs = actvation_faction (Wx_plus_b,)
              tf.summary.histogram (layer_name+'/outputs', outputs)

        return outputs

上述问题在 Zerone01 的帮助下现已解决,理解了之后,现将代码改为:

def add_layer (inputs, in_size, out_size, n_layer, actvation_faction=None):
    layer_name ='Layer_%s' % n_layer
    with tf.name_scope (layer_name):
        Weights = tf.Variable (tf.random_normal ([in_size, out_size]), name='w')
        tf.summary.histogram ('/weights', Weights)
        biases = tf.Variable (tf.zeros ([1, out_size])+0.1, name='b')
        tf.summary.histogram ('/biases', biases)
        Wx_plus_b = tf.add (tf.matmul (inputs, Weights), biases, name='wx_plus_b')
        if actvation_faction is None:
            outputs = Wx_plus_b
        else:
            outputs = actvation_faction (Wx_plus_b)
            tf.summary.histogram ('/outputs', outputs)

    return outputs

提问者:M 丶 Sulayman,发帖时间: 2018-4-27 11:16:26

我的理解是,网络的同一层使用同一个 name_scope,你那样写的话在同一层里嵌套了多个 name_scope,应该是把 name_scope 和 name 两个概念混淆了。
name_scope 相当于是给一个班级命名,name 是给班上的某位学生命名,这样即使不同班级中有同名学生也不会冲突,只要一个班里没有同名的学生就行。

可以这样写试试:

def add_layer (inputs, in_size, out_size, n_layer, actvation_faction=None):
    layer_name = 'n_layer%s' % n_layer
    with tf.name_scope (layer_name):
       Weights = tf.Variable (tf.random_normal ([in_size, out_size]), name='w')
       tf.summary.histogram (layer_name+'/weights', Weights)
       biases = tf.Variable (tf.zeros ([1, out_size])+0.1, name='b')
       tf.summary.histogram (layer_name+'/biases', biases)
       Wx_plus_b = tf.add (tf.matmul (inputs, Weights), biases,name='Wx_plus_b')

Zerone01,发表于 2018-4-28 08:25:26