```
import tensorflow as tf
from tensorflow.keras.datasets import imdb
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, GRU, Dense, Dropout
import numpy as np
import matplotlib.pyplot as plt
#加载IMDb数据集(限制词汇量为4000)
num_words = 4000
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=num_words)
# 序列填充(统一长度为400)
maxlen = 400
x_train = pad_sequences(x_train, maxlen=maxlen, padding='post')
x_test = pad_sequences(x_test, maxlen=maxlen, padding='post')
# 创建顺序模型
model = Sequential()
#嵌入层(词汇量4000,输出向量32,输入长度400)
model.add(Embedding(input_dim=num_words, output_dim=32, input_length=maxlen))
#Dropout层(丢弃率0.3)
model.add(Dropout(0.3))
#GRU层(输出维度64)
model.add(GRU(units=64))
#Dropout层(丢弃率0.3)
model.add(Dropout(0.3))
#输出层(二分类,Sigmoid激活)
model.add(Dense(1, activation='sigmoid'))
#显示模型结构
model.summary()
#编译模型(优化器RMSprop,二元交叉熵损失)
model.compile(
optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy']
)
#训练模型(batch_size=64,epochs=10,验证集20%)
history = model.fit(
x_train, y_train,
batch_size=64,
epochs=10,
validation_split=0.2
)
#评估测试集(batch_size=64,日志模式2)
test_loss, test_acc = model.evaluate(x_test, y_test, batch_size=64, verbose=2)
print(f"测试集准确率: {test_acc:.4f}")
# 绘制训练过程曲线
plt.figure(figsize=(12, 5))
plt.rcParams['font.family'] = 'FangSong'
plt.rcParams['axes.unicode_minus'] = False
# 子图1:损失函数
plt.subplot(1, 2, 1)
plt.plot(history.history['loss'], label='训练集损失')
plt.plot(history.history['val_loss'], label='验证集损失')
plt.title('损失函数变化曲线')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
# 子图2:准确率
plt.subplot(1, 2, 2)
plt.plot(history.history['accuracy'], label='训练集准确率')
plt.plot(history.history['val_accuracy'], label='验证集准确率')
plt.title('准确率变化曲线')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.tight_layout()
plt.show()```提高准确lu
最新发布