keras在构建LSTM模型时对变长序列的处理

  • Post author:
  • Post category:其他


print(np.shape(X))#(1920, 45, 20)

X=sequence.pad_sequences(X, maxlen=100, padding='post')
print(np.shape(X))#(1920, 100, 20)

model = Sequential()
model.add(Masking(mask_value=0,input_shape=(100,20)))
model.add(LSTM(128,dropout_W=0.5,dropout_U=0.5))
model.add(Dense(13,activation='softmax'))
model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

# 用于保存验证集误差最小的参数,当验证集误差减少时,保存下来
checkpointer = ModelCheckpoint(filepath="keras_rnn.hdf5", verbose=1, save_best_only=True, )
history = LossHistory()
result = model.fit(X, Y, batch_size=10,
                   nb_epoch=500, verbose=1, validation_data=(testX, testY),
                   callbacks=[checkpointer, history])

model.save('keras_rnn_epochend.hdf5')



版权声明:本文为xinfeng2005原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。