首先、在pytorch中本身带有参数初始化。使用初始化主要是因为在模型构建中的需求
1.使用apply()
from torch import nn def weights_init(mod): """对于卷积层和batchnorm进行初始化""" classname=mod.__class__.__name__ # 返回传入的module类型 if classname.find('Conv')!= -1: mod.weight.data.normal_(0.0,0.02) elif classname.find('BatchNorm')!= -1: mod.weight.data.normal_(1.0,0.02) #bn层里初始化γ,服从(1,0.02)的正态分布 mod.bias.data.fill_(0) #bn层里初始化β,默认为0 class Net(nn.Module): def __init__(self): ... def forward(self,x): ... net = Net() net.apply(weights_init) #初始化
2、在模型初始化中进行初始化
class DnCNN(nn.Module): def __init__(self, depth=17, n_channels=64, image_channels=3, use_bnorm=True, kernel_size=3): super(DnCNN, self).__init__() kernel_size = 3 padding = 1 layers = [] layers.append(nn.Conv2d(in_channels=image_channels, out_channels=n_channels, kernel_size=kernel_size, padding=padding, bias=True)) layers.append(nn.ReLU(inplace=True)) for _ in range(depth-2): layers.append(nn.Conv2d(in_channels=n_channels, out_channels=n_channels, kernel_size=kernel_size, padding=padding, bias=False)) layers.append(nn.BatchNorm2d(n_channels, eps=0.0001, momentum = 0.95)) layers.append(nn.ReLU(inplace=True)) layers.append(nn.Conv2d(in_channels=n_channels, out_channels=image_channels, kernel_size=kernel_size, padding=padding, bias=False)) self.dncnn = nn.Sequential(*layers) self._initialize_weights() def forward(self, x): y = x out = self.dncnn(x) return y-out def _initialize_weights(self): for m in self.modules(): if isinstance(m, nn.Conv2d): init.orthogonal_(m.weight) if m.bias is not None: init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm2d): init.constant_(m.weight, 1) init.constant_(m.bias, 0)