This is my first time working with tensorflow and keras and am having a problem saving and load models. I have figured out it specifically has to do with the Bidirectional LSTM layer but do not know what to do from there.
Here is my code:
model = Sequential()
model.add(Bidirectional(LSTM(128, activation='tanh'))) ### problem line
model.add(Dropout(0.2))
model.add(BatchNormalization())
model.add(Flatten())
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(1, activation='sigmoid'))
optimizer = Adam(learning_rate=0.001)
model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
early_stopping = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3, verbose=1)
model.fit(X_train_scaled, y_train, batch_size=32, epochs=3, validation_data=(X_test_scaled, y_test), callbacks=[early_stopping, reduce_lr])
model.summary()
model.save('models/sample_model.keras')
model = load_model('models/sample_model.keras')
Loading the model produces this error:
ValueError: A total of 1 objects could not be loaded. Example error message for object <LSTMCell name=lstm_cell, built=True>:
Layer 'lstm_cell' expected 3 variables, but received 0 variables during loading. Expected: ['kernel', 'recurrent_kernel', 'bias']
List of objects that could not be loaded:
[<LSTMCell name=lstm_cell, built=True>]
I have tried adding input_shape into the bidirectional parameters but the same error occurs. I also tried saving as .h5 file rather than .keras with no luck. I looked at the keras documentation and I thought I was doing it just as they suggested but I must have messed up somewhere.
I even commented one line at a time, saved, and loaded the models. The only time the error occurs is when I include the Bidirectional with an LSTM layer. Having an LSTM layer by itself did not cause an error.
Any ideas why this is happening?
EDIT: I am using tensorflow 2.16.1 and keras 3.2.0
The only solution that worked for me was to revert to tensorflow 2.15.1 and keras 2.15.0 and it now works as intended