使用卷積神經網絡CNN訓練識別mnist


算的的上是自己搭建的第一個卷積神經網絡。網絡結構比較簡單。

輸入為單通道的mnist數據集。它是一張28*28,包含784個特征值的圖片

我們第一層輸入,使用5*5的卷積核進行卷積,輸出32張特征圖,然后使用2*2的池化核進行池化 輸出14*14的圖片

第二層 使用5*5的卷積和進行卷積,輸出64張特征圖,然后使用2*2的池化核進行池化 輸出7*7的圖片

第三層為全連接層 我們總結有 7*7*64 個輸入,輸出1024個節點 ,使用relu作為激活函數,增加一個keep_prob的dropout層

第四層為輸出層,我們接收1024個輸入,輸出長度為10的one-hot向量。使用softmax作為激活函數

使用交叉熵作為損失函數

網絡模型代碼:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import sys
import tempfile
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
FLAGS = None
def weight_variable(shape):
    init=tf.truncated_normal(shape=shape,stddev=0.1,mean=1.)
    return tf.Variable(init)
def bias_variable(shape):
    init=tf.constant(0.1,shape=shape)
    return tf.Variable(init)
def conv2d(x,w):
    return tf.nn.conv2d(x,w,[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")

def deepnn(x):
    with tf.name_scope('reshape'):
        x_image=tf.reshape(x,[-1,28,28,1])
    #第一層卷積和池化
    with tf.name_scope('conv1'):
        #輸入為1張圖片 卷積核為5*5 生成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)
    with tf.name_scope('pool1'):
        h_pool1=max_pool_2x2(h_conv1)
    #第二層卷積和池化
    with tf.name_scope("conv2"):
        #輸入為32張特征圖,卷積核為5*5 輸出64張特征圖
        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)
    with tf.name_scope("pool2"):
        h_pool2=max_pool_2x2(h_conv2)
    #第一層全連接層,將特征圖展開為特征向量,與1024個節點連接
    with tf.name_scope("fc1"):
        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層,訓練時隨機讓某些隱含層節點權重不工作
    with tf.name_scope("dropout1"):
        keep_prob=tf.placeholder(tf.float32)
        h_fc1_drop=tf.nn.dropout(h_fc1,keep_prob)
    #第二個全連接層,連接1024個節點,輸出one-hot預測
    with tf.name_scope("fc2"):
        w_fc2=weight_variable([1024,10])
        b_fc2=bias_variable([10])
        h_fc2=tf.matmul(h_fc1_drop,w_fc2)+b_fc2
    return h_fc2,keep_prob
View Code

訓練代碼:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse
import sys
import tempfile

from tensorflow.examples.tutorials.mnist import input_data

import tensorflow as tf

import mnist_model

FLAGS = None
def main(_):
  mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)
  #設置輸入變量
  x=tf.placeholder(dtype=tf.float32,shape=[None,784])
  #設置輸出變量
  y_real=tf.placeholder(dtype=tf.float32,shape=[None,10])
  #實例化網絡
  y_pre,keep_prob=mnist_model.deepnn(x)
  #設置損失函數
  with tf.name_scope("loss"):
      cross_entropy=tf.nn.softmax_cross_entropy_with_logits(logits=y_pre,labels=y_real)
      loss=tf.reduce_mean(cross_entropy)
  #設置優化器
  with tf.name_scope("adam_optimizer"):
      train_step=tf.train.AdamOptimizer(1e-4).minimize(loss)
  #計算正確率:
  with tf.name_scope("accuracy"):
      correct_prediction = tf.equal(tf.argmax(y_pre, 1), tf.argmax(y_real, 1))
      correct_prediction = tf.cast(correct_prediction, tf.float32)
  accuracy = tf.reduce_mean(correct_prediction)
  #將神經網絡圖模型保存
  graph_location=tempfile.mkdtemp()
  print('saving graph to %s'%graph_location)
  train_writer=tf.summary.FileWriter(graph_location)
  train_writer.add_graph(tf.get_default_graph())
  #將訓練的網絡保存下來
  saver=tf.train.Saver()
  with tf.Session() as sess:
      sess.run(tf.global_variables_initializer())
      for i in range(5000):
          batch=mnist.train.next_batch(50)
          if i%100==0:
              train_accuracy = accuracy.eval(feed_dict={x: batch[0], y_real: batch[1], keep_prob: 1.0})
              print('step %d, training accuracy %g' % (i, train_accuracy))
          sess.run(train_step,feed_dict={x: batch[0], y_real: batch[1], keep_prob: 0.5})
      #在測試集上進行測試
      test_accuracy = 0
      for i in range(200):
          batch = mnist.test.next_batch(50)
          test_accuracy += accuracy.eval(feed_dict={x: batch[0], y_real: batch[1], keep_prob: 1.0}) / 200;

      print('test accuracy %g' % test_accuracy)
      save_path = saver.save(sess, "mnist_cnn_model.ckpt")
if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  parser.add_argument('--data_dir', type=str,
                      default='./',
                      help='Directory for storing input data')
  FLAGS, unparsed = parser.parse_known_args()
  tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
View Code

部分訓練結果:

step 3600, training accuracy 0.98
step 3700, training accuracy 0.98
step 3800, training accuracy 0.96
step 3900, training accuracy 1
step 4000, training accuracy 0.98
step 4100, training accuracy 0.96
step 4200, training accuracy 1
step 4300, training accuracy 1
step 4400, training accuracy 0.98
step 4500, training accuracy 0.98
step 4600, training accuracy 0.98
step 4700, training accuracy 1
step 4800, training accuracy 0.98
step 4900, training accuracy 1
test accuracy 0.9862

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM