What is the purpose of Keras custom objects when creating custom layers?

Viewed 1114

Looking at layers implementations of third parties, like tensorflow_addons, I see that each layer is being registered as a custom object. For example, you can see the use of the wrapper register_custom_keras_object call here.

This wrapper uses the function tf.keras.utils.get_custom_objects() to do the registering.

My question, is why should this be done for any custom layers? What is the benefit of registering the layer as a custom object?

1 Answers

Doing this allows you to then to refer to your custom object via string. You see this with keras default objects all the time. For example:

# You can either compile a model with the Adam optimizer like this
model.compile(optimizer='adam', ...)

# or like this
adam = keras.optimizers.Adam()
model.compile(optimizer=adam, ...)

Taken from the definition of custom_object_scope:

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(..., kernel_regularizer='MyObject')
    # save, load, etc. will recognize custom object by name

Defined as

def custom_object_scope(*args)

Arguments:

*args: Variable length list of dictionaries of name, class pairs to add to custom objects.

Related