Shortcuts

torcheval.metrics.WeightedCalibration

class torcheval.metrics.WeightedCalibration(*, num_tasks: int = 1, device: device | None = None)

Compute weighted calibration metric. When weight is not provided, it calculates the unweighted calibration. Its functional version is torcheval.metrics.functional.weighted_calibration().

weighted_calibration = sum(input * weight) / sum(target * weight)

Parameters:

num_tasks (int) – Number of tasks that need WeightedCalibration calculations. Default value is 1.

Raises:

ValueError – If value of weight is neither a float nor a int nor a torch.Tensor that matches the input tensor size.

Examples:

>>> import torch
>>> from torcheval.metrics import WeightedCalibration
>>> metric = WeightedCalibration()
>>> metric.update(torch.tensor([0.8, 0.4, 0.3, 0.8, 0.7, 0.6]),torch.tensor([1, 1, 0, 0, 1, 0]))
>>> metric.compute()
tensor([1.2], dtype=torch.float64)

>>> metric = WeightedCalibration()
>>> metric.update(torch.tensor([0.8, 0.4, 0.3, 0.8, 0.7, 0.6]),torch.tensor([1, 1, 0, 0, 1, 0]), torch.tensor([0.5, 1., 2., 0.4, 1.3, 0.9]))
>>> metric.compute()
tensor([1.1321], dtype=torch.float64)

>>> metric = WeightedCalibration(num_tasks=2)
>>> metric.update(torch.tensor([[0.8, 0.4], [0.8, 0.7]]),torch.tensor([[1, 1], [0, 1]]),)
>>> metric.compute()
tensor([0.6000, 1.5000], dtype=torch.float64)
__init__(*, num_tasks: int = 1, device: device | None = None) None

Initialize a metric object and its internal states.

Use self._add_state() to initialize state variables of your metric class. The state variables should be either torch.Tensor, a list of torch.Tensor, a dictionary with torch.Tensor as values, or a deque of torch.Tensor.

Methods

__init__(*[, num_tasks, device])

Initialize a metric object and its internal states.

compute()

Return the weighted calibration.

load_state_dict(state_dict[, strict])

Loads metric state variables from state_dict.

merge_state(metrics)

Merge the metric state with its counterparts from other metric instances.

reset()

Reset the metric state variables to their default value.

state_dict()

Save metric state variables in state_dict.

to(device, *args, **kwargs)

Move tensors in metric state variables to device.

update(input, target[, weight])

Update the metric state with the total sum of weighted inputs and the total sum of weighted labels.

Attributes

device

The last input device of Metric.to().

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources