CS231n 2016 通关 第五、六章 Batch Normalization 作业
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 作业的更多相关文章
- CS231n 2016 通关 第五章 Training NN Part1
在上一次总结中,总结了NN的基本结构. 接下来的几次课,对一些具体细节进行讲解. 比如激活函数.参数初始化.参数更新等等. ====================================== ...
- CS231n 2016 通关 第五、六章 Fully-Connected Neural Nets 作业
要求:实现任意层数的NN. 每一层结构包含: 1.前向传播和反向传播函数:2.每一层计算的相关数值 cell 1 依旧是显示的初始设置 # As usual, a bit of setup impor ...
- CS231n 2016 通关 第五、六章 Dropout 作业
Dropout的作用: cell 1 - cell 2 依旧 cell 3 Dropout层的前向传播 核心代码: train 时: if mode == 'train': ############ ...
- CS231n 2016 通关 第六章 Training NN Part2
本章节讲解 参数更新 dropout ================================================================================= ...
- CS231n 2016 通关 第四章-NN 作业
cell 1 显示设置初始化 # A bit of setup import numpy as np import matplotlib.pyplot as plt from cs231n.class ...
- CS231n 2016 通关 第三章-SVM与Softmax
1===本节课对应视频内容的第三讲,对应PPT是Lecture3 2===本节课的收获 ===熟悉SVM及其多分类问题 ===熟悉softmax分类问题 ===了解优化思想 由上节课即KNN的分析步骤 ...
- CS231n 2016 通关 第四章-反向传播与神经网络(第一部分)
在上次的分享中,介绍了模型建立与使用梯度下降法优化参数.梯度校验,以及一些超参数的经验. 本节课的主要内容: 1==链式法则 2==深度学习框架中链式法则 3==全连接神经网络 =========== ...
- CS231n 2016 通关 第三章-Softmax 作业
在完成SVM作业的基础上,Softmax的作业相对比较轻松. 完成本作业需要熟悉与掌握的知识: cell 1 设置绘图默认参数 mport random import numpy as np from ...
- CS231n 2016 通关 第三章-SVM 作业分析
作业内容,完成作业便可熟悉如下内容: cell 1 设置绘图默认参数 # Run some setup code for this notebook. import random import nu ...
随机推荐
- javascript 转义函数
// 字符转义 html2Escape(sHtml) { return sHtml.replace(/[<>&"]/g, function(c) { return { ' ...
- Spring学习资料
1.马士兵视频 2.SPRING技术内幕__深入解析SPRING架构与设计原理 3.jinnianshilongnian博客 4.Spring实战 (Spring IN Action) 5.官方文档
- 怎样创建.NET Web Service http://blog.csdn.net/xiaoxiaohai123/article/details/1546941
为什么需要Web Service 在通过internet网购买商品后,你可能对配送方式感到迷惑不解.经常的情况是因配送问题找配送公司而消耗你的大量时间,对于配送公司而言这也不是一项增值服务. 为了解决 ...
- 安卓UI适配限定符
引言 对于程序在不同尺寸的Android机器上执行,对UI的适用性造成了额外的开销,只是限定符的出现,非常方便的攻克了这个问题.通过创建限定符相关的文件夹来解决资源的载入. 限定符用处 限定符(mdp ...
- Android异步载入AsyncTask具体解释
曾看见有人说过.认为非常有道理.分享一下: 技术分为术和道两种: (1)具体做事的方法是术. (2)做事的原理和原则是道. 近期项目发现个重大问题.结果打log跟踪查是AsyncTask导 ...
- Linux驱动经典面试题目
1. linux驱动分类 2. 信号量与自旋锁 3. platform总线设备及总线设备怎样编写 4. kmalloc和vmalloc的差别 5. module_init的级别 6. 加入 ...
- caffe配置Makefile.config----ubuntu16.04--重点是matlab的编译
来源: http://blog.csdn.net/daaikuaichuan/article/details/61414219 配置Makefile.config(参考:http://blog.csd ...
- [未完结]数字微分分析法的直线绘制(DDA)
注意! 本文被第1次更新,可能存在后续更新 直线画法 直线的斜截式方程 在二维空间下,一条直线的方程可以被描述为若干种形式,其中比较常见的一种是斜截式方程: \[y=kx+b\] 其中\(k\)称为直 ...
- Java RESTful 框架
[转载] 最好的8个 Java RESTful 框架 - 2015 Top 8 Java RESTful Micro Frameworks – Pros/Cons - 2017 Restlet - f ...
- javaScript中innerHTML,innerText,outerHTML,outerText的区别
开头说下innerText和outerText只在chrome浏览器中有效 定义和用法 innerHTML 属性设置或返回表格行的开始和结束标签之间的 HTML,包括标签. 来看代码 <!DOC ...