sio277:

detach() or torch.no_grad()


Thank you for your reply!
detach will create a new tensor for modules that do not require gradients, which seems even less applicable. Regarding torch.no_grad(), it seems that can only be applied to the serialized training process below. If I need to unfrozen the model during training, what should I do with torch.no_grad().
class xxnet(nn.Module):
    def __init__():
        ....
        self.layer1 = xx
        self.layer2 = xx
        self.fc = xx

    def forward(self.x):
        with torch.no_grad():
            x = self.layer1(x)
            x = self.layer2(x)
        x = self.fc(x)
        return x

Thanks for patiently answering my questions