pytorch fixing portion Parameter

pytorch fixing portion Parameter

Without gradient

If you specify a Variable, you can initialize when

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

但是如果是m = nn.Linear(10,10)It is not requires_gradpassed

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

Another little trick is in nn.Module where this can be inserted in the middle

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

filter

optimizer.SGD(filter(lambda p: p.requires_grad, model.parameters()), lr=1e-3)

Guess you like

Origin www.cnblogs.com/icodeworld/p/12025075.html