Solve NotImplementedError: Layers with arguments in __init__ must override get_config

NotImplementedError: Layers with arguments in __init__ must override get_config is a common error which occurs in TensorFlow when you can’t save your model, because your model won’t be able to load.

In this post we will try to solve your error and see why it occurs in the first place, we will present multiple solutions so you can find the one which suits your case.

Explaining the Error : NotImplementedError: Layers with arguments in __init__ must override get_config

First, we need to understand that this error is a good thing, it is a feature which allows you to understand that you should make some changes, the error is letting you know that TensorFlow can not save this model since it is not going to able to load it.

The error says that TensorFlow wont be able to reinstantiate the custom Layer classes: encoder and decoder.

Bellow is the message of the error.

                                                                       #
NotImplementedError: Layers with arguments in `__init__` must override `get_config`
                                                                       #

Bellow we will take care of the error using multiple possible solutions according to your needs.

Solution 1 : override get_config

The first solution is to override your get_config method. In order to achieve that, you can use the following script.

                                                                       #
def get_config(self):
config = super().get_config().copy()
config.update({
'vocab_size': self.vocab_size,
'num_layers': self.num_layers,
'units': self.units,
'd_model': self.d_model,
'num_heads': self.num_heads,
'dropout': self.dropout,
})
return config
                                                                       #

Now, everything should be able to load fine as TensorFlowwill be able to reinstantiate all the custom classes.

Solution 2 : Do not mix imports between the libraries keras and tf.keras

In many occasions this error is caused by the developer maxing imports between two important libraries keras and tf.keras.

Make sure you use tf.keras.models only. Or, use keras.models instead.

Do not mix the imports between the two as this causes a lot of errors and conflicts.

The error above was hard to deal with, I spent hours looking for a proper solution or set of solutions.

I hope I made your day at work/studies better, you can help us with Kofi donations, we have a red button at the top of this page.

Summing-up

This is it, it is the end of our article, I hope I helped you solve this Python error, a simple upgrade will usually solve this issue. Do not give up, keep coding and learning, cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/