ValueError: Unknown initializer with clone_model and custom initialization

Issue

I created the minimum working example. You can reproduce it here

I created my own Initializer called ComplexGlorotUniform(Initializer).

Then I created a file like: init_dispatcher = {"complex_glorot_uniform": ComplexGlorotUniform()}

Finally, I did:

from tensorflow.keras.utils import get_custom_objects

get_custom_objects().update(init_dispatcher)

I generated a sequential model of Dense layers using kernel_initializer="complex_glorot_uniform".

Now when using tf.keras.models.clone_model I get the error:

ValueError: Unknown initializer: ComplexGlorotUniform. Please ensure this object is passed to the `custom_objects` argument. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object 

I DO think the custom_object is working correctly because he knows it’s ComplexGlorotUniform and not the string I gave. Also, the layer is created correctly, is when calling the clone model method that it gets broken.

Solution

Well, I am still unsure why but I solved it by changing the init_dispatcher to init_dispatcher = {"ComplexGlorotUniform": ComplexGlorotUniform} to make the name match the string.

I guess it is Ok as it works for me but I am unsure if this is how it is supposed to work.

Answered By – Agustin Barrachina

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published