【原创文章,转载请注明出处,栓Q】
众所周知,Batchnorm1d是用来归一化2D和3D数据的,即input的shape是
首先看input的shape是
import torch bn=torch.nn.BatchNorm1d(num_features=2) input = torch.tensor([[-1.,8.],[0.,0.],[1.,-8.]]) output=bn(input) print(input) print(output) print(output.shape)
运行结果为:
tensor([[-1., 8.], [ 0., 0.], [ 1., -8.]]) tensor([[-1.2247, 1.2247], [ 0.0000, 0.0000], [ 1.2247, -1.2247]], grad_fn=) torch.Size([3, 2])
看图:
2D数据的BN挺好理解的,就是在不同样本的同一个特征下做归一化。
----------------------------------------------------------------------------------------
然后看input的shape是
import torch bn=torch.nn.BatchNorm1d(num_features=4) input = torch.tensor([[[1.,2.],[3.,-2.],[-1.,-2.],[-3.,2.]],[[-1.,-2.],[-3.,2.],[1.,2.],[3.,-2.]]]) output=bn(input) print(input) print(output) print(output.shape)
运行结果为:
tensor([[[ 1., 2.], [ 3., -2.], [-1., -2.], [-3., 2.]], [[-1., -2.], [-3., 2.], [ 1., 2.], [ 3., -2.]]]) tensor([[[ 0.6325, 1.2649], [ 1.1767, -0.7845], [-0.6325, -1.2649], [-1.1767, 0.7845]], [[-0.6325, -1.2649], [-1.1767, 0.7845], [ 0.6325, 1.2649], [ 1.1767, -0.7845]]], grad_fn=) torch.Size([2, 4, 2])
看图:
这里为了清晰地说明通道和序列,特意画成行是序列,列是通道的样子,但是不影响理解,就是说哈,BN1d在BN 3D数据时的原理是,以通道一,也就是图中的c1为例,将c1下的两个样本的s1归一化,将c1下的两个样本的s2归一化。c2,c3,c4以此类推。宏观的理解是,假设input是2个样本的含有2个字的向量(比如是*[你,好]和[宝,贝]*),其中,每个字的词嵌入维度是4(就是每个字用4个数表示),BN1d是将“你”和“宝”的字向量按照对应的词嵌入维度进行归一化,然后再将“好”和“贝”的字向量按照对应的词嵌入维度进行归一化。
洞妖洞妖,over over