Skip to contents

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_*, as well as what is returned by custom metrics defined with Metric().


  initialize = NULL,
  update_state = NULL,
  result = NULL,
  public = list(),
  private = list(),
  inherit = NULL,
  parent_env = parent.frame()



String, the name of the custom class. (Conventionally, CamelCase).

initialize, update_state, result

Recommended methods to implement. See description section.

..., public

Additional methods or public members of the custom class.


Named list of R objects (typically, functions) to include in instance private environments. private methods will have all the same symbols in scope as public methods (See section "Symbols in Scope"). Each instance will have it's own private environment. Any objects in private will be invisible from the Keras framework and the Python runtime.


What the custom class will subclass. By default, the base keras class.


The R environment that all class methods will have as a grandparent.


A function that returns Metric instances, similar to the builtin metric functions.


Usage with compile():

model |> compile(
  optimizer = 'sgd',
  loss = 'mse',
  metrics = c(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")

Full Examples

Usage with compile():

model <- keras_model_sequential()
model |>
  layer_dense(64, activation = "relu") |>
  layer_dense(64, activation = "relu") |>
  layer_dense(10, activation = "softmax")
model |>
  compile(optimizer = optimizer_rmsprop(0.01),
          loss = loss_categorical_crossentropy(),
          metrics = metric_categorical_accuracy())

data <- random_uniform(c(1000, 32))
labels <- random_uniform(c(1000, 10))

model |> fit(data, labels, verbose = 0)

To be implemented by subclasses (custom metrics):

  • initialize(): All state variables should be created in this method by calling self$add_variable() like: self$var <- self$add_variable(...).

  • update_state(): Updates all the state variables like: self$var$assign(...).

  • result(): Computes and returns a scalar value or a named list of scalar values for the metric from the state variables.

Example subclass implementation:

metric_binary_true_positives <- Metric(
  classname = "BinaryTruePositives",

  initialize = function(name = 'binary_true_positives', ...) {
    super$initialize(name = name, ...)
    self$true_positives <-
      self$add_weight(shape = shape(),
                      initializer = 'zeros',
                      name = 'true_positives')

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

    values <- y_true & y_pred # `&` calls op_logical_and()
    values <- op_cast(values, self$dtype)
    if (!is.null(sample_weight)) {
      sample_weight <- op_cast(sample_weight, self$dtype)
      sample_weight <- op_broadcast_to(sample_weight, shape(values))
      values <- values * sample_weight # `*` calls op_multiply()
    self$true_positives$assign(self$true_positives + op_sum(values))

  result = function() {
model <- keras_model_sequential(input_shape = 32) |> layer_dense(10)
model |> compile(loss = loss_binary_crossentropy(),
                 metrics = list(metric_binary_true_positives()))
model |> fit(data, labels, verbose = 0)

Methods defined by the base Metric class:

  • __call__(...)

    Calling a metric instance self like m(...) is equivalent to calling:

    function(...) {

  • initialize(dtype=NULL, name=NULL)

    Initialize self.


    • name: (Optional) string name of the metric instance.

    • dtype: (Optional) data type of the metric result.

  • add_variable(shape, initializer, dtype=NULL, aggregation = 'sum', name=NULL)

  • add_weight(shape=shape(), initializer=NULL, dtype=NULL, name=NULL)

  • Return the serializable config of the metric.

  • Reset all of the metric state variables.

    This function is called between epochs/steps, when a metric is evaluated during training.

  • result()

    Compute the current metric value.

    Returns: A scalar tensor, or a named list of scalar tensors.

  • stateless_result(metric_variables)

  • stateless_reset_state()

  • stateless_update_state(metric_variables, ...)

  • update_state(...)

    Accumulate statistics for the metric.

Readonly properties

  • dtype

  • variables

Symbols in scope

All R function custom methods (public and private) will have the following symbols in scope:

  • self: The custom class instance.

  • super: The custom class superclass.

  • private: An R environment specific to the class instance. Any objects assigned here are invisible to the Keras framework.

  • __class__ and as.symbol(classname): the custom class type object.