Can't load LSTM encoder decoder model Keras IndexError: list assignment index out of range

I'm using keras and functional API to create an encoder decoder model containing 2 LSTM layers each for binary classification. The shape of input to encoder x is (samples, time steps, in_features) = (126144, 1, 113) The shape of labels y is (samples, time steps, out_features) =(126144, 1, 2) x and y are both numpy arrays.

import tensorflow as tf

import tensorflow.keras as keras
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import LSTM
from tensorflow.keras.layers import Dense
from numpy import array
from numpy import array_equal
from tensorflow.keras.layers import Lambda
from tensorflow.keras import backend as K
n_timesteps_in = 1
n_features = 113
out_features = 2
numberOfLSTMunits = 256
def create_hard_coded_decoder_input_model(batch_size):
# The first part is encoder
encoder_inputs = Input(shape=(n_timesteps_in, n_features), name='encoder_inputs')
encoder_lstm = LSTM(numberOfLSTMunits, return_state=True,return_sequences=True,  
encoder_outputs, state_h1, state_c1 = encoder_lstm(encoder_inputs)
# Second LSTM Added
encoder_lstm2 = LSTM(numberOfLSTMunits, return_state=True,  name='encoder_lstm2')
_, state_h2, state_c2 = encoder_lstm2(encoder_outputs) 

states = [state_h1, state_c1, state_h2, state_c2]
decoder_inputs = Input(shape=(1, out_features),  name='decoder_inputs')
decoder_lstm = LSTM(numberOfLSTMunits, return_sequences=True, return_state=True, 

# Second LSTM
decoder_lstm2 = LSTM(numberOfLSTMunits, return_sequences=True, return_state=True, 

decoder_dense = Dense(out_features, activation='softmax',  name='decoder_dense')
# New input decoder
all_outputs = []
decoder_input_data = np.zeros((batch_size, 1, out_features))
decoder_input_data[:, 0, 0] = -1 
inputs = decoder_input_data
states1 = [state_h1, state_c1]
states2 = [state_h2, state_c2]
for _ in range(n_timesteps_in):
  # Run the decoder on one time step
  outputs, dh1, dc1 = decoder_lstm(inputs,initial_state= states1)
  final, dh2, dc2 = decoder_lstm2(outputs, initial_state=states2)
  outputs = decoder_dense(final)
  # Store the current prediction (we will concatenate all predictions later)
  # Reinject the outputs as inputs for the next loop iteration
  # as well as update the states
  inputs = outputs
  states1 = [state_h1, state_c1]
  states2 = [state_h2, state_c2]
decoder_outputs = Lambda(lambda x: K.concatenate(x, axis=1))(all_outputs)
model = Model(encoder_inputs, decoder_outputs, name='model_encoder_decoder')
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
return model

I'm using 192 as the batch size. After training, I save the model with this code:'lstm.h5')

When I load the model:


I get this error:

  /usr/local/lib/python3.7/dist-packages/keras/layers/ in get_input_spec(shape)
  547       batch_index, time_step_index = (1, 0) if self.time_major else (0, 1)
  548       if not self.stateful:
  --> 549         input_spec_shape[batch_index] = None
  550       input_spec_shape[time_step_index] = None
  551       return InputSpec(shape=tuple(input_spec_shape))

  IndexError: list assignment index out of range

I've trying to solve the problem for days, but nothing seemed to work. I really appreciate any help with it.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum