Compute the DICE loss, similar to generalized IOU for masks Args: inputs: A float tensor of arbitrary shape. The predictions for each example. targets: A float tensor with the same shape as inputs. Stores the binary classification label for e
(inputs, targets)
| 5 | from bert.modeling_bert import BertModel |
| 6 | |
| 7 | def dice_loss(inputs, targets): |
| 8 | """ |
| 9 | Compute the DICE loss, similar to generalized IOU for masks |
| 10 | Args: |
| 11 | inputs: A float tensor of arbitrary shape. |
| 12 | The predictions for each example. |
| 13 | targets: A float tensor with the same shape as inputs. Stores the binary |
| 14 | classification label for each element in inputs |
| 15 | (0 for the negative class and 1 for the positive class). |
| 16 | """ |
| 17 | |
| 18 | inputs = inputs.sigmoid() |
| 19 | inputs = inputs.flatten(1) |
| 20 | targets = targets.flatten(1) |
| 21 | numerator = 2 * (inputs * targets).sum(1) |
| 22 | denominator = inputs.sum(-1) + targets.sum(-1) |
| 23 | loss = 1 - (numerator + 1) / (denominator + 1) |
| 24 | return loss.mean() |
| 25 | |
| 26 | def sigmoid_focal_loss(inputs, targets, alpha: float = 0.25, gamma: float = 2): |
| 27 | """ |