Conv2d 中的单一权重定义和冻结

Single weight definition and freezing in Conv2d

我正在处理一个 Conv2d(3,3,kernel_size=5, stride=1) 并且我需要将一些特定的权重设置为零并使它们不可更新。 例如,如果我做类似

model = nn.Conv2d(3,3,kernel_size=5, stride=1)

model.weight.requires_grad = False

一切正常,但它会影响整个图层。我想做这样的事情:

model = nn.Conv2d(3,3,kernel_size=5, stride=1)

model.weight[0,2].requires_grad = False  # this line does not work
model.weight[0,2] = 0                    # this line does not work either

它似乎不支持图层参数子组的分配和 requires_grad 操作。有没有人已经解决了这个问题?

您可以通过访问 data 属性

将此过滤通道清零
>>> model.weight.data[0, 2] = 0

或使用 torch.no_grad 上下文管理器:

>>> with torch.no_grad():
...     model.weight[0, 2] = 0

如您所见,您不能为子模块专门设置 requires_grad。因此,给定模块的所有参数元素共享相同的标志,它们要么更新,要么不更新。

另一种方法是在 后向传递被调用之后手动终止该通道的梯度,就在 之前优化器步骤:

>>> model(torch.rand(2, 3, 100, 100)).mean().backward()
>>> model.weight.grad[0, 2] = 0
>>> optim.step()

这样,过滤器 n°1 上的第 3 个通道将不会被反向传播更新,并保持在 0