Tensorflow cnn MNIST 笔记
写这个完全是记录看官网example时不懂,但后来弄懂的一些细节。当然这个可以算是对官方文档的补充,也许每个人遇到的不懂都不一样,但希望对大家有帮助。
先上代码
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import numpy as np
def weightVariable(shape):
initial = tf.truncated_normal(shape,stddev=0.1)
return tf.Variable(initial)
def biasVariable(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],padding='SAME')
def maxPool2x2(x):
return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
sess = tf.InteractiveSession()
x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])
xImage = tf.reshape(x,[-1,28,28,1])
wConv1 = weightVariable([5,5,1,32])
bConv1 = biasVariable([32]);
hConv1 = tf.nn.relu(conv2d(xImage,wConv1)+bConv1)
hPool1 = maxPool2x2(hConv1)
wConv2 = weightVariable([5,5,32,64])
bConv2 = biasVariable([64])
hConv2 = tf.nn.relu(conv2d(hPool1,wConv2)+bConv2)
hPool2 = maxPool2x2(hConv2)
wFc1 = weightVariable([7*7*64,1024])
bFc1 = biasVariable([1024])
h1d = tf.reshape(hPool2,[-1,7*7*64])
hFc1 = tf.nn.relu(tf.matmul(h1d,wFc1)+bFc1)
dropProb = tf.placeholder(tf.float32)
hFc1Drop = tf.nn.dropout(hFc1,dropProb)
wFc2 = weightVariable([1024,10])
bFc2 = biasVariable([10])
y = tf.matmul(hFc1Drop,wFc2)+bFc2
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(20000):
batch = mnist.train.next_batch(50)
if i % 100 == 0:
train_accuracy = accuracy.eval(feed_dict={x: batch[0], y_: batch[1], dropProb: 1.0})
print('step %d, training accuracy %g' % (i, train_accuracy))
train_step.run(feed_dict={x: batch[0], y_: batch[1], dropProb: 0.5})
print('test accuracy %g' % accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, dropProb: 1.0}))
## 一些需要看api文档的细节
tf.nn.conv2d(
input,
filter,
strides,
padding,
use_cudnn_on_gpu=None,
data_format=None,
name=None
)
这个函数是计算卷积的,第一个参数是输入的图片数据,在这个地方具体是一个1*784的vector,然后filter是滤波器,拿第一层卷积层说的话,fileter = wConv1 = weightVariable([5,5,1,32]). 就是说每一个卷积核是5x5的一个矩阵,滑步是1,同时这一层有32个卷积核,其实我也不懂为什么这里要提取32个特征,然后padding=’SAME’,就是说卷积后的图片大小依然是1x784,至于内部是如何做padding的,不用管。这样,经过这层卷积操作,就产生了32张图片,每张都代表原始图片不同的特征
然后就是关于bais的维度的疑问,我发现一些地方bias的维度和他相加的矩阵的维度不一致,我猜测这个相加应该是在最后一个维度上做广播,比如[M,10]+b,b是一个10维的向量,这个可能就是表示矩阵M每一个元素都加上b对应的一个分量
有了以上维度的解读,就可以知道,第一次卷积操作输出了32张不同特征的图片,大小没变。然后做了一个maxpool下采样,图片变小了。然后是第二层卷积操作,wConv2 = weightVariable([5,5,32,64])。可以看到其中第三个参数是32,因为现在有32张图片了,相当于有32个通道。然后第四个参数是64,是说明现在要提取64个特征。接下来做的是下采样,然后就是dropout,防止过拟合。其中过拟合需要有概率数据输入,所以设置了一个placeholder。
然后就是全链接层,发现全链接层其实就是简单的矩阵乘法。在FC1钱,先要将数据reshape,因为现在数据是7x7的图片,有64张,所以变成一个7x7x64的vector,然后由于全链接层有1000个神经元,所以weight的shape是[7x7x764,1000].接着又做了一次dropout,防止过拟合。然后就是第二层全连接层,然后输出10个class。
然后是tensorBoard的细节,下次补充
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)