神经网络(三)递归神经网络

# 使用 RNN 对来自 IMDB 影评数据库的数据进行分类

from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.callbacks import EarlyStopping

# 序列数据预处理
from keras.preprocessing import sequence

# 长短期记忆模型
# LSTM 是一种循环神经网络
# 包括 输入门 & 遗忘门 & 输出门
# 输入门表示对新的输入数据的接受程度
# 遗忘门表示对旧有数据的遗忘程度
# 输出门表示 LSTM 对数据的的输出程度
from keras.layers import LSTM

# GRU 也是一种循环神经网络
# 包括 更新门 & 重置门
# 更新门决定上一个隐层数据传递到下一隐层的程度
# 重置门表示对上一个隐层数据的遗忘程度
from keras.layers import GRU

# 嵌入层
# 嵌入层只能作为模型的第一层
# 嵌入层将正整数下标转换为具有固定大小的向量
from keras.layers import Embedding

# IMDB 数据库包含 25000 条影评
# 每条影评处理为由词下标构成的序列
# 每条影评被分为正向或负向
from keras.datasets import imdb

# 导入 IMDB 数据
# 导入出现频率最大的 2000个 词
n_words = 2000
(X_train, y_train), (X_val, y_val) = imdb.load_data(num_words=n_words)

# 填充序列
# 填充序列长度为 200
# RNN 接受序列输入
# keras 为了实现的方便
# 只允许相同长度的序列输入
max_len = 200
X_train = sequence.pad_sequences(X_train, maxlen=max_len)
X_val = sequence.pad_sequences(X_val, maxlen=max_len)

# 构造 LSTM

model_lstm = Sequential()

# n_words 表示字典长度
# 50 表示全连接嵌入的维度
model_lstm.add(Embedding(input_dim=n_words, output_dim=50, input_length=max_len))

model_lstm.add(Dropout(0.2))

# 添加 LSTM 层
# recurrent_dropout 表示循环步中 Dropout 的比例
model_lstm.add(LSTM(100, dropout=0.2, recurrent_dropout=0.2))

# 增加神经网络
model_lstm.add(Dense(250, activation='relu'))
model_lstm.add(Dropout(0.2))
model_lstm.add(Dense(1, activation='sigmoid'))

# 编译模型
model_lstm.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model_lstm.summary()

# 设置超参数
batch_size = 64
n_epochs = 10

# 训练模型
model_lstm.fit(X_train, y_train, batch_size=batch_size, epochs=n_epochs)

# 评价模型
print(model_lstm.evaluate(X_val, y_val))
# [ 误差             , 准确率 ]
# [0.323493696937561, 0.8642]

# 构造 GRU

model_gru = Sequential()

# n_words 表示字典长度
# 50 表示全连接嵌入的维度
model_gru.add(Embedding(input_dim=n_words, output_dim=50, input_length=max_len))

model_gru.add(Dropout(0.2))

# 添加 GRU 层
# recurrent_dropout 表示循环步中 Dropout 的比例
model_gru.add(GRU(100, dropout=0.2, recurrent_dropout=0.2))

# 增加神经网络
model_gru.add(Dense(250, activation='relu'))
model_gru.add(Dropout(0.2))
model_gru.add(Dense(1, activation='sigmoid'))

# 编译模型
model_gru.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model_gru.summary()

# 设置超参数
batch_size = 512
n_epochs = 100

# 设置回调函数
callbacks = [EarlyStopping(monitor='val_acc', patience=3)]

# 训练模型
model_gru.fit(X_train, y_train, batch_size=batch_size, epochs=n_epochs, validation_split=0.2, callbacks=callbacks)

# 评价模型
print(model_gru.evaluate(X_val, y_val))
# [ 误差             , 准确率 ]
# [0.3735375535917282, 0.84732]

参考书籍:《Python深度学习实战:75个有关神经网络建模、强化学习与迁移学习的解决方案》

猜你喜欢

转载自blog.csdn.net/lolimostlovely/article/details/82559382
今日推荐