roc.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. # Copyright The Lightning team.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import List, Optional, Union
  15. import torch
  16. from torch import Tensor
  17. from typing_extensions import Literal
  18. from torchmetrics.functional.classification.precision_recall_curve import (
  19. _binary_clf_curve,
  20. _binary_precision_recall_curve_arg_validation,
  21. _binary_precision_recall_curve_format,
  22. _binary_precision_recall_curve_tensor_validation,
  23. _binary_precision_recall_curve_update,
  24. _multiclass_precision_recall_curve_arg_validation,
  25. _multiclass_precision_recall_curve_format,
  26. _multiclass_precision_recall_curve_tensor_validation,
  27. _multiclass_precision_recall_curve_update,
  28. _multilabel_precision_recall_curve_arg_validation,
  29. _multilabel_precision_recall_curve_format,
  30. _multilabel_precision_recall_curve_tensor_validation,
  31. _multilabel_precision_recall_curve_update,
  32. )
  33. from torchmetrics.utilities import rank_zero_warn
  34. from torchmetrics.utilities.compute import _safe_divide, interp
  35. from torchmetrics.utilities.enums import ClassificationTask
  36. def _binary_roc_compute(
  37. state: Union[Tensor, tuple[Tensor, Tensor]],
  38. thresholds: Optional[Tensor],
  39. pos_label: int = 1,
  40. ) -> tuple[Tensor, Tensor, Tensor]:
  41. if isinstance(state, Tensor) and thresholds is not None:
  42. tps = state[:, 1, 1]
  43. fps = state[:, 0, 1]
  44. fns = state[:, 1, 0]
  45. tns = state[:, 0, 0]
  46. tpr = _safe_divide(tps, tps + fns).flip(0)
  47. fpr = _safe_divide(fps, fps + tns).flip(0)
  48. thres = thresholds.flip(0)
  49. else:
  50. fps, tps, thres = _binary_clf_curve(preds=state[0], target=state[1], pos_label=pos_label)
  51. # Add an extra threshold position to make sure that the curve starts at (0, 0)
  52. tps = torch.cat([torch.zeros(1, dtype=tps.dtype, device=tps.device), tps])
  53. fps = torch.cat([torch.zeros(1, dtype=fps.dtype, device=fps.device), fps])
  54. thres = torch.cat([torch.ones(1, dtype=thres.dtype, device=thres.device), thres])
  55. if fps[-1] <= 0:
  56. rank_zero_warn(
  57. "No negative samples in targets, false positive value should be meaningless."
  58. " Returning zero tensor in false positive score",
  59. UserWarning,
  60. )
  61. fpr = torch.zeros_like(thres)
  62. else:
  63. fpr = fps / fps[-1]
  64. if tps[-1] <= 0:
  65. rank_zero_warn(
  66. "No positive samples in targets, true positive value should be meaningless."
  67. " Returning zero tensor in true positive score",
  68. UserWarning,
  69. )
  70. tpr = torch.zeros_like(thres)
  71. else:
  72. tpr = tps / tps[-1]
  73. return fpr, tpr, thres
  74. def binary_roc(
  75. preds: Tensor,
  76. target: Tensor,
  77. thresholds: Optional[Union[int, list[float], Tensor]] = None,
  78. ignore_index: Optional[int] = None,
  79. validate_args: bool = True,
  80. ) -> tuple[Tensor, Tensor, Tensor]:
  81. r"""Compute the Receiver Operating Characteristic (ROC) for binary tasks.
  82. The curve consist of multiple pairs of true positive rate (TPR) and false positive rate (FPR) values evaluated at
  83. different thresholds, such that the tradeoff between the two values can be seen.
  84. Accepts the following input tensors:
  85. - ``preds`` (float tensor): ``(N, ...)``. Preds should be a tensor containing probabilities or logits for each
  86. observation. If preds has values outside [0,1] range we consider the input to be logits and will auto apply
  87. sigmoid per element.
  88. - ``target`` (int tensor): ``(N, ...)``. Target should be a tensor containing ground truth labels, and therefore
  89. only contain {0,1} values (except if `ignore_index` is specified). The value 1 always encodes the positive class.
  90. Additional dimension ``...`` will be flattened into the batch dimension.
  91. The implementation both supports calculating the metric in a non-binned but accurate version and a binned version
  92. that is less accurate but more memory efficient. Setting the `thresholds` argument to `None` will activate the
  93. non-binned version that uses memory of size :math:`\mathcal{O}(n_{samples})` whereas setting the `thresholds`
  94. argument to either an integer, list or a 1d tensor will use a binned version that uses memory of
  95. size :math:`\mathcal{O}(n_{thresholds})` (constant memory).
  96. Note that outputted thresholds will be in reversed order to ensure that they corresponds to both fpr and tpr which
  97. are sorted in reversed order during their calculation, such that they are monotome increasing.
  98. Args:
  99. preds: Tensor with predictions
  100. target: Tensor with true labels
  101. thresholds:
  102. Can be one of:
  103. - If set to `None`, will use a non-binned approach where thresholds are dynamically calculated from
  104. all the data. Most accurate but also most memory consuming approach.
  105. - If set to an `int` (larger than 1), will use that number of thresholds linearly spaced from
  106. 0 to 1 as bins for the calculation.
  107. - If set to an `list` of floats, will use the indicated thresholds in the list as bins for the calculation
  108. - If set to an 1d `tensor` of floats, will use the indicated thresholds in the tensor as
  109. bins for the calculation.
  110. ignore_index:
  111. Specifies a target value that is ignored and does not contribute to the metric calculation
  112. validate_args: bool indicating if input arguments and tensors should be validated for correctness.
  113. Set to ``False`` for faster computations.
  114. Returns:
  115. (tuple): a tuple of 3 tensors containing:
  116. - fpr: an 1d tensor of size (n_thresholds+1, ) with false positive rate values
  117. - tpr: an 1d tensor of size (n_thresholds+1, ) with true positive rate values
  118. - thresholds: an 1d tensor of size (n_thresholds, ) with decreasing threshold values
  119. Example:
  120. >>> from torchmetrics.functional.classification import binary_roc
  121. >>> preds = torch.tensor([0, 0.5, 0.7, 0.8])
  122. >>> target = torch.tensor([0, 1, 1, 0])
  123. >>> binary_roc(preds, target, thresholds=None) # doctest: +NORMALIZE_WHITESPACE
  124. (tensor([0.0000, 0.5000, 0.5000, 0.5000, 1.0000]),
  125. tensor([0.0000, 0.0000, 0.5000, 1.0000, 1.0000]),
  126. tensor([1.0000, 0.8000, 0.7000, 0.5000, 0.0000]))
  127. >>> binary_roc(preds, target, thresholds=5) # doctest: +NORMALIZE_WHITESPACE
  128. (tensor([0.0000, 0.5000, 0.5000, 0.5000, 1.0000]),
  129. tensor([0., 0., 1., 1., 1.]),
  130. tensor([1.0000, 0.7500, 0.5000, 0.2500, 0.0000]))
  131. """
  132. if validate_args:
  133. _binary_precision_recall_curve_arg_validation(thresholds, ignore_index)
  134. _binary_precision_recall_curve_tensor_validation(preds, target, ignore_index)
  135. preds, target, thresholds = _binary_precision_recall_curve_format(preds, target, thresholds, ignore_index)
  136. state = _binary_precision_recall_curve_update(preds, target, thresholds)
  137. return _binary_roc_compute(state, thresholds)
  138. def _multiclass_roc_compute(
  139. state: Union[Tensor, tuple[Tensor, Tensor]],
  140. num_classes: int,
  141. thresholds: Optional[Tensor],
  142. average: Optional[Literal["micro", "macro"]] = None,
  143. ) -> Union[tuple[Tensor, Tensor, Tensor], tuple[List[Tensor], List[Tensor], List[Tensor]]]:
  144. if average == "micro":
  145. return _binary_roc_compute(state, thresholds, pos_label=1)
  146. if isinstance(state, Tensor) and thresholds is not None:
  147. tps = state[:, :, 1, 1]
  148. fps = state[:, :, 0, 1]
  149. fns = state[:, :, 1, 0]
  150. tns = state[:, :, 0, 0]
  151. tpr = _safe_divide(tps, tps + fns).flip(0).T
  152. fpr = _safe_divide(fps, fps + tns).flip(0).T
  153. thres = thresholds.flip(0)
  154. tensor_state = True
  155. else:
  156. fpr_list, tpr_list, thres_list = [], [], []
  157. for i in range(num_classes):
  158. res = _binary_roc_compute((state[0][:, i], state[1]), thresholds=None, pos_label=i)
  159. fpr_list.append(res[0])
  160. tpr_list.append(res[1])
  161. thres_list.append(res[2])
  162. tensor_state = False
  163. if average == "macro":
  164. thres = thres.repeat(num_classes) if tensor_state else torch.cat(thres_list, dim=0)
  165. thres = thres.sort(descending=True).values
  166. mean_fpr = fpr.flatten() if tensor_state else torch.cat(fpr_list, dim=0)
  167. mean_fpr = mean_fpr.sort().values
  168. mean_tpr = torch.zeros_like(mean_fpr)
  169. for i in range(num_classes):
  170. mean_tpr += interp(
  171. mean_fpr, fpr[i] if tensor_state else fpr_list[i], tpr[i] if tensor_state else tpr_list[i]
  172. )
  173. mean_tpr /= num_classes
  174. return mean_fpr, mean_tpr, thres
  175. if tensor_state:
  176. return fpr, tpr, thres
  177. return fpr_list, tpr_list, thres_list
  178. def multiclass_roc(
  179. preds: Tensor,
  180. target: Tensor,
  181. num_classes: int,
  182. thresholds: Optional[Union[int, list[float], Tensor]] = None,
  183. average: Optional[Literal["micro", "macro"]] = None,
  184. ignore_index: Optional[int] = None,
  185. validate_args: bool = True,
  186. ) -> Union[tuple[Tensor, Tensor, Tensor], tuple[List[Tensor], List[Tensor], List[Tensor]]]:
  187. r"""Compute the Receiver Operating Characteristic (ROC) for multiclass tasks.
  188. The curve consist of multiple pairs of true positive rate (TPR) and false positive rate (FPR) values evaluated at
  189. different thresholds, such that the tradeoff between the two values can be seen.
  190. Accepts the following input tensors:
  191. - ``preds`` (float tensor): ``(N, C, ...)``. Preds should be a tensor containing probabilities or logits for each
  192. observation. If preds has values outside [0,1] range we consider the input to be logits and will auto apply
  193. softmax per sample.
  194. - ``target`` (int tensor): ``(N, ...)``. Target should be a tensor containing ground truth labels, and therefore
  195. only contain values in the [0, n_classes-1] range (except if `ignore_index` is specified).
  196. Additional dimension ``...`` will be flattened into the batch dimension.
  197. The implementation both supports calculating the metric in a non-binned but accurate version and a binned version
  198. that is less accurate but more memory efficient. Setting the `thresholds` argument to `None` will activate the
  199. non-binned version that uses memory of size :math:`\mathcal{O}(n_{samples})` whereas setting the `thresholds`
  200. argument to either an integer, list or a 1d tensor will use a binned version that uses memory of
  201. size :math:`\mathcal{O}(n_{thresholds} \times n_{classes})` (constant memory).
  202. Note that outputted thresholds will be in reversed order to ensure that they corresponds to both fpr and tpr which
  203. are sorted in reversed order during their calculation, such that they are monotome increasing.
  204. Args:
  205. preds: Tensor with predictions
  206. target: Tensor with true labels
  207. num_classes: Integer specifying the number of classes
  208. thresholds:
  209. Can be one of:
  210. - If set to `None`, will use a non-binned approach where thresholds are dynamically calculated from
  211. all the data. Most accurate but also most memory consuming approach.
  212. - If set to an `int` (larger than 1), will use that number of thresholds linearly spaced from
  213. 0 to 1 as bins for the calculation.
  214. - If set to an `list` of floats, will use the indicated thresholds in the list as bins for the calculation
  215. - If set to an 1d `tensor` of floats, will use the indicated thresholds in the tensor as
  216. bins for the calculation.
  217. average:
  218. If aggregation of curves should be applied. By default, the curves are not aggregated and a curve for
  219. each class is returned. If `average` is set to ``"micro"``, the metric will aggregate the curves by one hot
  220. encoding the targets and flattening the predictions, considering all classes jointly as a binary problem.
  221. If `average` is set to ``"macro"``, the metric will aggregate the curves by first interpolating the curves
  222. from each class at a combined set of thresholds and then average over the classwise interpolated curves.
  223. See `averaging curve objects`_ for more info on the different averaging methods.
  224. ignore_index:
  225. Specifies a target value that is ignored and does not contribute to the metric calculation
  226. validate_args: bool indicating if input arguments and tensors should be validated for correctness.
  227. Set to ``False`` for faster computations.
  228. Returns:
  229. (tuple): a tuple of either 3 tensors or 3 lists containing
  230. - fpr: if `thresholds=None` a list for each class is returned with an 1d tensor of size (n_thresholds+1, )
  231. with false positive rate values (length may differ between classes). If `thresholds` is set to something else,
  232. then a single 2d tensor of size (n_classes, n_thresholds+1) with false positive rate values is returned.
  233. - tpr: if `thresholds=None` a list for each class is returned with an 1d tensor of size (n_thresholds+1, )
  234. with true positive rate values (length may differ between classes). If `thresholds` is set to something else,
  235. then a single 2d tensor of size (n_classes, n_thresholds+1) with true positive rate values is returned.
  236. - thresholds: if `thresholds=None` a list for each class is returned with an 1d tensor of size (n_thresholds, )
  237. with decreasing threshold values (length may differ between classes). If `threshold` is set to something else,
  238. then a single 1d tensor of size (n_thresholds, ) is returned with shared threshold values for all classes.
  239. Example:
  240. >>> from torchmetrics.functional.classification import multiclass_roc
  241. >>> preds = torch.tensor([[0.75, 0.05, 0.05, 0.05, 0.05],
  242. ... [0.05, 0.75, 0.05, 0.05, 0.05],
  243. ... [0.05, 0.05, 0.75, 0.05, 0.05],
  244. ... [0.05, 0.05, 0.05, 0.75, 0.05]])
  245. >>> target = torch.tensor([0, 1, 3, 2])
  246. >>> fpr, tpr, thresholds = multiclass_roc(
  247. ... preds, target, num_classes=5, thresholds=None
  248. ... )
  249. >>> fpr # doctest: +NORMALIZE_WHITESPACE
  250. [tensor([0., 0., 1.]), tensor([0., 0., 1.]), tensor([0.0000, 0.3333, 1.0000]),
  251. tensor([0.0000, 0.3333, 1.0000]), tensor([0., 1.])]
  252. >>> tpr
  253. [tensor([0., 1., 1.]), tensor([0., 1., 1.]), tensor([0., 0., 1.]), tensor([0., 0., 1.]), tensor([0., 0.])]
  254. >>> thresholds # doctest: +NORMALIZE_WHITESPACE
  255. [tensor([1.0000, 0.7500, 0.0500]), tensor([1.0000, 0.7500, 0.0500]),
  256. tensor([1.0000, 0.7500, 0.0500]), tensor([1.0000, 0.7500, 0.0500]), tensor([1.0000, 0.0500])]
  257. >>> multiclass_roc(
  258. ... preds, target, num_classes=5, thresholds=5
  259. ... ) # doctest: +NORMALIZE_WHITESPACE
  260. (tensor([[0.0000, 0.0000, 0.0000, 0.0000, 1.0000],
  261. [0.0000, 0.0000, 0.0000, 0.0000, 1.0000],
  262. [0.0000, 0.3333, 0.3333, 0.3333, 1.0000],
  263. [0.0000, 0.3333, 0.3333, 0.3333, 1.0000],
  264. [0.0000, 0.0000, 0.0000, 0.0000, 1.0000]]),
  265. tensor([[0., 1., 1., 1., 1.],
  266. [0., 1., 1., 1., 1.],
  267. [0., 0., 0., 0., 1.],
  268. [0., 0., 0., 0., 1.],
  269. [0., 0., 0., 0., 0.]]),
  270. tensor([1.0000, 0.7500, 0.5000, 0.2500, 0.0000]))
  271. """
  272. if validate_args:
  273. _multiclass_precision_recall_curve_arg_validation(num_classes, thresholds, ignore_index, average)
  274. _multiclass_precision_recall_curve_tensor_validation(preds, target, num_classes, ignore_index)
  275. preds, target, thresholds = _multiclass_precision_recall_curve_format(
  276. preds,
  277. target,
  278. num_classes,
  279. thresholds,
  280. ignore_index,
  281. average,
  282. )
  283. state = _multiclass_precision_recall_curve_update(preds, target, num_classes, thresholds, average)
  284. return _multiclass_roc_compute(state, num_classes, thresholds, average)
  285. def _multilabel_roc_compute(
  286. state: Union[Tensor, tuple[Tensor, Tensor]],
  287. num_labels: int,
  288. thresholds: Optional[Tensor],
  289. ignore_index: Optional[int] = None,
  290. ) -> Union[tuple[Tensor, Tensor, Tensor], tuple[List[Tensor], List[Tensor], List[Tensor]]]:
  291. if isinstance(state, Tensor) and thresholds is not None:
  292. tps = state[:, :, 1, 1]
  293. fps = state[:, :, 0, 1]
  294. fns = state[:, :, 1, 0]
  295. tns = state[:, :, 0, 0]
  296. tpr = _safe_divide(tps, tps + fns).flip(0).T
  297. fpr = _safe_divide(fps, fps + tns).flip(0).T
  298. thres = thresholds.flip(0)
  299. else:
  300. fpr, tpr, thres = [], [], [] # type: ignore[assignment]
  301. for i in range(num_labels):
  302. preds = state[0][:, i]
  303. target = state[1][:, i]
  304. if ignore_index is not None:
  305. idx = target == ignore_index
  306. preds = preds[~idx]
  307. target = target[~idx]
  308. res = _binary_roc_compute((preds, target), thresholds=None, pos_label=1)
  309. fpr.append(res[0])
  310. tpr.append(res[1])
  311. thres.append(res[2])
  312. return fpr, tpr, thres
  313. def multilabel_roc(
  314. preds: Tensor,
  315. target: Tensor,
  316. num_labels: int,
  317. thresholds: Optional[Union[int, list[float], Tensor]] = None,
  318. ignore_index: Optional[int] = None,
  319. validate_args: bool = True,
  320. ) -> Union[tuple[Tensor, Tensor, Tensor], tuple[List[Tensor], List[Tensor], List[Tensor]]]:
  321. r"""Compute the Receiver Operating Characteristic (ROC) for multilabel tasks.
  322. The curve consist of multiple pairs of true positive rate (TPR) and false positive rate (FPR) values evaluated at
  323. different thresholds, such that the tradeoff between the two values can be seen.
  324. Accepts the following input tensors:
  325. - ``preds`` (float tensor): ``(N, C, ...)``. Preds should be a tensor containing probabilities or logits for each
  326. observation. If preds has values outside [0,1] range we consider the input to be logits and will auto apply
  327. sigmoid per element.
  328. - ``target`` (int tensor): ``(N, C, ...)``. Target should be a tensor containing ground truth labels, and therefore
  329. only contain {0,1} values (except if `ignore_index` is specified).
  330. Additional dimension ``...`` will be flattened into the batch dimension.
  331. The implementation both supports calculating the metric in a non-binned but accurate version and a binned version
  332. that is less accurate but more memory efficient. Setting the `thresholds` argument to `None` will activate the
  333. non-binned version that uses memory of size :math:`\mathcal{O}(n_{samples})` whereas setting the `thresholds`
  334. argument to either an integer, list or a 1d tensor will use a binned version that uses memory of
  335. size :math:`\mathcal{O}(n_{thresholds} \times n_{labels})` (constant memory).
  336. Note that outputted thresholds will be in reversed order to ensure that they corresponds to both fpr and tpr which
  337. are sorted in reversed order during their calculation, such that they are monotome increasing.
  338. Args:
  339. preds: Tensor with predictions
  340. target: Tensor with true labels
  341. num_labels: Integer specifying the number of labels
  342. thresholds:
  343. Can be one of:
  344. - If set to `None`, will use a non-binned approach where thresholds are dynamically calculated from
  345. all the data. Most accurate but also most memory consuming approach.
  346. - If set to an `int` (larger than 1), will use that number of thresholds linearly spaced from
  347. 0 to 1 as bins for the calculation.
  348. - If set to an `list` of floats, will use the indicated thresholds in the list as bins for the calculation
  349. - If set to an 1d `tensor` of floats, will use the indicated thresholds in the tensor as
  350. bins for the calculation.
  351. ignore_index:
  352. Specifies a target value that is ignored and does not contribute to the metric calculation
  353. validate_args: bool indicating if input arguments and tensors should be validated for correctness.
  354. Set to ``False`` for faster computations.
  355. Returns:
  356. (tuple): a tuple of either 3 tensors or 3 lists containing
  357. - fpr: if `thresholds=None` a list for each label is returned with an 1d tensor of size (n_thresholds+1, )
  358. with false positive rate values (length may differ between labels). If `thresholds` is set to something else,
  359. then a single 2d tensor of size (n_labels, n_thresholds+1) with false positive rate values is returned.
  360. - tpr: if `thresholds=None` a list for each label is returned with an 1d tensor of size (n_thresholds+1, )
  361. with true positive rate values (length may differ between labels). If `thresholds` is set to something else,
  362. then a single 2d tensor of size (n_labels, n_thresholds+1) with true positive rate values is returned.
  363. - thresholds: if `thresholds=None` a list for each label is returned with an 1d tensor of size (n_thresholds, )
  364. with decreasing threshold values (length may differ between labels). If `threshold` is set to something else,
  365. then a single 1d tensor of size (n_thresholds, ) is returned with shared threshold values for all labels.
  366. Example:
  367. >>> from torchmetrics.functional.classification import multilabel_roc
  368. >>> preds = torch.tensor([[0.75, 0.05, 0.35],
  369. ... [0.45, 0.75, 0.05],
  370. ... [0.05, 0.55, 0.75],
  371. ... [0.05, 0.65, 0.05]])
  372. >>> target = torch.tensor([[1, 0, 1],
  373. ... [0, 0, 0],
  374. ... [0, 1, 1],
  375. ... [1, 1, 1]])
  376. >>> fpr, tpr, thresholds = multilabel_roc(
  377. ... preds, target, num_labels=3, thresholds=None
  378. ... )
  379. >>> fpr # doctest: +NORMALIZE_WHITESPACE
  380. [tensor([0.0000, 0.0000, 0.5000, 1.0000]),
  381. tensor([0.0000, 0.5000, 0.5000, 0.5000, 1.0000]),
  382. tensor([0., 0., 0., 1.])]
  383. >>> tpr # doctest: +NORMALIZE_WHITESPACE
  384. [tensor([0.0000, 0.5000, 0.5000, 1.0000]),
  385. tensor([0.0000, 0.0000, 0.5000, 1.0000, 1.0000]),
  386. tensor([0.0000, 0.3333, 0.6667, 1.0000])]
  387. >>> thresholds # doctest: +NORMALIZE_WHITESPACE
  388. [tensor([1.0000, 0.7500, 0.4500, 0.0500]),
  389. tensor([1.0000, 0.7500, 0.6500, 0.5500, 0.0500]),
  390. tensor([1.0000, 0.7500, 0.3500, 0.0500])]
  391. >>> multilabel_roc(
  392. ... preds, target, num_labels=3, thresholds=5
  393. ... ) # doctest: +NORMALIZE_WHITESPACE
  394. (tensor([[0.0000, 0.0000, 0.0000, 0.5000, 1.0000],
  395. [0.0000, 0.5000, 0.5000, 0.5000, 1.0000],
  396. [0.0000, 0.0000, 0.0000, 0.0000, 1.0000]]),
  397. tensor([[0.0000, 0.5000, 0.5000, 0.5000, 1.0000],
  398. [0.0000, 0.0000, 1.0000, 1.0000, 1.0000],
  399. [0.0000, 0.3333, 0.3333, 0.6667, 1.0000]]),
  400. tensor([1.0000, 0.7500, 0.5000, 0.2500, 0.0000]))
  401. """
  402. if validate_args:
  403. _multilabel_precision_recall_curve_arg_validation(num_labels, thresholds, ignore_index)
  404. _multilabel_precision_recall_curve_tensor_validation(preds, target, num_labels, ignore_index)
  405. preds, target, thresholds = _multilabel_precision_recall_curve_format(
  406. preds, target, num_labels, thresholds, ignore_index
  407. )
  408. state = _multilabel_precision_recall_curve_update(preds, target, num_labels, thresholds)
  409. return _multilabel_roc_compute(state, num_labels, thresholds, ignore_index)
  410. def roc(
  411. preds: Tensor,
  412. target: Tensor,
  413. task: Literal["binary", "multiclass", "multilabel"],
  414. thresholds: Optional[Union[int, list[float], Tensor]] = None,
  415. num_classes: Optional[int] = None,
  416. num_labels: Optional[int] = None,
  417. average: Optional[Literal["micro", "macro"]] = None,
  418. ignore_index: Optional[int] = None,
  419. validate_args: bool = True,
  420. ) -> Union[tuple[Tensor, Tensor, Tensor], tuple[List[Tensor], List[Tensor], List[Tensor]]]:
  421. r"""Compute the Receiver Operating Characteristic (ROC).
  422. The curve consist of multiple pairs of true positive rate (TPR) and false positive rate (FPR) values evaluated at
  423. different thresholds, such that the tradeoff between the two values can be seen.
  424. This function is a simple wrapper to get the task specific versions of this metric, which is done by setting the
  425. ``task`` argument to either ``'binary'``, ``'multiclass'`` or ``'multilabel'``. See the documentation of
  426. :func:`~torchmetrics.functional.classification.binary_roc`,
  427. :func:`~torchmetrics.functional.classification.multiclass_roc` and
  428. :func:`~torchmetrics.functional.classification.multilabel_roc` for the specific details of each argument
  429. influence and examples.
  430. Legacy Example:
  431. >>> pred = torch.tensor([0.0, 1.0, 2.0, 3.0])
  432. >>> target = torch.tensor([0, 1, 1, 1])
  433. >>> fpr, tpr, thresholds = roc(pred, target, task='binary')
  434. >>> fpr
  435. tensor([0., 0., 0., 0., 1.])
  436. >>> tpr
  437. tensor([0.0000, 0.3333, 0.6667, 1.0000, 1.0000])
  438. >>> thresholds
  439. tensor([1.0000, 0.9526, 0.8808, 0.7311, 0.5000])
  440. >>> pred = torch.tensor([[0.75, 0.05, 0.05, 0.05],
  441. ... [0.05, 0.75, 0.05, 0.05],
  442. ... [0.05, 0.05, 0.75, 0.05],
  443. ... [0.05, 0.05, 0.05, 0.75]])
  444. >>> target = torch.tensor([0, 1, 3, 2])
  445. >>> fpr, tpr, thresholds = roc(pred, target, task='multiclass', num_classes=4)
  446. >>> fpr
  447. [tensor([0., 0., 1.]), tensor([0., 0., 1.]), tensor([0.0000, 0.3333, 1.0000]), tensor([0.0000, 0.3333, 1.0000])]
  448. >>> tpr
  449. [tensor([0., 1., 1.]), tensor([0., 1., 1.]), tensor([0., 0., 1.]), tensor([0., 0., 1.])]
  450. >>> thresholds
  451. [tensor([1.0000, 0.7500, 0.0500]),
  452. tensor([1.0000, 0.7500, 0.0500]),
  453. tensor([1.0000, 0.7500, 0.0500]),
  454. tensor([1.0000, 0.7500, 0.0500])]
  455. >>> pred = torch.tensor([[0.8191, 0.3680, 0.1138],
  456. ... [0.3584, 0.7576, 0.1183],
  457. ... [0.2286, 0.3468, 0.1338],
  458. ... [0.8603, 0.0745, 0.1837]])
  459. >>> target = torch.tensor([[1, 1, 0], [0, 1, 0], [0, 0, 0], [0, 1, 1]])
  460. >>> fpr, tpr, thresholds = roc(pred, target, task='multilabel', num_labels=3)
  461. >>> fpr
  462. [tensor([0.0000, 0.3333, 0.3333, 0.6667, 1.0000]),
  463. tensor([0., 0., 0., 1., 1.]),
  464. tensor([0.0000, 0.0000, 0.3333, 0.6667, 1.0000])]
  465. >>> tpr
  466. [tensor([0., 0., 1., 1., 1.]), tensor([0.0000, 0.3333, 0.6667, 0.6667, 1.0000]), tensor([0., 1., 1., 1., 1.])]
  467. >>> thresholds
  468. [tensor([1.0000, 0.8603, 0.8191, 0.3584, 0.2286]),
  469. tensor([1.0000, 0.7576, 0.3680, 0.3468, 0.0745]),
  470. tensor([1.0000, 0.1837, 0.1338, 0.1183, 0.1138])]
  471. """
  472. task = ClassificationTask.from_str(task)
  473. if task == ClassificationTask.BINARY:
  474. return binary_roc(preds, target, thresholds, ignore_index, validate_args)
  475. if task == ClassificationTask.MULTICLASS:
  476. if not isinstance(num_classes, int):
  477. raise ValueError(f"`num_classes` is expected to be `int` but `{type(num_classes)} was passed.`")
  478. return multiclass_roc(preds, target, num_classes, thresholds, average, ignore_index, validate_args)
  479. if task == ClassificationTask.MULTILABEL:
  480. if not isinstance(num_labels, int):
  481. raise ValueError(f"`num_labels` is expected to be `int` but `{type(num_labels)} was passed.`")
  482. return multilabel_roc(preds, target, num_labels, thresholds, ignore_index, validate_args)
  483. raise ValueError(f"Task {task} not supported, expected one of {ClassificationTask}.")