Import trained 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(LSTM(units = 60, activation = 'relu', return_sequences = 'True'))

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

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

model.add(Dense(units = 1))

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

# Save the model'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 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

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}
File “D:\Program_Files\Python312\Lib\site-packages\streamlit\runtime\scriptrunner\”, line 542, in run_script
exec(code, module.dict)
File “C:\Users\sanni\Desktop\Stock\”, line 72, in
model = load_model(‘LSTM_model.keras’)
File “D:\Program_Files\Python312\Lib\site-packages\keras\src\saving\”, line 176, in load_model
return saving_lib.load_model(
File “D:\Program_Files\Python312\Lib\site-packages\keras\src\saving\”, line 155, in load_model
model = deserialize_keras_object(
File “D:\Program_Files\Python312\Lib\site-packages\keras\src\saving\”, line 711, in deserialize_keras_object
instance = cls.from_config(inner_config)
File “D:\Program_Files\Python312\Lib\site-packages\keras\src\models\”, line 331, in from_config
layer = serialization_lib.deserialize_keras_object(
File “D:\Program_Files\Python312\Lib\site-packages\keras\src\saving\”, line 711, in deserialize_keras_object
instance = cls.from_config(inner_config)
File “D:\Program_Files\Python312\Lib\site-packages\keras\src\layers\rnn\”, line 646, in from_config
return cls(**config)
File “D:\Program_Files\Python312\Lib\site-packages\keras\src\layers\rnn\”, line 459, in init
cell = LSTMCell(
File “D:\Program_Files\Python312\Lib\site-packages\keras\src\layers\rnn\”, 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(

Hi @Sanniddha,

Thanks for sharing this question!

Do you have a repo with the code and model so we can try to replicate and troubleshoot?

Hi @Sanniddha

Have you also tried loading the model in Colab and see if that works to rule out any possibility of error in saving/loading the model.

@tonykip @dataprofessor 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 version mismatch.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.