group normalization

1、 Theory

look for this link for more information, actually only this image can illustrate the group normalization.you can ignore the rest of this artical.

2、 Code 

check this link for detailes about the formulation and the theory of the group normalziation, cause it's very good for you to understand the advanced konwledge about GL

# group normalization
import torch

# input = (batch_size,C,H,W)
batch_size = 1
C = 6
H = 3
W = 3

# divide input channels(C) into small groups
group = 2
input = torch.arange(batch_size*C*H*W).reshape(batch_size,C,H,W).float()
group_input = input.view(batch_size,group,-1)

# calculate the mean and square mean,then calculate var of the group_input
mean = group_input.mean(dim=-1,keepdim=True)
mean_square = (group_input**2).mean(dim=-1,keepdim=True)
var = mean_square - mean**2
eps = 1e-5
group_input_norm = (group_input - mean) / torch.sqrt(var + eps)
group_input_norm = group_input_norm.view(batch_size,C,-1)

# define gamma(scale) and beta(shift)
scale = torch.ones(C,).view(1,-1,1)
shift = torch.zeros(C,).view(1,-1,1)
output = group_input_norm * scale + shift
output = output.view(batch_size,C,H,W)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值