Pytroch进行模型权重初始化

参考博客:
pytorch系列10 — 如何自定义参数初始化方式 ,apply()
PyTorch中model.modules(), model.named_modules(), model.children(), model.named_children()的详解
pytroch——网络参数初始化
torch中manual_seed的作用
Pytroch常见的模型参数初始化方法有apply和model.modules()。Pytroch会自动给模型进行初始化,当需要自己定义模型初始化时才需要这两个方法。

一、 使用apply函数进行初始化

举一个包含自己命名层名称和利用nn.Sequential实现的网络。

from torch import nn
import torch

class Net(nn.Module):
       
    def __init__(self):
        super().__init__()
        self.layer1 = nn.Linear(2, 4)
        self.relu1 = nn.ReLU()
        self.layer2 = nn.Sequential(
            nn.Linear(4, 2),
            nn.ReLU(),
            nn.Linear(2, 2),
        )
        
    def forward(x):
        output = self.layer2(self.relu1(self.layer1(x)))
        return output
    
a = Net()

首先简单介绍一下model.modules()和model.children()。
model.modules()迭代遍历模型的所有子层,所有子层即指nn.Module子类,下面输出一下model.modules()

for i in a.modules():
    print(i, '\n')
########输出结果
Net(
  (layer1): Linear(in_features=2, out_features=4, bias=True)
  (relu1): ReLU()
  (layer2): Sequential(
    (0): Linear(in_features=4, out_features=2, bias=True)
    (1): ReLU()
    (2): Linear(in_features=2, out_features=2, bias=True)
  )
) 

Linear(in_features=2, out_features=4, bias=True) 

ReLU() 

Sequential(
  (0): Linear(in_features=4, out_features=2, bias=True)
  (1): ReLU()
  (2): Linear(in_features=2, out_features=2, bias=True)
) 

Linear(in_features=4, out_features=2, bias=True) 

ReLU() 

Linear(in_features=2, out_features=2, bias=True) 

model.children()只会遍历模型的子层,这里即是layer1、relu1、layer2。

for i in a.children():
    print(i, '\n')
#########输出结果
Linear(in_features=2, out_features=4, bias=True) 

ReLU() 

Sequential(
  (0): Linear(in_features=4, out_features=2, bias=True)
  (1): ReLU()
  (2): Linear(in_features=2, out_features=2, bias=True)
) 

接下来看一下apply函数在官方文档上的解释(官方文档):

将函数fn递归的运用在每个子模块上,这些子模块由self.children()返回.

接下来我们便可以利用apply进行参数初始化。

## 进行模型参数初始化
def init_weights1(model):
    if isinstance(model, nn.Linear):
        torch.nn.init.xavier_uniform(model.weight)
        model.bias.data.fill_(1)

a.apply(init_weights1)

二、 利用model.modules()实现参数初始化

for layer in a.modules():
        if isinstance(layer, nn.Linear):
            torch.nn.init.xavier_uniform(layer.weight)
            layer.bias.data.fill_(2)

这个和利用apply等价。

三、Pytroch常用的初始化函数

Pytroch常用的初始化函数

ps:手动设置种子一般可用于固定随机初始化的权重值,这样就可以让每次重新从头训练网络时的权重的初始值虽然是随机生成的但却是固定的。

这篇博客我参考了很多人的文章,主要想把几篇博客的内容进行总结方便我日后使用。

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
扎眼的阳光的头像扎眼的阳光普通用户
上一篇 2023年4月5日
下一篇 2023年4月5日

相关推荐