Import trained LSTM model on Streamlit

I have trained a LSTM model on Colab and saved the model. Below is the code of my model:

from keras.layers import Dense, Dropout, LSTM
from keras.models import Sequential

# Build LSTM model
model = Sequential()  # Initialize the Sequential model
model.add(LSTM(units=50, activation='relu', return_sequences=True, input_shape=(x_train.shape[1], 1)))
model.add(Dropout(0.2))



model.add(LSTM(units = 60, activation = 'relu', return_sequences = 'True'))
model.add(Dropout(0.3))

model.add(LSTM(units = 80, activation = 'relu', return_sequences = 'True'))
model.add(Dropout(0.4))

model.add(LSTM(units = 120, activation = 'relu'))
model.add(Dropout(0.5))


model.add(Dense(units = 1))

# Fit the model on traing data
model.compile(optimizer='adam', loss = 'mean_squared_error')
model.fit(x_train, y_train, epochs = 100)

# Save the model
model.save('LSTM_model.keras')

Then, I load the model on Streamlit with the below code:

from tensorflow.keras.models import load_model

# Load the model
model = load_model('LSTM_model.keras')

The trained model LSTM_model.keras and the streamlit app named main.py both are kept in same directory/path. I run the straemlit app by executing the below command from the same directory where the two files are kept.

streamlit run main.py

But when I run the Streamlit app, I get the error:

ValueError: Could not interpret initializer identifier: {‘module’: ‘keras.initializers’, ‘class_name’: ‘Orthogonal’, ‘config’: {‘gain’: 1.0, ‘seed’: None}, ‘registered_name’: None, ‘shared_object_id’: 140299714528304}
Traceback:
File “D:\Program_Files\Python312\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py”, line 542, in run_script
exec(code, module.dict)
File “C:\Users\sanni\Desktop\Stock\main.py”, line 72, in
model = load_model(‘LSTM_model.keras’)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “D:\Program_Files\Python312\Lib\site-packages\keras\src\saving\saving_api.py”, line 176, in load_model
return saving_lib.load_model(
^^^^^^^^^^^^^^^^^^^^^^
File “D:\Program_Files\Python312\Lib\site-packages\keras\src\saving\saving_lib.py”, line 155, in load_model
model = deserialize_keras_object(
^^^^^^^^^^^^^^^^^^^^^^^^^
File “D:\Program_Files\Python312\Lib\site-packages\keras\src\saving\serialization_lib.py”, line 711, in deserialize_keras_object
instance = cls.from_config(inner_config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “D:\Program_Files\Python312\Lib\site-packages\keras\src\models\sequential.py”, line 331, in from_config
layer = serialization_lib.deserialize_keras_object(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “D:\Program_Files\Python312\Lib\site-packages\keras\src\saving\serialization_lib.py”, line 711, in deserialize_keras_object
instance = cls.from_config(inner_config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “D:\Program_Files\Python312\Lib\site-packages\keras\src\layers\rnn\lstm.py”, line 646, in from_config
return cls(**config)
^^^^^^^^^^^^^
File “D:\Program_Files\Python312\Lib\site-packages\keras\src\layers\rnn\lstm.py”, line 459, in init
cell = LSTMCell(
^^^^^^^^^
File “D:\Program_Files\Python312\Lib\site-packages\keras\src\layers\rnn\lstm.py”, line 122, in init
self.recurrent_initializer = initializers.get(recurrent_initializer)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Program_Files\Python312\Lib\site-packages\keras\src\initializers_init.py", line 117, in get
raise ValueError(

The issue has been solved. The Google Colab use older version of Keras and Tensorflow, whereas in my PC has latest version of Keras and Tensorflow. I build and saved the model in Jupyter Notebook in my localhost, and it works as it use the latest version of Keras and Tensorflow installed in my PC since I am using localhost. I think the error occurred due to version mismatch.

1 Like