How to stop a layer updating in pytorch?
class DR(nn.Module):
    def __init__(self, orginal, latent_dims):
        super(DR, self).__init__()
        self.latent_dims=latent_dims
        self.linear1 = nn.Linear(orginal, 1000)
        self.linear2 = nn.Linear(1000, 2000)
        self.linear3 = nn.Linear(2000, latent_dims)
        


    def forward(self, x):
        x = F.relu(self.linear1(x))
        x = F.relu(self.linear2(x))
        x = F.relu(self.linear3(x))
        return x

In this DR class, i don't want to update linear1, and linear2 in the traning time. That means, the layer should be same as it initialized, I only want to update linear3 in the traning time. How can i do this ?
I expect a solution of this proeblem. Thank you

What appears like an underused feature of PyTorch is that the requires_grad, which is meant to disable inference caching for gradient computation can be called on torch.Tensor (as an in place and out of place operation) but also on nn.Module (in place).
In your case, you can simply write in two lines:
model.linear1.requires_grad_(False)
model.linear2.requires_grad_(False)

