您的位置:首页 > 其它

tensorflow cnn训练mnist数据集源码

2019-04-16 21:36 169 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/qq_34400242/article/details/89342872
import tensorflow as tf
sess = tf.InteractiveSession()
from tensorflow.examples.tutorials.mnist import input_data
#读取本地mnist数据
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
#为输入图像和目标输出类别创建节点,来开始构建计算图
x = tf.placeholder("float", shape=[None, 784])
y_ = tf.placeholder("float", shape=[None, 10])

#定义weight和bias
**def weight_variable(shape):**
# 以一个截断正太分布初始化权重张量,tf.truncated_normal(shape, mean=0.0, 		   stddev=1.0, dtype=tf.float32, seed=None, name=None)中,
# shape为张量维度,mean为均值,stddev为标准差,seed如果有效,指每次生成随机数都一样,name为操作的名字
# 该函数如果生成的正态分布的值大于(mean+stdev*2),则丢弃,重新生成。
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
#偏置固定为0.1
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
#定义卷积层和maxpooling层
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
#最大池化操作
tf.nn.max_pool(value, ksize, strides, padding, name=None)
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
#为了神经网络的layer可以使用image数据,我们要将其转化成4d的tensor: (Number, width, height, channels)
x_image = tf.reshape(x, [-1,28,28,1])
#第一层卷积
#该层卷积将得出32个由32个卷积核分别卷积得出的特征图。
#前两个维度为卷积核的大小。第三个为输入通道数,第四个为输出通道数W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
#第二层卷积
W_conv2 = weight_variable([5, 5, 32, 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)
#全连接层
W_fc1 = weight_variable([7 * 7 * 64, 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)
#dropout
keep_prob = tf.placeholder("float")
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
#输出层
softmaxW_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
#训练和评估
cross_entropy=-tf.reduce_sum(y_*tf.log(y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
sess.run(tf.global_variables_initializer())
for i in range(20000):
batch = mnist.train.next_batch(100)
if i%100 == 0:
train_accuracy = accuracy.eval(feed_dict={
x:batch[0], y_: batch[1], keep_prob: 1.0})
print("step %d, training accuracy %g"%(i, train_accuracy))
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
print("test accuracy %g"%accuracy.eval(feed_dict={    x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))

关键函数解析:
1.tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
表示以一个截断正太分布初始化权重张量, shape为张量维度,mean为均值,stddev为标准差,seed如果有效,指每次生成随机数都一样,name为操作的名字。
该函数如果生成的正态分布的值大于(mean+stdev2),则丢弃,重新生成。
2.tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None)
卷积函数,详见另外一篇博文。
3.tf.nn.max_pool(value, ksize, strides, padding, name=None)
最大池化函数,用法与卷积类似。
参数value:需要池化的输入,一般池化层紧跟卷积层后面,所以输入通常 是feature map,shape为[batch, in_height, in_width, in_channels]*
参数ksize:池化窗口的大小,四维向量,通常取[1,height, width, 1],
参数strides:窗口在每一个维度的步长,常取[1, stride,stride, 1]
参数padding:可以取’VALID’ 或者’SAME’
返回一个张量,类型不变,shape仍然是**[batch, in_height, in_width, in_channels]**
4.numpy.reshape(a, newshape, order=‘C’)
在不改变原tensor的值的情况下改变其shape。
a为原tensor,newshape为需要转变的目的shape,例如[2,2,2]指目标tensor为一个三维数组,长宽高分别为2。也可以填入-1,当某一维值为-1时,表示该维度值不确定,需要由原tensor的长度和newshape的其他维度值来确定。例如a为222的tensor,即长度为8,如果newshape定义为[-1,4]则表示转变为一个两行四列的tensor。2行由8/4计算所得。
order表示读取顺序,有顺序,逆序等。
5.tf.nn.relu()
激活函数

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: