Registers a custom object with the Keras serialization framework.
Source:R/model-persistence.R
register_keras_serializable.RdThis function registers a custom class or function with the Keras custom
object registry, so that it can be serialized and deserialized without
needing an entry in the user-provided custom_objects argument. It also injects a
function that Keras will call to get the object's serializable string key.
Note that to be serialized and deserialized, classes must implement the
get_config() method. Functions do not have this requirement.
The object will be registered under the key 'package>name' where name,
defaults to the object name if not passed.
Arguments
- object
A keras object.
- name
The name to serialize this class under in this package.
- package
The package that this class belongs to. This is used for the
key(which is"package>name") to identify the class. Defaults to the current package name, or"Custom"outside of a package.
Value
object is returned invisibly, for convenient piping. This is
primarily called for side effects.
Examples
# Note that `'my_package'` is used as the `package` argument here, and since
# the `name` argument is not provided, `'MyDense'` is used as the `name`.
layer_my_dense <- Layer("MyDense")
register_keras_serializable(layer_my_dense, package = "my_package")
MyDense <- environment(layer_my_dense)$`__class__` # the python class obj
stopifnot(exprs = {
get_registered_object('my_package>MyDense') == MyDense
get_registered_name(MyDense) == 'my_package>MyDense'
})See also
Other saving and loading functions: export_savedmodel.keras.src.models.model.Model() layer_tfsm() load_model() load_model_weights() save_model() save_model_config() save_model_weights() with_custom_object_scope()
Other serialization utilities: deserialize_keras_object() get_custom_objects() get_registered_name() get_registered_object() serialize_keras_object() with_custom_object_scope()