torcheval.metrics.AUC¶
- class torcheval.metrics.AUC(*, reorder: bool = True, n_tasks: int = 1, device: device | None = None)¶
使用梯形法则计算曲线下面积 (AUC)。支持 x 和 y 为二维张量,每行被视为自己的 x 和 y 坐标列表,返回一维张量,应返回每行计算的 AUC。
参数:reorder (bool):重新排序输入张量以进行 auc 计算。默认值为 True。num_tasks (int):需要 AUC 计算的任务数。默认值为 1。
>>> from torcheval.metrics.aggregation.auc import AUC
>>> metric = AUC() >>> metric.update(torch.tensor([0,.2,.3,.1]), torch.tensor([1,1,1,1])) >>> metric.compute() tensor([0.3000]) >>> metric.reset() >>> metric.update(torch.tensor([0,.1,.13,.2]), torch.tensor([1,1,2,4])) >>> metric.update(torch.tensor([1.,2.,.1, 3.]), torch.tensor([1,2,3,2])) >>> metric.compute() tensor([5.8850]) >>> metric = AUC(n_tasks=2) # n_tasks should be equal to first dimension of x, y in update() >>> x = torch.tensor([[0.3941, 0.2980, 0.3080], [0.1448, 0.6090, 0.2462]]) >>> y = torch.tensor([[1, 0, 4], [0, 4, 2]]) >>> metric.update(x, y) >>> x1 = torch.tensor([[0.4562, 0.1200, 0.4238], [0.4076, 0.4448, 0.1476]]) >>> y1 = torch.tensor([[3, 4, 3], [2, 0, 4]]) >>> metric.update(x1, y1) >>> metric.compute() tensor([0.7479, 0.9898])
- __init__(*, reorder: bool = True, n_tasks: int = 1, device: device | None = None) None ¶
初始化度量对象及其内部状态。
使用
self._add_state()
初始化度量类状态变量。状态变量应为torch.Tensor
、torch.Tensor
列表、以torch.Tensor
作为值的字典,或torch.Tensor
的双端队列。
方法
__init__
(*[, reorder, n_tasks, device])初始化度量对象及其内部状态。
compute
()根据以前传递给
update
的输入计算 AUC。load_state_dict
(state_dict[, strict])从 state_dict 加载度量状态变量。
merge_state
(metrics)实现此方法以将当前度量的状态变量更新为当前度量和输入度量的合并状态。
reset
()将度量状态变量重置为其默认值。
state_dict
()将度量状态变量保存在 state_dict 中。
to
(device, *args, **kwargs)将度量状态变量中的张量移动到设备上。
update
(x, y)更新并返回计算曲线下面积所需的变量。
属性
device
Metric.to()
的最后一个输入设备。