【TensorFlow】batch normalization的TensorFlow实现

本文介绍了在TensorFlow中使用tf.nn.batch_normalization实现批量归一化的详细过程,包括滑动平均的运用和理解。通过示例代码,解释了tf.control_dependencies和tf.identity的作用,以及ema.apply()和ema.average()的功能。
摘要由CSDN通过智能技术生成

TensorFlow中batch normalization的实现有两种,一种是tf.layers.batch_normalization,一种是tf.nn.batch_normalization,在此处,我使用的是第二种。

原理就不贴了。

代码如下:

def BN(input, isTraining=False, name='BatchNorm', moving_decay=0.9, eps=1e-4):
    #print('BN isTraining: ', isTraining)
    _in = input
    shape = input.get_shape().as_list()
    assert len(shape) in [2, 4]

    with tf.variable_scope(name):
        gamma = tf.Variable(tf.constant(1.0, dtype=tf.float32, shape=[shape[-1]]), name='gamma')
        beta = tf.Variable(tf.constant(0.0, dtype=tf.float32, shape=[shape[-1]]), name='beta')

        axes = list(range(len(shape)-1))
        batch_mean, batch_var = tf.nn.moments(input, axes=axes)

        ema = tf.train.ExponentialMovingAverage(decay=moving_decay)
        def mean_var_with_update():
            ema_apply_op = ema
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值