首页 > 解决方案 > PyTorch 指定模型参数

问题描述

我正在尝试在 PyTorch 中创建一个卷积模型

这是模型定义的示例代码:

import torch.nn as nn

class Net(nn.Module):
    def __init__(self, weights_fixed, weights_guess):
        super(Net, self).__init__()
        self.convL1 = nn.Conv1d(1, 3, 3, bias=False)
        self.convL1.weight = weights_fixed # I want to keep these weights fixed

        self.convL2 = nn.Conv1d(3, 1, 1, bias=False)
        self.convL1.weight = weights_guess # I want to learn these weights

    def forward(self, inp_batch):
        out1 = self.convL1(inp_batch)
        out2 = self.convL2(out1)

        return out2

和样本用途:

weights_fixed = ...
weights_guess = ...

model = Net(weights_fixed, weights_guess)

loss_fn = nn.CrossEntropyLoss()
optim = torch.optim.SGD(model.parameters(), lr=0.1, momentum=0.9)

train_dataset = ... #define training set here

for (X, y) in train_dataset:
    optim.zero_grad()
    out = model(X)
    loss = loss_fn(out, y)
    loss.backward()
    optim.step() 

如何使权重 weights_fixed - fixed 和 weights_guess - 可学习?

我的猜测是 weights_fixed = nn.Parameter(W1,requires_grad=False) weights_guess = nn.Parameter(W2,requires_grad=True) 为了完整起见 import numpy as np import torch

krnl = np.zeros((5,order+1))
krnl[:,0] = [ 0. , 1., 0. ]
krnl[:,1] = [-0.5, 0., 0.5]
krnl[:,2] = [ 1. ,-2., 1. ]
W1 = torch.tensor(krnl)

a = np.array((1.,2.,3.))
W2 = torch.tensor(a)

但我完全糊涂了。任何建议或参考将不胜感激。当然,我查看了 PyTorch 文档,但它并没有使我的理解更加清晰。

标签: modelpytorch

解决方案


只需用nn.Parameter(requires_grad=True是默认值,无需指定此) 包装可学习参数,并将固定权重作为没有nn.Parameter包装器的张量。

所有nn.Parameter的权重都会自动添加到 中net.parameters(),所以当你像 一样训练时optimizer = optim.SGD(net.parameters(), lr=0.01),固定的权重不会改变。

所以基本上是这样的:

weights_fixed = W1
weights_guess = nn.Parameter(W2)

推荐阅读