深度学习中的batch normalization

本文介绍了深度学习中Batch Normalization的作用,旨在减少Internal Covariate Shift并加速模型训练。详细阐述了TensorFlow中的实现,并指出在训练和测试时,均值和方差的计算方式不同,训练时使用mini-batch,测试时采用样本滑动平均值。此外,还提供了相关资源链接以供深入研究。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    论文提出了batch normalization,用于减少Internal Covariate Shift,防止梯度弥散,还可加速模型的训练。本文就tensorflow中的实现过程和训练测试时的使用差异进行说明。  

图1 算法说明

    tf实现函数体如下:

  with ops.name_scope(name, "batchnorm", [x, mean, variance, scale, offset]):
    inv = math_ops.rsqrt(variance + variance_epsilon)
    if scale is not None:
      inv *= scale
    # Note: tensorflow/contrib/quantize/python/fold_batch_norms.py depends on
    # the precise order of ops that are generated by the expression below.
    return x * math_ops.cast(inv, x.dtype) + math_ops.cast(
        offset - mean * inv if offset is not None else -mean * inv, x.dtype)

    把图1中normalize和scale and shift(第3行代入4行)合并,就得到函数体中return结果。

    了解过原理和实现后,看下如何使用。此处以此处代码为例。

  • 使用位置:非线性函数之前
Y1l = tf.matmul(XX, W1)
Y1bn, update_ema1 = batchnorm(Y1l, O1, S1, tst, iter)
Y1 = tf.nn.sigmoid(Y1bn)
  • 训练和测试时的不同:训练时,均值和方差由mini-batch直接获得;测试时,均值和方差通过样本的滑动平均值获得。
def batchnorm(Ylogits, Offset, Scale, is_test, iteration):
    exp_moving_avg = tf.train.ExponentialMovingAverage(0.998, iteration) # adding the iteration prevents from averaging across non-existing iterations
    bnepsilon = 1e-5
    mean, variance = tf.nn.moments(Ylogits, [0])
    update_moving_averages = exp_moving_avg.apply([mean, variance])
    m = tf.cond(is_test, lambda: exp_moving_avg.average(mean), lambda: mean)
    v = tf.cond(is_test, lambda: exp_moving_avg.average(variance), lambda: variance)
    Ybn = tf.nn.batch_normalization(Ylogits, m, v, Offset, Scale, bnepsilon)
    return Ybn, update_moving_averages

    关于滑动平均值的计算过程可查看这里

    关于batch normalization的更多讨论可查看这里


参考文献:

  1. https://github.com/GoogleCloudPlatform/tensorflow-without-a-phd
  2. http://www.cnblogs.com/guqiangjs/p/7808326.html
  3. https://www.zhihu.com/question/38102762/answer/85238569
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值