继续。
# 确定序列的长度
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
也没有报错。
接下来是一连串的name_scope,也就是设置命名空间。
命名空间的作用是什么呢?
- 在某个tf.name_scope()指定的区域中定义的所有对象及各种操作,他们的“name”属性上会增加该命名区的区域名,用以区别对象属于哪个区域;
- 将不同的对象及操作放在由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中,我们可以看到,
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,只有一个通道,但要加上。
接下来进入大家最喜欢的淘宝推荐环节。。。哦不,卷积池化层。
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[]列表中,卷积池化图构建完成!