MNIST的两层卷积神经网络

知识来源:《21个项目玩转深度学习 基于TensorFlow的实践详解》,Tensorflow中文社区,《深度学习入门: 基于Python3的理论与实现 》 ,《深度学习之TensorFlow入门原理与进阶实战》

了解卷积神经网络

《深度学习入门: 基于Python3的理论与实现 》这本书第7章对其解释的挺好的,可以找来看看。

1数据加载

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("MNIST/",one_hot = True)

2模型搭建前的准备

x = tf.placeholder(tf.float32,[None,784])  #输入图像
_y = tf.placeholder(tf.float32,[None,10])  #输入图像对应的labels
image = tf.reshape(x,[-1,28,28,1]) #[图片的数目(猜测),图片height,图片width,channle]

def weight_var(shape):
    inital = tf.truncated_normal(shape,stddev = 0.1) #stddev 方差, normal正态分布   
    return tf.Variable(inital)

def bias_variable(shape):
    initial = tf.constant(0.1,shape = shape)
    return tf.Variable(initial) 

def conv2d(x,w):
    return tf.nn.conv2d(x,w,strides = [1,1,1,1],  #stride =每一维卷积的步长
    padding = 'SAME')  #padding 填充。  VAILD:不自动填充
   #SAME:填充,卷积后矩阵大小与卷积核大小无关。卷积后矩阵size = 输入矩阵size/各自维度卷积步长 
def max_pool_2x2(x):
    return tf.nn.max_pool(x,ksize = [1,2,2,1],    #ksize = 池化核size。 不希望再输入矩阵的数目,channle上池化,所以为1.
    strides = [1,2,2,1],padding = 'SAME')  #stride, padding同上

3模型搭建
模型总结构


模型框架
#模型搭建
#第一层卷积conv - Relu -池化Pool
w_conv1 = weight_var([5,5,1,32])  #5x5,1channel,32个卷积核 
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(image,w_conv1)+b_conv1)
h_pool1 = max_pool_2x2(h_conv1)

#第二层卷积conv - Relu -池化Pool
w_conv2 = weight_var([5,5,32,64])  #64的确定
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1,w_conv2)+b_conv2)
h_pool2 = max_pool_2x2(h_conv2)

#全连接层-Relu
w_fc1 = weight_var([7*7*64,1024]) # 28/2/2 = 7,1024自行选择定的。
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2,[-1,7*7*64]) 
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat,w_fc1)+b_fc1)
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob) #防止过拟合的一种措施

#全连接层 - Softmax
w_fc2 = weight_var([1024,10])
b_fc2 = bias_variable([10])
y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop,w_fc2)+b_fc2)

4损失函数设计与优化.(没错就这么两段代码...,但这是深度学习瓶颈的地方)

#损失函数设计与优化
cross_entropy = tf.reduce_mean(-tf.reduce_sum(_y*tf.log(y_conv)))
train_step = tf.train.AdamOptimizer(1e-3).minimize(cross_entropy)

5模型迭代训练、正确率计算代码

correct_prediction = tf.equal(tf.argmax(y_conv,1),tf.argmax(_y,1))  #equal,判断两者是否相等
accuray = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) #cast 数据转换

#很重要的变量初始化
sess = tf.InteractiveSession()
init = tf.global_variables_initializer()
sess.run(init)

for i in range(2000):
    batch = mnist.train.next_batch(50)
    if i % 100 == 0:
        train_accuracy = accuray.eval(feed_dict={x:batch[0],_y:batch[1],keep_prob:1.0})
        print(f"step{i},training accuracy{train_accuracy}")
    train_step.run(feed_dict={x:batch[0],_y:batch[1],keep_prob:0.5})

train_accuracy1 = accuray.eval(feed_dict={x:mnist.test.images,_y:mnist.test.labels,keep_prob:1.0})
print(f"test accuray {train_accuracy1}")
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容