pytorch对模型参数初始化

Posted wanghui-garcia

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pytorch对模型参数初始化相关的知识,希望对你有一定的参考价值。

举例说明:

  • Encoder :设计的编码其模型
  • weights_init(): 用来初始化模型
  • model.apply():实现初始化
# coding:utf-8
from torch import nn

def weights_init(mod):
    """设计初始化函数"""
    classname=mod.__class__.__name__
    # 返回传入的module类型
    print(classname)
    if classname.find(Conv)!= -1:    #这里的Conv和BatchNnorm是torc.nn里的形式
        mod.weight.data.normal_(0.0,0.02)
    elif classname.find(BatchNorm)!= -1:
        mod.weight.data.normal_(1.0,0.02) #bn层里初始化γ,服从(10.02)的正态分布
        mod.bias.data.fill_(0)  #bn层里初始化β,默认为0

class Encoder(nn.Module):
    def __init__(self, input_size, input_channels, base_channnes, z_channels):

        super(Encoder, self).__init__()
        # input_size必须为16的倍数
        assert input_size % 16 == 0, "input_size has to be a multiple of 16"

        models = nn.Sequential()
        models.add_module(Conv2-0-1.format(input_channels, base_channnes), nn.Conv2d(input_channels, base_channnes, 4, 2, 1, bias=False))
        models.add_module(LeakyReLU-0.format(base_channnes), nn.LeakyReLU(0.2, inplace=True))
        # 此时图片大小已经下降一倍
        temp_size = input_size/2

        # 直到特征图高宽为4
        # 目的是保证无论输入什么大小的图片,经过这几层后特征图大小为4*4
        while temp_size > 4 :
            models.add_module(Conv2-0-1.format(base_channnes, base_channnes*2), nn.Conv2d(base_channnes, base_channnes*2, 4, 2, 1, bias=False))
            models.add_module(BatchNorm2d-0.format(base_channnes*2), nn.BatchNorm2d(base_channnes*2))
            models.add_module(LeakyReLU-0.format(base_channnes*2), nn.LeakyReLU(0.2, inplace=True))
            base_channnes *= 2
            temp_size /= 2

        # 特征图高宽为4后面则添加上最后一层
        # 让输出为1*1
        models.add_module(Conv2-0-1.format(base_channnes, z_channels), nn.Conv2d(base_channnes, z_channels, 4, 1, 0, bias=False))
        self.models = models

    def forward(self, x):
        x = self.models(x)
        return x

if __name__ == __main__:
    e = Encoder(256, 3, 64, 100)
    # 对e模型中的每个module和其本身都会调用一次weights_init函数,mod参数的值即这些module
    e.apply(weights_init)
    # 根据名字来查看参数
    for name, param in e.named_parameters():
        print(name)
        # 举个例子看看是否按照设计进行初始化
        # 可见BatchNorm2d的weight是正态分布形的参数,bias参数都是0
        if name == models.BatchNorm2d-128.weight or name == models.BatchNorm2d-128.bias:
            print(param)

返回:

/anaconda3/envs/deeplearning/bin/python3.6 /Users/user/pytorch/iforest_autoencoder/autoencoder.py
# 返回的是依次传入初始化函数的module
Conv2d
LeakyReLU
Conv2d
BatchNorm2d
LeakyReLU
Conv2d
BatchNorm2d
LeakyReLU
Conv2d
BatchNorm2d
LeakyReLU
Conv2d
BatchNorm2d
LeakyReLU
Conv2d
BatchNorm2d
LeakyReLU
Conv2d
Sequential
Encoder

# 输出name的格式,并根据条件打印出BatchNorm2d-128的两个参数
models.Conv2-3-64.weight
models.Conv2-64-128.weight
models.BatchNorm2d-128.weight
Parameter containing:
tensor([0.9737, 0.9825, 1.0013, 1.0038, 1.0145, 1.0123, 0.9710, 0.9802, 0.9921,
        0.9991, 0.9697, 1.0093, 1.0183, 0.9882, 1.0499, 0.9782, 1.0252, 1.0002,
        1.0002, 0.9648, 0.9999, 1.0166, 1.0086, 0.9585, 1.0112, 1.0027, 1.0129,
        0.9993, 1.0105, 1.0015, 1.0130, 0.9883, 0.9819, 0.9977, 1.0069, 1.0052,
        0.9889, 1.0120, 0.9969, 0.9744, 0.9735, 0.9675, 1.0091, 1.0371, 0.9837,
        1.0087, 1.0112, 1.0187, 0.9862, 0.9890, 1.0268, 1.0211, 1.0530, 0.9952,
        0.9837, 0.9852, 0.9984, 0.9983, 0.9799, 0.9840, 0.9961, 1.0157, 1.0371,
        1.0252, 1.0190, 1.0086, 0.9949, 0.9933, 0.9920, 1.0028, 0.9915, 0.9741,
        0.9996, 1.0064, 1.0132, 0.9887, 1.0218, 1.0061, 1.0010, 0.9904, 0.9641,
        0.9813, 1.0139, 1.0312, 1.0217, 0.9971, 0.9722, 0.9817, 1.0017, 0.9918,
        0.9990, 0.9990, 1.0157, 1.0110, 1.0149, 0.9859, 1.0236, 1.0176, 1.0219,
        1.0035, 1.0018, 1.0449, 0.9978, 1.0013, 0.9806, 1.0075, 0.9687, 1.0065,
        1.0110, 1.0141, 1.0595, 0.9958, 1.0081, 1.0053, 0.9846, 0.9971, 0.9876,
        1.0357, 0.9888, 0.9943, 0.9797, 1.0254, 0.9893, 0.9932, 0.9815, 1.0306,
        0.9821, 0.9898], requires_grad=True)
models.BatchNorm2d-128.bias
Parameter containing:
tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0.], requires_grad=True)
models.Conv2-128-256.weight
models.BatchNorm2d-256.weight
models.BatchNorm2d-256.bias
models.Conv2-256-512.weight
models.BatchNorm2d-512.weight
models.BatchNorm2d-512.bias
models.Conv2-512-1024.weight
models.BatchNorm2d-1024.weight
models.BatchNorm2d-1024.bias
models.Conv2-1024-2048.weight
models.BatchNorm2d-2048.weight
models.BatchNorm2d-2048.bias
models.Conv2-2048-100.weight

 

以上是关于pytorch对模型参数初始化的主要内容,如果未能解决你的问题,请参考以下文章

PyTorch模型读写参数初始化Finetune

从头学pytorch:模型参数访问/初始化/共享

Pytorch不同层设置不同学习率

Pytorch不同层设置不同学习率

pytorch 参数初始化

Pytorch学习笔记——Sequential类参数管理与GPU