Hi, I’m attempting to write my own custom loss function, for the log likelihood of a Gaussian, ie. least squares.
I’ve written the following:
> class LSE_loss(torch.autograd.Function):
>     @staticmethod
>     def forward(ctx, mu, sigma, x):
>         detsig = linalg.det(sigma.numpy())
>         invsig = linalg.inv(sigma.numpy())
>         result = 0.5*np.log(detsig) -0.5 * np.transpose(x - mu.numpy(),(0,2,1)).matmul(invsig.matmul(x- mu.numpy()))
>         ctx.save_for_backward(mu,sigma, x)
>         return torch.FloatTensor(result)
>     @staticmethod
>     def backward(ctx, grad_output):
>         x, mu, sigma = ctx.saved_variables
>         invsig = linalg.pinv(sigma.numpy())
>         grad_mu = invsig.matmul(x-mu.numpy())
>         grad_sig = -0.5*(invsig - (invsig.matmul(x-mu.numpy())).matmul((x-mu.numpy()).matmul(invsig)))
>         return torch.FloatTensor(grad_mu), torch.FloatTensor(grad_sig), None

But when I make an instance of the loss, and call loss.backward(), I get the error "TypeError: backward() takes exactly 2 arguments (0 given).
What am I doing wrong?
ref for formulae: http://www.notenoughthoughts.net/posts/normal-log-likelihood-gradient.html 135 , I know calculating inverse s isn’t ideal, open to suggestions for alternatives…
Cheers.