Compute the gradient of a neural network