1. 程式人生 > >python3下tensorflow練習(五)之CNN下的MNIST識別

python3下tensorflow練習(五)之CNN下的MNIST識別

1. 資料準備

import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

2.建立共享函式

def weight(shape):
    return tf.Variable(tf.truncated_normal(shape, stddev=0.1),
                       name ='W')
def bias(shape):
    return tf.Variable(tf.constant(0.1, shape=shape)
                       , name = 'b')
def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1,1,1,1], 
                        padding='SAME')
def max_pool_2x2(x):
    return tf.nn.max_pool(x, ksize=[1,2,2,1], 
                          strides=[1,2,2,1], 
                          padding='SAME')
tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
引用:https://blog.csdn.net/u013713117/article/details/65446361
從截斷的正態分佈中輸出隨機值。 
生成的值服從具有指定平均值和標準偏差的正態分佈,如果生成的值大於平均值2個標準偏差的值則丟棄重新選擇。

在正態分佈的曲線中,橫軸區間(μ-σ,μ+σ)內的面積為68.268949%。 
橫軸區間(μ-2σ,μ+2σ)內的面積為95.449974%。 
橫軸區間(μ-3σ,μ+3σ)內的面積為99.730020%。 
X落在(μ-3σ,μ+3σ)以外的概率小於千分之三,在實際問題中常認為相應的事件是不會發生的,基本上可以把區間(μ-3σ,μ+3σ)看作是隨機變數X實際可能的取值區間,這稱之為正態分佈的“3σ”原則。 
在tf.truncated_normal中如果x的取值在區間(μ-2σ,μ+2σ)之外則重新進行選擇。這樣保證了生成的值都在均值附近。

引數:
shape: 一維的張量,也是輸出的張量。
mean: 正態分佈的均值。
stddev: 正態分佈的標準差。
dtype: 輸出的型別。
seed: 一個整數,當設定之後,每次生成的隨機數都一樣。
name: 操作的名字。

3.輸入層 Input Layer

with tf.name_scope('Input_Layer'):
    x = tf.placeholder("float",shape=[None, 784]
                       ,name="x")    
    x_image = tf.reshape(x, [-1, 28, 28, 1])

4.卷積層1

with tf.name_scope('C1_Conv'):
    W1 = weight([5,5,1,16])
    b1 = bias([16])
    Conv1=conv2d(x_image, W1)+ b1
    C1_Conv = tf.nn.relu(Conv1 )
with tf.name_scope('C1_Pool'):
    C1_Pool = max_pool_2x2(C1_Conv)

5.卷積層2

with tf.name_scope('C2_Conv'):
    W2 = weight([5,5,16,36])
    b2 = bias([36])
    Conv2=conv2d(C1_Pool, W2)+ b2
    C2_Conv = tf.nn.relu(Conv2)
with tf.name_scope('C2_Pool'):
    C2_Pool = max_pool_2x2(C2_Conv) 

6.全連線層

with tf.name_scope('D_Flat'):
    D_Flat = tf.reshape(C2_Pool, [-1, 1764])#7*7*56=1766
with tf.name_scope('D_Hidden_Layer'):
    W3= weight([1764, 128])
    b3= bias([128])
    D_Hidden = tf.nn.relu(
                  tf.matmul(D_Flat, W3)+b3)
    D_Hidden_Dropout= tf.nn.dropout(D_Hidden, ####若在結構上不定義keep_prob,則需要在結構中定義keep_prob = tf.placeholder(tf.float32),###在session()中用feed_dict()喂引數進去
                                keep_prob=0.8)####在結構上直接定義引數大小,直接賦值

7.輸出層Output

with tf.name_scope('Output_Layer'):
    W4 = weight([128,10])
    b4 = bias([10])
    y_predict= tf.nn.softmax(
                 tf.matmul(D_Hidden_Dropout,
                           W4)+b4)

8.設定訓練模型最優化步驟(反向傳播更新引數)

with tf.name_scope("optimizer"):
    
    y_label = tf.placeholder("float", shape=[None, 10], 
                              name="y_label")
    
    loss_function = tf.reduce_mean(
                      tf.nn.softmax_cross_entropy_with_logits
                         (logits=y_predict , 
                          labels=y_label))
    
    optimizer = tf.train.AdamOptimizer(learning_rate=0.0001) \
                    .minimize(loss_function)
    saver_path=saver.save(sess, "saveModel/CNN_model1)##儲存模型  

9.評估模型準確率

with tf.name_scope("evaluate_model"):
    correct_prediction = tf.equal(tf.argmax(y_predict, 1),
                                  tf.argmax(y_label, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

10.訓練模型

trainEpochs = 30##30個週期,每個週期對訓練的樣本55000樣本進行訓練
batchSize = 100#批尺寸100
totalBatchs = int(mnist.train.num_examples/batchSize)##55000/100=550
epoch_list=[];accuracy_list=[];loss_list=[];
from time import time
startTime=time()
sess = tf.Session()
sess.run(tf.global_variables_initializer())

for epoch in range(trainEpochs):

    
    for i in range(totalBatchs):
        batch_x, batch_y = mnist.train.next_batch(batchSize)
        sess.run(optimizer,feed_dict={x: batch_x,
                                      y_label: batch_y})
        
    
    loss,acc = sess.run([loss_function,accuracy],
                        feed_dict={x: mnist.validation.images, 
                                   y_label: mnist.validation.labels})

    epoch_list.append(epoch)
    loss_list.append(loss);accuracy_list.append(acc)    
    
    print("Train Epoch:", '%02d' % (epoch+1), \
          "Loss=","{:.9f}".format(loss)," Accuracy=",acc)
    
duration =time()-startTime
print("Train Finished takes:",duration)         

訓練結果:


11.視覺化cost和acc:

%matplotlib inline
import matplotlib.pyplot as plt
plt.plot(epoch_list, accuracy_list,label="accuracy" )
fig = plt.gcf()
fig.set_size_inches(4,2)
plt.ylim(0.8,1)
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend()
plt.show()

fig = plt.gcf()
fig.set_size_inches(4,2)
plt.plot(epoch_list, loss_list, label = 'loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['loss'], loc='upper left')

12.儲存模型

saver = tf.train.Saver()##session()上面
#save_path = saver.save(sess, "saveModel/CNN_model1")這句話寫在session()裡面
merged = tf.summary.merge_all()
train_writer = tf.summary.FileWriter('log/CNN',sess.graph)

13.載入模型和做出預測

saver = tf.train.Saver()
  with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    saver.restore(sess,"saveModel/CNN_model1")
    y_predict=sess.run(y_predict, feed_dict={x: xxxxx})###做出預測傳入要預測的圖片xxxxx
    print('test accuracy %g' % accuracy.eval(feed_dict={###列印訓練好的模型和測試集相比的準確率
        x: mnist.test.images, y_label: mnist.test.labels, keep_prob: 1.0}))