简体   繁体   中英

Loading saved model (Bidirectional LSTM) in Keras

I trained and saved a Bidirectional LSTM model in Keras successfully with:

model = Sequential()
model.add(Bidirectional(LSTM(N_HIDDEN_NEURONS,
                        return_sequences=True,
                        activation="tanh",
                        input_shape=(SEGMENT_TIME_SIZE, N_FEATURES))))
model.add(Bidirectional(LSTM(N_HIDDEN_NEURONS)))
model.add(Dropout(0.5))
model.add(Dense(N_CLASSES, activation='sigmoid'))
model.compile('adam', 'binary_crossentropy', metrics=['accuracy'])

model.fit(X_train, y_train,
          batch_size=BATCH_SIZE,
          epochs=N_EPOCHS,
          validation_data=[X_test, y_test])

model.save('model_keras/model.h5')

However, when I want to load it with:

model = load_model('model_keras/model.h5')

I get an error:

ValueError: You are trying to load a weight file containing 3 layers into a model with 0 layers.

I also tried different methods like saving and loading model architecture and weights separately but none of them worked for me. Also, previously, when I was using normal (unidirectional) LSTMs, loading the model worked fine.

As mentioned by @mpariente and @today , the input_shape is an argument of Bidirectional, not LSTM, see Keras documentation . My solution:

# Model
model = Sequential()
model.add(Bidirectional(LSTM(N_HIDDEN_NEURONS,
                             return_sequences=True,
                             activation="tanh"), 
                        input_shape=(SEGMENT_TIME_SIZE, N_FEATURES)))
model.add(Bidirectional(LSTM(N_HIDDEN_NEURONS)))
model.add(Dropout(0.5))
model.add(Dense(N_CLASSES, activation='sigmoid'))
model.compile('adam', 'binary_crossentropy', metrics=['accuracy'])

model.fit(X_train, y_train,
          batch_size=BATCH_SIZE,
          epochs=N_EPOCHS,
          validation_data=[X_test, y_test])

model.save('model_keras/model.h5')

and then, to load, simply do:

model = load_model('model_keras/model.h5')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM