Custom tweedie loss throwing an error in pytorch

I (and when I say I, I mean a friend) figured this out late yesterday. I had to change the function and we’re not sure why changing the simple math enabled it to run. But in case anyone wants a custom function that maximizes the Tweedie QLL, the below works:

def QLL(predicted, observed):
    p = torch.tensor(1.5)
    QLL = QLL = torch.pow(predicted, (-p))*(((predicted*observed)/(1-p)) - ((torch.pow(predicted, 2))/(2-p)))

    return QLL
        
def tweedieloss(predicted, observed, n):
    '''
    Custom loss fuction designed to minimize the deviance using stochastic gradient descent
    tweedie deviance from McCullagh 1983

    '''
    d = -2*QLL(predicted, observed)
#     loss = (weight*d)/1


    return torch.mean(d)
3 Likes