【问题标题】:PyTorch specify model parametersPyTorch 指定模型参数
【发布时间】:2019-08-11 13:07:32
【问题描述】:

我正在尝试在 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 - 固定和 weights_guess - 可学习?

我的猜测是 weights_fixed = nn.Parameter(W1,requires_grad=False) weights_guess = nn.Parameter(W2,requires_grad=True) 为了完整起见,在哪里 将 numpy 导入为 np 进口火炬

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 文档,但它并没有使我的理解更加清晰。

【问题讨论】:

    标签: model pytorch


    【解决方案1】:

    你可以这样做:

    # this will be inside your class mostly
    self.conv1.weight.requires_grad = False
    

    这将是您定义优化器的地方:

    optimizer = optim.SGD(filter(lambda p: p.requires_grad, net.parameters()), lr=0.1)

    因此,优化器将只使用启用了渐变的参数。

    【讨论】:

      【解决方案2】:

      只需将可学习的参数用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)
      

      【讨论】:

        【解决方案3】:

        您可以只将您想学习的参数传递给优化器:

        optim = torch.optim.SGD(model.convL2.parameters(), lr=0.1, momentum=0.9)
        # Now optimizer bypass parameters from convL1
        

        如果您的模型有更多层,则必须将参数转换为列表:

        params_to_update = list(model.convL2.parameters()) + list(model.convL3.parameters())
        optim = torch.optim.SGD(params_to_update, lr=0.1, momentum=0.9)
        

        如此处所述:https://discuss.pytorch.org/t/giving-multiple-parameters-in-optimizer/869

        【讨论】:

          【解决方案4】:

          将您的模型定义修改为:

          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.convL1.requires_grad = False
          
                  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
          

          【讨论】:

            猜你喜欢
            • 2017-10-30
            • 1970-01-01
            • 2018-08-18
            • 2020-01-29
            • 2012-05-16
            • 2022-01-08
            • 2020-04-01
            • 2020-10-22
            • 2020-06-14
            相关资源
            最近更新 更多