利⽤卷积神经⽹络(cnn)实现⽂本分类
卷积神经⽹络在情感分析中取得了很好的成果,相⽐于之前浅层的机器学习⽅法如NB、SVM效果更好,特别实在数据集较⼤的情况下,并且CNN不⽤我们⼿动去提取特征,原浅层ML是需要进⾏⽂本特征提取、⽂本特征表⽰、归⼀化、最后进⾏⽂本分类,⽂本特征提取主要可以分为四步:(1):对全部训练⽂档进⾏分词,由这些词作为向量的维数来表⽰⽂本;(2):统计每⼀类⽂档中所有出现的词语及其频率,然后过滤,剔除停⽤词和单字词;(3):统计每⼀类内出现词语的总词频,并取若⼲个频率更⾼的词汇作为这⼀类的特征词集;(4):去除每⼀类别中都出现的词,合并所有类别的特征词集,形成总特征词集,最后得到的特征词集是我们⽤到的特征集合,再⽤该集合去筛选测试集中的特征。⽂本的特征表⽰是利⽤TF-IDF公式来计算词的权值,这也充分利⽤的是特征提取时提取的特征来计算特征权值⼤⼩的,归⼀化处理需要处理的数据,经过处理后限制在⼀定范围内,经过处理后,我们原来的⽂本信息已经抽象成⼀个向量化的样本集,然后将样本集和训练好的模板进⾏相似度计算,若属于该类别,则与其他类别的模板⽂件进⾏计算,直到分进相应的类别,这是浅层ML进⾏⽂本分类的⽅式;
CNN进⾏⽂本分类相对简单⼀些,我结合最近做的⼀些实验总结了⼀下:
在利⽤CNN进⾏⽂本分类的时候,⾸先要将原始⽂本进⾏预处理,主要还是分词、去除停⽤词等,然后对预处理后的⽂本进⾏向量化利⽤word2vec,我利⽤的时word2vec中的skip-gram模型,将搜狗数据集
表⽰为了200维的词向量形式;转化为词向量后就可以将每⼀句话转化为⼀个矩阵的形式,这样就跟利⽤CNN处理图像分类很相似;
说⼀下实验,我的实验环境:
tensorflow1.2、gpu1050Ti、Ubuntu16.04、pycharm、python2.7
# encoding=utf-8
from __future__ import unicode_literals
import tensorflow as tf
import numpy as np
class TextCNN(object):
"""
使⽤CNN⽤于情感分析
整个CNN架构包括词嵌⼊层,卷积层,max-pooling层和softmax层
"""
def __init__(
self, sequence_length, num_classes,vocab_size,embedding_size, embedding_table,
filter_sizes, num_filters, l2_reg_lambda=0.0):
# 输⼊,输出,dropout的placeholder
self.input_x = tf.placeholder(tf.int32, [None, sequence_length], name="input_x")
self.input_y = tf.placeholder(tf.float32, [None, num_classes], name="input_y")
self.dropout_keep_prob = tf.placeholder(tf.float32, name="dropout_keep_prob")
# Keeping track of l2 regularization loss (optional)
l2_loss = tf.constant(0.0)
# 词嵌⼊层
with tf.device('/cpu:0'), tf.name_scope("embedding"):
W = tf.Variable(embedding_table,name="W")
#embedding_table就是词向量表,W还有另⼀种简单的表达
#W=tf.Variable([vocab_size,embedding_size],-1.0,1.0),随机初始化这个词向量表;
#这个bedding_lookup()的作⽤就是从词向量表中去input_x所对应的词向量;
#由于CNN输⼊都是四维,所以在最后⼀维添加⼀个维度,与CNN的输⼊维度对照起来。
# ⽣成卷积层和max-pooling层
pooled_outputs = []
for i, filter_size in enumerate(filter_sizes):
with tf.name_scope("conv-maxpool-%s" % filter_size):
# Convolution Layer
filter_shape = [filter_size, embedding_size, 1, num_filters]
W = tf.uncated_normal(filter_shape, stddev=0.1), name="W")
b = tf.stant(0.1, shape=[num_filters]), name="b")
conv = v2d(
W,
strides=[1, 1, 1, 1],
padding="VALID",
name="conv")variable used in lambda
# Apply nonlinearity
# h = bias_add(conv, b), name="relu")
bias_add(conv,b),name="relu")
# Maxpooling over the outputs
# pooled = tf.nn.max_pool(
# h,
# ksize=[1, sequence_length - filter_size + 1, 1, 1],
# strides=[1, 1, 1, 1],
# padding='VALID',
# name="pool")
# pooled_outputs.append(pooled)
pooled = tf.nn.avg_pool(
h,
ksize=[1, sequence_length - filter_size + 1, 1, 1],
strides=[1, 1, 1, 1],
padding='VALID',
name="pool")
pooled_outputs.append(pooled)
# 将max-pooling层的各种特征整合在⼀起
num_filters_total = num_filters * len(filter_sizes)
self.h_pool = tf.concat(pooled_outputs,3)
self.h_pool_flat = tf.reshape(self.h_pool, [-1, num_filters_total])
# 添加全连接层,⽤于分类
with tf.name_scope("full-connection"):
W_fc1 = tf.uncated_normal([num_filters_total,500], stddev=0.1))
b_fc1 = tf.stant(0.1,shape=[500]))
self.h_fc1 = lu6(tf.matmul(self.h_pool_flat, W_fc1) + b_fc1)
# 添加dropout层⽤于缓和过拟化
with tf.name_scope("dropout"):
# self.h_drop = tf.nn.dropout(self.h_pool_flat, self.dropout_keep_prob)
self.h_drop = tf.nn.dropout(self.h_fc1, self.dropout_keep_prob)
# 产⽣最后的输出和预测
with tf.name_scope("output"):
# W = tf.get_variable(
# "W",
# shape=[num_filters_total, num_classes],
# ib.layers.xavier_initializer())
W = tf.get_variable(
"W",
shape=[500, num_classes],
ib.layers.xavier_initializer())
b = tf.stant(0.1, shape=[num_classes]), name="b")
l2_loss += tf.nn.l2_loss(W)
l2_loss += tf.nn.l2_loss(b)
self.scores = tf.nn.xw_plus_b(self.h_drop, W, b, name="scores")
self.scores = tf.nn.xw_plus_b(self.h_drop, W, b, name="scores")
self.predictions = tf.argmax(self.scores, 1, name="predictions")
# 定义模型的损失函数
with tf.name_scope("loss"):
losses = tf.nn.softmax_cross_entropy_with_logits(logits=self.scores, labels=self.input_y)
self.loss = tf.reduce_mean(losses) + l2_reg_lambda * l2_loss
# 定义模型的准确率
with tf.name_scope("accuracy"):
correct_predictions = tf.equal(self.predictions, tf.argmax(self.input_y, 1))
self.accuracy = tf.reduce_mean(tf.cast(correct_predictions, "float"), name="accuracy")
以上时TextCNN的模型结构代码,然后开始进⾏train,并利⽤summary和checkpoints来记录模型和训练时的参数等等,利⽤⼗折交叉验证来产⽣准确率,最后利⽤tensorboard查看accuracy、loss、w、b等等变化图;训练py的代码:
#! /usr/bin/env python
# encoding=utf-8
import tensorflow as tf
import numpy as np
import os
import time
import datetime
import data_loader
from cnn_graph import TextCNN
ib import learn
from sklearn import cross_validation
import preprocessing
# tf.global_variables
# 伴随tensorflow的summary和checkout
# ==================================================
# Model Hyperparameters
tf.flags.DEFINE_integer("embedding_dim", 200, "Dimensionality of character embedding (default: 128)")
tf.flags.DEFINE_string("filter_sizes", "3,4,5", "Comma-separated filter sizes (default: '3,4,5')")
tf.flags.DEFINE_integer("num_filters", 40, "Number of filters per filter size (default: 128)")
tf.flags.DEFINE_float("dropout_keep_prob", 0.5, "Dropout keep probability (default: 0.5)")
tf.flags.DEFINE_float("l2_reg_lambda", 3.0, "L2 regularizaion lambda (default: 0.0)")
# Training parameters
tf.flags.DEFINE_integer("batch_size", 50, "Batch Size (default: 64)")
tf.flags.DEFINE_integer("num_epochs", 100, "Number of training epochs (default: 200)")
tf.flags.DEFINE_integer("evaluate_every", 100, "Evaluate model on dev set after this many steps (default: 100)")
tf.flags.DEFINE_integer("checkpoint_every", 100, "Save model after this many steps (default: 100)")
# Misc Parameters
tf.flags.DEFINE_boolean("allow_soft_placement", True, "Allow device soft device placement")
tf.flags.DEFINE_boolean("log_device_placement", False, "Log placement of ops on devices")
# w2v⽂件路径
tf.flags.DEFINE_string("w2v_path", "./w2v_model/retrain_vectors_100.bin", "w2v file")
tf.flags.DEFINE_string("file_dir","./data_process/jd","train/test dataSet")
FLAGS = tf.flags.FLAGS
FLAGS._parse_flags()
print("\n Parameters:")
for attr, value in sorted(FLAGS.__flags.items()):
print("{}={}".format(attr.upper(), value))
print("")
# Data Preparatopn
# ==================================================
# Load data
# Load data
print("")
files = ["","reviews.pos"]
# 加载所有的未切分的数据
x_text, y_labels,neg_examples,pos_examples = data_loader.\
load_data_and_labels(data_dir=FLAGS.file_dir,files=files,splitable=False)
# 获取消极数据的2/3,得到的评论的长度离散度更低
neg_accept_length = preprocessing.freq_factor(neg_examples,
percentage=0.8, drawable=False)
neg_accept_length = [item[0] for item in neg_accept_length]
neg_examples = data_loader.load_data_by_length(neg_examples,neg_accept_length)
# 获取积极数据的2/3,得到的评论的长度离散度更低
pos_accept_length = preprocessing.freq_factor(pos_examples,
percentage=0.8, drawable=False)
pos_accept_length = [item[0] for item in pos_accept_length]
pos_examples = data_loader.load_data_by_length(pos_examples,pos_accept_length)
x_text = neg_examples + pos_examples
neg_labels = [[1,0] for _ in neg_examples]
pos_labels = [[0,1] for _ in pos_examples]
y_labels = np.concatenate([neg_labels,pos_labels], axis=0)
print("Loading data finish")
# Build vocabulary
max_document_length = max([len(x.split(" ")) for x in x_text]) # 最长的句⼦的长度
print(max_document_length)
vocab_processor = learn.preprocessing.VocabularyProcessor(max_document_length)
x = np.array(list(vocab_processor.fit_transform(x_text)))
# 加载提前训练的w2v数据集
word_vecs = data_loader.load_bin_vec(fname=FLAGS.w2v_path,
vocab=list(vocab_processor.vocabulary_._mapping),
bedding_dim)
# 加载嵌⼊层的table
W = _W(word_vecs=word_vecs,
vocab_ids_map=vocab_processor.vocabulary_._mapping,
bedding_dim,is_rand=False)
# 随机化数据
np.random.seed(10)
shuffle_indices = np.random.permutation(np.arange(len(y_labels)))
x_shuffled = x[shuffle_indices]
y_shuffled = y_labels[shuffle_indices]
out_path = os.path.abspath(os.path.join(os.path.curdir, "runs","parameters"))
parameters = "新全连接+jd数据+10\n" \
"embedding_dim: {},\n" \
"filter_sizes:{},\n" \
"num_filters:{},\n" \
"dropout_keep_prob:{},\n" \
"l2_reg_lambda:{},\n" \
"num_epochs:{},\n" \
"batch_size:{}".bedding_dim,FLAGS.filter_sizes,FLAGS.num_filters, FLAGS.dropout_keep_prob,FLAGS.l2_reg_lambda,FLAGS.num_epochs, FLAGS.batch_size)
open(out_path, 'w').write(parameters)
# Training
# ==================================================
def train(X_train, X_dev, x_test, y_train, y_dev, y_test):
with tf.Graph().as_default():
session_conf = tf.ConfigProto(
allow_soft_placement=FLAGS.allow_soft_placement,
log_device_placement=FLAGS.log_device_placement)
sess = tf.Session(config=session_conf)
log_device_placement=FLAGS.log_device_placement)
sess = tf.Session(config=session_conf)
with sess.as_default():
cnn = TextCNN(
sequence_length=max_document_length,
num_classes=2,
vocab_size=len(vocab_processor.vocabulary_),
embedding_bedding_dim,
embedding_table=W,
filter_sizes=list(map(int, FLAGS.filter_sizes.split(","))),
num_filters=FLAGS.num_filters,
l2_reg_lambda=FLAGS.l2_reg_lambda)
# Define Training procedure
global_step = tf.Variable(0, name="global_step", trainable=False)
optimizer = tf.train.AdamOptimizer(1e-3)
grads_and_vars = optimizerpute_gradients(cnn.loss)
train_op = optimizer.apply_gradients(grads_and_vars, global_step=global_step)
# Keep track of gradient values and sparsity (optional)
grad_summaries = []
for g, v in grads_and_vars:
if g is not None:
grad_hist_summary = tf.summary.histogram("{}/grad/hist".format(v.name), g)
sparsity_summary = tf.summary.scalar("{}/grad/sparsity".format(v.name), _fraction(g)) grad_summaries.append(grad_hist_summary)
grad_summaries.append(sparsity_summary)
grad_summaries_merged = (grad_summaries)
# Output directory for models and summaries
timestamp = str(int(time.time()))
out_dir = os.path.abspath(os.path.join(os.path.curdir, "runs", timestamp))
print("Writing to {}\n".format(out_dir))
# Summaries for loss and accuracy
loss_summary = tf.summary.scalar("loss", cnn.loss)
acc_summary = tf.summary.scalar("accuracy", cnn.accuracy)
# Train Summaries
train_summary_op = ([loss_summary, acc_summary, grad_summaries_merged]) train_summary_dir = os.path.join(out_dir, "summaries", "train")
train_summary_writer = tf.summary.FileWriter(train_summary_dir, aph)
# Dev summaries
dev_summary_op = ([loss_summary, acc_summary])
dev_summary_dir = os.path.join(out_dir, "summaries", "dev")
dev_summary_writer = tf.summary.FileWriter(dev_summary_dir, aph)
# Checkpoint directory. Tensorflow assumes this directory already exists so we need to create it
checkpoint_dir = os.path.abspath(os.path.join(out_dir, "checkpoints"))
checkpoint_prefix = os.path.join(checkpoint_dir, "model")
if not ists(checkpoint_dir):
os.makedirs(checkpoint_dir)
saver = tf.train.Saver(tf.global_variables())
# Write vocabulary
vocab_processor.save(os.path.join(out_dir, "vocab"))
# Initialize all variables
# sess.run(tf.initialize_all_variables())
sess.run(tf.global_variables_initializer())
def train_step(x_batch, y_batch):
"""
A single training step
"""
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论