原理:
TextCNN出处:论文https://aclanthology.org/D14-1181/
核心论点:
1.Represent sentence with static and non-static channels
2.Convolve with multiple filter widths and feature maps
3.Use max-over-time pooling
4.Use fully connected layer with dropout and softmax output
本文实现:
TextCNN的网络结构:
模型构建与训练
定义网络结构
from tensorflow.keras import Input , Model
from tensorflow.keras.layers import Embedding ,Dense , Conv1D , GlobalMaxPooling1D,Concatenate,Dropout
class TextCNN(object):
def __init__(self,maxlen , max_features , embedding_dims,class_num = 5 , last_activation = 'softmax'):
self.maxlen = maxlen
self.max_features = max_features
self.embedding_dim = embedding_dims
self.class_num = class_num
self.last_activation = last_activation
def get_model(self):
input = Input((self.max_len,))
embdding = Embedding(self.max_features , self.embedding_dims , input_length = self.max_len)(input)
convs = []
for kernel_size in [3,4,5]:
c = Con1D(128,kernel_size,activation = 'relu')(embedding)
c = GlobalMaxPooling1D(c)
convs.append(c)
x = Concatenate()(convs)
output = Dense(self.class_num , activation = self.last_activation)(x)
model = Model(inputs = input,outputs = output)
return model
数据处理与训练
from tensorflow.keras.proprecessing import sequence
import random
from sklearn.model_selection import train_test_split
from tensorflow.keras.callbacks import EarlyStopping,ModelCheckpoint
from tensorflow.keras.utils import to_categorical
from utils import *
#路径配置
data_dir = './processed_data'
vocab_file = './vocab/vocab.txt'
vocab_size = 40000
#神经网络配置
max_features = 40001
maxlen = 100
batch_size = 64
embedding_dims = 50
epochs = 8
print('数据预处理与加载数据...')
#如果不存在词汇表,重建
if not os.path.exists(vocab_file):
build_vocab(data_dir , vocab_file , vocab_size)
#获得 词汇/类别 与id映射字典
categories , cat_to_id = read_category()
words , word_to_id = read_vocab(vocab_file)
#全部数据
x , y = read_files(data_dir)
data = list(zip(x,y))
del x , y
#乱序
random.shuffle(data)
#切分训练集与测试集
train_data , test_data = train_test_split(data)
#对文本的词id和类别id进行编码
x_train = encode_sentences([content[0] for content in train_data] , word_to_id)
y_train = to_categorical(encode_cate([content[1] for content in train_data] , cat_to_id))
x_test = encode_sentences([content[0] for content in test_data] , word_to_id)
y_test = to_categorical(encode_cate([content[1] for content in test_data] , cat_to_id))
print('对序列做padding,保证是samples * timestep的维度')
x_train = sequence.pad_sequences(x_train , maxlen = maxlen)
x_test = sequence.pad_sequences(x_test , maxlen = maxlen)
print('x_train shape:' , x_train.shape)
print('x_test_shape:' , x_test.shape)
print('构建模型.....')
model = TextCNN(maxlen,max_features , embedding_dims).get_model()
model.compile('adam' , 'categorical_crossentropy' , metrics = ['accuracy'])
print('训练')
#设定callbacks回调函数
my_callbacks = [
ModelCheckpoint('./cnn_model.h5',verbose = 1),
EarlyStopping(monitor = 'val_accuracy' , patience = 2 , mode = 'max')
]
#fit拟合数据
history = model.fit(x_train , y_train, batch_size = batch_size ,epochs = epochs , callbacks = my_callbacks,validation_data = (x_test , y_test))
print('对测试集预测....')
result = model.predict(x_test)
训练中间信息输出
import matplotlib.pyplot as plt
plt.switch_bacakend('agg')
%matplotlib inline
fig1 = plt.figure()
plt.plot(history.history['loss'] , 'r' , linewidth = 3.0)
plt.plot(history.history['val_loss'],'b' , linewidth = 3.0)
plt.legend(['Training loss' , 'Validation Loss'] , fontsize = 18)
plt.xlabel('Epochs' , fontsize = 16)
plt.ylabel('Loss' , fontsize = 16)
plt.title('Loss Curves :CNN',fontsize = 16)
fig1.savefig('loss_cnn,png')
plt.show()
fig2=plt.figure()
plt.plot(history.history['acc'],'r',linewidth=3.0)
plt.plot(history.history['val_acc'],'b',linewidth=3.0)
plt.legend(['Training Accuracy', 'Validation Accuracy'],fontsize=18)
plt.xlabel('Epochs ',fontsize=16)
plt.ylabel('Accuracy',fontsize=16)
plt.title('Accuracy Curves : CNN',fontsize=16)
fig2.savefig('accuracy_cnn.png')
plt.show()
模型结构打印
from tensorflow.keras.utils import plot_model
# model.summary()
plot_model(model, show_shapes=True, show_layer_names=True)