BN层在实际中应用广泛。

上一次总结了使得训练变得简单的方法,比如SGD+momentum RMSProp Adam,BN是另外的方法。

cell 1 依旧是初始化设置

cell 2 读取cifar-10数据

cell 3 BN的前传

 # Check the training-time forward pass by checking means and variances
# of features both before and after batch normalization # Simulate the forward pass for a two-layer network
N, D1, D2, D3 = 200, 50, 60, 3
X = np.random.randn(N, D1)
W1 = np.random.randn(D1, D2)
W2 = np.random.randn(D2, D3)
a = np.maximum(0, X.dot(W1)).dot(W2) print 'Before batch normalization:'
print ' means: ', a.mean(axis=0)
print ' stds: ', a.std(axis=0) # Means should be close to zero and stds close to one
print 'After batch normalization (gamma=1, beta=0)'
a_norm, _ = batchnorm_forward(a, np.ones(D3), np.zeros(D3), {'mode': 'train'})
print ' mean: ', a_norm.mean(axis=0)
print ' std: ', a_norm.std(axis=0) # Now means should be close to beta and stds close to gamma
gamma = np.asarray([1.0, 2.0, 3.0])
beta = np.asarray([11.0, 12.0, 13.0])
a_norm, _ = batchnorm_forward(a, gamma, beta, {'mode': 'train'})
print 'After batch normalization (nontrivial gamma, beta)'
print ' means: ', a_norm.mean(axis=0)
print ' stds: ', a_norm.std(axis=0)

  相应的核心代码:

     buf_mean = np.mean(x, axis=0)
buf_var = np.var(x, axis=0)
x_hat = x - buf_mean
x_hat = x_hat / (np.sqrt(buf_var + eps)) out = gamma * x_hat + beta
#running_mean = momentum * running_mean + (1 - momentum) * sample_mean
#running_var = momentum * running_var + (1 - momentum) * sample_var
running_mean = momentum * running_mean + (1- momentum) * buf_mean
running_var = momentum * running_var + (1 - momentum) * buf_var

  running_mean  running_var 是在test时使用的,test时不再另外计算均值和方差。

  test 时的前传核心代码:

 x_hat = x - running_mean
x_hat = x_hat / (np.sqrt(running_var + eps))
out = gamma * x_hat + beta

cell 5 BN反向传播

  通过反向传播,计算beta gamma等参数。

  核心代码:

   dx_hat = dout * cache['gamma']
dgamma = np.sum(dout * cache['x_hat'], axis=0)
dbeta = np.sum(dout, axis=0)
#x_hat = x - buf_mean
#x_hat = x_hat / (np.sqrt(buf_var + eps))
t1 = cache['x'] - cache['mean']
t2 = (-0.5)*((cache['var'] + cache['eps'])**(-1.5))
t1 = t1 * t2
d_var = np.sum(dx_hat * t1, axis=0) tmean1 = (-1)*((cache['var'] + cache['eps'])**(-0.5))
d_mean = np.sum(dx_hat * tmean1, axis=0) tmean1 = (-1)*tmean1
tx1 = dx_hat * tmean1
tx2 = d_mean * (1.0 / float(N))
tx3 = d_var * (2 * (cache['x'] - cache['mean']) / N)
dx = tx1 + tx2 + tx3

cell 9 BN与其他层结合

  形成的结构:   {affine - [batch norm] - relu - [dropout]} x (L - 1) - affine - softmax

  原理依旧。

之后是对cell 9 的模型,对cifar-10数据训练。

值得注意的是:

  使用BN后,正则项与dropout层的需求降低。可以使用较高的学习率加快模型收敛。

附:通关CS231n企鹅群:578975100 validation:DL-CS231n

CS231n 2016 通关 第五、六章 Batch Normalization 作业的更多相关文章

  1. CS231n 2016 通关 第五章 Training NN Part1

    在上一次总结中,总结了NN的基本结构. 接下来的几次课,对一些具体细节进行讲解. 比如激活函数.参数初始化.参数更新等等. ====================================== ...

  2. CS231n 2016 通关 第五、六章 Fully-Connected Neural Nets 作业

    要求:实现任意层数的NN. 每一层结构包含: 1.前向传播和反向传播函数:2.每一层计算的相关数值 cell 1 依旧是显示的初始设置 # As usual, a bit of setup impor ...

  3. CS231n 2016 通关 第五、六章 Dropout 作业

    Dropout的作用: cell  1 - cell 2 依旧 cell 3 Dropout层的前向传播 核心代码: train 时: if mode == 'train': ############ ...

  4. CS231n 2016 通关 第六章 Training NN Part2

    本章节讲解 参数更新 dropout ================================================================================= ...

  5. CS231n 2016 通关 第四章-NN 作业

    cell 1 显示设置初始化 # A bit of setup import numpy as np import matplotlib.pyplot as plt from cs231n.class ...

  6. CS231n 2016 通关 第三章-SVM与Softmax

    1===本节课对应视频内容的第三讲,对应PPT是Lecture3 2===本节课的收获 ===熟悉SVM及其多分类问题 ===熟悉softmax分类问题 ===了解优化思想 由上节课即KNN的分析步骤 ...

  7. CS231n 2016 通关 第四章-反向传播与神经网络(第一部分)

    在上次的分享中,介绍了模型建立与使用梯度下降法优化参数.梯度校验,以及一些超参数的经验. 本节课的主要内容: 1==链式法则 2==深度学习框架中链式法则 3==全连接神经网络 =========== ...

  8. CS231n 2016 通关 第三章-Softmax 作业

    在完成SVM作业的基础上,Softmax的作业相对比较轻松. 完成本作业需要熟悉与掌握的知识: cell 1 设置绘图默认参数 mport random import numpy as np from ...

  9. CS231n 2016 通关 第三章-SVM 作业分析

    作业内容,完成作业便可熟悉如下内容: cell 1  设置绘图默认参数 # Run some setup code for this notebook. import random import nu ...

随机推荐

  1. react webapp 开发小结

    1.监听props的方法 componentWillReceiveProps(nextProps) { // } 2.监听state的方法 3.props 传递的方法 <AlarmList {. ...

  2. WMS8_基本操作

    建立分拣[收货.出货.领料]         点击仪表盘上的任何一个 All operations 链接切换至分拣 列表视图         点击 creae 按钮,建立一个新的分拣     part ...

  3. 【转载】读懂IL代码就这么简单(三)完结篇

    一 前言 写了两篇关于IL指令相关的文章,分别把值类型与引用类型在 堆与栈上的操作区别详细的写了一遍这第三篇也是最后一篇,之所以到第三篇就结束了,是因为以我现在的层次,能理解到的都写完了,而且个人认为 ...

  4. Fighting regressions with git bisect---within git bisect algorithm

    https://www.kernel.org/pub/software/scm/git/docs/git-bisect-lk2009.html Fighting regressions with gi ...

  5. 华为OJ 名字美丽度

    这是一道坑爹的题目,为什么这么说,且看我慢慢分析-- 题目例如以下: 给出一个名字,该名字有26个字符串组成,定义这个字符串的"美丽度"是其全部字母"美丽度"的 ...

  6. ios开发动物园管理 继承多态的实现

    // // main.m // 继承 // // #import <Foundation/Foundation.h> #import "Animal.h" #impor ...

  7. mysql 较为高效的分页

    直接上代码 DaoImpl: /** * 开发转让页面展示 ,查询搜索数据,而且分页展示 * @param zrdp 搜索条件封装对象 * @return */ @SuppressWarnings(& ...

  8. wordpress系列1:安装

    https://wordpress.org/download/release-archive/ 官方中文网站:https://cn.wordpress.org/ readme.html文件,可查看Wo ...

  9. Edit conflicts

    Edit conflicts 当副本修改处和服务器版本相同处被修改并下载到本地时,就会发生文件冲突. 操作步骤如下所示: Ø 执行"SVN Update" Ø 若发生冲突,会出现如 ...

  10. 怎样得到QML package的具体API接口

    虽然我们的developer站点有丰富的API介绍,可是,有些API的介绍可能并不全,有些API也在不断地演进中. 为了得到更具体的API,我们能够通过例如以下的命令来得到更加具体的信息.比方我们对& ...