Wraps a stateless metric function with the Mean metric

metric_mean_wrapper(..., fn, name = NULL, dtype = NULL)



named arguments to pass on to fn.


The metric function to wrap, with signature fn(y_true, y_pred, ...).


(Optional) string name of the metric instance.


(Optional) data type of the metric result.


A (subclassed) Metric instance that can be passed directly to compile(metrics = ), or used as a standalone object. See ?Metric for example usage.


You could use this class to quickly build a mean metric from a function. The function needs to have the signature fn(y_true, y_pred) and return a per-sample loss array. MeanMetricWrapper$result() will return the average metric value across all samples seen so far.

For example:

accuracy <- function(y_true, y_pred)
  k_cast(y_true == y_pred, 'float32')

accuracy_metric <- metric_mean_wrapper(fn = accuracy)

model %>% compile(..., metrics=accuracy_metric)