赞
踩
2018年10月4日笔记
tensorflow是谷歌google的深度学习框架,tensor中文叫做张量,flow叫做流。
CNN是convolutional neural network的简称,中文叫做卷积神经网络。
文本分类是NLP(自然语言处理)的经典任务。
操作系统:Win10
tensorflow版本:1.6
tensorboard版本:1.6
python版本:3.6
本文是作者学习《使用卷积神经网络以及循环神经网络进行中文文本分类》的成果,感激前辈;
github链接:https://github.com/gaussic/text-classification-cnn-rnn
使用卷积神经网络模型要求有较高的机器配置,如果使用CPU版tensorflow会花费大量时间。
读者在有nvidia显卡的情况下,安装GPU版tensorflow会提高计算速度50倍。
安装教程链接:https://blog.csdn.net/qq_36556893/article/details/79433298
如果没有nvidia显卡,但有visa信用卡,请阅读我的另一篇文章《在谷歌云服务器上搭建深度学习平台》,链接:https://www.jianshu.com/p/893d622d1b5a
数据集下载链接: https://pan.baidu.com/s/1oLZZF4AHT5X_bzNl2aF2aQ 提取码: 5sea
下载压缩文件cnews.zip完成后,选择解压到cnews,如下图所示:
代码文件需要放到和cnews文件夹同级目录。
给读者提供完整代码,旨在读者能够直接运行代码,有直观的感性认识。
如果要理解其中代码的细节,请阅读后面的章节。
- with open('./cnews/cnews.train.txt', encoding='utf8') as file:
- line_list = [k.strip() for k in file.readlines()]
- train_label_list = [k.split()[0] for k in line_list]
- train_content_list = [k.split(maxsplit=1)[1] for k in line_list]
- with open('./cnews/cnews.vocab.txt', encoding='utf8') as file:
- vocabulary_list = [k.strip() for k in file.readlines()]
- word2id_dict = dict([(b, a) for a, b in enumerate(vocabulary_list)])
- content2idList = lambda content : [word2id_dict[word] for word in content if word in word2id_dict]
- train_idlist_list = [content2idList(content) for content in train_content_list]
- vocab_size = 5000 # 词汇表大小
- seq_length = 600 # 序列长度
- embedding_dim = 64 # 词向量维度
- num_classes = 10 # 类别数
- num_filters = 256 # 卷积核数目
- kernel_size = 5 # 卷积核尺寸
- hidden_dim = 128 # 全连接层神经元
- dropout_keep_prob = 0.5 # dropout保留比例
- learning_rate = 1e-3 # 学习率
- batch_size = 64 # 每批训练大小
- import tensorflow.contrib.keras as kr
- train_X = kr.preprocessing.sequence.pad_sequences(train_idlist_list, seq_length)
- from sklearn.preprocessing import LabelEncoder
- labelEncoder = LabelEncoder()
- train_y = labelEncoder.fit_transform(train_label_list)
- train_Y = kr.utils.to_categorical(train_y, num_classes)
- import tensorflow as tf
- tf.reset_default_graph()
- X_holder = tf.placeholder(tf.int32, [None, seq_length])
- Y_holder = tf.placeholder(tf.float32, [None, num_classes])
-
- embedding = tf.get_variable('embedding', [vocab_size, embedding_dim])
- embedding_inputs = tf.nn.embedding_lookup(embedding, X_holder)
- conv = tf.layers.conv1d(embedding_inputs, num_filters, kernel_size)
- max_pooling = tf.reduce_max(conv, reduction_indices=[1])
- full_connect = tf.layers.dense(max_pooling, hidden_dim)
- full_connect_dropout = tf.contrib.layers.dropout(full_connect, keep_prob=0.75)
- full_connect_activate = tf.nn.relu(full_connect_dropout)
- softmax_before = tf.layers.dense(full_connect_activate, num_classes)
- predict_Y = tf.nn.softmax(softmax_before)
- cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(labels=Y_holder, logits=softmax_before)
- loss = tf.reduce_mean(cross_entropy)
- optimizer = tf.train.AdamOptimizer(learning_rate)
- train = optimizer.minimize(loss)
- isCorrect = tf.equal(tf.argmax(Y_holder, 1), tf.argmax(predict_Y, 1))
- accuracy = tf.reduce_mean(tf.cast(isCorrect, tf.float32))
-
- init = tf.global_variables_initializer()
- session = tf.Session()
- session.run(init)
-
- with open('./cnews/cnews.test.txt', encoding='utf8') as file:
- line_list = [k.strip() for k in file.readlines()]
- test_label_list = [k.split()[0] for k in line_list]
- test_content_list = [k.split(maxsplit=1)[1] for k in line_list]
- test_idlist_list = [content2idList(content) for content in test_content_list]
- test_X = kr.preprocessing.sequence.pad_sequences(test_idlist_list, seq_length)
- test_y = labelEncoder.transform(test_label_list)
- test_Y = kr.utils.to_categorical(test_y, num_classes)
- import random
- for i in range(3000):
- selected_index = random.sample(list(range(len(train_y))), k=batch_size)
- batch_X = train_X[selected_index]
- batch_Y = train_Y[selected_index]
- session.run(train, {X_holder:batch_X, Y_holder:batch_Y})
- step = i + 1
- if step % 100 == 0:
- selected_index = random.sample(list(range(len(test_y))), k=200)
- batch_X = test_X[selected_index]
- batch_Y = test_Y[selected_index]
- loss_value, accuracy_value = session.run([loss, accuracy], {X_holder:batch_X, Y_holder:batch_Y})
- print('step:%d loss:%.4f accuracy:%.4f' %(step, loss_value, accuracy_value))
上面一段代码的运行结果如下(只截取前十行):
step:100 loss:0.8617 accuracy:0.7500
step:200 loss:0.4324 accuracy:0.8700
step:300 loss:0.2810 accuracy:0.9150
step:400 loss:0.1910 accuracy:0.9500
step:500 loss:0.2348 accuracy:0.9350
step:600 loss:0.2127 accuracy:0.9300
step:700 loss:0.2282 accuracy:0.9400
step:800 loss:0.1539 accuracy:0.9550
step:900 loss:0.1268 accuracy:0.9750
step:1000 loss:0.1339 accuracy:0.9600
第1-4行代码是加载训练集的数据;
第1行代码调用open方法打开文本文件;
第2行代码使用列表推导式得到文本文件中的行内容列表赋值给变量label_list;
第3行代码得到训练集的标签列表赋值给变量train_label_list;
第4行代码得到训练集的内容列表赋值给变量train_content_list。
第5-6行代码得到词汇表文件cnews.vocab.txt中的词汇列表赋值给变量vocabulary_list;
第7行代码使用列表推导式得到词汇及其id对应的列表,并调用dict方法将列表强制转换为字典。
打印变量word2id_dict的前5项,如下图所示:
- with open('./cnews/cnews.train.txt', encoding='utf8') as file:
- line_list = [k.strip() for k in file.readlines()]
- train_label_list = [k.split()[0] for k in line_list]
- train_content_list = [k.split(maxsplit=1)[1] for k in line_list]
- with open('./cnews/cnews.vocab.txt', encoding='utf8') as file:
- vocabulary_list = [k.strip() for k in file.readlines()]
- word2id_dict = dict([(b, a) for a, b in enumerate(vocabulary_list)])
- content2idList = lambda content : [word2id_dict[word] for word in content if word in word2id_dict]
- train_idlist_list = [content2idList(content) for content in train_content_list]
- vocab_size = 5000 # 词汇表大小
- seq_length = 600 # 序列长度
- embedding_dim = 64 # 词向量维度
- num_classes = 10 # 类别数
- num_filters = 256 # 卷积核数目
- kernel_size = 5 # 卷积核尺寸
- hidden_dim = 128 # 全连接层神经元
- dropout_keep_prob = 0.5 # dropout保留比例
- learning_rate = 1e-3 # 学习率
- batch_size = 64 # 每批训练大小
- import tensorflow.contrib.keras as kr
- train_X = kr.preprocessing.sequence.pad_sequences(train_idlist_list, seq_length)
- from sklearn.preprocessing import LabelEncoder
- labelEncoder = LabelEncoder()
- train_y = labelEncoder.fit_transform(train_label_list)
- train_Y = kr.utils.to_categorical(train_y, num_classes)
- import tensorflow as tf
- tf.reset_default_graph()
- X_holder = tf.placeholder(tf.int32, [None, seq_length])
- Y_holder = tf.placeholder(tf.float32, [None, num_classes])
第1行代码调用tf库的get_variable方法实例化可以更新的模型参数embedding,矩阵形状为5000*64
;
第2行代码调用tf.nn库的embedding_lookup方法将输入数据做词嵌入,得到新变量embedding_inputs的形状为batch_size*600*64
;
理解word2vec原理,推荐阅读文章链接:https://www.jianshu.com/p/471d9bfbd72f
第3行代码调用tf.layers.conv1d方法,方法需要3个参数,第1个参数是输入数据,第2个参数是卷积核数量num_filters,第3个参数是卷积核大小kernel_size。方法结果赋值给变量conv,形状为batch_size*596*num_filters
,596是600-5+1
的结果;
第4行代码调用tf.reduce_max方法对变量conv的第1个维度做求最大值操作。方法结果赋值给变量max_pooling,形状为batch_size*256
;
第5行代码添加全连接层,tf.layers.dense方法结果赋值给变量full_connect,形状为batch_size*128
;
第6行代码调用tf.contrib.layers.dropout方法,方法需要2个参数,第1个参数是输入数据,第2个参数是保留比例;
第7行代码调用tf.nn.relu方法,即激活函数;
第8行代码添加全连接层,tf.layers.dense方法结果赋值给变量softmax_before,形状为batch_size*num_classes
;
第9行代码调用tf.nn.softmax方法,方法结果是预测概率值;
第10、11行代码使用交叉熵作为损失函数;
第12行代码调用tf.train.Optimizer方法定义优化器optimizer;
第13行代码调用优化器对象的minimize方法,即最小化损失;
第14、15行代码计算预测准确率;
- embedding = tf.get_variable('embedding', [vocab_size, embedding_dim])
- embedding_inputs = tf.nn.embedding_lookup(embedding, X_holder)
- conv = tf.layers.conv1d(embedding_inputs, num_filters, kernel_size)
- max_pooling = tf.reduce_max(conv, reduction_indices=[1])
- full_connect = tf.layers.dense(max_pooling, hidden_dim)
- full_connect_dropout = tf.contrib.layers.dropout(full_connect, keep_prob=0.75)
- full_connect_activate = tf.nn.relu(full_connect_dropout)
- softmax_before = tf.layers.dense(full_connect_activate, num_classes)
- predict_Y = tf.nn.softmax(softmax_before)
- cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(labels=Y_holder, logits=softmax_before)
- loss = tf.reduce_mean(cross_entropy)
- optimizer = tf.train.AdamOptimizer(learning_rate)
- train = optimizer.minimize(loss)
- isCorrect = tf.equal(tf.argmax(Y_holder, 1), tf.argmax(predict_Y, 1))
- accuracy = tf.reduce_mean(tf.cast(isCorrect, tf.float32))
对于神经网络模型,重要是其中的W、b这两个参数。
开始神经网络模型训练之前,这两个变量需要初始化。
第1行代码调用tf.global_variables_initializer实例化tensorflow中的Operation对象。
第2行代码调用tf.Session方法实例化会话对象;
第3行代码调用tf.Session对象的run方法做变量初始化。
- init = tf.global_variables_initializer()
- session = tf.Session()
- session.run(init)
第1-8行代码获取文本文件cnews.test.txt,即测试集中的数据;
第9行代码导入random库;
第10行表示模型迭代训练3000次;
第11-13行代码从训练集中选取batch_size大小,即64个样本做批量梯度下降;
第14行代码每运行1次,表示模型训练1次;
第15行代码记录当前步数,赋值给变量step;
第16行代码表示每间隔100步打印;
第17-19行代码从测试集中随机选取200个样本;
第20行代码表示计算损失值loss_value、准确率accuracy_value;
第21行代码表示打印步数step、损失值loss_value、准确率accuracy_value。
- with open('./cnews/cnews.test.txt', encoding='utf8') as file:
- line_list = [k.strip() for k in file.readlines()]
- test_label_list = [k.split()[0] for k in line_list]
- test_content_list = [k.split(maxsplit=1)[1] for k in line_list]
- test_idlist_list = [content2idList(content) for content in test_content_list]
- test_X = kr.preprocessing.sequence.pad_sequences(test_idlist_list, seq_length)
- test_y = labelEncoder.transform(test_label_list)
- test_Y = kr.utils.to_categorical(test_y, num_classes)
- import random
- for i in range(3000):
- selected_index = random.sample(list(range(len(train_y))), k=batch_size)
- batch_X = train_X[selected_index]
- batch_Y = train_Y[selected_index]
- session.run(train, {X_holder:batch_X, Y_holder:batch_Y})
- step = i + 1
- if step % 100 == 0:
- selected_index = random.sample(list(range(len(test_y))), k=200)
- batch_X = test_X[selected_index]
- batch_Y = test_Y[selected_index]
- loss_value, accuracy_value = session.run([loss, accuracy], {X_holder:batch_X, Y_holder:batch_Y})
- print('step:%d loss:%.4f accuracy:%.4f' %(step, loss_value, accuracy_value))
经过前文5-8章的讲解,已经完成卷积神经网络的训练。
本项目提供词汇表文件cnews.vocab.txt,但在实践中需要自己统计语料的词汇表。
下面代码可以由内容列表content_list产生词汇表:
- from collections import Counter
-
- def getVocabularyList(content_list, vocabulary_size):
- allContent_str = ''.join(content_list)
- counter = Counter(allContent_str)
- vocabulary_list = [k[0] for k in counter.most_common(vocabulary_size)]
- return vocabulary_list
-
- def makeVocabularyFile(content_list, vocabulary_size):
- vocabulary_list = getVocabularyList(content_list, vocabulary_size)
- with open('vocabulary.txt', 'w', encoding='utf8') as file:
- for vocabulary in vocabulary_list:
- file.write(vocabulary + '\n')
-
- makeVocabularyFile(train_content_list, 5000)
本段代码产生的文件,与提供的词汇表文件cnews.vocab.txt稍有不同。
造成原因有2点:
1.词汇表文件的第1个字<PAD>
是源代码作者的特殊设计,本文作者没有体会到实际用处;
2.源代码作者使用了训练集、验证集、测试集作为总语料库,上面一段代码只使用了训练集作为语料库。
- import numpy as np
- import pandas as pd
- from sklearn.metrics import confusion_matrix
-
- def predictAll(test_X, batch_size=100):
- predict_value_list = []
- for i in range(0, len(test_X), batch_size):
- selected_X = test_X[i: i + batch_size]
- predict_value = session.run(predict_Y, {X_holder:selected_X})
- predict_value_list.extend(predict_value)
- return np.array(predict_value_list)
-
- Y = predictAll(test_X)
- y = np.argmax(Y, axis=1)
- predict_label_list = labelEncoder.inverse_transform(y)
- pd.DataFrame(confusion_matrix(test_label_list, predict_label_list),
- columns=labelEncoder.classes_,
- index=labelEncoder.classes_ )
上面一段代码的运行结果如下图所示:
从上图的结果可以看出,家居类新闻分类效果较差。
下面一段代码能够成功运行的前提是已经运行第10章代码。
- import numpy as np
- from sklearn.metrics import precision_recall_fscore_support
-
- def eval_model(y_true, y_pred, labels):
- # 计算每个分类的Precision, Recall, f1, support
- p, r, f1, s = precision_recall_fscore_support(y_true, y_pred)
- # 计算总体的平均Precision, Recall, f1, support
- tot_p = np.average(p, weights=s)
- tot_r = np.average(r, weights=s)
- tot_f1 = np.average(f1, weights=s)
- tot_s = np.sum(s)
- res1 = pd.DataFrame({
- u'Label': labels,
- u'Precision': p,
- u'Recall': r,
- u'F1': f1,
- u'Support': s
- })
- res2 = pd.DataFrame({
- u'Label': ['总体'],
- u'Precision': [tot_p],
- u'Recall': [tot_r],
- u'F1': [tot_f1],
- u'Support': [tot_s]
- })
- res2.index = [999]
- res = pd.concat([res1, res2])
- return res[['Label', 'Precision', 'Recall', 'F1', 'Support']]
-
- eval_model(test_label_list, predict_label_list, labelEncoder.classes_)
上面一段代码的运行结果如下图所示:
本文是作者第4个NLP小型项目,数据共有65000条。
分类模型的评估指标为0.96左右,总体来说这个分类模型较优秀,能够投入实际应用。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。