Hello many thanks for your support l am trying add two loss functions but l got this error message. They 2 loss functions has been made my myself in 2 .py file.
TypeError Traceback (most recent call last)
Cell In[20], line 3
1 epochs=10
2 for epoch in range(1,epochs+1):
----> 3 training(epoch)
Cell In[19], line 78, in training(epochs)
72 loss2 = focalTverskyLoss(output,label)
74 #label = torch.randn(3) .softmax(dim=0)
75
76 #loss3 =torch.nn.CrossEntropyLoss()(output,label)
—> 78 total_loss = (loss1 + loss2)
80 total_loss.backward()
82 # Update Weight
TypeError: unsupported operand type(s) for +: ‘diceLoss’ and ‘focalTverskyLoss’
my two loss functions :
class focalTverskyLoss(_Loss):
def init(self, weight=None, size_average=True):
super(focalTverskyLoss, self).init()
def forward(self, inputs, targets, smooth=1, alpha=ALPHA, beta=BETA, gamma=GAMMA):
#comment out if your model contains a sigmoid or equivalent activation layer
inputs = F.sigmoid(inputs)
#flatten label and prediction tensors
inputs = inputs.view(-1)
targets = targets.view(-1)
#True Positives, False Positives & False Negatives
TP = (inputs * targets).sum()
FP = ((1-targets) * inputs).sum()
FN = (targets * (1-inputs)).sum()
Tversky = (TP + smooth) / (TP + alpha*FP + beta*FN + smooth)
FocalTversky = (1 - Tversky)**gamma
return FocalTversky
class diceLoss(_Loss):
def init(self, weight=None, size_average=True):
super(diceLoss, self).init()
def forward(self, inputs, targets, smooth=1):
#comment out if your model contains a sigmoid or equivalent activation layer
inputs = F.sigmoid(inputs)
#flatten label and prediction tensors
inputs = inputs.view(-1)
targets = targets.view(-1)
intersection = (inputs * targets).sum()
dice = (2.*intersection + smooth)/(inputs.sum() + targets.sum() + smooth)
loss=1 - dice
return loss