Solved by ptrblck in post #2 
              
              
                Could you try to use torch.no_grad() in a with statement: 
model.eval()
with torch.no_grad():
    xbatches = ...

If I’m not mistaken, your current approach shouldn’t change the gradient behavior.