Computes Area Under the Receiver Operating Characteristic Curve (ROC AUC). Referring to: `sklearn.metrics.roc_auc_score `_. Args: y_pred: input data to compute, t
(
y_pred: torch.Tensor, y: torch.Tensor, average: Average | str = Average.MACRO
)
| 110 | |
| 111 | |
| 112 | def compute_roc_auc( |
| 113 | y_pred: torch.Tensor, y: torch.Tensor, average: Average | str = Average.MACRO |
| 114 | ) -> np.ndarray | float | npt.ArrayLike: |
| 115 | """Computes Area Under the Receiver Operating Characteristic Curve (ROC AUC). Referring to: |
| 116 | `sklearn.metrics.roc_auc_score <https://scikit-learn.org/stable/modules/generated/ |
| 117 | sklearn.metrics.roc_auc_score.html#sklearn.metrics.roc_auc_score>`_. |
| 118 | |
| 119 | Args: |
| 120 | y_pred: input data to compute, typical classification model output. |
| 121 | the first dim must be batch, if multi-classes, it must be in One-Hot format. |
| 122 | for example: shape `[16]` or `[16, 1]` for a binary data, shape `[16, 2]` for 2 classes data. |
| 123 | y: ground truth to compute ROC AUC metric, the first dim must be batch. |
| 124 | if multi-classes, it must be in One-Hot format. |
| 125 | for example: shape `[16]` or `[16, 1]` for a binary data, shape `[16, 2]` for 2 classes data. |
| 126 | average: {``"macro"``, ``"weighted"``, ``"micro"``, ``"none"``} |
| 127 | Type of averaging performed if not binary classification. |
| 128 | Defaults to ``"macro"``. |
| 129 | |
| 130 | - ``"macro"``: calculate metrics for each label, and find their unweighted mean. |
| 131 | This does not take label imbalance into account. |
| 132 | - ``"weighted"``: calculate metrics for each label, and find their average, |
| 133 | weighted by support (the number of true instances for each label). |
| 134 | - ``"micro"``: calculate metrics globally by considering each element of the label |
| 135 | indicator matrix as a label. |
| 136 | - ``"none"``: the scores for each class are returned. |
| 137 | |
| 138 | Raises: |
| 139 | ValueError: When ``y_pred`` dimension is not one of [1, 2]. |
| 140 | ValueError: When ``y`` dimension is not one of [1, 2]. |
| 141 | ValueError: When ``average`` is not one of ["macro", "weighted", "micro", "none"]. |
| 142 | |
| 143 | Note: |
| 144 | ROCAUC expects y to be comprised of 0's and 1's. `y_pred` must be either prob. estimates or confidence values. |
| 145 | |
| 146 | """ |
| 147 | y_pred_ndim = y_pred.ndimension() |
| 148 | y_ndim = y.ndimension() |
| 149 | if y_pred_ndim not in (1, 2): |
| 150 | raise ValueError( |
| 151 | f"Predictions should be of shape (batch_size, num_classes) or (batch_size, ), got {y_pred.shape}." |
| 152 | ) |
| 153 | if y_ndim not in (1, 2): |
| 154 | raise ValueError(f"Targets should be of shape (batch_size, num_classes) or (batch_size, ), got {y.shape}.") |
| 155 | if y_pred_ndim == 2 and y_pred.shape[1] == 1: |
| 156 | y_pred = y_pred.squeeze(dim=-1) |
| 157 | y_pred_ndim = 1 |
| 158 | if y_ndim == 2 and y.shape[1] == 1: |
| 159 | y = y.squeeze(dim=-1) |
| 160 | |
| 161 | if y_pred_ndim == 1: |
| 162 | return _calculate(y_pred, y) |
| 163 | |
| 164 | if y.shape != y_pred.shape: |
| 165 | raise ValueError(f"data shapes of y_pred and y do not match, got {y_pred.shape} and {y.shape}.") |
| 166 | |
| 167 | average = look_up_option(average, Average) |
| 168 | if average == Average.MICRO: |
| 169 | return _calculate(y_pred.flatten(), y.flatten()) |
searching dependent graphs…