还是希望大家带着批判的眼光看问题,有的人说L0不是范数。
BN:其能一定程度的起到正则化作用,几乎代替Dropout
注意可训练参数 的维度 等于 批次内 每一个张量的 channels。 如果是R、G、B图像,维度就是3。也就是每一个channel都需要一个,所以它俩各是1 X 3的列向量。
summary
OrderedDict([('Conv2d-1',
OrderedDict([('input_shape', [-1, 3, 418, 418]),
('output_shape', [-1, 64, 209, 209]),
('trainable', True),
('nb_params', tensor(9408))])),
('BatchNorm2d-2',
OrderedDict([('input_shape', [-1, 64, 209, 209]),
('output_shape', [-1, 64, 209, 209]),
('trainable', True)]))])
len(summary)
2
module.weight.requires_grad
True
module.weight
Parameter containing:
tensor([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], device='cuda:0',
requires_grad=True)
module.weight.size()
torch.Size([64])
module.bias.size()
torch.Size([64])
module.bias
Parameter containing:
tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
device='cuda:0', requires_grad=True)
module.bias.size()
torch.Size([64])
eg1:
eg2:
import torch.nn as nn
import torch
a = torch.tensor([[
[[1., 2]],
[[2, 1]],
[[2, 2]],
],
[
[[2., 2]],
[[3, 1]],
[[1, 2]],
]
])
print(a, type(a), a.dtype)
print(a.shape)
b = nn.BatchNorm2d(3)
d = b(a)
print(d)
tensor([[[[1., 2.]],
[[2., 1.]],
[[2., 2.]]],
[[[2., 2.]],
[[3., 1.]],
[[1., 2.]]]]) <class 'torch.Tensor'> torch.float32
torch.Size([2, 3, 1, 2])
tensor([[[[-1.7320, 0.5773]],
[[ 0.3015, -0.9045]],
[[ 0.5773, 0.5773]]],
[[[ 0.5773, 0.5773]],
[[ 1.5075, -0.9045]],
[[-1.7320, 0.5773]]]], grad_fn=<NativeBatchNormBackward>)
正则化: