【问题标题】:Forcing NN weights to always be in a certain range强制 NN 权重始终在某个范围内
【发布时间】:2022-01-16 15:51:10
【问题描述】:

我有一个简单的模型:

class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.fc1 = nn.Linear(3, 10)
        self.fc2 = nn.Linear(10, 30)
        self.fc3 = nn.Linear(30, 2)

    def forward(self, x):
        x = torch.relu(self.fc1(x))
        x = torch.relu(self.fc2(x))
        x = torch.tanh(self.fc3(x)) 
        return x

    net = Model()

我怎样才能使权重始终在某个值(例如 -1,1)之间?

我尝试了以下方法:

self.fc1 = torch.tanh(nn.Linear(3, 10))

我不完全确定这会始终将它们保持在该值(即使梯度更新试图将它们推得更远)。

但出现以下错误:

TypeError: tanh(): argument 'input' (position 1) must be Tensor, not Linear

【问题讨论】:

  • 您希望线性层的权重在 (-1, 1) 范围内还是线性层的输出在 (-1, 1) 范围内?如果您希望输出在该范围内,请使用tanh,否则使用@yakhyo 指定的Clipping 技术。
  • 我运行了上面你指定的代码sn-p。它运行良好,并没有引发上述指定的错误。

标签: python neural-network pytorch


【解决方案1】:

根据讨论.pytorch,您可以创建额外的类来剪辑给定范围之间的权重。 Link 参与讨论。

class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.fc1 = nn.Linear(3, 10)
        self.fc2 = nn.Linear(10, 30)
        self.fc3 = nn.Linear(30, 2)

    def forward(self, x):
        x = torch.relu(self.fc1(x))
        x = torch.relu(self.fc2(x))
        x = torch.tanh(self.fc3(x)) 
        return x

你应该添加重量剪:

class WeightClipper(object):
    
    def __call__(self, module):
        # filter the variables to get the ones you want
        if hasattr(module, 'weight'):
            w = module.weight.data
            w = w.clamp(-1,1)
            module.weight.data = w


model = Model()
clipper = WeightClipper()
model.apply(clipper)

【讨论】:

  • 看起来不错。只有两个问题,是否可以将其应用于特定层而不是模型的所有参数?假设我只想将其应用于self.fc1。而且,我试图在你分享的链接中找到这个,但不太明白。我是否需要在每次通过后调用model.apply,还是它会在每次梯度更新时自动裁剪权重?
  • 我还没有找到只指定一些图层的方法。从here 看来我必须在每个纪元之后打电话给apply,只是想和你核实一下
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-02
  • 2018-07-16
  • 2019-10-04
相关资源
最近更新 更多