model - 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 文档,但它并没有使我的理解更加清晰。
解决方案
只需用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)
推荐阅读
- java - 在“@”之前屏蔽电子邮件中的中间字符
- sql - SQL(SQL/Oracle)使用序列从选择语句中插入值
- classification - 在分类中使用线性判别分析和 k 最近邻时,哪个更好?
- node.js - 在具有不同 publicPath 的 webpack 中两次注入相同的资产
- fortran - 在 Fortran 不好的做法中声明虚拟参数的大小(元素数量)
- python-3.x - 使用子进程从标准输出读取数据时出现意外的额外反斜杠
- python - 从两个多维 numpy 数组复制到另一个具有不同形状的数组
- go - Golang 序列化 go-radix Tree 到文件?
- javascript - setTimeout 回调执行后清除超时
- python - 'VoiceClient' 对象不能用 discord API/youtube_dl 调用