# coding=utf-8 # Copyright 2020 The Google Research Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Base class for evaluation metrics.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import abc class Scorer(object): """Abstract base class for computing evaluation metrics.""" __metaclass__ = abc.ABCMeta def __init__(self): self._updated = False self._cached_results = {} @abc.abstractmethod def update(self, results): self._updated = True @abc.abstractmethod def get_loss(self): pass @abc.abstractmethod def _get_results(self): return [] def get_results(self, prefix=""): results = self._get_results() if self._updated else self._cached_results self._cached_results = results self._updated = False return [(prefix + k, v) for k, v in results] def results_str(self): return " - ".join(["{:}: {:.2f}".format(k, v) for k, v in self.get_results()])