text
stringlengths
0
1.46k
Exposes custom classes/functions to Keras deserialization internals.
Under a scope with custom_object_scope(objects_dict), Keras methods such as tf.keras.models.load_model or tf.keras.models.model_from_config will be able to deserialize any custom object referenced by a saved config (e.g. a custom layer or metric).
Example
Consider a custom regularizer my_regularizer:
layer = Dense(3, kernel_regularizer=my_regularizer)
config = layer.get_config() # Config contains a reference to `my_regularizer`
...
# Later:
with custom_object_scope({'my_regularizer': my_regularizer}):
layer = Dense.from_config(config)
Arguments
*args: Dictionary or dictionaries of {name: object} pairs.
get_custom_objects function
tf.keras.utils.get_custom_objects()
Retrieves a live reference to the global dictionary of custom objects.
Updating and clearing custom objects using custom_object_scope is preferred, but get_custom_objects can be used to directly access the current collection of custom objects.
Example
get_custom_objects().clear()
get_custom_objects()['MyObject'] = MyObject
Returns
Global dictionary of names to classes (_GLOBAL_CUSTOM_OBJECTS).
register_keras_serializable function
tf.keras.utils.register_keras_serializable(package="Custom", name=None)
Registers an object with the Keras serialization framework.
This decorator injects the decorated class or function into the Keras custom object dictionary, so that it can be serialized and deserialized without needing an entry in the user-provided custom object dict. 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
package: The package that this class belongs to.
name: The name to serialize this class under in this package. If None, the class' name will be used.
Returns
A decorator that registers the decorated class with the passed names.
serialize_keras_object function
tf.keras.utils.serialize_keras_object(instance)
Serialize a Keras object into a JSON-compatible representation.
Calls to serialize_keras_object while underneath the SharedObjectSavingScope context manager will cause any objects re-used across multiple layers to be saved with a special shared object ID. This allows the network to be re-created properly during deserialization.
Arguments
instance: The object to serialize.
Returns
A dict-like, JSON-compatible representation of the object's config.
deserialize_keras_object function
tf.keras.utils.deserialize_keras_object(
identifier, module_objects=None, custom_objects=None, printable_module_name="object"
)
Turns the serialized form of a Keras object back into an actual object.
This function is for mid-level library implementers rather than end users.
Importantly, this utility requires you to provide the dict of module_objects to use for looking up the object config; this is not populated by default. If you need a deserialization utility that has preexisting knowledge of built-in Keras objects, use e.g. keras.layers.deserialize(config), keras.metrics.deserialize(config), etc.
Calling deserialize_keras_object while underneath the SharedObjectLoadingScope context manager will cause any already-seen shared objects to be returned as-is rather than creating a new object.
Arguments
identifier: the serialized form of the object.
module_objects: A dictionary of built-in objects to look the name up in. Generally, module_objects is provided by midlevel library implementers.
custom_objects: A dictionary of custom objects to look the name up in. Generally, custom_objects is provided by the end user.
printable_module_name: A human-readable string representing the type of the object. Printed in case of exception.
Returns
The deserialized object.
Example
A mid-level library implementer might want to implement a utility for retrieving an object from its config, as such:
def deserialize(config, custom_objects=None):
return deserialize_keras_object(
identifier,
module_objects=globals(),
custom_objects=custom_objects,
name="MyObjectType",
)
This is how e.g. keras.layers.deserialize() is implemented.Python & NumPy utilities
to_categorical function
tf.keras.utils.to_categorical(y, num_classes=None, dtype="float32")
Converts a class vector (integers) to binary class matrix.