AttributeError: 'CustomComboLoss' object has no attribute 'clone'


I am trying to train a Unet learner using Fastai v2.4 for semantic segmentation. I receive attribute error saying loss object has not attribute clone when execute learn.lr_find or fit_one_cycle functions. Below is the specification of CustomComboLoss:

class CustomComboLoss:
“Dice and Focal combined”
def init(self, axis=1, smooth=1., alpha=1.):
self.focal_loss = FocalLossFlat(axis=axis)
self.dice_loss = DiceLoss(axis, smooth)

def __call__(self, pred, targ):
    return self.focal_loss(pred, targ) + self.alpha * self.dice_loss(pred, targ)

def decodes(self, x):    return x.argmax(dim=self.axis)
def activation(self, x): return F.softmax(x, dim=self.axis

See the error below:

AttributeError Traceback (most recent call last)
in ()
1 learn = unet_learner(dls, resnet34, loss_func=CustomComboLoss, metrics=[Dice, DiceMulti])
----> 2 learn.lr_find()

11 frames
/usr/local/lib/python3.7/dist-packages/fastai/ in _do_one_batch(self)
174 if len(self.yb):
175 self.loss_grad = self.loss_func(self.pred, *self.yb)
→ 176 self.loss = self.loss_grad.clone()
177 self(‘after_loss’)
178 if not or not len(self.yb): return

AttributeError: ‘CustomComboLoss’ object has no attribute ‘clone’

Any idea what I might be doing wrong.

Thanks a lot.

Get it resolved. The error was with the class name CustomComboLoss. It was missing the parenthesis like CustomComboLoss().


Interestingly, I get this error at random (sometimes not at all, sometimes in the middle of an lr_finder() run) but I can’t identify any particular issue with my loss function.

From the description, it sounds like at some point the fastai self.loss_grad becomes an int instead of a torch.tensor, but it’s not clear to me yet how that happens.