PyTorch模型权重初始化全攻略

参考博客:

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 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值