It all depends how do you want to change it, there’s not a single good recipe. You should be able to implement it as a Python function that operates on Variables, and that will get the gradient computed automatically. Another approach would be to implement your own function. You can find some notes on that in the docs.