Skip to content

Commit

Permalink
add functions for serializing keras models to R 'raw' objects
Browse files Browse the repository at this point in the history
  • Loading branch information
jjallaire committed Jul 31, 2017
1 parent 7013ef9 commit dec2cb5
Show file tree
Hide file tree
Showing 19 changed files with 363 additions and 18 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ export(reset_states)
export(save_model_hdf5)
export(save_model_weights_hdf5)
export(sequences_to_matrix)
export(serialize_model)
export(set_weights)
export(skipgrams)
export(tensorboard)
Expand All @@ -233,6 +234,7 @@ export(tf_config)
export(time_distributed)
export(to_categorical)
export(train_on_batch)
export(unserialize_model)
export(use_condaenv)
export(use_python)
export(use_virtualenv)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

# keras 2.0.6 (unreleased)

- Added `serialize_model()` and `unserialize_model()` functions for saving
Keras models as 'raw' R objects.


# keras 2.0.5

Expand Down
55 changes: 53 additions & 2 deletions R/model-persistence.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
#' This allows you to save the entirety of the state of a model
#' in a single file.
#'
#' Saved models can be reinstantiated via `load_model()`. The model returned by
#' `load_model()` is a compiled model ready to be used (unless the saved model
#' Saved models can be reinstantiated via `load_model_hdf5()`. The model returned by
#' `load_model_hdf5()` is a compiled model ready to be used (unless the saved model
#' was never compiled in the first place or `compile = FALSE` is specified.
#'
#' @note The [serialize_model()] function enables saving Keras models to
#' R objects that can be persisted across R sessions.
#'
#' @family model persistence
#'
Expand Down Expand Up @@ -170,5 +173,53 @@ model_from_yaml <- function(yaml, custom_objects = NULL) {
keras$models$model_from_yaml(yaml, custom_objects)
}

#' Serialize a model to an R object
#'
#' Model objects are external references to Keras objects which cannot be saved
#' and restored across R sessions. The `serialize_model()` and
#' `unserialize_model()` functions provide facilities to convert Keras models to
#' R objects for persistence within R data files.
#'
#' @note The [save_model_hdf5()] function enables saving Keras models to
#' external hdf5 files.
#'
#' @inheritParams save_model_hdf5
#' @param model Keras model or R "raw" object containing serialized Keras model.
#'
#' @return `serialize_model()` returns an R "raw" object containing an hdf5
#' version of the Keras model. `unserialize_model()` returns a Keras model.
#'
#' @family model persistence
#'
#' @export
serialize_model <- function(model, include_optimizer = TRUE) {

if (!inherits(model, "keras.engine.training.Model"))
stop("You must pass a Keras model object to serialize_model")

# write hdf5 file to temp file
tmp <- tempfile(pattern = "keras_model", fileext = ".h5")
on.exit(unlink(tmp), add = TRUE)
save_model_hdf5(model, tmp, include_optimizer = include_optimizer)

# read it back intoa raw vector
readBin(tmp, what = "raw", n = file.size(tmp))
}

#' @rdname serialize_model
#' @export
unserialize_model <- function(model, custom_objects = NULL, compile = TRUE) {

# write raw hdf5 bytes to temp file
tmp <- tempfile(pattern = "keras_model", fileext = ".h5")
on.exit(unlink(tmp), add = TRUE)
writeBin(model, tmp)

# read in from hdf5
load_model_hdf5(tmp, custom_objects = custom_objects, compile = compile)
}





2 changes: 1 addition & 1 deletion docs/articles/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion docs/reference/get_weights.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion docs/reference/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion docs/reference/model_to_json.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion docs/reference/model_to_yaml.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions docs/reference/save_model_hdf5.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion docs/reference/save_model_weights_hdf5.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit dec2cb5

Please sign in to comment.