16-3-RNN-recognise-malicious-comments-LSTM.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from datasets import Datasets
  2. import tensorflow as tf
  3. from sklearn.model_selection import train_test_split
  4. import matplotlib.pyplot as plt
  5. def main():
  6. # 导入数据
  7. x, y = Datasets.load_movie_review()
  8. x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=.3)
  9. # 数据预处理,词袋
  10. tokenizer = tf.keras.preprocessing.text.Tokenizer()
  11. tokenizer.fit_on_texts(x)
  12. x_train = tokenizer.texts_to_sequences(x_train)
  13. x_test = tokenizer.texts_to_sequences(x_test)
  14. num_words = len(tokenizer.word_index)
  15. # 序列编码one-hot
  16. x_train = tf.keras.preprocessing.sequence.pad_sequences(x_train, maxlen=200)
  17. x_test = tf.keras.preprocessing.sequence.pad_sequences(x_test, maxlen=200)
  18. y_train = tf.keras.utils.to_categorical(y_train, num_classes=2)
  19. y_test = tf.keras.utils.to_categorical(y_test, num_classes=2)
  20. # 顺序模型(层直接写在里面,省写add)
  21. model = tf.keras.Sequential([
  22. tf.keras.layers.Embedding(
  23. input_dim=num_words + 1, # 字典长度 加1 不然会报错
  24. output_dim=300, # 全连接嵌入的维度,常用256或300
  25. input_length=200, # 当输入序列的长度固定时,该值为其长度
  26. trainable=True, # 代表词向量作为参数进行更新
  27. ),
  28. tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(128, implementation=2)), # 双向LSTM
  29. tf.keras.layers.Dropout(0.5), # 丢弃50%,防止过拟合
  30. tf.keras.layers.Dense(2, activation="softmax"),
  31. ])
  32. # 编译模型
  33. model.compile(
  34. optimizer="adam", # 优化器
  35. loss="categorical_crossentropy", # 损失函数
  36. metrics=["acc"], # 观察值, acc正确率
  37. )
  38. # 训练
  39. history = model.fit(
  40. x_train, y_train,
  41. batch_size=32, # 一次放入多少样本
  42. epochs=10,
  43. validation_data=(x_test, y_test),
  44. )
  45. # loss: 0.0013 - acc: 1.0000 - val_loss: 0.5421 - val_acc: 0.7350
  46. # 画图 正确率(是否过拟合)
  47. plt.plot(history.epoch, history.history.get("acc"))
  48. plt.plot(history.epoch, history.history.get("val_acc"))
  49. plt.show()
  50. if __name__ == "__main__":
  51. main()