Implementation:Kornia Kornia Tversky Loss
| Knowledge Sources | |
|---|---|
| Domains | Vision, Loss_Functions |
| Last Updated | 2026-02-09 15:00 GMT |
Overview
Tversky Loss computes the Tversky Coefficient loss, a generalization of the Dice loss that allows independent control over false positive and false negative penalties for segmentation tasks.
Description
The Tversky loss is based on the Tversky index (Salehi et al., 2017), which generalizes the Dice coefficient by introducing two parameters that control the balance between false positives (FPs) and false negatives (FNs).
The Tversky index is defined as:
Where:
- and are the predicted and ground truth labels.
- controls the penalty for false positives.
- controls the penalty for false negatives.
Special cases:
- gives the Dice coefficient.
- gives the Tanimoto coefficient.
- gives the F-beta coefficient.
The loss is:
Usage
Import this loss for segmentation tasks where you want fine-grained control over the trade-off between precision and recall. Setting penalizes false negatives more, which is useful when missing detections are more costly than false alarms (e.g., medical diagnosis).
Code Reference
Source Location
- Repository: Kornia
- File: kornia/losses/tversky.py
- Lines: 1-170
Signature
def tversky_loss(
pred: torch.Tensor,
target: torch.Tensor,
alpha: float,
beta: float,
eps: float = 1e-8,
ignore_index: Optional[int] = -100,
) -> torch.Tensor: ...
class TverskyLoss(nn.Module):
def __init__(
self,
alpha: float,
beta: float,
eps: float = 1e-8,
ignore_index: Optional[int] = -100,
) -> None: ...
def forward(self, pred: torch.Tensor, target: torch.Tensor) -> torch.Tensor: ...
Import
from kornia.losses import TverskyLoss
from kornia.losses import tversky_loss
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| pred | torch.Tensor | Yes | Logits tensor with shape (N, C, H, W) where C = number of classes |
| target | torch.Tensor | Yes | Labels tensor with shape (N, H, W) where each value is in [0, C-1] |
| alpha | float | Yes | Coefficient controlling false positive penalty |
| beta | float | Yes | Coefficient controlling false negative penalty |
| eps | float | No | Scalar for numerical stability (default: 1e-8) |
| ignore_index | Optional[int] | No | Label value to ignore in loss computation (default: -100) |
Outputs
| Name | Type | Description |
|---|---|---|
| loss | torch.Tensor | Scalar Tversky loss value (1 - Tversky index), averaged over the batch |
Usage Examples
import torch
from kornia.losses import TverskyLoss
# Multi-class segmentation
N = 5 # num_classes
pred = torch.randn(1, N, 3, 5, requires_grad=True)
target = torch.empty(1, 3, 5, dtype=torch.long).random_(N)
# Dice-equivalent (alpha=0.5, beta=0.5)
criterion = TverskyLoss(alpha=0.5, beta=0.5)
output = criterion(pred, target)
output.backward()
# Favor recall (penalize FNs more)
criterion_recall = TverskyLoss(alpha=0.3, beta=0.7)
output = criterion_recall(pred, target)
# Favor precision (penalize FPs more)
criterion_precision = TverskyLoss(alpha=0.7, beta=0.3)
output = criterion_precision(pred, target)