tensorflow / models

Models and examples built with TensorFlow
Other
76.78k stars 45.85k forks source link

Trying to Load Keras Model Returns ListIndex Error #11175

Closed tundeonpoint closed 3 months ago

tundeonpoint commented 4 months ago

I've built a seq2seq model which trains well and eventually gets to 100% validation accuracy. I save the full model using a callback into a '.keras' file format. However, when I try to load one of the saved files, I get an error saying IndexError: list assignment index out of range.

The model definition is as follows:

def enc_dec_model():
  # define encoder block
  enc_input = Input(shape=(seq_length,num_features),name='enc_input')
  # embed_layer = Embedding(input_dim=len(char2idx),output_dim=EMBEDDING_DIM,
  #                         trainable=False,mask_zero=True)
  enc_lstm = Bidirectional(LSTM(lstm_size,return_state=True,name='enc_lstm'))
  enc_out,enc_state_h_f,enc_state_h_r,enc_state_c_f,enc_state_c_r=enc_lstm(enc_input)#embed_layer(enc_input))
  enc_state_h = tf.concat([enc_state_h_f,enc_state_h_r],axis=1)
  enc_state_c = tf.concat([enc_state_c_f,enc_state_c_r],axis=1)
  states = [enc_state_h,enc_state_c]

  # print(enc_lstm.output_shape)

  # decoder block
  dec_lstm = LSTM(lstm_size * 2,return_state=True,return_sequences=True,name='dec_lstm')
  dec_input_data = np.zeros((BATCH_SIZE,1,num_features))
  dec_input_data[:,0,char2idx[PAD_CHAR]] = 1
  decoder_dense = Dense(num_features, activation='softmax',  name='decoder_dense')
  final_dec_out = Lambda(lambda x: K.concatenate(x, axis=1))
  final_conc_out = Concatenate(axis=1)

  all_outputs =[]

  for _ in range(seq_length):
    dec_out,dec_state_h,dec_state_c = dec_lstm(dec_input_data,initial_state=states)
    states = [dec_state_h,dec_state_c]
    output = decoder_dense(dec_out)
    all_outputs.append(output)
    dec_input_data=output

  final_output = final_conc_out(all_outputs)

  model = models.Model(inputs=enc_input,outputs=final_output,name='enc_dec_model')
  model.compile(optimizer=tf.keras.optimizers.RMSprop(0.001),loss=tf.keras.losses.CategoricalCrossentropy(),
              metrics='accuracy')

  return model`

The callback definition is as follows:

sf = 2

spb = x_train.shape[0]/BATCH_SIZE #steps per batch
sf_epoch = spb * sf

myoptimizer = optimizers.Adam(learning_rate=0.0001)

filepath="/content/drive/MyDrive/NLP/Models/SCModels/cp{epoch:02d}.keras"
checkpoint = EpochModelCheckpoint(filepath,frequency=sf,save_weights_only=False)

reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5,verbose=1,
                              patience=3)
e_stop = EarlyStopping(monitor='val_loss',patience=5,verbose=1)

callbacks_list = [checkpoint,reduce_lr,e_stop]

The fit code is as follows:

n_model = enc_dec_model()

history = n_model.fit(x=x_samp_train,y = y_samp_train,validation_data=(x_samp_val,y_samp_val),epochs=120,
                      callbacks=callbacks_list,batch_size=batch_size)

The EpochModelCheckpoint definition is as follows:

class EpochModelCheckpoint(tf.keras.callbacks.ModelCheckpoint):

    def __init__(self,
                 filepath,
                 frequency=1,
                 monitor='val_accuracy',
                 verbose=1,
                 save_best_only=False,
                 save_weights_only=False,
                 mode='auto',
                 options=None,
                 **kwargs):
        super(EpochModelCheckpoint, self).__init__(filepath, monitor, verbose, save_best_only, save_weights_only,
                                                   mode, "epoch", options)
        self.epochs_since_last_save = 0
        self.frequency = frequency

    def on_epoch_end(self, epoch, logs=None):
        self.epochs_since_last_save += 1
        # pylint: disable=protected-access
        if self.epochs_since_last_save % self.frequency == 0:
            self._save_model(epoch=epoch, batch=None, logs=logs)

    def on_train_batch_end(self, batch, logs=None):
        pass

The code to load the model is as follows:

n_model = tf.keras.models.load_model('/content/drive/MyDrive/NLP/Models/SCModels/cp84.keras')

Please help with this. Thanks.

laxmareddyp commented 4 months ago

Hi @tundeonpoint,

Could you please create a bug in the keras repo instead on tensorflow/models repo as It is not related to models.

Thanks.

github-actions[bot] commented 3 months ago

This issue has been marked stale because it has no recent activity since 7 days. It will be closed if no further activity occurs. Thank you.

github-actions[bot] commented 3 months ago

This issue was closed due to lack of activity after being marked stale for past 7 days.

google-ml-butler[bot] commented 3 months ago

Are you satisfied with the resolution of your issue? Yes No