0

I am trying to load my model that is saved on another file so that it can be called when a prediction is need.It a LSTM model to predict RUL.

Loading model code

import tensorflow as tf
from tensorflow.keras import optimizers

# Define custom optimizer dictionary
custom_objects = {'RMSprop': optimizers.RMSprop}

# Load the model with custom optimizer
new_model = tf.keras.models.load_model(r'path\lstm_model.h5',
                                       custom_objects=custom_objects)

# Print model summary
new_model.summary()

model structure

model=Sequential()
model.add(LSTM(units=128,
               return_sequences=True,
              input_shape=(sequence_length,nb_features)))
model.add(BatchNormalization())
model.add(Dropout(0.2))
model.add(LSTM(units=56,
              return_sequences=False))
model.add(Dropout(0.2))
model.add(Dense(units=1,activation='relu'))
model.add(Activation("relu"))
model.compile(loss="mse",optimizer="rmsprop",metrics=['mse'])

model.summary()

the code gives me this error

Traceback (most recent call last):
  File "pathloadtest.py", line 8, in <module>
    new_model = tf.keras.models.load_model(r'C:path\lstm_model.h5',
  File "\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\utils\generic_utils.py", line 562, in class_and_config_for_serialized_keras_object
    raise ValueError(
ValueError: Unknown optimizer: Custom>RMSprop. Please ensure this object is passed to the `custom_objects` argument. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.

I am trying to save the optimizer so that it could be called from the other code but still currently unable to.

  • link : https://stackoverflow.com/questions/54835331/how-do-i-load-a-keras-saved-model-with-custom-optimizer. Not sure if this helps but do check it. – Nandan May 03 '23 at 04:29

0 Answers0