Calculate accuracy. Args: pad_outputs (Tensor): Prediction tensors (B * Lmax, D). pad_targets (LongTensor): Target label tensors (B, Lmax, D). ignore_label (int): Ignore label id. Returns: float: Accuracy value (0.0 - 1.0).
(pad_outputs, pad_targets, ignore_label)
| 2 | |
| 3 | |
| 4 | def th_accuracy(pad_outputs, pad_targets, ignore_label): |
| 5 | """Calculate accuracy. |
| 6 | |
| 7 | Args: |
| 8 | pad_outputs (Tensor): Prediction tensors (B * Lmax, D). |
| 9 | pad_targets (LongTensor): Target label tensors (B, Lmax, D). |
| 10 | ignore_label (int): Ignore label id. |
| 11 | |
| 12 | Returns: |
| 13 | float: Accuracy value (0.0 - 1.0). |
| 14 | |
| 15 | """ |
| 16 | pad_pred = pad_outputs.view( |
| 17 | pad_targets.size(0), pad_targets.size(1), pad_outputs.size(1) |
| 18 | ).argmax(2) |
| 19 | mask = pad_targets != ignore_label |
| 20 | numerator = torch.sum(pad_pred.masked_select(mask) == pad_targets.masked_select(mask)) |
| 21 | denominator = torch.sum(mask) |
| 22 | return float(numerator) / float(denominator) |
| 23 | |
| 24 | |
| 25 | def compute_accuracy(pad_outputs, pad_targets, ignore_label): |
no test coverage detected
searching dependent graphs…