Writing your own callbacks

Guide to writing Keras callbacks for customizing the behavior during model training, evaluation, or inference.

Introduction

A callback is a powerful tool to customize the behavior of a Keras model during training, evaluation, or inference. Examples include callback_tensorboard() to visualize training progress and results with TensorBoard, or callback_model_checkpoint() to periodically save your model during training.

In this guide, you will learn what a Keras callback is, what it can do, and how you can build your own. We provide a few demos of simple callback applications to get you started.

Setup

library(tensorflow)
library(keras)
envir::import_from(dplyr, last)

tf_version()
Loaded Tensorflow version 2.9.1
[1] '2.9'

Keras callbacks overview

All callbacks subclass the keras$callbacks$Callback class, and override a set of methods called at various stages of training, testing, and predicting. Callbacks are useful to get a view on internal states and statistics of the model during training.

You can pass a list of callbacks (as a named argument callbacks) to the following keras model methods:

  • fit()
  • evaluate()
  • predict()

An overview of callback methods

Global methods

on_(train|test|predict)_begin(logs = NULL)

Called at the beginning of fit/evaluate/predict.

on_(train|test|predict)_end(logs = NULL)

Called at the end of fit/evaluate/predict.

Batch-level methods for training/testing/predicting

on_(train|test|predict)_batch_begin(logs = NULL)

Called right before processing a batch during training/testing/predicting.

on_(train|test|predict)_batch_end(batch, logs = NULL)

Called at the end of training/testing/predicting a batch. Within this method, logs is a dict containing the metrics results.

Epoch-level methods (training only)

on_epoch_begin(epoch, logs = NULL)

Called at the beginning of an epoch during training.

on_epoch_end(epoch, logs = NULL)

Called at the end of an epoch during training.

A basic example

Let’s take a look at a concrete example. To get started, let’s import tensorflow and define a simple Sequential Keras model:

get_model <- function() {
  model <- keras_model_sequential() %>%
    layer_dense(1, input_shape = 784) %>%
    compile(
      optimizer = optimizer_rmsprop(learning_rate=0.1),
      loss = "mean_squared_error",
      metrics = "mean_absolute_error"
    )
  model
}

Then, load the MNIST data for training and testing from Keras datasets API:

mnist <- dataset_mnist()

flatten_and_rescale <- function(x) {
  x <- array_reshape(x, c(-1, 784))
  x <- x / 255
  x
}

mnist$train$x <- flatten_and_rescale(mnist$train$x)
mnist$test$x  <- flatten_and_rescale(mnist$test$x)

# limit to 500 samples
mnist$train$x <- mnist$train$x[1:500, ]
mnist$train$y <- mnist$train$y[1:500]
mnist$test$x  <- mnist$test$x[1:500, ]
mnist$test$y  <- mnist$test$y[1:500]

Now, define a simple custom callback that logs:

  • When fit/evaluate/predict starts & ends
  • When each epoch starts & ends
  • When each training batch starts & ends
  • When each evaluation (test) batch starts & ends
  • When each inference (prediction) batch starts & ends
show <- function(msg, logs) {
  cat(glue::glue(msg, .envir = parent.frame()),
      "got logs: ", sep = "; ")
  logs %>% 
    lapply(signif, digits = 3) %>% 
    dput(control = "niceNames")
}

CustomCallback(keras$callbacks$Callback) %py_class% {
  on_train_begin <- function(logs = NULL)
    show("Starting training", logs)

  on_train_end <- function(logs = NULL)
    show("Stop training", logs)

  on_epoch_begin <- function(epoch, logs = NULL)
    show("Start epoch {epoch} of training", logs)

  on_epoch_end <- function(epoch, logs = NULL)
    show("End epoch {epoch} of training", logs)

  on_test_begin <- function(logs = NULL)
    show("Start testing", logs)

  on_test_end <- function(logs = NULL)
    show("Stop testing", logs)

  on_predict_begin <- function(logs = NULL)
    show("Start predicting", logs)

  on_predict_end <- function(logs = NULL)
    show("Stop predicting", logs)

  on_train_batch_begin <- function(batch, logs = NULL)
    show("...Training: start of batch {batch}", logs)

  on_train_batch_end <- function(batch, logs = NULL)
    show("...Training: end of batch {batch}",  logs)

  on_test_batch_begin <- function(batch, logs = NULL)
    show("...Evaluating: start of batch {batch}", logs)

  on_test_batch_end <- function(batch, logs = NULL)
    show("...Evaluating: end of batch {batch}", logs)

  on_predict_batch_begin <- function(batch, logs = NULL)
    show("...Predicting: start of batch {batch}", logs)

  on_predict_batch_end <- function(batch, logs = NULL)
    show("...Predicting: end of batch {batch}", logs)
}

Let’s try it out:

model <- get_model()
model %>% fit(
  mnist$train$x,
  mnist$train$y,
  batch_size = 128,
  epochs = 2,
  verbose = 0,
  validation_split = 0.5,
  callbacks = list(CustomCallback())
)
Starting training; got logs: list()
Start epoch 0 of training; got logs: list()
...Training: start of batch 0; got logs: list()
...Training: end of batch 0; got logs: list(loss = 29.3, mean_absolute_error = 4.58)
...Training: start of batch 1; got logs: list()
...Training: end of batch 1; got logs: list(loss = 462, mean_absolute_error = 16.1)
Start testing; got logs: list()
...Evaluating: start of batch 0; got logs: list()
...Evaluating: end of batch 0; got logs: list(loss = 28.3, mean_absolute_error = 4.53)
...Evaluating: start of batch 1; got logs: list()
...Evaluating: end of batch 1; got logs: list(loss = 27.2, mean_absolute_error = 4.36)
Stop testing; got logs: list(loss = 27.2, mean_absolute_error = 4.36)
End epoch 0 of training; got logs: list(loss = 462, mean_absolute_error = 16.1, val_loss = 27.2, 
    val_mean_absolute_error = 4.36)
Start epoch 1 of training; got logs: list()
...Training: start of batch 0; got logs: list()
...Training: end of batch 0; got logs: list(loss = 24.9, mean_absolute_error = 4.17)
...Training: start of batch 1; got logs: list()
...Training: end of batch 1; got logs: list(loss = 17.4, mean_absolute_error = 3.43)
Start testing; got logs: list()
...Evaluating: start of batch 0; got logs: list()
...Evaluating: end of batch 0; got logs: list(loss = 7.04, mean_absolute_error = 2.18)
...Evaluating: start of batch 1; got logs: list()
...Evaluating: end of batch 1; got logs: list(loss = 7.23, mean_absolute_error = 2.23)
Stop testing; got logs: list(loss = 7.23, mean_absolute_error = 2.23)
End epoch 1 of training; got logs: list(loss = 17.4, mean_absolute_error = 3.43, val_loss = 7.23, 
    val_mean_absolute_error = 2.23)
Stop training; got logs: list(loss = 17.4, mean_absolute_error = 3.43, val_loss = 7.23, 
    val_mean_absolute_error = 2.23)
res <- model %>%
  evaluate(
    mnist$test$x,
    mnist$test$y,
    batch_size = 128,
    verbose = 0,
    callbacks = list(CustomCallback())
  )
Start testing; got logs: list()
...Evaluating: start of batch 0; got logs: list()
...Evaluating: end of batch 0; got logs: list(loss = 7.08, mean_absolute_error = 2.19)
...Evaluating: start of batch 1; got logs: list()
...Evaluating: end of batch 1; got logs: list(loss = 6.53, mean_absolute_error = 2.1)
...Evaluating: start of batch 2; got logs: list()
...Evaluating: end of batch 2; got logs: list(loss = 6.48, mean_absolute_error = 2.1)
...Evaluating: start of batch 3; got logs: list()
...Evaluating: end of batch 3; got logs: list(loss = 6.74, mean_absolute_error = 2.14)
Stop testing; got logs: list(loss = 6.74, mean_absolute_error = 2.14)
res <- model %>%
  predict(mnist$test$x,
          batch_size = 128,
          callbacks = list(CustomCallback()))
Start predicting; got logs: list()
...Predicting: start of batch 0; got logs: list()
...Predicting: end of batch 0; got logs: list(outputs = c(3.73, 2.91, 1.57, 3.98, 2.77, 2.2, 3.85, 3.83, 
3.76, 6.35, 1.77, 2.12, 5.25, 2.15, 3.28, 2.09, 3.89, 3.57, 3.51, 
3.11, 5.52, 3.23, 2.82, 3.28, 2.34, 4.9, 3.95, 3.55, 3.44, 2.36, 
3.96, 2.23, 3.69, 2.18, 4.83, 3.4, 4.18, 2.8, 1.79, 3.35, 1.44, 
3.12, 4.49, 1.41, 3.52, 3.06, 3.6, 3.03, 6.5, 3.17, 3.39, 4.19, 
3.69, 1.63, 2.74, 3.25, 3.29, 1.63, 5.23, 2, 5.38, 4.39, 2.71, 
3.48, 5.54, 3.21, 2.5, 3.17, 5.52, 1.4, 3.59, 2.49, 2.8, 5.57, 
3, 4.96, 2.65, 2.41, 4.04, 7.09, 5.13, 4.08, 3.72, 4.51, 3.37, 
5.2, 5.02, 3.77, 3.88, 2.96, 2.97, 4.07, 3.22, 6.16, 4.7, 6.25, 
2.36, 5.17, 3.09, 4.85, 2.12, 1.82, 4.42, 3.06, 2.82, 4.23, 2.07, 
1.8, 4.46, 3.69, 4.29, 3.09, 3.51, 5.25, 3.39, 3.7, 3.46, 3.51, 
3.51, 4.25, 4.68, 3.41, 3.3, 4.27, 5.21, 3.73, 1.91, 4.52))
...Predicting: start of batch 1; got logs: list()
...Predicting: end of batch 1; got logs: list(outputs = c(6.3, 3.93, 5.46, 5.58, 4.1, 4.88, 4.95, 3.42, 
2.63, 3.22, 2.91, 4.2, 2.62, 5.14, 2.46, 1.99, 4.55, 2.01, 4.91, 
4.76, 2.63, 1.41, 3.81, 3.57, 3.52, 2.31, 2.29, 5.03, 2.46, 2.46, 
2, 3.24, 4.28, 2.84, 4.51, 5.17, 2.89, 3.11, 3.71, 2.9, 2.87, 
3.72, 3.29, 5.59, 2.89, 2.22, 3.63, 1.26, 1.58, 3.97, 2.7, 4.23, 
2.48, 4.28, 3.4, 1.98, 4.14, 4.09, 4.41, 3.32, 3.17, 1.69, 3.78, 
2.64, 3.16, 3.69, 2.95, 2.41, 3.61, 5.06, 4.14, 5.59, 6.71, 2.81, 
1.74, 1.93, 1.64, 2.52, 6.5, 2.4, 2.71, 6.07, 3.25, 3.41, 6, 
2.82, 6.09, 4.12, 3.75, 3.77, 3.47, 2.94, 4.78, 3.35, 5.59, 4.97, 
1.03, 3.19, 5.46, 2.55, 2.1, 2.51, 3.67, 2.39, 4.19, 5.91, 2.84, 
3.72, 3.88, 3.27, 4.1, 2.55, 3.4, 2.97, 5.79, 4.28, 2.77, 2.56, 
1.31, 4.13, 4.52, 2.59, 6.05, 1.87, 7.07, 2.97, 3.55, 3.35))
...Predicting: start of batch 2; got logs: list()
...Predicting: end of batch 2; got logs: list(outputs = c(3.32, 4.43, 4.23, 2.23, 3.96, 3.17, 6.49, 4.19, 
4.02, 3.5, 4.57, 3.91, 3.1, 1.33, 3.32, 2.86, 2.45, 4.88, 4.56, 
4.63, 2.74, 5.64, 3.96, 2.14, 3.82, 5.21, 3.49, 3.05, 3.19, 2.26, 
4.19, 3.75, 2.84, 2.29, 4.25, 2.79, 6.24, 4.85, 2.57, 4.04, 3.23, 
1.4, 3.09, 4.28, 2.84, 5.26, 2.62, 1.88, 4.48, 1.69, 2.63, 3.37, 
4.86, 2.3, 2.38, 4.68, 3.68, 3.16, 3.25, 4.62, 3.62, 3.92, 3.06, 
2.76, 3.28, 2.9, 6.26, 3.08, 1.79, 3.57, 2.96, 3.56, 4.28, 3.41, 
2.85, 2.35, 2.78, 3.58, 2.47, 2.45, 4.53, 3.81, 3.35, 2.87, 4.06, 
1.74, 2.78, 2.96, 3.06, 3.9, 4.25, 5.86, 2.56, 3.59, 3.81, 3.84, 
2.16, 5.83, 1.67, 4.34, 4.41, 1.57, 4.11, 4.92, 3.9, 3.77, 3.56, 
2.11, 3.02, 3.08, 3.66, 2.65, 3.32, 2.69, 5.2, 2.48, 1.02, 4.39, 
3.8, 3.97, 4.54, 2.01, 2.4, 3.49, 0.672, 2.63, 4.1, 2.74))
...Predicting: start of batch 3; got logs: list()
...Predicting: end of batch 3; got logs: list(outputs = c(3.98, 1.8, 3.16, 2.09, 1.68, 5.87, 3.35, 4.76, 
4.12, 2.59, 3.35, 3.28, 3.7, 2.26, 2.86, 3.58, 3.97, 3.06, 2.84, 
3.92, 2.83, 4.27, 4.36, 2.73, 2.96, 3.29, 6.36, 2.56, 6.46, 5.96, 
3.87, 4.73, 2.9, 4.97, 3.12, 2.08, 2.39, 2.92, 4.73, 3.37, 1.61, 
4.6, 3.39, 2.29, 3.82, 3.82, 2.12, 3.38, 3.31, 3.72, 3.8, 5.14, 
4.24, 6.69, 3.59, 3.56, 4.39, 2.86, 4.56, 2.15, 3, 1.73, 4.48, 
5.53, 5.82, 4.98, 6.2, 5.78, 2.63, 1.99, 4.37, 2.94, 2.06, 3.41, 
4.61, 1.8, 4.61, 4.69, 5, 3.79, 4.89, 5.85, 4.24, 3.96, 5.09, 
3.62, 3.51, 3.61, 3.04, 1.54, 5.57, 3.78, 2.81, 2.8, 2.63, 2.34, 
1.32, 4.24, 3.71, 1.58, 3.5, 2.3, 4.65, 4.94, 3.75, 1.57, 1.92, 
3.89, 3.01, 4.08, 4.72, 6.54, 3.71, 3.6, 4.11, 2.43))
Stop predicting; got logs: list()

Usage of logs

The logs named list contains the loss value, and all the metrics at the end of a batch or epoch. Example includes the loss and mean absolute error.

LossAndErrorPrintingCallback(keras$callbacks$Callback) %py_class% {
  on_train_batch_end <- function(batch, logs = NULL)
    cat(sprintf("Up to batch %i, the average loss is %7.2f.\n",
                batch,  logs$loss))

  on_test_batch_end <- function(batch, logs = NULL)
    cat(sprintf("Up to batch %i, the average loss is %7.2f.\n",
                batch, logs$loss))

  on_epoch_end <- function(epoch, logs = NULL)
    cat(sprintf(
      "The average loss for epoch %2i is %9.2f and mean absolute error is %7.2f.\n",
      epoch, logs$loss, logs$mean_absolute_error
    ))
}

model <- get_model()
model %>% fit(
  mnist$train$x,
  mnist$train$y,
  batch_size = 128,
  epochs = 2,
  verbose = 0,
  callbacks = list(LossAndErrorPrintingCallback())
)
Up to batch 0, the average loss is   23.28.
Up to batch 1, the average loss is  440.17.
Up to batch 2, the average loss is  299.87.
Up to batch 3, the average loss is  232.65.
The average loss for epoch  0 is    232.65 and mean absolute error is    9.52.
Up to batch 0, the average loss is    7.37.
Up to batch 1, the average loss is    6.63.
Up to batch 2, the average loss is    6.33.
Up to batch 3, the average loss is    6.07.
The average loss for epoch  1 is      6.07 and mean absolute error is    2.03.
res = model %>% evaluate(
  mnist$test$x,
  mnist$test$y,
  batch_size = 128,
  verbose = 0,
  callbacks = list(LossAndErrorPrintingCallback())
)
Up to batch 0, the average loss is    5.40.
Up to batch 1, the average loss is    4.96.
Up to batch 2, the average loss is    4.97.
Up to batch 3, the average loss is    4.99.

Usage of self$model attribute

In addition to receiving log information when one of their methods is called, callbacks have access to the model associated with the current round of training/evaluation/inference: self$model.

Here are of few of the things you can do with self$model in a callback:

  • Set self$model$stop_training <- TRUE to immediately interrupt training.
  • Mutate hyperparameters of the optimizer (available as self$model$optimizer), such as self$model$optimizer$learning_rate.
  • Save the model at period intervals.
  • Record the output of predict(model) on a few test samples at the end of each epoch, to use as a sanity check during training.
  • Extract visualizations of intermediate features at the end of each epoch, to monitor what the model is learning over time.
  • etc.

Let’s see this in action in a couple of examples.

Examples of Keras callback applications

Early stopping at minimum loss

This first example shows the creation of a Callback that stops training when the minimum of loss has been reached, by setting the attribute self$model$stop_training (boolean). Optionally, you can provide an argument patience to specify how many epochs we should wait before stopping after having reached a local minimum.

keras$callbacks$EarlyStopping provides a more complete and general implementation.

EarlyStoppingAtMinLoss(keras$callbacks$Callback) %py_class% {
  "Stop training when the loss is at its min, i.e. the loss stops decreasing.

  Arguments:
      patience: Number of epochs to wait after min has been hit. After this
        number of no improvement, training stops.
  "

  initialize <- function(patience = 0) {
    # call keras$callbacks$Callback$__init__(), so it can setup `self`
    super$initialize()
    self$patience <- patience
    # best_weights to store the weights at which the minimum loss occurs.
    self$best_weights <- NULL
  }

  on_train_begin <- function(logs = NULL) {
    # The number of epoch it has waited when loss is no longer minimum.
    self$wait <- 0
    # The epoch the training stops at.
    self$stopped_epoch <- 0
    # Initialize the best as infinity.
    self$best <- Inf
  }

  on_epoch_end <- function(epoch, logs = NULL) {
    current <- logs$loss
    if (current < self$best) {
      self$best <- current
      self$wait <- 0
      # Record the best weights if current results is better (less).
      self$best_weights <- self$model$get_weights()
    } else {
      self$wait %<>% `+`(1)
      if (self$wait >= self$patience) {
        self$stopped_epoch <- epoch
        self$model$stop_training <- TRUE
        cat("Restoring model weights from the end of the best epoch.\n")
        self$model$set_weights(self$best_weights)
      }
    }
  }

  on_train_end <- function(logs = NULL)
    if (self$stopped_epoch > 0)
      cat(sprintf("Epoch %05d: early stopping\n", self$stopped_epoch + 1))

}


model <- get_model()
model %>% fit(
  mnist$train$x,
  mnist$train$y,
  batch_size = 64,
  steps_per_epoch = 5,
  epochs = 30,
  verbose = 0,
  callbacks = list(LossAndErrorPrintingCallback(),
                   EarlyStoppingAtMinLoss())
)
Up to batch 0, the average loss is   20.51.
Up to batch 1, the average loss is  457.14.
Up to batch 2, the average loss is  313.42.
Up to batch 3, the average loss is  237.60.
Up to batch 4, the average loss is  191.72.
The average loss for epoch  0 is    191.72 and mean absolute error is    8.08.
Up to batch 0, the average loss is    6.77.
Up to batch 1, the average loss is    5.98.
Up to batch 2, the average loss is    5.80.
Up to batch 3, the average loss is    5.73.
Up to batch 4, the average loss is    5.50.
The average loss for epoch  1 is      5.50 and mean absolute error is    1.98.
Up to batch 0, the average loss is    4.17.
Up to batch 1, the average loss is    4.51.
Up to batch 2, the average loss is    4.30.
Up to batch 3, the average loss is    4.32.
Up to batch 4, the average loss is    4.21.
The average loss for epoch  2 is      4.21 and mean absolute error is    1.67.
Up to batch 0, the average loss is    4.06.
Up to batch 1, the average loss is    3.78.
Up to batch 2, the average loss is    4.21.
Up to batch 3, the average loss is    4.23.
Up to batch 4, the average loss is    4.55.
The average loss for epoch  3 is      4.55 and mean absolute error is    1.74.
Restoring model weights from the end of the best epoch.
Epoch 00004: early stopping

Learning rate scheduling

In this example, we show how a custom Callback can be used to dynamically change the learning rate of the optimizer during the course of training.

See keras$callbacks$LearningRateScheduler for a more general implementations (in RStudio, press F1 while the cursor is over LearningRateScheduler and a browser will open to this page).

CustomLearningRateScheduler(keras$callbacks$Callback) %py_class% {
  "Learning rate scheduler which sets the learning rate according to schedule.

  Arguments:
      schedule: a function that takes an epoch index
          (integer, indexed from 0) and current learning rate
          as inputs and returns a new learning rate as output (float).
  "

  `__init__` <- function(schedule) {
    super()$`__init__`()
    self$schedule <- schedule
  }

  on_epoch_begin <- function(epoch, logs = NULL) {
    ## When in doubt about what types of objects are in scope (e.g., self$model)
    ## use a debugger to interact with the actual objects at the console!
    # browser()

    if (!"learning_rate" %in% names(self$model$optimizer))
      stop('Optimizer must have a "learning_rate" attribute.')

    # # Get the current learning rate from model's optimizer.
    # use as.numeric() to convert the tf.Variable to an R numeric
    lr <- as.numeric(self$model$optimizer$learning_rate)
    # # Call schedule function to get the scheduled learning rate.
    scheduled_lr <- self$schedule(epoch, lr)
    # # Set the value back to the optimizer before this epoch starts
    self$model$optimizer$learning_rate <- scheduled_lr
    cat(sprintf("\nEpoch %05d: Learning rate is %6.4f.\n", epoch, scheduled_lr))
  }
}


LR_SCHEDULE <- tibble::tribble(~ start_epoch, ~ learning_rate,
                                           0,           0.1  ,
                                           3,           0.05 ,
                                           6,           0.01 ,
                                           9,           0.005,
                                          12,           0.001)


lr_schedule <- function(epoch, learning_rate) {
  "Helper function to retrieve the scheduled learning rate based on epoch."
  if (epoch <= last(LR_SCHEDULE$start_epoch))
    with(LR_SCHEDULE, learning_rate[which.min(epoch > start_epoch)])
  else
    learning_rate
}


model <- get_model()
model %>% fit(
  mnist$train$x,
  mnist$train$y,
  batch_size = 64,
  steps_per_epoch = 5,
  epochs = 15,
  verbose = 0,
  callbacks = list(
    LossAndErrorPrintingCallback(),
    CustomLearningRateScheduler(lr_schedule)
  )
)

Epoch 00000: Learning rate is 0.1000.
Up to batch 0, the average loss is   26.16.
Up to batch 1, the average loss is  517.86.
Up to batch 2, the average loss is  355.71.
Up to batch 3, the average loss is  268.95.
Up to batch 4, the average loss is  216.62.
The average loss for epoch  0 is    216.62 and mean absolute error is    8.60.

Epoch 00001: Learning rate is 0.0500.
Up to batch 0, the average loss is    6.34.
Up to batch 1, the average loss is    5.76.
Up to batch 2, the average loss is    6.21.
Up to batch 3, the average loss is    6.12.
Up to batch 4, the average loss is    5.98.
The average loss for epoch  1 is      5.98 and mean absolute error is    2.03.

Epoch 00002: Learning rate is 0.0500.
Up to batch 0, the average loss is    5.61.
Up to batch 1, the average loss is    5.14.
Up to batch 2, the average loss is    5.06.
Up to batch 3, the average loss is    4.66.
Up to batch 4, the average loss is    4.47.
The average loss for epoch  2 is      4.47 and mean absolute error is    1.71.

Epoch 00003: Learning rate is 0.0500.
Up to batch 0, the average loss is    5.25.
Up to batch 1, the average loss is    4.60.
Up to batch 2, the average loss is    4.37.
Up to batch 3, the average loss is    4.35.
Up to batch 4, the average loss is    4.25.
The average loss for epoch  3 is      4.25 and mean absolute error is    1.70.

Epoch 00004: Learning rate is 0.0100.
Up to batch 0, the average loss is    4.58.
Up to batch 1, the average loss is    3.97.
Up to batch 2, the average loss is    4.12.
Up to batch 3, the average loss is    3.85.
Up to batch 4, the average loss is    3.58.
The average loss for epoch  4 is      3.58 and mean absolute error is    1.54.

Epoch 00005: Learning rate is 0.0100.
Up to batch 0, the average loss is    3.71.
Up to batch 1, the average loss is    3.69.
Up to batch 2, the average loss is    3.72.
Up to batch 3, the average loss is    3.84.
Up to batch 4, the average loss is    3.79.
The average loss for epoch  5 is      3.79 and mean absolute error is    1.58.

Epoch 00006: Learning rate is 0.0100.
Up to batch 0, the average loss is    4.96.
Up to batch 1, the average loss is    3.81.
Up to batch 2, the average loss is    3.82.
Up to batch 3, the average loss is    3.72.
Up to batch 4, the average loss is    3.74.
The average loss for epoch  6 is      3.74 and mean absolute error is    1.53.

Epoch 00007: Learning rate is 0.0050.
Up to batch 0, the average loss is    3.24.
Up to batch 1, the average loss is    3.59.
Up to batch 2, the average loss is    3.25.
Up to batch 3, the average loss is    3.20.
Up to batch 4, the average loss is    3.34.
The average loss for epoch  7 is      3.34 and mean absolute error is    1.51.

Epoch 00008: Learning rate is 0.0050.
Up to batch 0, the average loss is    4.55.
Up to batch 1, the average loss is    3.63.
Up to batch 2, the average loss is    3.25.
Up to batch 3, the average loss is    3.15.
Up to batch 4, the average loss is    3.24.
The average loss for epoch  8 is      3.24 and mean absolute error is    1.42.

Epoch 00009: Learning rate is 0.0050.
Up to batch 0, the average loss is    3.04.
Up to batch 1, the average loss is    3.14.
Up to batch 2, the average loss is    3.58.
Up to batch 3, the average loss is    3.50.
Up to batch 4, the average loss is    3.64.
The average loss for epoch  9 is      3.64 and mean absolute error is    1.57.

Epoch 00010: Learning rate is 0.0010.
Up to batch 0, the average loss is    3.22.
Up to batch 1, the average loss is    3.00.
Up to batch 2, the average loss is    2.91.
Up to batch 3, the average loss is    3.10.
Up to batch 4, the average loss is    3.11.
The average loss for epoch 10 is      3.11 and mean absolute error is    1.41.

Epoch 00011: Learning rate is 0.0010.
Up to batch 0, the average loss is    3.17.
Up to batch 1, the average loss is    3.73.
Up to batch 2, the average loss is    3.25.
Up to batch 3, the average loss is    3.37.
Up to batch 4, the average loss is    3.54.
The average loss for epoch 11 is      3.54 and mean absolute error is    1.51.

Epoch 00012: Learning rate is 0.0010.
Up to batch 0, the average loss is    1.88.
Up to batch 1, the average loss is    2.45.
Up to batch 2, the average loss is    2.88.
Up to batch 3, the average loss is    2.80.
Up to batch 4, the average loss is    2.82.
The average loss for epoch 12 is      2.82 and mean absolute error is    1.35.

Epoch 00013: Learning rate is 0.0010.
Up to batch 0, the average loss is    3.00.
Up to batch 1, the average loss is    2.92.
Up to batch 2, the average loss is    2.96.
Up to batch 3, the average loss is    3.11.
Up to batch 4, the average loss is    3.06.
The average loss for epoch 13 is      3.06 and mean absolute error is    1.40.

Epoch 00014: Learning rate is 0.0010.
Up to batch 0, the average loss is    3.70.
Up to batch 1, the average loss is    3.69.
Up to batch 2, the average loss is    3.58.
Up to batch 3, the average loss is    3.52.
Up to batch 4, the average loss is    3.29.
The average loss for epoch 14 is      3.29 and mean absolute error is    1.45.

Built-in Keras callbacks

Be sure to check out the existing Keras callbacks by reading the API docs. Applications include logging to CSV, saving the model, visualizing metrics in TensorBoard, and a lot more!

Environment Details

tensorflow::tf_config()
TensorFlow v2.9.1 (~/.virtualenvs/r-tensorflow-site/lib/python3.9/site-packages/tensorflow)
Python v3.9 (~/.virtualenvs/r-tensorflow-site/bin/python)
sessionInfo()
R version 4.2.1 (2022-06-23)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.4 LTS

Matrix products: default
BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/libmkl_rt.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] keras_2.9.0.9000      tensorflow_2.9.0.9000

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.9           compiler_4.2.1       pillar_1.7.0        
 [4] envir_0.2.1          base64enc_0.1-3      tools_4.2.1         
 [7] zeallot_0.1.0        digest_0.6.29        tibble_3.1.7        
[10] jsonlite_1.8.0       evaluate_0.15        lifecycle_1.0.1     
[13] lattice_0.20-45      pkgconfig_2.0.3      png_0.1-7           
[16] rlang_1.0.4          Matrix_1.4-1         DBI_1.1.3           
[19] cli_3.3.0            yaml_2.3.5           xfun_0.31           
[22] fastmap_1.1.0        stringr_1.4.0        dplyr_1.0.9         
[25] knitr_1.39           generics_0.1.3       htmlwidgets_1.5.4   
[28] vctrs_0.4.1          rprojroot_2.0.3      tidyselect_1.1.2    
[31] grid_4.2.1           here_1.0.1           reticulate_1.25-9000
[34] glue_1.6.2           R6_2.5.1             fansi_1.0.3         
[37] rmarkdown_2.14       purrr_0.3.4          magrittr_2.0.3      
[40] whisker_0.4          tfruns_1.5.0         htmltools_0.5.2     
[43] ellipsis_0.3.2       assertthat_0.2.1     utf8_1.2.2          
[46] stringi_1.7.8        crayon_1.5.1        
system2(reticulate::py_exe(), c("-m pip freeze"), stdout = TRUE) |> writeLines()
absl-py==1.1.0
asttokens==2.0.5
astunparse==1.6.3
backcall==0.2.0
beautifulsoup4==4.11.1
cachetools==5.2.0
certifi==2022.6.15
charset-normalizer==2.1.0
decorator==5.1.1
dill==0.3.5.1
etils==0.6.0
executing==0.8.3
filelock==3.7.1
flatbuffers==1.12
gast==0.4.0
gdown==4.5.1
google-auth==2.9.0
google-auth-oauthlib==0.4.6
google-pasta==0.2.0
googleapis-common-protos==1.56.4
grpcio==1.47.0
h5py==3.7.0
idna==3.3
importlib-metadata==4.12.0
importlib-resources==5.8.0
ipython==8.4.0
jedi==0.18.1
keras==2.9.0
Keras-Preprocessing==1.1.2
keras-tuner==1.1.2
kt-legacy==1.0.4
libclang==14.0.1
Markdown==3.3.7
matplotlib-inline==0.1.3
numpy==1.23.1
oauthlib==3.2.0
opt-einsum==3.3.0
packaging==21.3
pandas==1.4.3
parso==0.8.3
pexpect==4.8.0
pickleshare==0.7.5
Pillow==9.2.0
promise==2.3
prompt-toolkit==3.0.30
protobuf==3.19.4
ptyprocess==0.7.0
pure-eval==0.2.2
pyasn1==0.4.8
pyasn1-modules==0.2.8
pydot==1.4.2
Pygments==2.12.0
pyparsing==3.0.9
PySocks==1.7.1
python-dateutil==2.8.2
pytz==2022.1
PyYAML==6.0
requests==2.28.1
requests-oauthlib==1.3.1
rsa==4.8
scipy==1.8.1
six==1.16.0
soupsieve==2.3.2.post1
stack-data==0.3.0
tensorboard==2.9.1
tensorboard-data-server==0.6.1
tensorboard-plugin-wit==1.8.1
tensorflow==2.9.1
tensorflow-datasets==4.6.0
tensorflow-estimator==2.9.0
tensorflow-hub==0.12.0
tensorflow-io-gcs-filesystem==0.26.0
tensorflow-metadata==1.9.0
termcolor==1.1.0
toml==0.10.2
tqdm==4.64.0
traitlets==5.3.0
typing_extensions==4.3.0
urllib3==1.26.10
wcwidth==0.2.5
Werkzeug==2.1.2
wrapt==1.14.1
zipp==3.8.1
TF Devices:
-  PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU') 
-  PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU') 
CPU cores: 12 
Date rendered: 2022-07-14 
Page render time: 8 seconds