zoukankan      html  css  js  c++  java
  • 深度学习面试题22:批量归一化在实践中的应用

    目录

      指数移动平均

      BN在卷积网络中的使用

      参考资料


    假设已经训练好一个带有BN操作的卷积神经网络,但是在使用它预测时,往往每次只输入一个样本,那么经过该网络时,计算平均值和方差的意义就不大了,常采用的策略是计算训练阶段的平均值和方差的指数移动平均,然后在预测阶段使用它们作为BN操作时的平均值和方差。

    指数移动平均

    假设变量xt随时间t变化,按照以下规则定义其指数移动平均值

    假设α=0.7

    当t=1时,x1=5,则ema(1)=x1=5

    当t=2时,x2=10,则ema(2)=α*ema(1)+(1-α)*x2=0.7*5+(1-0.7)*10=6.5

    当t=3时,x3=15,则ema(3)=α*ema(2)+(1-α)*x3=0.7*6.5+(1-0.7)*15=9.05

    当t=4时,x4=20,则ema(4)=α*ema(3)+(1-α)*x4=0.7*9.05+(1-0.7)*20=12.335

    经过四次运算后,最后的移动平均值为12.335

    对应代码为:

    import numpy as np
    import matplotlib.pyplot as plt
    t = [1,2,3,4]
    x = [5,10,15,20]
    res = [x[0]]
    for i in x[1:]:
        a = 0.7*res[-1]+0.3*i
        res.append(a)
    plt.plot(t,x,"r")
    plt.plot(t,res,"b")
    View Code

    换一个复杂一点的图像观测指数移动平均,可以发现,他在会保留原来的走势,并且适应新的走势:

    import numpy as np
    import random
    import matplotlib.pyplot as plt
    random.seed(20190725)
    t = np.linspace(-5,5,100)
    x = [-i**2+random.random()*15 for i in t]
    res = [x[0]]
    for i in x[1:]:
        a = 0.7*res[-1]+0.3*i
        res.append(a)
    plt.plot(t,x,"r")
    plt.plot(t,res,"b")
    View Code

     返回目录

    BN在卷积网络中的使用

    以下图BN操作为例说明:

     

     

    每个BN层最终都会保存一对最终的均值和方差,可以用于测试阶段

     返回目录

    参考资料

    《图解深度学习与神经网络:从张量到TensorFlow实现》_张平

    Batch Normalization_ Accelerating Deep Network Training by Reducing Internal Covariate Shift

     返回目录

  • 相关阅读:
    什么是webview
    juqery.fn.extend和jquery.extend
    LeetCode
    5. Longest Palindromic Substring
    42. Trapping Rain Water
    11. Container With Most Water
    621. Task Scheduler
    49. Group Anagrams
    739. Daily Temperatures
    3. Longest Substring Without Repeating Characters
  • 原文地址:https://www.cnblogs.com/itmorn/p/11243099.html
Copyright © 2011-2022 走看看