Can't load weights of a keras model saved with old keras version using new keras version

Viewed 225

I'm trying to set weights of a keras model (loaded with new keras version - 2.2.4-tf, which is now under tensorflow package) which was saved to a DB using old keras, but it is failing with the error:

ValueError: Layer weight shape (8,) not compatible with provided weight shape (1, 9, 8, 4)

Here is a code snippet depicting what we were trying to do:

model = keras.models.model_from_json(...)
model.compile(...)
model.set_weights(...)

As stated before, the code is crashing in the model.set_weights(...) function, producing the above error.

We found out that the same layers exist both in the model loaded using an old keras version (2.3.1) - which also succeeded to set the weights properly - and also with the model loaded using new keras version (2.2.4-tf), but not in the same order.

Below is the output of the code:

collection.Counter([ww.shape for ww in m_old.get_weights()])

and the code:

collection.Counter([ww.shape for ww in m_new.get_weights()])

where m_old is the model loaded by the old keras version and the m_new is the model loaded by the new keras version:

Out[20]:
Counter({(7, 1, 1, 8): 5,
         (8,): 20,
         (1, 9, 8, 4): 5,
         (4,): 20,
         (120, 1): 5,
         (1,): 5})
Out[21]:
Counter({(7, 1, 1, 8): 5,
         (8,): 20,
         (1, 9, 8, 4): 5,
         (4,): 20,
         (120, 1): 5,
         (1,): 5})

We will appreciate any help.

0 Answers
Related