卷积神经网络应用于MNIST数据集分类

下面这句图是不是很酷炫
上面这句图是不是很酷炫
这里介绍几种常见有趣的神经网络的图

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

mnist=input_data.read_data_sets('MNIST_data',one_hot=True)
#每个批次的大小
batch_size = 64
#计算有多少个批次
n_batch =mnist.train.num_exaples//batch_size
#定义两个placeholder
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
#初始化权值
def weight_variable(shape):
	initial = tf.truncated_normal(shape,stddv=0.1)#生成一个截断的正态分布
	return tf.Variable(initial)
# 初始化偏置值
def bias_variable(shape):
	initial = tf.constant(0.1,shape=shape)
	return tf.Variable(inital)
#卷积层
def conv2d(x,W):
	return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME') 
	#shift+tab对函数进行查看其描述
	####x#### input tensor of shape [batch,in_height,in_width,inchannels]//定义输入图像的属性  通道数、高度、宽度、通道数  如果是灰色图像通道就是1  如果是彩色图像通道数就是3
	####W#### filter/kernel tensor of shape[filter_height,filter_width,in_channels,out_channels]//卷积层的四个权值(#其实就是卷积核里的数值#),卷积窗口的高度、卷积窗口的宽度、输入通道数、输出通道数
	#strides[0]=stride[3]=1 (没有特别的意思就是固定的).strides[1]代表x方向的步长,strides[2]代表y方向的步长
	#padding:A string from:"SAME","VALID"

#池化层
def max_pool_2x2(x):
	#ksize[1,x,y,1]
	#池化与卷积一样,需要一个四维的属性
	return tf.nn.max_pool(x,W,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
	#ksize是池化窗口的大小,后面的2*2是池化窗口的步长


#改变x的格式转化为##4D##的格式[batch,in_height,in_width,in_channels]
x_image = tf.reshape(x,[-1,28,28,1])


# 初始化第一个卷积层的权值和偏置
W_conv1 = weight_variable[5,5,1,32]#5*5的采样窗口,输入通道是1,输出通道是32
b_conv1 = bias_variable([32])#每一个卷积核一个偏置值

#把x_imagine的权值和向量卷积,再加上偏置值,然后应用于relu激活函数
h_conv1 = tf.nn.relu(conv2d(x_image,W_conv1)+b_conv1)
h_pool1 = max_pool_2x2(h_conv1)#进行max pooling

#初始化第二个卷积层的权值和偏置
W_conv2 = weight_variable([5,5,32,64])#5*5的采样窗口,输入通道是32,输出通道是64
b_conv2 = bias_variable([64])

#把h pool1的权值向量进行卷积,再加上偏置值,然后应用于relu激活函数
h_conv2 = tf.nn.relu(conv2d(h_pool1,W_conv2)+b_conv2)
h_pool2 = max_pool_2x2(h_conv2)

#28*28的图片第一次卷积后还是28*28(卷积的步长是1*1,padding是smae),第一次池化后变为14*14(池化窗口是2*2)
#第二次卷积后是14*14,第二次池化变成了7*7
#通过上面操作变成64张 7*7的平面

#初始化第一个全连接层的权值
#上一层有7*7*64个神经元,全连接层有1024个神经元
W_fcl = weight_variable([7*7*64,1024])
b_fcl = bias_variable([1024])


#把池化层的2的输出偏平化为一维
h_pool2_flat = tf.reshape(h_pool2,[-1,7*7*64])
#求一个全连接层的输出
h_fcl = tf.nn.relu(tf.matmul(h_pool2_flat,W_fcl)+b_fcl)

#keep prob用来表示神经元的输出概率
keep_prob = weight_variable([1024,10])
h_fcl_drop = tf.nn.dropout(h_fcl,keep_prob)

#初始化第一个全连接层
W_fc2=weight_variable([1024,10])#10个输出
b_fc2=bias_variable([10])#偏置

#计算输出
prediction = tf.nn.softmax(tf.matmul(h_fcl_drop,W_fc2)+b_fc2)

#交叉熵代价函数
cross_entropy = tf.losses.softmax_cross_entropy(y,prediction) 
#使用adamoptimizer优化器
train_step = tf.train.AdamOptimizer(le-4).minimize(cross_entropy)
#结果放入一个布尔列表中
correct_prediction = tf.equal(tf.argmax(prediction,1),tf.argmax(y,1))
#求准确率
accuracy = tf.reduce_mean(correct_prediction,tf.float32)

with tf.Session() as sess:
	sess.run(tf.global_variable_initialzier())
	for epoch in range(21):
		for batch in range(21):
			batch_xs,batch_ys= mnist.train.next_batch
			sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys,keep_prob:0.7})

		acc=sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.label,keep_prob:1.0})
		print("Tter"+str(epoch)+",Testing Accuracy="+str(acc))

结果如下
在这里插入图片描述

发布了76 篇原创文章 · 获赞 14 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_36444039/article/details/102169090