Python tensorflow.keras.metrics() Examples

The following are 17 code examples of tensorflow.keras.metrics(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tensorflow.keras , or try the search function .
Example #1
Source File: utils.py    From MultiPlanarUNet with MIT License 6 votes vote down vote up
def _init(string_list, tf_funcs, custom_funcs, logger=None, **kwargs):
    """
    Helper for 'init_losses' or 'init_metrics'.
    Please refer to their docstrings.

    Args:
        string_list:  (list)   List of strings, each giving a name of a metric
                               or loss to use for training. The name should
                               refer to a function or class in either tf_funcs
                               or custom_funcs modules.
        tf_funcs:     (module) A Tensorflow.keras module of losses or metrics,
                               or a list of various modules to look through.
        custom_funcs: (module) A custom module or losses or metrics
        logger:       (Logger) A Logger object
        **kwargs:     (dict)   Parameters passed to all losses or metrics which
                               are represented by a class (i.e. not a function)

    Returns:
        A list of len(string_list) of initialized classes of losses or metrics
        or references to loss or metric functions.
    """
    initialized = []
    tf_funcs = ensure_list_or_tuple(tf_funcs)
    for func_or_class in ensure_list_or_tuple(string_list):
        modules_found = list(filter(None, [getattr(m, func_or_class, None)
                                           for m in tf_funcs]))
        if modules_found:
            initialized.append(modules_found[0])  # return the first found
        else:
            # Fall back to look in custom module
            initialized.append(getattr(custom_funcs, func_or_class))
    return initialized 
Example #2
Source File: metrics_tracking.py    From keras-tuner with Apache License 2.0 5 votes vote down vote up
def get_best_step(self, name):
        self._assert_exists(name)
        return self.metrics[name].get_best_step() 
Example #3
Source File: metrics_tracking.py    From keras-tuner with Apache License 2.0 5 votes vote down vote up
def infer_metric_direction(metric):
    # Handle str input and get canonical object.
    if isinstance(metric, six.string_types):
        metric_name = metric

        if metric_name.startswith('val_'):
            metric_name = metric_name.replace('val_', '', 1)

        if metric_name.startswith('weighted_'):
            metric_name = metric_name.replace('weighted_', '', 1)

        # Special-cases (from `keras/engine/training_utils.py`)
        if metric_name in {'loss', 'crossentropy', 'ce'}:
            return 'min'
        elif metric_name == 'acc':
            return 'max'

        try:
            metric = keras.metrics.get(metric_name)
        except ValueError:
            try:
                metric = keras.losses.get(metric_name)
            except:
                # Direction can't be inferred.
                return None

    # Metric class, Loss class, or function.
    if isinstance(metric, (keras.metrics.Metric, keras.losses.Loss)):
        name = metric.__class__.__name__
        if name == 'MeanMetricWrapper':
            name = metric._fn.__name__
    else:
        name = metric.__name__

    if name in _MAX_METRICS or name in _MAX_METRIC_FNS:
        return 'max'
    elif hasattr(keras.metrics, name) or hasattr(keras.losses, name):
        return 'min'

    # Direction can't be inferred.
    return None 
Example #4
Source File: metrics_tracking.py    From keras-tuner with Apache License 2.0 5 votes vote down vote up
def _assert_exists(self, name):
        if name not in self.metrics:
            raise ValueError('Unknown metric: %s' % (name,)) 
Example #5
Source File: metrics_tracking.py    From keras-tuner with Apache License 2.0 5 votes vote down vote up
def from_proto(cls, proto):
        instance = cls()
        instance.metrics = {
            name: MetricHistory.from_proto(metric_history)
            for name, metric_history in proto.metrics.items()}
        return instance 
Example #6
Source File: metrics_tracking.py    From keras-tuner with Apache License 2.0 5 votes vote down vote up
def to_proto(self):
        return kerastuner_pb2.MetricsTracker(metrics={
            name: metric_history.to_proto()
            for name, metric_history in self.metrics.items()}) 
Example #7
Source File: metrics_tracking.py    From keras-tuner with Apache License 2.0 5 votes vote down vote up
def from_config(cls, config):
        instance = cls()
        instance.metrics = {
            name: MetricHistory.from_config(metric_history)
            for name, metric_history in config['metrics'].items()}
        return instance 
Example #8
Source File: metrics_tracking.py    From keras-tuner with Apache License 2.0 5 votes vote down vote up
def get_direction(self, name):
        self._assert_exists(name)
        return self.metrics[name].direction 
Example #9
Source File: metrics_tracking.py    From keras-tuner with Apache License 2.0 5 votes vote down vote up
def get_last_value(self, name):
        self._assert_exists(name)
        return self.metrics[name].get_last_value() 
Example #10
Source File: metrics_tracking.py    From keras-tuner with Apache License 2.0 5 votes vote down vote up
def get_statistics(self, name):
        self._assert_exists(name)
        return self.metrics[name].get_statistics() 
Example #11
Source File: metrics_tracking.py    From keras-tuner with Apache License 2.0 5 votes vote down vote up
def get_best_value(self, name):
        self._assert_exists(name)
        return self.metrics[name].get_best_value() 
Example #12
Source File: metrics_tracking.py    From keras-tuner with Apache License 2.0 5 votes vote down vote up
def get_history(self, name):
        self._assert_exists(name)
        return self.metrics[name].get_history() 
Example #13
Source File: metrics_tracking.py    From keras-tuner with Apache License 2.0 5 votes vote down vote up
def update(self, name, value, step=0):
        value = float(value)
        if not self.exists(name):
            self.register(name)

        prev_best = self.metrics[name].get_best_value()
        self.metrics[name].update(value, step=step)
        new_best = self.metrics[name].get_best_value()

        improved = new_best != prev_best
        return improved 
Example #14
Source File: metrics_tracking.py    From keras-tuner with Apache License 2.0 5 votes vote down vote up
def register(self, name, direction=None):
        if self.exists(name):
            raise ValueError('Metric already exists: %s' % (name,))
        if direction is None:
            direction = infer_metric_direction(name)
            if direction is None:
                # Objective direction is handled separately, but
                # non-objective direction defaults to min.
                direction = 'min'
        self.metrics[name] = MetricHistory(direction) 
Example #15
Source File: metrics_tracking.py    From keras-tuner with Apache License 2.0 5 votes vote down vote up
def register_metrics(self, metrics=None):
        metrics = metrics or []
        for metric in metrics:
            self.register(metric.name) 
Example #16
Source File: metrics_tracking.py    From keras-tuner with Apache License 2.0 5 votes vote down vote up
def __init__(self, metrics=None):
        # str -> MetricHistory
        self.metrics = {}
        self.register_metrics(metrics) 
Example #17
Source File: utils.py    From MultiPlanarUNet with MIT License 5 votes vote down vote up
def init_metrics(metric_string_list, logger=None, **kwargs):
    """
    Same as 'init_losses', but for metrics.
    Please refer to the 'init_losses' docstring.
    """
    return _init(
        metric_string_list, metrics, custom_metrics, logger, **kwargs
    )