A Metric object encapsulates metric logic and state that can be used to track model performance during training. It is what is returned by the family of metric functions that start with prefix metric_*.



(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.

Usage with compile

model %>% compile(
  optimizer = 'sgd',
  loss = 'mse',
  metrics = list(metric_SOME_METRIC(), metric_SOME_OTHER_METRIC())

Standalone usage

m <- metric_SOME_METRIC()
for (e in seq(epochs)) {
  for (i in seq(train_steps)) {
    c(y_true, y_pred, sample_weight = NULL) %<-% ...
    m$update_state(y_true, y_pred, sample_weight)
  cat('Final epoch result: ', as.numeric(m$result()), "\n")

Custom Metric (subclass)

To be implemented by subclasses:

  • initialize(): All state variables should be created in this method by calling self$add_weight() like:

    self$var <- self$add_weight(...)
  • update_state(): Has all updates to the state variables like:

  • result(): Computes and returns a value for the metric from the state variables.

Example custom metric subclass:

metric_binary_true_positives <- new_metric_class(
  classname = "BinaryTruePositives",
  initialize = function(name = 'binary_true_positives', ...) {
    super$initialize(name = name, ...)
    self$true_positives <-
      self$add_weight(name = 'tp', initializer = 'zeros')

  update_state = function(y_true, y_pred, sample_weight = NULL) {
    y_true <- k_cast(y_true, "bool")
    y_pred <- k_cast(y_pred, "bool")

    values <- y_true & y_pred
    values <- k_cast(values, self$dtype)
    if (!is.null(sample_weight)) {
      sample_weight <- k_cast(sample_weight, self$dtype)
      sample_weight <- tf$broadcast_to(sample_weight, values$shape)
      values <- values * sample_weight

  result = function()
model %>% compile(..., metrics = list(metric_binary_true_positives()))

The same metric_binary_true_positives could be built with %py_class% like this:

metric_binary_true_positives(keras$metrics$Metric) %py_class% {
  initialize <- <same-as-above>,
  update_state <- <same-as-above>,
  result <- <same-as-above>