[专题1]TensorFlow构造神经网络(1)

我们以手写体书 识别为例.  需要使用tensorFlow库: 

#-*-coding:UTF-8-*-

import tensorflow as tf

采用的数据集是 MNIST , 由于tensorFlow 中已经对MNIST 数据集合的封装,所以自己直接写:

from tensorflow.examples.tutorials.mnist import input_data

#加载数据

mnist = input_data.read_data_sets(‘/tmp/data/’,one_hot=True)

以上是把准备工作做好了.

接下来为了更好的说明程序的逻辑, 给出流程图:

《[专题1]TensorFlow构造神经网络(1)》 流程图

Step1: 修改输入数据的结构:

以上这句话就是说 要对inputs的结构做变形(reshape),inputs原有的形状为[-1, 28*28] , 现在要修改为 [-1, 28,28, 1] , 其中-1 是代表数量大小不确定

#把inputs更改为4维张量,第1维代表样本数量,第2维和第3维代表图像长宽, 第4维代表图像通道数, 1表示黑白; 

#如果有维度=-1,代表数量不确定

x = tf.reshape(inputs,[-1,28,28,1])

Step2: 声明超参数

超惨数一般是  学习率和 dropout

learning_rate=0.001 #学习率

dropout=0.75

Step3: 声明所需变量

这部分要根据实际情况分析;在这里我们知道label和dropout将来会在训练的时候使用,所以需要提前声明。

#训练标签数据

labels = tf.placeholder(tf.float32,shape=[None,10])

#dropout

keep_prob = tf.placeholder(tf.float32) # step2中的dropout=0.75 将来会feed 到这里

Step4: 设计CNN结构

把所需要的变量声明完毕以后,接下来我们开始设计卷积神经网络的结构(CNN).

实际上,为了操作方便。tensorflow中常用的做法和论文中描述的不太一样,在卷积过程中并不会像论文中表述的那样卷积后缩小维度,大小不变,但是卷积后会增加”厚度“;但在pooling的时候会降低维度(这点和论文上表述的一样);当然,tensorflow也提供了与论文卷积操作描述一样的函数(详情请见 机器学习沉思录的ConvolutionCommon Layers

4.1 第一部分

4.1.1第一层卷积

conv1_weights = tf.Variable(tf.random_normal([3,3,1,64]))#卷积核大小为3*3,当前层深度为1, 过滤器深度为64

#卷积

conv1 = tf.nn.conv2d(x,conv1_weights,strides=[1,1,1,1],padding=’SAME’)#移动步长为1,使用全0填充

conv1_biases = tf.Variable(tf.random_normal([64]))

#激活函数Relu去线性化

relu1 = tf.nn.relu( tf.nn.bias_add(conv1,conv1_biases) )

#最大池化

pool1 = tf.nn.max_pool(relu1,ksize=[1,2,2,1],strides=[1,2,2,1],padding=’SAME’)#池化层过滤器的大小为2*2,移动步长为2,使用全0填充

#规范化

norm1 = tf.nn.lrn(pool1,depth_radius=4,bias=1.0,alpha=0.001/9.0,beta=0.75)

norm1 = tf.nn.dropout(norm1,keep_prob)

print(norm1.get_shape())#14*14*64

为了更加直观的看到,我画了一张图:

《[专题1]TensorFlow构造神经网络(1)》 第一层对应的代码的图示

4.1.2第二层卷积

conv2_weights = tf.Variable(tf.random_normal([3,3,64,128]))#卷积核大小为3*3,当前层深度为64, 过滤器深度为128

conv2 = tf.nn.conv2d(norm1,conv2_weights,strides=[1,1,1,1],padding=’SAME’)#移动步长为1,使用全0填充

conv2_biases = tf.Variable(tf.random_normal([128]))

relu2 = tf.nn.relu( tf.nn.bias_add(conv2,conv2_biases) )

pool2 = tf.nn.max_pool(relu2,ksize=[1,2,2,1],strides=[1,2,2,1],padding=’SAME’)

norm2 = tf.nn.lrn(pool2,depth_radius=4,bias=1.0,alpha=0.001/9.0,beta=0.75)

norm2 = tf.nn.dropout(norm2,keep_prob)

print(norm2.get_shape())#7*7*128

《[专题1]TensorFlow构造神经网络(1)》 第二层对应的图示

4.1.3第三层卷积

conv3_weights= tf.Variable(tf.random_normal([3,3,128,256]))#卷积核大小为3*3,当前层深度为128, 过滤器深度为256

conv3 = tf.nn.conv2d(norm2,conv3_weights,strides=[1,1,1,1],padding=’SAME’)#移动步长为1,使用全0填充

print(“conv3=”)

print(conv3.get_shape())

conv3_biases = tf.Variable(tf.random_normal([256]))

relu3 = tf.nn.relu( tf.nn.bias_add(conv3,conv3_biases) )

print(“relu=”)

print(relu3.get_shape())

pool3 = tf.nn.max_pool(relu3,ksize=[1,2,2,1],strides=[1,2,2,1],padding=’SAME’)

print(“pool3=”)

print(pool3.get_shape())

norm3 = tf.nn.lrn(pool3,depth_radius=4,bias=1.0,alpha=0.001/9.0,beta=0.75)

norm3 = tf.nn.dropout(norm3,keep_prob)

print(norm3.get_shape())#4*4*256

《[专题1]TensorFlow构造神经网络(1)》 第三层对应的代码

4.4.4全连接层

fc1_weights= tf.Variable(tf.random_normal([4*4*256,1024]))

fc1_biases = tf.Variable(tf.random_normal([1024]))

fc1 = tf.reshape(norm3,[ -1,fc1_weights.get_shape().as_list()[0] ] )

fc1 = tf.add(tf.matmul(fc1,fc1_weights),fc1_biases)

fc1 = tf.nn.relu(fc1)

fc2_weights= tf.Variable(tf.random_normal([1024,1024]))

fc2_biases = tf.Variable(tf.random_normal([1024]))

fc2 = tf.reshape(fc1,[ -1,fc2_weights.get_shape().as_list()[0] ] )

fc2 = tf.add(tf.matmul(fc2,fc2_weights),fc2_biases)

fc2 = tf.nn.relu(fc2)

4.1.5输出层

out_weights= tf.Variable(tf.random_normal([1024,10]))

out_biases = tf.Variable(tf.random_normal([10]))

pred = tf.add(tf.matmul(fc2,out_weights),out_biases)

4.2 第二部分:

4.2.1定义损失函数 

这里用的是交叉熵

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred,labels=labels))

4.2.3选择优化器

train_op = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)

4.3.4定义评估函数

correct_prediction = tf.equal(tf.argmax(pred,1),tf.argmax(labels,1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

Step5. 训练模型

在这里会使用道我们之前的palaceholder 一些数据需要feed

#sess.run(tf.global_variables_initializer())

sess.run(tf.initialize_all_variables())

for i in range(10000):

    batch = mnist.train.next_batch(100)

    sess.run(train_op,feed_dict={inputs: batch[0],labels: batch[1],keep_prob:0.75})#训练阶段使用75%的Dropout

   if i %100==0:

   train_accuracy = sess.run(accuracy,feed_dict={inputs:batch[0],labels: batch[1],keep_prob:1.0})#评估阶段不使用Dropout

   print(“step %d, training accuracy %g”% (i,train_accuracy))

Step6. 评估模型

使用256个测试数据

print(“test accuracy %g”% sess.run(accuracy,feed_dict={inputs: mnist.test.images[:256],labels: mnist.test.labels[:256],keep_prob:1.0}))#评估阶段不使用Dropout

实际输出(部分):

最后的准确率还是比较高的, 也不准备贴全部的, 没啥意义  看看就好;

评估模型的方法还有很多  acc只是其中一个;

《[专题1]TensorFlow构造神经网络(1)》

感受:

卷积神经网络的实践确实不难,但是理论基础确实有需要深入。这里不在赘述。还要有tensorflow,matlab ,sklearn 等一系列的深度学习工具帮我们实现了关键部分(比如:激励函数,反馈,递归,dropout等等)。

    原文作者:斐波那契的数字
    原文地址: https://www.jianshu.com/p/3631dfe9cba7
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞