tensorflow之cnn详细注解


          

from __future__ import print_function  
import tensorflow as tf  
from tensorflow.examples.tutorials.mnist import input_data  
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)  
  
def weight_variable(shape):    
    initial = tf.truncated_normal(shape, stddev=0.1)    
    return tf.Variable(initial)    
def bias_variable(shape):    
    initial = tf.constant(0.1, shape=shape)    
    return tf.Variable(initial)    
def conv2d(x, W):      
    # 卷积遍历各方向步数为1,SAME:边缘外自动补0,遍历相乘    
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')      
def max_pool_2x2(x):      
    # 池化卷积结果(conv2d)池化层采用kernel大小为2*2,步数也为2,周围补0,取最大值。数据量缩小了4倍    
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')    
  
xs = tf.placeholder(tf.float32, [None, 784])     
ys = tf.placeholder(tf.float32, [None, 10])    
keep_prob = tf.placeholder(tf.float32)    
x_image = tf.reshape(xs, [-1, 28, 28, 1])   # 输入-1代表图片数量不定 , 28*28图片,1通道灰色图  
  
############### 第一层卷积 #####################  
w_conv1 = weight_variable([5, 5, 1, 32])   # 5*5卷积核大小,1通道输入,32通道输出(32张图)  
b_conv1 = bias_variable([32])              # 32通道输出,对应32偏置量  
h_conv1 = tf.nn.relu(conv2d(x_image, w_conv1) + b_conv1) # 卷积结果28x28x32      
h_pool1 = max_pool_2x2(h_conv1)            # 池化结果14x14x32  
  
############### 第二层卷积 #####################  
w_conv2 = weight_variable([5,5,32,64])    # 5*5卷积核大小,第一层卷积32输出通道作为输入,64通道输出(64张图)  
b_conv2 = bias_variable([64])             # 64通道输出,对应64偏置量    
h_conv2 = tf.nn.relu(conv2d(h_pool1, w_conv2) + b_conv2) # 卷积结果14x14x64    
h_pool2 = max_pool_2x2(h_conv2)           # 池化结果7x7x64   
  
############### 第三层全连接 ###################  
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)    
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)  
  
############### 第四层全连接 ###################  
w_fc2 = weight_variable([1024, 10])  
b_fc2 = bias_variable([10])    
y_fc2 = tf.matmul(h_fc1_drop, w_fc2) + b_fc2  
  
############### 通过y_fc2与ys训练收敛参数 ######  
y_predict = tf.nn.softmax_cross_entropy_with_logits(logits=y_fc2, labels=ys)     
cross_entropy = tf.reduce_mean(y_predict)  # 定义交叉熵为loss函数   
train_step = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(cross_entropy)   
  
############### 通过y_fc2与ys计算正确率 ########  
correct_prediction = tf.equal(tf.argmax(y_fc2, 1), tf.argmax(ys, 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(2000):   
        # 分批次取出数据  
        batch = mnist.train.next_batch(50)  
        # 进行分批次训练  
        sess.run(train_step, feed_dict={xs:batch[0], ys:batch[1], keep_prob:0.7})  
        if i%100 == 0:  
            # 计算训练数据集正确率  
            train_accuracy = accuracy.eval(feed_dict={xs:batch[0], ys:batch[1], keep_prob:1.})   
            print("step "+str(i)+ " training accuracy "+"{:.5f}".format(train_accuracy))  
    # 计算测试数据集正确率  
    test_accuracy = sess.run(accuracy, feed_dict={xs:mnist.test.images[:256], ys:mnist.test.labels[:256], keep_prob:1.})  
    print("test accuracy "+"{:.5f}".format(test_accuracy))  
结果:

step 0 training accuracy 0.20000  
step 100 training accuracy 0.86000  
step 200 training accuracy 0.94000  
step 300 training accuracy 0.90000  
step 400 training accuracy 0.92000  
step 500 training accuracy 0.94000  
step 600 training accuracy 0.96000  
step 700 training accuracy 1.00000  
step 800 training accuracy 0.96000  
step 900 training accuracy 0.96000  
step 1000 training accuracy 1.00000  
step 1100 training accuracy 1.00000  
step 1200 training accuracy 0.98000  
step 1300 training accuracy 0.98000  
step 1400 training accuracy 1.00000  
step 1500 training accuracy 0.98000  
step 1600 training accuracy 1.00000  
step 1700 training accuracy 0.98000  
step 1800 training accuracy 0.98000  
step 1900 training accuracy 0.96000  
test accuracy 0.99219