当前位置:网站首页>Understanding of batchnorm2d() function in pytorch
Understanding of batchnorm2d() function in pytorch
2022-07-27 10:14:00 【Chen Zhuangshi's programming life】
List of articles
1. brief introduction
Machine learning , Before model training , The data need to be normalized , Make it uniformly distributed . In the process of deep neural network training , Usually a workout is a batch, Not all data . Every batch Having different distributions produces internal covarivate shift problem —— In the process of training , The data distribution will change , It brings difficulties to the learning of the next layer network .Batch Normalization Force the data back to the mean value of 0, The variance of 1 On the Zhengtai distribution of , On the one hand, it makes the data distribution consistent , On the other hand, avoid the disappearance of the gradient .
2. Calculation
As shown in the figure :
Above is input data , Its shape=[5, 3, h, w]
Step1: Calculation Mean value under the same channel , As shown in the red block , Both represent the same channel 
Step2: Calculation Variance under the same channel , As shown in the red block , Both represent the same channel 
Step3: Normalize each data under the current channel 
Among them x Represents a specific point , Such as x = X[0][0][0][0][0] This data point .
Step4: Add zoom and translation variables γ \gamma γ and β \beta β, The normalized value is 
among , ϵ \epsilon ϵ Is a set constant , The default is 1e^-5, Its function is to prevent the elimination of 0. γ \gamma γ and β \beta β These two parameters generally do not need our attention ( If , Parameters affine=true, We need to give ).
3. Pytorch Medium nn.BatchNorm2d() Function interpretation
It mainly requires input 4 Parameters :
(1)num_features: The input data is shape It's usually [batch_size, channel, height, width], num_features Among them channel;
(2)eps: A value added to the denominator , The purpose is to calculate the stability of , Default :1e-5;
(3)momentum: An estimation parameter for the mean and variance in the operation process , The default value is 0.1.
(4)affine: When set to true when , Given the coefficient matrix that can be learned γ \gamma γ and β \beta β
4. Code example :
import torch
data = torch.ones(size=(2, 2, 3, 4))
data[0][0][0][0] = 25
print("data = ", data)
print("\n")
print("========================= Use encapsulated BatchNorm2d() Calculation ================================")
BN = torch.nn.BatchNorm2d(num_features=2, eps=0, momentum=0)
BN_data = BN(data)
print("BN_data = ", BN_data)
print("\n")
print("========================= Calculate by yourself ================================")
x = torch.cat((data[0][0], data[1][0]), dim=1) # 1. Splice the same channel ( That is, treat the same channel as a whole )
x_mean = torch.Tensor.mean(x) # 2. Calculate the average value of ownership of the same channel ( That is, the mean value after splicing )
x_var = torch.Tensor.var(x, False) # 3. Calculate the variance of ownership of the same channel ( That is, the variance after splicing )
# 4. Use the first number to find BatchNorm After the value of
bn_first = ((data[0][0][0][0] - x_mean) / ( torch.pow(x_var, 0.5))) * BN.weight[0] + BN.bias[0]
print("bn_first = ", bn_first)
Running results :
(1) The original data 
(2) Use BatchNorm() function

(3) Calculate the normalized value of the batch by yourself 
The data of the two boxes marked red in the figure are completely equal , End of the flower !!!
notes :
There is reference The article
边栏推荐
- 邮件服务器
- 文件上传漏洞相关
- Snowflake vs. Databricks谁更胜一筹?2022年最新战报
- After one year, the paper was finally accepted by the international summit
- Open3d library installation, CONDA common instructions, importing open3d times this error solving environment: failed with initial frozen solve Retrying w
- Gbase 8A MPP cluster capacity expansion practice
- Visual slam lecture notes (I): Lecture 1 + Lecture 2
- Practice and exploration of overseas site Seata of ant group
- Review summary of engineering surveying examination
- Leetcode.1260. 2D grid migration____ In situ violence / dimensionality reduction + direct positioning of circular array
猜你喜欢

Shell综合应用案例,归档文件、发送消息
![Flash memory usage and stm32subemx installation tutorial [day 3]](/img/ff/43ef2d0e1bdfd24fbc5c99e76455f1.png)
Flash memory usage and stm32subemx installation tutorial [day 3]

Shell流程控制(重点)、if 判断、case 语句、let用法、for 循环中有for (( 初始值;循环控制条件;变量变化 ))和for 变量 in 值 1 值 2 值 3… 、while 循环

When I went to oppo for an interview, I got numb

WGAN、WGAN-GP、BigGAN

食品安全 | 无糖是真的没有糖吗?这些真相要知道

备战金九银十Android面试准备(含面试全流程,面试准备工作面试题和资料等)
![Shell中的文本处理工具、cut [选项参数] filename 说明:默认分隔符是制表符、awk [选项参数] ‘/pattern1/{action1}filename 、awk 的内置变量](/img/ed/941276a15d1c4ab67d397fb3286022.png)
Shell中的文本处理工具、cut [选项参数] filename 说明:默认分隔符是制表符、awk [选项参数] ‘/pattern1/{action1}filename 、awk 的内置变量

蚂蚁集团境外站点 Seata 实践与探索

Shell的read 读取控制台输入、read的使用
随机推荐
NFT系统开发-教程
并发之park与unpark说明
面试必备:虾皮服务端15连问
Configuration of pytorch deep learning environment based on cuda10.0
Vs2019 Community Edition Download tutorial (detailed)
Ant高级-task
二叉树习题总结
3D人脸重建:Joint 3D Face Reconstruction and Dense Alignment with position Map Regression Network
Live countdown 3 days sofachannel 29 P2P based file and image acceleration system Dragonfly
Text processing tool in shell, cut [option parameter] filename Description: the default separator is the built-in variable of tab, awk [option parameter] '/pattern1/{action1}filename and awk
SE(Squeeze and Excitation)模块的理解以及代码实现
Why is redis so fast? Redis threading model and redis multithreading
Shell运算符、$((运算式))” 或 “$[运算式]、expr方法、条件判断、test condition、[ condition ]、两个整数之间比较、按照文件权限进行判断、按照文件类型进行判断
Switch port mirroring Configuration Guide
Shell流程控制(重点)、if 判断、case 语句、let用法、for 循环中有for (( 初始值;循环控制条件;变量变化 ))和for 变量 in 值 1 值 2 值 3… 、while 循环
圆环工件毛刺(凸起)缺口(凹陷)检测案例
省应急管理厅:广州可争取推广幼儿应急安全宣教经验
食品安全 | 还在吃酵米面吗?当心这些食物有毒!
文件上传漏洞相关
食品安全 | 菜板环境很重要,这些使用细节你知道吗?