卷积神经网络实现手写数字识别

import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
mnist=input_data.read_data_sets("mnist_data/")

num_classes=10
batch_size=64
fully_layer_dim=64
epochs=10

class CNN_network:
    def __init__(self,):
        tf.reset_default_graph()

    def add_placeholder(self):
        self.xs=tf.placeholder(shape=[None,784],dtype=tf.float32)
        self.ys=tf.placeholder(shape=[None,],dtype=tf.int32)

    def cnn_layer(self):
        cnn_input=tf.reshape(tensor=self.xs,shape=[-1,28,28,1])#对应于(batch_size,input_height,input_width,in_channel)
        cnn_weights=tf.Variable(tf.random_normal(shape=[5,5,1,32],dtype=tf.float32))
        conv_result=tf.nn.relu(tf.nn.conv2d(cnn_input,cnn_weights,strides=[1,2,2,1],padding="VALID"))
        #注意卷积函数中输入的数字是四维张量[kernel_weight,kernel_width,in_channel,out_channel],#
        #卷积核采用5x5,由于是VALID没有padding,同时步长是2,所以计算方法是
        #(28-5+1)/2=12
        pool_result=tf.nn.max_pool(conv_result,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME")
        #由于池化层的padding是same(没有-2+1),但是步长是2,所以计算方法是(12/2)
        #(batch_size,6,6,32)
        self.cnn_output=tf.reshape(tensor=pool_result,shape=[-1,6*6*32])

    def output_layer(self):
        weights=tf.Variable(tf.random_normal(shape=[6*6*32,fully_layer_dim],dtype=tf.float32))
        biases=tf.Variable(tf.random_normal(shape=[fully_layer_dim],dtype=tf.float32))
        output=tf.matmul(self.cnn_output,weights)+biases
        #(batch_size,64)
        self.predict=tf.contrib.layers.fully_connected(output,num_outputs=num_classes,activation_fn=None)
        #输出层采用全连接的方式,不用激活函数,因为损失函数利用的是sparse_softmax_cross_entropy_with_logits
        #这个函数会计算一次softmax的,注意这个函数的logits和labels是不一样的形状,
        #logits的shape是(batch_size,num_classes),dtype是float32
        #labels的shape是(batch_size,),dtype是int32,每一个数值代表logits属于哪一个类,
        #手写数字识别中num_classes=10
    def loss_layer(self):
        self.loss=tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=self.predict,labels=self.ys))
        self.train_op=tf.train.AdamOptimizer(0.01).minimize(self.loss)
        self.accuracy=tf.reduce_mean(tf.cast(tf.equal(tf.argmax(self.predict,1),tf.cast(self.ys,dtype=tf.int64)),dtype=tf.float32))
    
    def build_graph(self):
        self.add_placeholder()
        self.cnn_layer()
        self.output_layer()
        self.loss_layer()

    def train(self):
        num_batches=mnist.train.num_examples//batch_size
        saver=tf.train.Saver()
        print("num_batches is ",num_batches)
        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            for epoch in range(epochs):
                total_loss=0.0
                for i in range(num_batches):
                    batch_data=mnist.train.next_batch(batch_size)
                    batch_x,batch_y=batch_data
                    feed_dict={
     self.xs:batch_x,self.ys:batch_y}
                    _,loss_value=sess.run([self.train_op,self.loss],feed_dict=feed_dict)
                    total_loss+=loss_value

                print("Average loss value is ",total_loss/num_batches)
                test_x,test_y=mnist.test.next_batch(batch_size*2)
                print("test_x's shape is ",test_x.shape,"test_y's shape is ",test_y.shape)
                print("After one epoch ,accuracy is ",sess.run(self.accuracy,feed_dict={
     self.xs:test_x,self.ys:test_y}))
                saver.save(sess,'checkpoints/cnn_mnist.ckpt')

if __name__=='__main__':
    model=CNN_network()
    model.build_graph()
    model.train()
    ```

你可能感兴趣的:(神经网络,tensorflow,python,深度学习,numpy)