本笔记记录使用SimpleRNNCell做一个IMDB评价系统情感二分类问题的例子。
import os
import time
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import datasets, layers, optimizers, Sequential, metrics, Inputos.environ['TF_CPP_MIN_LOG_LEVEL']='2'
#tf.random.set_seed(12345)
#np.random.seed(22)
tf.__version__#取常见的10000个单词
total_words = 10000
#句子最长的单词数量设置为80
max_review_len = 80
#embedding设置为100,表示每个单词用100维向量表示
embedding_len = 100
#加载IMDB数据集
(x_train,y_train), (x_test, y_test) = datasets.imdb.load_data(num_words = total_words)
#对训练数据和测试数据的句子进行填充或截断
x_train = keras.preprocessing.sequence.pad_sequences(x_train, maxlen=max_review_len)
x_test = keras.preprocessing.sequence.pad_sequences(x_test, maxlen=max_review_len)#构建数据集
batchsize = 128
db_train = tf.data.Dataset.from_tensor_slices((x_train, y_train))
db_train = db_train.shuffle(1000).batch(batchsize, drop_remainder=True)
db_test = tf.data.Dataset.from_tensor_slices((x_test, y_test))
db_test = db_test.batch(batchsize, drop_remainder=True)#x_train包含25000个句子,每个句子包含80个单词,y_train标签为1表示好评,0表示差评
print('x_train: shape - ', x_train.shape, ' y_train: max/min -', tf.reduce_max(y_train).numpy(), '/', tf.reduce_min(y_train).numpy())
print('x_test: shape - ', x_test.shape)class MyRNN(keras.Model):#units:state的维度def __init__(self, total_words, embedding_len, max_review_len, units):super(MyRNN, self).__init__()#初始的序列状态初始化为0(第0时刻的状态)self.state0 = [tf.zeros([batchsize, units])]self.state1 = [tf.zeros([batchsize, units])]#embedding层,将文本转换为embedding表示#[b, 80] => [b, 80, 100]self.embedding = layers.Embedding(total_words, embedding_len, input_length=max_review_len)#[b, 80, 100] , units: 64 - 转换为64维的state [b, 64]self.rnn_cell0 = layers.SimpleRNNCell(units, dropout=0.2)self.rnn_cell1 = layers.SimpleRNNCell(units, dropout=0.2)#全连接层 [[b, 64] => [b, 1]self.outlayer = layers.Dense(1)#inputs: [b, 80] def call(self, inputs, training=None):x = inputs#做embedding,[b,80] => [b, 80, 100]x = self.embedding(x)#做RNN cell计算#[b, 80, 100] => [b, 64]#遍历句子中的每个单词# word: [b, 100]state0 = self.state0state1 = self.state1for word in tf.unstack(x, axis=1):#h1 = x*w_xh + h0*w_hhout0, state0 = self.rnn_cell0(word, state0, training)out1, state1 = self.rnn_cell1(out0, state1)#循环完毕后,得到的out为[b, 64],表示每个句子最终得到的状态x = self.outlayer(out1)#计算最终评价结果prob = tf.sigmoid(x)return probdef main():units = 64epochs = 15lr = 0.001model = MyRNN(total_words, embedding_len, max_review_len, units)model.compile(optimizer = optimizers.Adam(lr), loss = tf.losses.BinaryCrossentropy(),metrics=['accuracy'])model.fit(db_train, epochs=epochs, validation_data=db_test)model.evaluate(db_test)if __name__ == '__main__':main()
运行结果: