专栏名称: 深度学习与神经网络
关注深度学习教育,关注人工智能前沿科技
目录
相关文章推荐
姑苏晚报  ·  突发!西藏连续地震 ·  昨天  
天津日报  ·  刚刚!西藏连发地震 ·  昨天  
江苏新闻  ·  刚刚!连发两次地震! ·  昨天  
北京日报  ·  突发!西藏4.7级地震 ·  昨天  
51好读  ›  专栏  ›  深度学习与神经网络

浅入浅出TensorFlow 3 — MNIST手写体识别

深度学习与神经网络  · 公众号  ·  · 2018-04-19 12:50

正文

上篇,Amusi带着大家学习了如何 浅入浅出TensorFlow 2 — 零基础安装 ,今天继续给大家介绍linolzhang大佬的 TensorFlow系列课程 ,带大家学习 MNIST手写体识别


正文

MNIST 手写体识别通常是神经网络入门的一个例子,每个Deep Learning框架 都有相关的Demo,作为快速入门学习的示例。


一. MNIST数据

MNIST为 0-9的手写阿拉伯数字,提供了6万的 训练集数据(mnist.train) 和 1万的 测试集数据(mnist.test)

下载地址: http://yann.lecun.com/exdb/mnist/index.html


如上图所示,有四个数字字符,每个字符的数据格式为 28*28的灰度图,图片表示为矩阵形式(有填充的地方为正值,无填充为0):


每个Data(数据)均包含对应标签 Label(Ground Truth),标签结果是一个10维的float数组,对应每个数字的概率:

0 -> [1,0,0,0,0,0,0,0,0,0 ]

5 -> [ 0,0,0,0,0,1,0,0,0,0 ]

即对应位的概率为 1,其他位为 0。

这样我们就得到了一组对应的 输入-输出,即 输入为 28*28=784维向量,输出为10维向量。

训练集的Data和Label如下所示:


二. MNIST网络结构

MNIST 采用LeNet-5,因Lecun而命名,该网络拓扑图结构(caffe结构图,看着比Tensor的清晰一些):



数据层 Data -> 隐层 Layer1 -> 隐层 Layer2 -> 全连接层 FC1 -> 全连接层 FC2 ->  Loss | Accuracy


激活层根据需要添加,一般放在 Pooling层后面,这个就根据需要了。


三. TensorFlow运行

TensorFlow提供了MNIST 的例子,我们直接上Python代码,可以自己测试运行:

 1#coding=utf-8  
2import tensorflow as tf  
3from tensorflow.examples.tutorials.mnist import input_data  
4
5mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)  
6sess = tf.InteractiveSession()  
7
8# define W & b  
9def weight_variable(para):  
10    # 采用截断的正态分布,标准差stddev=0.1  
11    initial = tf.truncated_normal(para,stddev=0.1)  
12    return tf.Variable(initial)  
13
14def bias_variable(para):  
15    initial = tf.constant(0.1, shape=para)  
16    return tf.Variable(initial)  
17
18# define conv & pooling  
19def conv2d(x,W):  
20    return tf.nn.conv2d( x,W,strides=[1,1,1,1],padding='SAME' )  
21
22def max_pool_2(x):  
23    return tf.nn.max_pool(x,ksize=[1,2,2,1 ],strides=[1,2,2,1],padding='SAME')  
24
25# define using data  
26x = tf.placeholder(tf.float32, [None,784])        # 28*28=784 dim  
27
28x_input = tf.reshape(x, [-1,28,28,1])             # reshape for conv, -1表示不固定数量,1为通道数  
29y_label = tf.placeholder(tf.float32, [None, 10])  # label - 10 dim  
30
31# define layer1  
32W_conv1 = weight_variable([5,5,1,32]) # Weight in:1  out:32  
33b_conv1 = bias_variable([32])         # bias  
34h_relu1 = tf.nn.relu(conv2d(x_input,W_conv1) + b_conv1) # relu  
35h_pool1 = max_pool_2(h_relu1)         # pool after relu1  
36
37# define layer2  
38W_conv2 = weight_variable([5,5,32,64]) # Weight in:32  out:64  
39b_conv2 = bias_variable([64])          # bias for 64 kernel  
40h_relu2 = tf.nn.relu(conv2d(h_pool1,W_conv2) + b_conv2) # relu  
41h_pool2 = max_pool_2(h_relu2)          # pool after relu2  
42
43# define full connection layer1  
44W_fc1 = weight_variable([7*7*64,1024]) # Weight in:7*7res*64  out:1024  
45b_fc1 = bias_variable([1024])          # bias for 1024  
46h_pool2_flat = tf.reshape(h_pool2, [-1,7*7*64])  
47h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)  
48
49# 添加Drop Out层,预防过拟合,通过keep_prob传入需要保持(不Drop)的样本比率  
50keep_prob = tf.placeholder(tf.float32)  
51drop_fc1 = tf.nn.dropout(h_fc1,keep_prob)  
52
53# 第二个全连接层,采用softmax执行回归  
54W_fc2 = weight_variable([1024,10]) # Weight in:1024  out:10  
55b_fc2 = bias_variable([10])        # bias for 10, 10类划分  
56y = tf.nn.softmax(tf.matmul(drop_fc1,W_fc2) + b_fc2) # 计算结果  
57
58# 定义loss  
59cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_label*tf.log(y),reduction_indices=[1]))  
60train_step = tf.train.AdamOptimizer(0.001).minimize(cross_entropy)  # Adam 替代SGD  
61
62# 定义准确率  
63correct_pred = tf.equal(tf.argmax(y,1), tf.argmax(y_label,1))  
64accuracy = tf.reduce_mean(tf.cast(correct_pred,tf.float32))  
65






请到「今天看啥」查看全文