公式:
pytorch官网
官方api:
公式实现:
两者是一一致的。
当然处理的维度是可以调整的,官方给的图像例子是:
>>> N, C, H, W = 20, 5, 10, 10
>>> input = torch.randn(N, C, H, W)
>>> # Normalize over the last three dimensions (i.e. the channel and spatial dimensions)
>>> # as shown in the image below
>>> layer_norm = nn.LayerNorm([C, H, W])
>>> output = layer_norm(input)
说明layernorm是按channel的维度做normalize,NLP中的假设有10个单词,每个单词用128维度表示,tensor.shape=[10,128],然后做layer norm,对于transfomer架构的图像任务,一般会变成[token,c]的表示,token就是将一张图分成多少个小的patch,c=patch_hpatch_w3,然后使用layernorm。结合上面的输出即可意会。
batchnorm:
如果是图像(batchnorm2d)的话,每一个channel对应一组公式的y,b:
初始化的参数是通道数量。