Customizing activation function and loading saved H5 file issue

Picturing

I’m creating a customize activation function and even though it is working during training and I can save it as an h5 file, once I’m trying to load the h5 file the image above keeps telling me to include keras.utils.custom_object_scope() and even though I’m including the object scope the error still occurs every time I’m trying to open the h5 file that is why I can’t access it. Last 2022 I’m trying to create several customize activation functions and I’m just using tf.keras.utils.get_custom_objects().update() and the saved h5 file opens and I can use it to make sample predictions but now I can’t.

Can anyone give me a sample code of how to customize an activation function properly and how can I solve my current issue in opening the h5 file with the customize activation function?

I would appreciate any comments, suggestions, etc… Thank you!

Hi @luther88, If you are loading a model with custom activation, at the time of loading, you have to register the custom objects with a custom_object_scope. For example, to save and load a model using custom activation

#define the activation function

def custom_activation(x):
    return tf.nn.tanh(x) ** 2

#define the model using custom activation

inputs = keras.Input((32,))
x = keras.layers.Dense(16)(inputs)
outputs = keras.layers.Activation(custom_activation)(x)
model = keras.Model(inputs, outputs)

While loading the saved model

# Retrieve the config
config = model.get_config()

# At loading time, register the custom objects with a `custom_object_scope`:
custom_objects = {"custom_activation": custom_activation}
with keras.utils.custom_object_scope(custom_objects):
    new_model = keras.Model.from_config(config)

Thank You.

Thank you @Kiran_Sai_Ramineni, I see it still needs to be configurate. Because as you can see from the tensorflow documentation about saving and loading of the model it was mentioned that there is no need to use the get_config method for custom functions. I also try using the get_config method but failed to load the h5 file, given your code I’ll try to use it again hoping I’ll be able to load the h5 file with the customized activation function. Thank you.

Your recommended code works @Kiran_Sai_Ramineni. Just put the fit and compile method outside and use the new_model instead of the model. Thank you!