keras icon indicating copy to clipboard operation
keras copied to clipboard

ValueError when loading models that reuse weights

Open K1521 opened this issue 1 year ago • 2 comments

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

:)

K1521 avatar Sep 30 '24 16:09 K1521

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 59.70%. Comparing base (c0f3524) to head (9d4a741).

Additional details and impacted files
@@            Coverage Diff            @@
##             master    #5634   +/-   ##
=========================================
  Coverage     59.69%   59.70%           
- Complexity     3852     3853    +1     
=========================================
  Files           559      560    +1     
  Lines         16006    16011    +5     
  Branches       3072     3072           
=========================================
+ Hits           9555     9559    +4     
- Misses         6046     6047    +1     
  Partials        405      405           

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov-commenter avatar Oct 08 '24 06:10 codecov-commenter