vak.nn.loss.dice.dice_loss¶
- vak.nn.loss.dice.dice_loss(input: Tensor, target: Tensor, eps: float = 1e-08) Tensor [source]¶
Criterion that computes Sørensen-Dice Coefficient loss, adapted to work on a 1-dimensional time series.
According to [1], we compute the Sørensen-Dice Coefficient as follows: .. math:
\text{Dice}(x, class) = \frac{2 |X| \cap |Y|}{|X| + |Y|}
- Where:
\(X\) scores that estimator assigns to each class.
\(Y\) true class labels.
the loss, is finally computed as: .. math:
\text{loss}(x, class) = 1 - \text{Dice}(x, class)
- Parameters:
input (torch.Tensor) – logits tensor with shape \((N, C, T)\) where C = number of classes, and T = number of timebins
labels (torch.Tensor) – labels tensor with shape \((N, T)\) where each value is \(0 ≤ targets[i] ≤ C−1\). Converted to one-hot vector with shape \((N, C, T)\) to compute the loss.
eps (float, optional) – Scalar to enforce numerical stabiliy. Default: 1e-8.
- Returns:
the computed loss.
- Return type:
Example
>>> N = 5 # num_classes >>> input = torch.randn(1, N, 20, requires_grad=True) >>> target = torch.empty(1, 20, dtype=torch.long).random_(N) >>> output = dice_loss(input, target) >>> output.backward()