Solved by ptrblck in post #4 
              
              
                You could try to access the handle from inside the hook: 
def hook(grad):
    print(grad)
    handle.remove()
    
model = nn.Linear(1, 1)
handle = model.weight.register_hook(hook)

model(torch.randn(1, 1)).backward()
model(torch.randn(1, 1)).backward()

This code will print the gradient only once a…