You don’t need to write your own backward function. You just need to write your forward function and your backward function will be generated automatically via autograd. See http://pytorch.org/tutorials/beginner/blitz/neural_networks_tutorial.html 141 for an example.