from keras.layers import LSTM model = Sequential() model.add(embedding_layer) model.add(LSTM(32)) #当结果是输出多个分类的概率时,用softmax激活函数,它将为30个分类提供不同的可能性概率值 model.add(layers.Dense(len(int_category), activation='softmax')) #对于输出多个分类结果,最好的损失函数是categorical_crossentropy model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) history = model.fit(x_train, y_train, epochs=20, validation_data=(x_val, y_val), batch_size=512)
acc = history.history['acc'] val_acc = history.history['val_acc'] loss = history.history['loss'] val_loss = history.history['val_loss'] epochs = range(1, len(acc) + 1) plt.title('Training and validation accuracy') plt.plot(epochs, acc, 'red', label='Training acc') plt.plot(epochs, val_acc, 'blue', label='Validation acc') plt.legend() plt.show()