If you want to access the derivative dl/dx (the jacobian)) you can access x_.grad after loss.backward(), as this attribute is set after specifying requires grad = True and backpropping. I did not test your code with the detach() on x_ and would simply write x_ = train_x.requires_grad_(True); as x_ needs to be in the graph anyways.