在Keras中加载已保存的模型(双向LSTM)

时间:2021-06-17 13:54:53

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

我成功地在Keras训练并保存了双向LSTM模型:

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.

ValueError:您正在尝试将包含3个图层的权重文件加载到具有0个图层的模型中。

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.

我也尝试过不同的方法,比如单独保存和加载模型架构和权重,但它们都不适用于我。此外,以前,当我使用普通(单向)LSTM时,加载模型工作正常。

1 个解决方案

#1


0  

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

如@mpariente和@today所述,input_shape是Bidirectional的参数,而不是LSTM,请参阅Keras文档。我的解决方案

# 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')

#1


0  

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

如@mpariente和@today所述,input_shape是Bidirectional的参数,而不是LSTM,请参阅Keras文档。我的解决方案

# 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')