Hi, I’m quite new to pytorch.
I’ve been trying to write a custom loss function. This is my first try:
class GammaLoss(Function):
    
    @staticmethod
    def forward(ctx, out, target):
        ctx.save_for_backward(out, target)
        #return -(torch.log(target) * (self.shape - 1) - (target / out) * (self.shape - 1)  - torch.log((out / target)) *  self.shape)
        ratio = torch.div(out, target)
        output = torch.log(ratio)
        return output
    
    @staticmethod
    def backward(ctx, grad_output):
        out, target = ctx.saved_variables
        dout = - (1 / (out * out))((ctx.shape - 1) * target - ctx.shape * out)
        return grad_output.mm(dout), None, None


a = Variable(torch.randn(4) + 10)
b = Variable(torch.randn(4) + 10)
loss = GammaLoss()
print(loss(a, b))

The previous code gives me TypeError: forward() missing 1 required positional argument: 'target'. This seems to happen because it takes ctx as a. I tried to change ctx to self, it worked but in this case I receive nothing when I call self.saved_variables or self.saved_tensors.
I think I must be doing some rookie mistake here, by I don’t know where.