CustomObjectScope
keras.utils.generic_utils.CustomObjectScope()
Provides a scope that changes to _GLOBAL_CUSTOM_OBJECTS
cannot escape.
Code within a with
statement will be able to access custom objects
by name. Changes to global custom objects persist within the enclosing with
statement. At end of the with
statement, global custom objects are reverted to state at beginning of the with
statement.
Example
Consider a custom object MyObject
with CustomObjectScope({"MyObject":MyObject}):
layer = Dense(..., W_regularizer="MyObject")
# save, load, etc. will recognize custom object by name
custom_object_scope
custom_object_scope()
Provides a scope that changes to _GLOBAL_CUSTOM_OBJECTS
cannot escape.
Convenience wrapper for CustomObjectScope
. Code within a with
statement will be able to access custom objects
by name. Changes to global custom objects persist within the enclosing with
statement. At end of the with
statement, global custom objects are reverted to state at beginning of the with
statement.
Example
Consider a custom object MyObject
with custom_object_scope({"MyObject":MyObject}):
layer = Dense(..., W_regularizer="MyObject")
# save, load, etc. will recognize custom object by name
Arguments
- *args: Variable length list of dictionaries of name, class pairs to add to custom objects.
Returns
Object of type CustomObjectScope
.
get_custom_objects
get_custom_objects()
Retrieves a live reference to the global dictionary of custom objects (_GLOBAL_CUSTOM_OBJECTS
).
Updating and clearing custom objects using custom_object_scope
is preferred, but get_custom_objects
can
be used to directly access _GLOBAL_CUSTOM_OBJECTS
.
Example
get_custom_objects().clear()
get_custom_objects()["MyObject"] = MyObject
Returns
Global dictionary of names to classes (_GLOBAL_CUSTOM_OBJECTS
).
get_from_module
get_from_module(identifier, module_params, module_name, instantiate=False, kwargs=None)
Retrieves a class or function member of a module.
First checks _GLOBAL_CUSTOM_OBJECTS
for module_name
, then checks module_params
.
Arguments
- identifier: the object to retrieve. It could be specified
by name (as a string), or by dict. In any other case,
identifier
itself will be returned without any changes. - module_params: the members of a module
(e.g. the output of
globals()
). - module_name: string; the name of the target module. Only used to format error messages.
- instantiate: whether to instantiate the returned object (if it's a class).
- kwargs: a dictionary of keyword arguments to pass to the
class constructor if
instantiate
isTrue
.
Returns
The target object.
Raises
- ValueError: if the identifier cannot be found.
func_dump
func_dump(func)
Serializes a user defined function.
Arguments
- func: the function to serialize.
Returns
A tuple (code, defaults, closure)
.
func_load
func_load(code, defaults=None, closure=None, globs=None)
Deserializes a user defined function.
Arguments
- code: bytecode of the function.
- defaults: defaults of the function.
- closure: closure of the function.
- globs: dictionary of global objects.
Returns
A function object.