PyTorch学习笔记:针对一个网络的权重初始化方法

# 针对一个网络的权重初始化方法
import torch
import torch.nn as nn


## 建立一个测试网络
class TestNet(nn.Module):
    def __init__(self):
        super(TestNet, self).__init__()
        self.conv1 = nn.Conv2d(3, 16, 3)
        self.hidden = nn.Sequential(
            nn.Linear(100, 100),
            nn.ReLU(),
            nn.Linear(100, 50),
            nn.ReLU(),
        )
        self.cla = nn.Linear(50, 10)

    # 定义网络的前向传播路径
    def forward(self, x):
        x = self.conv1(x)
        x = x.view(x.shape[0], -1)
        x = self.hidden(x)
        output = self.cla(x)
        return output


def init_weights(m):
    '''
    对不同类型层的参数使用不同的方法进行初始化
    :param m: 网络的某一层
    :return:
    '''
    # 如果是卷积层
    if type(m) == nn.Conv2d:
        torch.nn.init.normal_(m.weight, mean=0, std=0.5)
        # UserWarning: nn.init.normal is now deprecated in favor of nn.init.normal_.
    # 如果是全连接层
    if type(m) == nn.Linear:
        torch.nn.init.uniform_(m.weight, a=-0.1, b=0.1)
        m.bias.data.fill_(0.01)


if __name__ == '__main__':
    # 输出网络结构
    testnet = TestNet()
    print(testnet)

    # 使用网络的apply方法进行权重初始化
    torch.manual_seed(13)
    testnet.apply(init_weights)

    print(testnet.conv1.weight)  # 输出参数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值