Skip to contents

Save and re-load models configurations as JSON. Note that the representation does not include the weights, only the architecture.

Usage

save_model_config(model, filepath = NULL, overwrite = FALSE)

load_model_config(filepath, custom_objects = NULL)

Arguments

model

Model object to save

filepath

path to json file with the model config.

overwrite

Whether we should overwrite any existing model configuration json at filepath, or instead ask the user via an interactive prompt.

custom_objects

Optional named list mapping names to custom classes or functions to be considered during deserialization.

Value

This is called primarily for side effects. model is returned, invisibly, to enable usage with the pipe.

Details

Note: save_model_config() serializes the model to JSON using serialize_keras_object(), not get_config(). serialize_keras_object() returns a superset of get_config(), with additional information needed to create the class object needed to restore the model. See example for how to extract the get_config() value from a saved model.

Example

model <- keras_model_sequential(input_shape = 10) |> layer_dense(10)
file <- tempfile("model-config-", fileext = ".json")
save_model_config(model, file)

# load a new model instance with the same architecture but different weights
model2 <- load_model_config(file)

stopifnot(exprs = {
  all.equal(get_config(model), get_config(model2))

  # To extract the `get_config()` value from a saved model config:
  all.equal(
      get_config(model),
      structure(jsonlite::read_json(file)$config,
                "__class__" = keras_model_sequential()$`__class__`)
  )
})