from keras.layers import model = Sequential() model.add(embedding_layer) #使用一维卷积网络切割输入数据,参数5表示每各个单词作为切割小段 model.add(layers.Conv1D(32, 5, activation='relu')) #参数3表示,上层传下来的数据中,从每3个数值中抽取最大值 model.add(layers.MaxPooling1D(3)) #添加一个有记忆性的GRU层,其原理与LSTM相同,运行速度更快,准确率有所降低 model.add(layers.GRU(32, dropout=0.1)) 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()