常用归一化/正则化层:InstanceNorm1d、InstanceNorm2d、

归一化

零、前言

1.官网链接

https://pytorch.org/docs/stable/nn.html#normalization-layers

2.归一化公式

常用归一化/正则化层:InstanceNorm1d、InstanceNorm2d、
函数内部实现其实就是严格完成上面图中的公式,其中E(x)表示均值或者期望,而Var(x)则表示对应的方差。

; 3.介绍

批量归一化与实例归一化的最大区别在于计算均值及方差的依据不同, 实例归一化是对每个样本沿着通道方向独立对各个通道进行计算,而批量归一化则是对所有样本沿着batch的方向对各个通道分别进行计算
比如:
输入特征图形状为:(2,3,256,512),表示有两个256×512的特征图,特征图通道数为3,假设为RGB三个通道
1)实例归一化:

  • 依次对样本1,样本2分别计算R、G、B三个通道的均值、方差,每次计算其实是对256×512个元素值记性计算;
  • 依据计算出的均值和方差,对各通道的元素实现图中的公式;

常用归一化/正则化层:InstanceNorm1d、InstanceNorm2d、

2)批量归一化:

  • 对整个批次的样本,对各个通道分别求出均值和方差,每次计算其实是对2×256×512个元素值记性计算;
  • 依据计算出的均值和方差,对各通道的元素实现图中的公式;
    常用归一化/正则化层:InstanceNorm1d、InstanceNorm2d、

一、InstanceNorm1d

1. 介绍

一维实例归一化:对一个批次中每个样本,依次按照通道计算对应的均值及均方差。

torch.nn.InstanceNorm1d(
     num_features,
     eps=1e-05,
     momentum=0.1,
     affine=False,
     track_running_stats=False,
     device=None,
     dtype=None)
Input: (N, C, L)
Output: (N, C, L)

2.实例

import torch
import math

epsilon=1e-5
N,C,L=2,3,5
inp=torch.randint(5,size=(N,C,L),dtype=torch.float32)
print(inp)
print("-"*25)

mean_list=[]
for b in range(N):
    for c in range(C):
        mean_list.append(torch.sum(inp[b,c,:])/L)

var_list=[]
for b in range(N):
    for c in range(C):

        var_list.append(torch.var(inp[b,c,:]))

print(mean_list)
print(var_list)

out=torch.zeros_like(inp)

for b in range(N):
    for c in range(C):
        index=b*C+c
        out[b,c,:]=(inp[b,c,:]-mean_list[index])/math.sqrt(var_list[index]+epsilon)

print(out)

print("-"*25)

out1=torch.nn.InstanceNorm1d(C)(inp)
print(out1)

二、InstanceNorm2d

1. 介绍

二维实例归一化:计算过程及调用方式同一维实例归一化,二者区别主要在于输入特征形状的变化。

torch.nn.InstanceNorm2d(
    num_features,
    eps=1e-05,
    momentum=0.1,
    affine=False,
    track_running_stats=False,
    device=None,
    dtype=None)
Input: (N, C, H, W)
Output: (N, C, H, W)

2.实例

import torch
import math

inp=torch.randn(size=(1,3,2,2))
print(inp)
print(inp.shape)
print('-'*20)
out=torch.nn.InstanceNorm2d(3)(inp)
print(out)
print(out.shape)

三、BatchNorm1d

1.介绍

一维批量归一化:其实还是实现前言中介绍的公式,但与实例归一化的最大区别在于计算均值及方差的依据不同。

torch.nn.BatchNorm1d(
    num_features,
    eps=1e-05,
    momentum=0.1,
    affine=True,
    track_running_stats=True,
    device=None,
    dtype=None)
Input: (N,C,L), where N is the batch size, C is the number of features or channels, and L is the sequence length
Output: (N,C,L) (same shape as input)

2.实例

import torch
import math

epsilon=1e-5
N,C,L=128,3,512
inp=torch.randn(size=(N,C,L))

mean_1, mean_2, mean_3 = torch.sum(inp[:,0,:])/(N*L), torch.sum(inp[:,1,:])/(N*L), torch.sum(inp[:,2,:])/(N*L)
mean_list=[mean_1, mean_2, mean_3 ]

var_1, var_2, var_3 = torch.var(inp[:,0,:]), torch.var(inp[:,1,:]), torch.var(inp[:,2,:])
var_list=[var_1, var_2, var_3 ]
print(mean_list,var_list)

out=torch.zeros_like(inp)
for c in range(C):
    out[:,c,:]=(inp[:,c,:]-mean_list[c])/math.sqrt(var_list[c]+epsilon)

out1=out

print("-"*25)

out=torch.nn.BatchNorm1d(C)(inp)

print(True in (out1==out))

四、BatchNorm2d

1.介绍

二维批量归一化:计算过程及调用方式同一维批量归一化,二者区别主要在于输入特征形状的变化。

torch.nn.BatchNorm2d(
    num_features,
    eps=1e-05,
    momentum=0.1,
    affine=True,
    track_running_stats=True,
    device=None,
    dtype=None)
Input: (N, C, H, W)
Output: (N, C, H, W)(same shape as input)

2.实例

import torch

inp=torch.randn(size=(2,3,5,6))
print(inp)
print(inp.shape)
print('-'*20)
out=torch.nn.BatchNorm2d(3)(inp)
print(out)
print(out.shape)

Original: https://blog.csdn.net/qq_43665602/article/details/126551756
Author: NorthSmile
Title: 常用归一化/正则化层:InstanceNorm1d、InstanceNorm2d、

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/670307/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球