From 5a78fc2e33c28968a69d033cb10d638f4f63fed1 Mon Sep 17 00:00:00 2001 From: aktersnurra Date: Sun, 5 Jul 2020 22:27:08 +0200 Subject: Working on getting experiment loop. --- src/text_recognizer/models/metrics.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/text_recognizer/models/metrics.py (limited to 'src/text_recognizer/models/metrics.py') diff --git a/src/text_recognizer/models/metrics.py b/src/text_recognizer/models/metrics.py new file mode 100644 index 0000000..e2a30a9 --- /dev/null +++ b/src/text_recognizer/models/metrics.py @@ -0,0 +1,19 @@ +"""Utility functions for models.""" + +import torch + + +def accuracy(outputs: torch.Tensor, labels: torch.Tensro) -> float: + """Computes the accuracy. + + Args: + outputs (torch.Tensor): The output from the network. + labels (torch.Tensor): Ground truth labels. + + Returns: + float: The accuracy for the batch. + + """ + _, predicted = torch.max(outputs.data, dim=1) + acc = (predicted == labels).sum().item() / labels.shape[0] + return acc -- cgit v1.2.3-70-g09d2