Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError when loading models that reuse weights #20307

Open
K1521 opened this issue Sep 30, 2024 · 2 comments
Open

ValueError when loading models that reuse weights #20307

K1521 opened this issue Sep 30, 2024 · 2 comments
Assignees
Labels

Comments

@K1521
Copy link

K1521 commented Sep 30, 2024

if I create a model wich reuses layers I get a Error when trying to load it again.

import tensorflow as tf
from keras.models import load_model,save_model
from keras import layers

inputs = layers.Input(shape=(10,))
x=inputs

t=layers.Dense(10)
x = t(x)
x = layers.Dense(10)(x)
x = t(x)
model=tf.keras.Model(inputs, x)

model.summary()
save_model(model,'testmodel.keras')

model2=load_model('testmodel.keras')
model2.summary()

I also found out how to fix it:
https://github.com/keras-team/keras/blob/d3671cf276d838599dd8acec9616845ac262d52a/keras/src/models/functional.py#L687C3-L690C56

This ValueError has to be a IndexError (like in the legacy case in the code above).

That way it can be caught here:
https://github.com/keras-team/keras/blob/d3671cf276d838599dd8acec9616845ac262d52a/keras/src/models/functional.py#L517C1-L525C36

:)

@mehtamansi29
Copy link
Collaborator

Hi @K1521 -

Thanks for reporting this issue. Here While creating the model you can reuse the layer weight store in variable(t) t=layers.Dense(10) instead of declaring new layer x = layers.Dense(10)(x).
Attached gist here for your reference.

@K1521
Copy link
Author

K1521 commented Oct 9, 2024

I know that it works if you feed the layer in itself, but that is not what i want to do. I have reduced a larger example down to this example.
As i have said , i have solved the problem by replacing the ValueError with a IndexError. That way the code is more similar to the legacy case here:

raise IndexError(

i think somebody just translated the legacy case wrong

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants