代码之家  ›  专栏  ›  技术社区  ›  Eric Auld

在TensorFlow中,如何查看批处理规范化参数?

  •  1
  • Eric Auld  · 技术社区  · 7 年前

    我用的是 tf.layers.batch_normalization 在我的网络层。如您所知,批量标准化将可训练参数gamma和beta应用于该层中的每个单位uïi,为各种输入x选择其自身的标准偏差和uïi(x)的平均值。通常gamma初始化为1,beta初始化为0。

    我感兴趣的是窥探各个单位正在学习的gamma和beta的值,以收集有关它们在网络训练后最终走向何方的统计数据。如何在每个训练实例中查看它们的当前值?

    1 回复  |  直到 7 年前
        1
  •  4
  •   Vlad-HC    7 年前

    您可以获取批处理规范化层范围内的所有变量并打印它们。例子:

    import tensorflow as tf
    
    tf.reset_default_graph()
    x = tf.constant(3.0, shape=(3,))
    x = tf.layers.batch_normalization(x)
    
    print(x.name) # batch_normalization/batchnorm/add_1:0
    
    variables = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                                  scope='batch_normalization')
    print(variables)
    
    #[<tf.Variable 'batch_normalization/gamma:0' shape=(3,) dtype=float32_ref>,
    # <tf.Variable 'batch_normalization/beta:0' shape=(3,) dtype=float32_ref>,
    # <tf.Variable 'batch_normalization/moving_mean:0' shape=(3,) dtype=float32_ref>,
    #  <tf.Variable 'batch_normalization/moving_variance:0' shape=(3,) dtype=float32_ref>]
    
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        gamma = sess.run(variables[0])
        print(gamma) # [1. 1. 1.]
    
    推荐文章