textcnn用于文本分类详解(四)

继续。

# 确定序列的长度
sequence_length = train_input.shape[1]
print('该训练集中词汇表大小:{:d}'.format(vocabulary_size))
print('一个句子序列的长度为:{:d}'.format(sequence_length))

# 构建数据流图
graph = tf.Graph()
with graph.as_default():
    # 训练数据
    with tf.name_scope('inputs'):
        inputs = tf.placeholder(tf.int32, [None, sequence_length], name='inputs')
    with tf.name_scope('labels'):
        labels = tf.placeholder(tf.float32, [None, classes_num], name='labels')
    with tf.name_scope('keep_prob'):
        keep_prob = tf.placeholder(tf.float32, name='keep_prob')
    with tf.name_scope('l2_loss'):
        l2_loss = tf.constant(0.0, tf.float32, name='l2_loss')

    # 词向量
    with tf.device('/cpu:0'):
        with tf.name_scope('embedding_layer'):
            # 词嵌入库
            embeddings = tf.Variable(tf.random_normal([vocabulary_size, embedding_size], -1.0, 1.0), name='embeddings')
            # 输入数据是每个句子的单词的索引id,则就可以直接查表,得到改词的词向量
            embed = tf.nn.embedding_lookup(embeddings, inputs, name='embed')
            # 作为卷积的直接输入。卷积要求必须有通道数,虽然文本的厚度为1,只有一个通道,但要加上
            conv_inputs = tf.expand_dims(embed, -1)

        with tf.name_scope('conv_pooling_layer'):
            # 存储处理好后的特征,注意feature要加s,不要混淆
            features_pooled = []
            for filter_height, filter_num in zip(filters_height, filter_num_per_height):
                with tf.name_scope('conv_filter'):
                    # 卷积核四个维度[高,宽,通道,个数]
                    conv_filter = tf.Variable(tf.truncated_normal([filter_height, embedding_size, 1, filter_num], stddev=0.1), name='conv_filer')
                # 卷积操作
                with tf.name_scope('conv'):
                    conv = tf.nn.conv2d(conv_inputs, conv_filter, strides=[1, 1, 1, 1], padding='VALID', name='conv')
                # 偏置,一个滤波器对应一个偏置
                with tf.name_scope('bias'):
                    bias = tf.Variable(tf.constant(0.1, shape=[filter_num]))
                # 非线性,Relu
                with tf.name_scope('Relu'):
                    feature_map = tf.nn.relu(tf.nn.bias_add(conv, bias), name='Relu')
                # 池化
                # tf.nn.max_pool(value,ksize,strides,padding)
                # value: 4维张量;ksize:包含4个元素的1维张量,对应输入张量每一维度窗口的大小,就是kernel size;
                with tf.name_scope('max_pooling'):
                    feature_pooled = tf.nn.max_pool(feature_map, ksize=[1, sequence_length-filter_height+1, 1, 1],
                                                    strides=[1, 1, 1, 1], padding='VALID', name='max_pooling')
                features_pooled.append(feature_pooled)

        with tf.name_scope('full_connected_layer'):
            filter_num_total = sum(filter_num_per_height)
            # 就是平铺,tf.concat(features_pooled, 3):第4个维度进行拼接
            features_pooled_flat = tf.reshape(tf.concat(features_pooled, 3), [-1, filter_num_total])
            # 该层要dropout
            with tf.name_scope('drop_out'):
                features_pooled_flat_drop = tf.nn.dropout(features_pooled_flat, keep_prob=keep_prob, name='drop_out')
            with tf.name_scope('weight'):
                weight = tf.Variable(tf.truncated_normal(shape=[filter_num_total, classes_num], dtype=tf.float32), name='weight')
                tf.summary.histogram('weight', weight)
            with tf.name_scope('bias'):
                bias = tf.Variable(tf.constant(0.1, shape=[classes_num]), name='bias')
                tf.summary.histogram('bias', bias)
            # L2范数正则化
            with tf.name_scope('L2'):
                l2_loss += tf.nn.l2_loss(weight)
                l2_loss += tf.nn.l2_loss(bias)
            # xw_plus_b
            with tf.name_scope('xw_plus_b'):
                scores = tf.nn.xw_plus_b(features_pooled_flat_drop, weight, bias, name='xw_plus_b')
                tf.summary.histogram('xw_plus_b', scores)
                # 保存每个标签值的得分,以便在预测时候使用。将预测值放入该列表中
                tf.add_to_collection('pred_network', scores)
            # cross_entropy loss
            with tf.name_scope('softmax_cross_entropy'):
                losses = tf.nn.softmax_cross_entropy_with_logits_v2(labels=labels, logits=scores, name='losses')
            # loss, is a scalar
            with tf.name_scope('train_loss'):
                train_loss = tf.reduce_mean(losses) + l2_lambda * l2_loss
                tf.summary.scalar('train_loss', train_loss)
            with tf.name_scope('test_loss'):
                test_loss = tf.reduce_mean(losses) + l2_lambda * l2_loss
                tf.summary.scalar('test_loss', test_loss)

            # 预测
            with tf.name_scope('prediction'):
                predictions = tf.argmax(scores, 1)
                correct_predictions = tf.equal(predictions, tf.argmax(labels, 1), name='correct_predictions')
            # accuracy
            with tf.name_scope('train_accuracy'):
                train_accuracy = tf.reduce_mean(tf.cast(correct_predictions, 'float'))
                tf.summary.scalar('train_accuracy', train_accuracy)
            # accuracy
            with tf.name_scope('test_accuracy'):
                test_accuracy = tf.reduce_mean(tf.cast(correct_predictions, 'float'))
                tf.summary.scalar('test_accuracy', test_accuracy)

这一段主要是讲构建网络结构图,也就是搭建模型。框架的搭建在神经网络中是很重要的。
首先我们看到这一句:

graph = tf.Graph()
with graph.as_default():

我们经常听人说构建图模型,就我目前的理解,什么是Tensorflow呢?中文意思是张量的流动,何为"张量"?通俗来讲就是数据,各种数据。你可以把一个张量想象成一个n维的数组或列表,一个张量有一个静态类型和动态类型的维数,张量可以在图的结点之间流动。
张量是所有深度学习框架中最核心的组件,因为后续的所有运算和优化算法与数据结构知识库都是基于张量进行的。
举例来说,我们可以将任意一张RGB彩色图片表示成一个三阶张量(三个维度分别是图片的高度、宽度和色彩数据)
张量,在哪流动呢?怎么流动呢?怎么流动是第三步,且让我们把第二步慢慢走好。在哪流动,我的理解是在里流动,如果把进行深度学习训练比作画一幅画,那么图就相当于画布,在代码中添加的操作(画中的结点)和数据(画中的线条)都是画在布上的“画”。
tf.Graph() 表示实例化了一个类,一个用于 tensorflow 计算和表示用的数据流图。
TensorFlow的官方文档给了两个构建图模型的方法:

import tensorflow as tf
c = tf.constant(5.0)
# 看看主程序新建的一个变量是不是在默认图里
assert c.graph is tf.get_default_graph()

最终没有报错。

import tensorflow as tf
graph = tf.Graph
with g.as_default():
  c = constant(5.0)
  assert c.graph is g

也没有报错。


image.png

接下来是一连串的name_scope,也就是设置命名空间。
命名空间的作用是什么呢?

  1. 在某个tf.name_scope()指定的区域中定义的所有对象及各种操作,他们的“name”属性上会增加该命名区的区域名,用以区别对象属于哪个区域;
  2. 将不同的对象及操作放在由tf.name_scope()指定的区域中,便于在tensorboard中展示清晰的逻辑关系图,这点在复杂关系图中特别重要。
    在我的理解就相当于构建了几个文件夹,然后各个细分的具体文件存放在在对应文件夹中,便于组织和管理。
    举个栗子。
import tensorflow as tf;  
tf.reset_default_graph()

# 无tf.name_scope()
a = tf.constant(1,name='my_a') #定义常量
b = tf.Variable(2,name='my_b') #定义变量
c = tf.add(a,b,name='my_add') #二者相加(操作)
print("a.name = "+a.name)
print("b.name = "+b.name)
print("c.name = "+c.name)

# 有tf.name_scope()
# with tf.name_scope('cgx_name_scope'): #定义一块名为cgx_name_scope的区域,并在其中工作
#     a = tf.constant(1,name='my_a')
#     b = tf.Variable(2,name='my_b')
#     c = tf.add(a,b,name='my_add')
# print("a.name = "+a.name)
# print("b.name = "+b.name)
# print("c.name = "+c.name)

# 保存graph用于tensorboard绘图
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    writer = tf.summary.FileWriter("./test",sess.graph)
    print(sess.run(c))
writer.close()

输出结果:

# 输出结果
# 无tf.name_scope()
a.name = my_a:0
b.name = my_b:0
c.name = my_add:0

# 有tf.name_scope()
a.name = cgx_name_scope/my_a:0
b.name = cgx_name_scope/my_b:0
c.name = cgx_name_scope/my_add:0

于是,在tensorboard中,我们可以看到,


image.png

OK,书归正传,我们在默认的图模型里构建了几个命名空间inouts、labels、keep_prob和l2_loss。
tf.device('/cpu:0')指定在第1块cpu上运行程序。
接下来在embedding_layers这一命名域中建立词向量。

    with tf.device('/cpu:0'):
        with tf.name_scope('embedding_layer'):
            # 词嵌入库
            embeddings = tf.Variable(tf.random_normal([vocabulary_size, embedding_size], -1.0, 1.0), name='embeddings')
            # 输入数据是每个句子的单词的索引id,则就可以直接查表,得到改词的词向量
            embed = tf.nn.embedding_lookup(embeddings, inputs, name='embed')
            # 作为卷积的直接输入。卷积要求必须有通道数,虽然文本的厚度为1,只有一个通道,但要加上
            conv_inputs = tf.expand_dims(embed, -1)

首先随机初始化一个[vocabulary_size, embedding_size]维度的词向量。
输入数据inputs是每个句子中每个单词的索id,通过embedding_lookup函数查表,得到该词的词向量。
tf.expend_dims对卷积后的数据进行升维,-1表示增加一维。

卷积要求必须有通道数,虽然文本的厚度为1,只有一个通道,但要加上。


image.png

接下来进入大家最喜欢的淘宝推荐环节。。。哦不,卷积池化层。

        with tf.name_scope('conv_pooling_layer'):
            # 存储处理好后的特征,注意feature要加s,不要混淆
            features_pooled = []
            for filter_height, filter_num in zip(filters_height, filter_num_per_height):
                with tf.name_scope('conv_filter'):
                    # 卷积核四个维度[高,宽,通道,个数]
                    conv_filter = tf.Variable(tf.truncated_normal([filter_height, embedding_size, 1, filter_num], stddev=0.1), name='conv_filer')
                # 卷积操作
                with tf.name_scope('conv'):
                    conv = tf.nn.conv2d(conv_inputs, conv_filter, strides=[1, 1, 1, 1], padding='VALID', name='conv')
                # 偏置,一个滤波器对应一个偏置
                with tf.name_scope('bias'):
                    bias = tf.Variable(tf.constant(0.1, shape=[filter_num]))
                # 非线性,Relu
                with tf.name_scope('Relu'):
                    feature_map = tf.nn.relu(tf.nn.bias_add(conv, bias), name='Relu')
                # 池化
                # tf.nn.max_pool(value,ksize,strides,padding)
                # value: 4维张量;ksize:包含4个元素的1维张量,对应输入张量每一维度窗口的大小,就是kernel size;
                with tf.name_scope('max_pooling'):
                    feature_pooled = tf.nn.max_pool(feature_map, ksize=[1, sequence_length-filter_height+1, 1, 1],
                                                    strides=[1, 1, 1, 1], padding='VALID', name='max_pooling')
                features_pooled.append(feature_pooled)

其中,filter_height,filter_num在前面已经定义过了。

filters_height = [2, 3, 4]
filter_num_per_height = [100, 100, 100]

这里是把filter_height,filter_num分别打包了一下,也就是在for循环里,构建了三个高度分别为2,3,4,数量分别为100,100,100的卷积核。

                with tf.name_scope('conv_filter'):
                    # 卷积核四个维度[高,宽,通道,个数]
                    conv_filter = tf.Variable(tf.truncated_normal([filter_height, embedding_size, 1, filter_num], stddev=0.1), name='conv_filer')
                # 卷积操作

接下来,在conv_filter这一命名域中,初始化卷积核,我们用tensorflow.truncated_normal命令,生成一个指定形状并用正态分布片段的随机值填充的张量。
卷积核有四个维度:【高、宽、通道、个数】,stddev是标准差,根据这个值来产生正态分布。mean是均值,默认为0,这里我们采用默认项。

                # 卷积操作
                with tf.name_scope('conv'):
                    conv = tf.nn.conv2d(conv_inputs, conv_filter, strides=[1, 1, 1, 1], padding='VALID', name='conv')

接下来我们在conv命名域中定义卷积操作,tensorflow.nn.conv2d是TensorFlow中2维卷积函数,关于此函数的讲解我会放在另一章节。

                # 偏置,一个滤波器对应一个偏置
                with tf.name_scope('bias'):
                    bias = tf.Variable(tf.constant(0.1, shape=[filter_num]))

设置偏置,tensorflow.constant函数将其置位0.1常量。

                # 非线性,Relu
                with tf.name_scope('Relu'):
                    feature_map = tf.nn.relu(tf.nn.bias_add(conv, bias), name='Relu')

这里对偏置处理过的卷积核进行非线性处理并赋值给feature_map,一般使用ReLu函数。

                # 池化
                # tf.nn.max_pool(value,ksize,strides,padding)
                # value: 4维张量;ksize:包含4个元素的1维张量,对应输入张量每一维度窗口的大小,就是kernel size;
                with tf.name_scope('max_pooling'):
                    feature_pooled = tf.nn.max_pool(feature_map, ksize=[1, sequence_length-filter_height+1, 1, 1],
                                                    strides=[1, 1, 1, 1], padding='VALID', name='max_pooling')
                    features_pooled.append(feature_pooled)

池化处理,这里我们采用最大池化技术,tf.nn.max_pool()函数,最后将池化处理过的单元传入我们最初定义的features_pool[]列表中,卷积池化图构建完成!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,542评论 6 504
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,822评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,912评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,449评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,500评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,370评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,193评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,074评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,505评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,722评论 3 335
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,841评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,569评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,168评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,783评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,918评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,962评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,781评论 2 354

推荐阅读更多精彩内容