Pytorch模型中同时定义多个可训练的单变量

定义多个可训练的尺度权重系数

import torch
from torch import nn

class Scale(nn.Module):
    def __init__(self, init_value=1.0):
        super(Scale, self).__init__()
        self.scale = nn.Parameter(torch.FloatTensor([init_value]))

    def forward(self, input):
        return input * self.scale

#定义的多个可训练单变量
self.scales = nn.ModuleList([Scale(init_value=1.0) for _ in range(5)])#可以学习的参数,并设定初始值

参数权重初始化

# initialization
for modules in [self.cls_tower, self.bbox_tower,
                self.cls_logits, self.bbox_pred,
                self.centerness]:
for l in modules.modules():
    if isinstance(l, nn.Conv2d):
        torch.nn.init.normal_(l.weight, std=0.01)
        torch.nn.init.constant_(l.bias, 0)

你可能感兴趣的:(Python,pytorch)