CovAtxRisk#

class torch_uncertainty.metrics.classification.CovAtxRisk(risk_threshold, **kwargs)[source]#

Provide coverage at x Risk.

If there are multiple coverage values corresponding to the given risk, i.e., the risk(coverage) is not monotonic, the coverage at x risk is the maximum coverage value corresponding to the given risk. If no there is no coverage value corresponding to the given risk, return float(“nan”).

Parameters:
  • risk_threshold (float) – The risk threshold at which to compute the coverage.

  • kwargs – Additional arguments to pass to the metric class.

Example:

from torch_uncertainty.metrics.classification import CovAtxRisk

# Define a more diverse dataset with probabilities and targets
probs = torch.tensor(
    [
        [0.9, 0.1],  # Correct prediction (confidence 0.9)
        [0.6, 0.4],  # Incorrect prediction (confidence 0.6)
        [0.8, 0.2],  # Correct prediction (confidence 0.8)
        [0.5, 0.5],  # Incorrect prediction (confidence 0.5)
        [0.7, 0.3],  # Correct prediction (confidence 0.7)
    ]
)
targets = torch.tensor([0, 1, 0, 1, 0])  # Ground truth labels

# Instantiate the CovAtxRisk metric with a risk threshold
metric = CovAtxRisk(risk_threshold=0.3)
metric.update(probs, targets)
coverage_at_risk = metric.compute()

print(f"Coverage at risk: {coverage_at_risk.item()}")
# tensor(0.800000011920929)
compute()[source]#

Compute the coverage at x Risk.

Returns:

The coverage at x risk.

Return type:

Tensor

update(probs, targets)[source]#

Store the scores and their associated errors for later computation.

Parameters:
  • probs (Tensor) – The predicted probabilities of shape \((N, C)\).

  • targets (Tensor) – The ground truth labels of shape \((N,)\).