Shortcuts

torcheval.metrics.WindowedWeightedCalibration

class torcheval.metrics.WindowedWeightedCalibration(*, num_tasks: int = 1, max_num_updates: int = 100, enable_lifetime: bool = True, device: device | None = None)

计算加权校准指标。当未提供权重时,它计算未加权的校准。 其功能版本是 torcheval.metrics.functional.weighted_calibration()

加权校准 = sum(input * weight) / sum(target * weight)

Parameters:
  • num_tasks (int) – 需要进行WeightedCalibration计算的任务数量。默认值为1。

  • max_num_updates (int) – 可以容纳更新次数的最大窗口大小。

  • enable_lifetime (bool) – 一个布尔值指示器,用于确定是否计算生命周期值。

Raises:

ValueError – 如果权重的值既不是float,也不是int,也不是与输入张量大小匹配的torch.Tensor

示例:

>>> import torch
>>> from torcheval.metrics import WindowedWeightedCalibration
>>> metric = WindowedWeightedCalibration(max_num_updates=2, enable_lifetime=False)
>>> metric.update(torch.tensor([0.8, 0.4]),torch.tensor([1, 1]))
>>> metric.update(torch.tensor([0.3, 0.8]),torch.tensor([0, 0]))
>>> metric.update(torch.tensor([0.7, 0.6]),torch.tensor([1, 0]))
>>> metric.compute()
tensor([2.4], dtype=torch.float64)

>>> metric = WindowedWeightedCalibration(max_num_updates=2, enable_lifetime=True)
>>> metric.update(torch.tensor([0.8, 0.4]),torch.tensor([1, 1]))
>>> metric.update(torch.tensor([0.3, 0.8]),torch.tensor([0, 0]))
>>> metric.update(torch.tensor([0.7, 0.6]),torch.tensor([1, 0]))
>>> metric.compute()
(
tensor([1.2], dtype=torch.float64)
tensor([2.4], dtype=torch.float64)
)

>>> metric = WindowedWeightedCalibration(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, max_num_updates: int = 100, enable_lifetime: bool = True, device: device | None = None) None

初始化一个度量对象及其内部状态。

使用 self._add_state() 来初始化你的度量类的状态变量。 状态变量应该是 torch.Tensor,一个 torch.Tensor 的列表,一个以 torch.Tensor 为值的字典, 或者一个 torch.Tensor 的双端队列。

方法

__init__(*[, 任务数量, 最大更新次数, ...])

初始化一个度量对象及其内部状态。

compute()

返回加权校准。

load_state_dict(state_dict[, strict])

从state_dict加载度量状态变量。

merge_state(metrics)

将度量状态与其他度量实例的对应部分合并。

reset()

将度量状态变量重置为其默认值。

state_dict()

将度量状态变量保存在state_dict中。

to(device, *args, **kwargs)

将度量状态变量中的张量移动到设备。

update(input, target[, weight])

使用加权输入的总和和加权标签的总和更新度量状态。

属性

device

Metric.to()的最后一个输入设备。