Detach the loaded model

Is there a correct way to detach a loaded model, when we don’t need to compute its gradients ?

For the moment, I’m doing that:

model = torch.load('mymodel.pth')
for variable in model.parameters():
    variable.detach_()

Here, I am lucky because the model contains Variable parameters, but it could contain sub-models…

the correct way is to make the model’s parameters not require gradients:

model = torch.load('mymodel.pth')
for p in model.parameters()
    p.requires_grad = False

Alternatively, if you are purely using the model for inference with no gradients needed, the input to the model can be volatile (this is better, it saves more memory as well):

myinput = torch.randn(10, 20) # example input
model = torch.load('mymodel.pth')
input = Variable(myinput, volatile=True)
output = model(input)

See these notes for more hints.

3 Likes

Ah! good point for the input!

Ok, thanks a lot!

NVM - sorry I figured it out my mistake.