通过torch.nn.init更改模型初始化参数

比如模型如下:

class Net(nn.Module):
    # 初始化定义网络的结构:也就是定义网络的层
    def __init__(self):
        super(Net,self).__init__()
        self.layer1 = nn.Sequential(            
            nn.Conv2d(3,6,kernel_size=5,stride=1,padding=0),
            # 激活函数
            nn.ReLU(),
            # kernel_size:pooling size,stride:down-sample
            nn.MaxPool2d(kernel_size=2,stride=2))
        self.layer2 = nn.Sequential(            
            nn.Conv2d(6,16,kernel_size=5,stride=1,padding=0),
            # 激活函数
            nn.ReLU(),
            # kernel_size:pooling size,stride:down-sample
            nn.MaxPool2d(kernel_size=2,stride=2))

        self.fc1 = nn.Sequential(
            nn.Linear(16 * 5 * 5, 120),
            nn.ReLU()
        )
        self.fc2 = nn.Sequential(
            nn.Linear(120, 84),
            nn.ReLU()
        )
        
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = self.layer1(x)
        x = self.layer2(x)
        # x = x.reshape(x.size(0),-1)
        x = torch.flatten(x, 1) # flatten all dimensions except batch
        x = self.fc1(x)
        x = self.fc2(x)
        x = self.fc3(x)
        return x

model= Net().to(device)

在外部修改:

w1 = torch.empty(model.layer1[0].weight.shape) #according to the layer shape to create tensor

nn.init.kaiming_uniform_(w1, mode='fan_in', nonlinearity='relu')

model.layer1[0].weight.data.copy_(w1)

通过这种方式,模型的第一个卷积层的参数就被修改了。

这里用到的是He Kaiming的uniform的初始化方式,还有normal,以及很多其他人的方式也可以用,比如Xavier initialization, 详情可见这个页面, pytorch提供了一系列初始化参数的方法,可以对还没开始训练的模型进行参数修改。

当然也可以放在模型内部就修改。

class Net(nn.Module):
    # 初始化定义网络的结构:也就是定义网络的层
    def __init__(self):
        super(Net,self).__init__()
        self.layer1 = nn.Sequential(            
            nn.Conv2d(3,6,kernel_size=5,stride=1,padding=0),
            # 激活函数
            nn.ReLU(),
            # kernel_size:pooling size,stride:down-sample
            nn.MaxPool2d(kernel_size=2,stride=2))
        self.layer2 = nn.Sequential(            
            nn.Conv2d(6,16,kernel_size=5,stride=1,padding=0),
            # 激活函数
            nn.ReLU(),
            # kernel_size:pooling size,stride:down-sample
            nn.MaxPool2d(kernel_size=2,stride=2))
        w1 = torch.empty(self.layer1[0].weight.shape) #according to the layer shape to create tensor
        
        nn.init.kaiming_normal_(w1, mode='fan_in', nonlinearity='relu')
        
        self.layer1[0].weight.data.copy_(w1)

        self.fc1 = nn.Sequential(
            nn.Linear(16 * 5 * 5, 120),
            nn.ReLU()
        )
        self.fc2 = nn.Sequential(
            nn.Linear(120, 84),
            nn.ReLU()
        )
        
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = self.layer1(x)
        x = self.layer2(x)
        # x = x.reshape(x.size(0),-1)
        x = torch.flatten(x, 1) # flatten all dimensions except batch
        x = self.fc1(x)
        x = self.fc2(x)
        x = self.fc3(x)
        return x

model= Net().to(device)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值