Solved by SimonW in post #2 
              
              
                Your input_var is an intermediate result, nor a leaf because you created a variable that requires_grad, and then .type creates a new one that is an intermediate result. 
Also, your code has some other issues: 

You don’t need Variable wrappers anymore. Just write input_var = torch.as_tensor(X, dtyp…