I tried to implement my own custom loss based on the tutorial in extending autograd.
Here is the implementation outline:
class MyCustomLoss(Function):
def forward(self, input, target):
... # implementation
return loss # a single number (averaged loss over batch samples)
def backward(self, grad_output):
... # implementation
return grad_input
The forward function take an input from the previous layer and target which contains array of labels (categorical, possible value = {0,…,k-1}, k is the number of class).
In the backward function I write a gradient of the loss with respect to the input. When I run, I got an error says that it needs one more gradient. I assume that pytorch also require to also write the gradient of the loss with respect to the target, which in this case does not really make sense (target is a categorical variable), and we do not need that to backpropagate the gradient.
Here you need to write functions for init() and forward().
backward is not requied. But how do I indicate that the target does not need to compute gradient?
2)using Functional (this post)
Here you need to write functions for both forward() and backward()
I need to also implement backward because I use some operations that autograd’s automatic gradient won’t work. In the case that you just use standard operation, I think you do not need to extend backward method.
Here’s my example for how to create a custom loss function (along with several other important things in PyTorch). See if going through it is of any help!
Have you solved your problem? I guess it may be because of the type of the variables in your forward method are all numpy arrays. The error message effectively said there were no input arguments to the backward method, which means, both ctx and grad_output are None. This then means ‘ctx.save_for_backward(mu, signa, x)’ method did nothing during forward call. Maybe change mu, sigma and x to torch tensors or Variable could solve your problem.