pytorch固定部分参数

pytorch固定部分参数

不用梯度

如果是Variable,则可以初始化时指定

j = Variable(torch.randn(5,5), requires_grad=True)

但是如果是m = nn.Linear(10,10)是没有requires_grad传入的

for i in m.parameters():
    i.requires_grad=False

另外一个小技巧就是在nn.Module里,可以在中间插入这个

for p in self.parameters():
    p.requires_grad=False
    
 
# eg  前面的参数就是False,而后面的不变
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 6, 5)
        self.conv2 = nn.Conv2d(6, 16, 5)

        for p in self.parameters():
            p.requires_grad=False

        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)
def freeze(test_net):
    ct = 0
    for child in test_net.children():
        ct += 1
        if ct < 3:
            for param in child.parameters():
                param.requires_grad = False

过滤

optimizer.SGD(filter(lambda p: p.requires_grad, model.parameters()), lr=1e-3)
原文地址:https://www.cnblogs.com/icodeworld/p/12025075.html