text
stringlengths 0
4.99k
|
---|
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. |
E.g. for use with categorical_crossentropy. |
Arguments |
y: class vector to be converted into a matrix (integers from 0 to num_classes). |
num_classes: total number of classes. If None, this would be inferred as the (largest number in y) + 1. |
dtype: The data type expected by the input. Default: 'float32'. |
Returns |
A binary matrix representation of the input. The classes axis is placed last. |
Example |
>>> a = tf.keras.utils.to_categorical([0, 1, 2, 3], num_classes=4) |
>>> a = tf.constant(a, shape=[4, 4]) |
>>> print(a) |
tf.Tensor( |
[[1. 0. 0. 0.] |
[0. 1. 0. 0.] |
[0. 0. 1. 0.] |
[0. 0. 0. 1.]], shape=(4, 4), dtype=float32) |
>>> b = tf.constant([.9, .04, .03, .03, |
... .3, .45, .15, .13, |
... .04, .01, .94, .05, |
... .12, .21, .5, .17], |
... shape=[4, 4]) |
>>> loss = tf.keras.backend.categorical_crossentropy(a, b) |
>>> print(np.around(loss, 5)) |
[0.10536 0.82807 0.1011 1.77196] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.