Learner.get_preds error with custom loss function (FocalLoss)

I trained a cnn_learner with a custom loss function. Now that I call get_preds on it with test dataset, it returns attribute error.
Here is the error.

Also I did not write the loss function myself, searched and found it. But it was working so I copied it in.
Here is the code:

    class FocalLoss(nn.Module):
    def __init__(self, gamma=2):
        self.gamma = gamma

    def forward(self, logit, target):
        target = target.float()
        max_val = (-logit).clamp(min=0)
        loss = logit - logit * target + max_val + \
               ((-max_val).exp() + (-logit - max_val).exp()).log()

        invprobs = F.logsigmoid(-logit * (target * 2.0 - 1.0))
        loss = (invprobs * self.gamma).exp() * loss
        if len(loss.size())==2:
            loss = loss.sum(dim=1)
        return loss.mean()

I called learner.loss_fn = FocalLoss() to set the loss function.

Could you point out what is causing this error and how I can get around it?

Did you add test_data while creating the learner?

1 Like

Yes. I used add_test() on it.

The issue is fixed though. I was using a custom data sampler and forgot to make a dataloader for test set.