1. 程式人生 > >TensorFlow+實戰Google深度學習框架學習筆記(12)------Mnist識別和卷積神經網路LeNet

TensorFlow+實戰Google深度學習框架學習筆記(12)------Mnist識別和卷積神經網路LeNet

一、卷積神經網路的簡述

卷積神經網路將一個影象變窄變長。原本【長和寬較大,高較小】變成【長和寬較小,高增加】

卷積過程需要用到卷積核【二維的滑動視窗】【過濾器】,每個卷積核由n*m(長*寬)個小格組成,每個小格都有自己的權重值,

長寬變窄:過濾器的長寬決定的

高度變高:過濾器的個數決定的

 

二、程式碼:

1、資料集:

下載好Mnist資料集加壓到資料夾'MNIST_data’中。載入資料

import tensorflow.examples.tutorials.mnist.input_data as input_data
mnist = input_data.read_data_sets('
MNIST_data',one_hot = True) #列印資料集大小 print('訓練集大小:',mnist.train.num_examples) print('驗證集大小:',mnist.validation.num_examples) print('測試集大小:',mnist.test.num_examples) #列印樣本 print(mnist.train.images[0]) print(mnist.train.labels[0])
訓練集大小: 55000 驗證集大小: 5000 測試集大小: 10000
x:[0.         0.         0.         0.         0.         0.……0.9960785,……0]
y:[0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]

 

 2、卷積層:tf.nn.conv2d

(1)過濾器:【維度大小、權重w、偏正b、padding、stride】

設定過濾器的引數:

tf.nn.conv2d(輸入矩陣,權重,strides,padding),其中strides的第一個1和最後一個1必須有,中間為輸入矩陣尺寸的x和y的大小。padding有兩種值,SAME和VALLD。

#w,b
filter_w = tf.get_variable('weight',[5,5,3,16],initializer = tf.truncated_normal_initializer(stddev = 0.1))
filter_b 
= tf.get_variable('biases',[16],initializer = tf.constant_initializer(0.1)) #卷積的前向傳播:將【32,32,3】輸入通過 16個 【5,5,3】的過濾器得到【28,28,16】。w :【5,5,3,16】,b:【16】 conv = tf.nn.conv2d(input,filter_w,strides = [1,1,1,1],padding = 'SAME') # tf.nn.bias_add表示【5,5,3】個數都要加上biases。 bias = tf.nn.bias_add(conv,biases) #結果通過Relu啟用函式 actived_conv = tf.nn.relu(bias)

3、池化層:可加快計算速度也可防止過擬合。tf.nn.max_pool

卷積層之間加一個池化層,可縮小矩陣的尺寸,減少全連線層中的引數。

tf.nn.max_pool(傳入當前層的節點矩陣,ksize = 池化層過濾器的尺寸,strides,padding),ksize的第一維和最後一維必須為1

實現了最大池化層的前向傳播過程,引數和conv2d相似。

4、全部程式碼:

 

#載入模組和資料
import tensorflow as tf
from tensorflow.examplesamples.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 biase_variable(shape):
    initial = tf.constant(0.1,shape = shape)
    return tf.Variable(initial)
def conv2d(x,w):
    conv = tf.nn.conv2d(x,w,strides=[1,1,1,1],padding='SAME')
    return conv
def max_pool(x):
    return tf.nn.max_pool(x,ksize = [1,2,2,1],strides = [1,2,2,1],padding = 'SAME')

#訓練
def train(mnist):
    x = tf.placeholder(tf.float32,[None,784])
    y = tf.placeholder(tf.float32,[None,10])
    keep_prob =  tf.placeholder(tf.float32)
    x_image = tf.reshape(x,[-1,28,28,1])
    
    #前向傳播
    #layer1
    with tf.variable_scope('layer1'):
        w = weight_variable([5,5,1,32])
        b = biase_variable([32])
        conv1 = tf.nn.bias_add(conv2d(x_image,w),b)
        relu_conv1 = tf.nn.relu(conv1)
        pool1 = max_pool(relu_conv1)
    with tf.variable_scope('layer2'):
        w = weight_variable([5,5,32,64])
        b = biase_variable([64])
        conv2 = tf.nn.bias_add(conv2d(pool1,w),b)
        relu_conv2 = tf.nn.relu(conv2)
        pool2 = max_pool(relu_conv2)
    with tf.variable_scope('func1'):
        w = weight_variable([7*7*64,1024])
        b = biase_variable([1024])
        pool2_reshape = tf.reshape(pool2,[-1,7*7*64])
        func1 = tf.nn.relu(tf.matmul(pool2_reshape,w) + b)
        func1_drop = tf.nn.dropout(func1,keep_prob)
    with tf.variable_scope('func2'):
        w = weight_variable([1024,10])
        b = biase_variable([10])
        prediction = tf.nn.softmax(tf.matmul(func1_drop,w) + b)
        
    #後向傳播
    cross_entropy = tf.reduce_mean(-tf.reduce_sum(y * tf.log(prediction),
                                                  reduction_indices=[1]))       # loss
    train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
    
    #會話訓練
    sess = tf.Session()
    if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1:
        init = tf.initialize_all_variables()
    else:
        init = tf.global_variables_initializer()
    sess.run(init)
    for i in range(1000):
        batch_x, batch_y = mnist.train.next_batch(100)
        sess.run(train_step, feed_dict={x: batch_x, y: batch_y, keep_prob: 0.5})
        if i % 50 == 0:
            correct_prediction = tf.equal(tf.argmax(prediction,1), tf.argmax(y,1))
            accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
            result = sess.run(accuracy, feed_dict={x: mnist.test.images[:1000], y: mnist.test.labels[:1000], keep_prob: 1})
            print(result)

if __name__ == '__main__':
    train(mnist)

訓練結果:迭代結束為95%的準確率。