Pytorch自定义网络方法

本文侧重于介绍Pytorch如何从最底层介绍“定义网络结构”的方法;

1、如何区别模型参数、中间变量或者输入输出变量

(1)模型参数:

定义需要引入方法:torch.nn.parameter import Parameter

参数定义用法

self.weight = Parameter(torch.Tensor(out_features, in_features))

(2)中间变量或者输入输出变量

variable(tensor)

2、初始化方法

torch.nn.init

例如

  • torch.nn.init.constant(tensor, val) 常量初始化
  • torch.nn.init.normal(tensor, mean=0, std=1) 高斯随机初始化

注意:这里面所有的初始化方法,初始化前先要建立参数权重Parameter(torch.Tensor(维度,维度,…)),然后再利用这里的torch.nn.init进行填充。

3、打印模型与参数信息核对模型

如下:

    # creat and load ConvLSTM model
    model = RNNConvLSTM(input_channels, input_seq_number, output_seq_number, shape, hidden_channels, kernel_size).cuda()
    try:
        model_path = './model/checkpoint.pkl'
        model.load_state_dict(torch.load(model_path))
        print('load model success')
    except:
        print('not found model, recreat model')
    print('convlstm module:', model)
    print('params:')
    params = model.parameters()
    for p in params:
        print('param ', p.size())
        print('mean ', torch.mean(p))

4、加载模型方法

这种方法避免频繁修改代码,提高效率

    try:
        model_path = './model/checkpoint.pkl'
        model.load_state_dict(torch.load(model_path))
        print('load model success')
    except:
        print('not found model, recreat model')

2、例子

类网络定义,即Class.

这里以pytorch中的Linear网络为例晚上

import math

import torch
from torch.nn.parameter import Parameter
from .. import functional as F
from .module import Module

class Linear(Module):
    r"""Applies a linear transformation to the incoming data: :math:`y = Ax + b`
    Args:
        in_features: size of each input sample
        out_features: size of each output sample
        bias: If set to False, the layer will not learn an additive bias. Default: True
    Shape:
        - Input: :math:`(N, in\_features)`
        - Output: :math:`(N, out\_features)`
    Attributes:
        weight: the learnable weights of the module of shape (out_features x in_features)
        bias:   the learnable bias of the module of shape (out_features)
    Examples::
        >>> m = nn.Linear(20, 30)
        >>> input = autograd.Variable(torch.randn(128, 20))
        >>> output = m(input)
        >>> print(output.size())
    """

    def __init__(self, in_features, out_features, bias=True):
        super(Linear, self).__init__()
        self.in_features = in_features
        self.out_features = out_features
        ##################参数定义##############################
        self.weight = Parameter(torch.Tensor(out_features, in_features))
        if bias:
            self.bias = Parameter(torch.Tensor(out_features))
        else:
            self.register_parameter('bias', None)
        ##################参数初始化函数#######################################    
        self.reset_parameters()

    def reset_parameters(self):
        stdv = 1. / math.sqrt(self.weight.size(1))
        self.weight.data.uniform_(-stdv, stdv)
        if self.bias is not None:
            self.bias.data.uniform_(-stdv, stdv)

    原文作者:知客
    原文地址: https://zhuanlan.zhihu.com/p/53562337
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞