TensorFlow Study Note Five: mnist example-convolutional neural Network (CNN)

Source: Internet
Author: User

The mnist examples of convolutional neural networks and the neural network examples in the previous blog post are mostly the same. But CNN has more layers, and the network model needs to be built on its own.

The procedure is more complicated, I will be divided into several parts to describe.

First, download and load the data:

Importimport= Input_data.read_data_sets ("mnist_data/" , One_hot=true)     # Download and load mnist data x = Tf.placeholder (Tf.float32, [None, 784])                        #  input data placeholder y_actual = Tf.placeholder (Tf.float32, Shape=[none)            # Enter the label placeholder 

Define four functions, which are used to initialize the weight value W, initialize the offset item B, build the convolution layer, and build the pooling layer.

#defines a function that initializes all weights Wdefweight_variable (Shape): initial= Tf.truncated_normal (Shape, stddev=0.1)  returnTF. Variable (initial)#defines a function that initializes all offset items Bdefbias_variable (Shape): initial= Tf.constant (0.1, shape=shape)returnTF. Variable (initial)#define a function to build the convolution layerdefconv2d (x, W):returntf.nn.conv2d (x, W, strides=[1, 1, 1, 1], padding='same')#define a function for building the pooling layerdefMax_pool (x):returnTf.nn.max_pool (x, Ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='same')

Next build the network. The entire network consists of two convolutional layers (containing the activation layer and the pooling layer), an all-connected layer, a dropout layer, and a softmax layer.

#Build a networkX_image = Tf.reshape (x, [ -1,28,28,1])#transforms the input data shape so that it can be used on the networkW_CONV1 = Weight_variable ([5, 5, 1, 32]) B_conv1= Bias_variable ([32]) H_conv1= Tf.nn.relu (conv2d (X_image, W_CONV1) + b_conv1)#First convolutional layerH_pool1 = Max_pool (H_CONV1)#First pooled layerW_conv2= Weight_variable ([5, 5, 32, 64]) B_conv2= Bias_variable ([64]) H_conv2= Tf.nn.relu (conv2d (h_pool1, w_conv2) + b_conv2)#A second convolutional layerH_pool2 = Max_pool (h_conv2)#Second pooled layerW_FC1= Weight_variable ([7 * 7 * 64, 1024]) B_fc1= Bias_variable ([1024]) H_pool2_flat= Tf.reshape (H_pool2, [-1, 7*7*64])#reshape into VectorH_FC1 = Tf.nn.relu (Tf.matmul (H_pool2_flat, W_FC1) + b_fc1)#first fully connected layerKeep_prob= Tf.placeholder ("float") H_fc1_drop= Tf.nn.dropout (H_FC1, Keep_prob)#Dropout LayerW_FC2= Weight_variable ([1024, 10]) B_FC2= Bias_variable ([10]) y_predict=tf.nn.softmax (Tf.matmul (H_fc1_drop, W_FC2) + B_FC2)#Softmax Layer

Once the network is built, you are ready to start training.

Cross_entropy =-tf.reduce_sum (Y_actual*tf.log (y_predict))#Cross EntropyTrain_step = Tf.train.GradientDescentOptimizer (1e-3). Minimize (Cross_entropy)#Gradient Descent MethodCorrect_prediction = Tf.equal (Tf.argmax (y_predict,1), Tf.argmax (y_actual,1)) Accuracy= Tf.reduce_mean (Tf.cast (Correct_prediction,"float"))#Accuracy Calculationsess=TF. InteractiveSession () Sess.run (Tf.initialize_all_variables ()) forIinchRange (20000): Batch= Mnist.train.next_batch (50)  ifi%100 = = 0:#Train 100 times, verify onceTRAIN_ACC = Accuracy.eval (Feed_dict={x:batch[0], y_actual:batch[1], keep_prob:1.0})    Print('Step'I'Training Accuracy', TRAIN_ACC) train_step.run (feed_dict={x:batch[0], y_actual:batch[1], keep_prob:0.5}) Test_acc=accuracy.eval (Feed_dict={x:mnist.test.images, Y_actual:mnist.test.labels, keep_prob:1.0})Print("Test Accuracy", TEST_ACC)

The TensorFlow relies on an efficient C + + backend for computation. This connection to the back end is called the session. In general, the process of using the TensorFlow program is to create a diagram first and then launch it in session.

Here, we use a more convenient InteractiveSession class. It allows you to build your code more flexibly. It allows you to insert some calculation diagrams when you run the diagram, which are made up of some operations (operations). This is very handy for people working in interactive environments, such as using Ipython.

After training 20,000 times, the test can reach 99% accuracy.

Full code:

#-*-coding:utf-8-*-"""Created on Thu Sep 8 15:29:48 2016@author:root"""ImportTensorFlow as TFImportTensorflow.examples.tutorials.mnist.input_data as Input_datamnist= Input_data.read_data_sets ("mnist_data/", one_hot=true)#download and load mnist datax = Tf.placeholder (Tf.float32, [None, 784])#the data placeholder enteredY_actual = Tf.placeholder (Tf.float32, Shape=[none, 10])#the label placeholder entered#defines a function that initializes all weights Wdefweight_variable (Shape): initial= Tf.truncated_normal (Shape, stddev=0.1)  returnTF. Variable (initial)#defines a function that initializes all offset items Bdefbias_variable (Shape): initial= Tf.constant (0.1, shape=shape)returnTF. Variable (initial)#define a function to build the convolution layerdefconv2d (x, W):returntf.nn.conv2d (x, W, strides=[1, 1, 1, 1], padding='same')#define a function for building the pooling layerdefMax_pool (x):returnTf.nn.max_pool (x, Ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='same')#Build a networkX_image = Tf.reshape (x, [ -1,28,28,1])#transforms the input data shape so that it can be used on the networkW_CONV1 = Weight_variable ([5, 5, 1, 32]) B_conv1= Bias_variable ([32]) H_conv1= Tf.nn.relu (conv2d (X_image, W_CONV1) + b_conv1)#First convolutional layerH_pool1 = Max_pool (H_CONV1)#First pooled layerW_conv2= Weight_variable ([5, 5, 32, 64]) B_conv2= Bias_variable ([64]) H_conv2= Tf.nn.relu (conv2d (h_pool1, w_conv2) + b_conv2)#A second convolutional layerH_pool2 = Max_pool (h_conv2)#Second pooled layerW_FC1= Weight_variable ([7 * 7 * 64, 1024]) B_fc1= Bias_variable ([1024]) H_pool2_flat= Tf.reshape (H_pool2, [-1, 7*7*64])#reshape into VectorH_FC1 = Tf.nn.relu (Tf.matmul (H_pool2_flat, W_FC1) + b_fc1)#first fully connected layerKeep_prob= Tf.placeholder ("float") H_fc1_drop= Tf.nn.dropout (H_FC1, Keep_prob)#Dropout LayerW_FC2= Weight_variable ([1024, 10]) B_FC2= Bias_variable ([10]) y_predict=tf.nn.softmax (Tf.matmul (H_fc1_drop, W_FC2) + B_FC2)#Softmax Layercross_entropy=-tf.reduce_sum (Y_actual*tf.log (y_predict))#Cross EntropyTrain_step = Tf.train.GradientDescentOptimizer (1e-3). Minimize (Cross_entropy)#Gradient Descent MethodCorrect_prediction = Tf.equal (Tf.argmax (y_predict,1), Tf.argmax (y_actual,1)) Accuracy= Tf.reduce_mean (Tf.cast (Correct_prediction,"float"))#Accuracy Calculationsess=TF. InteractiveSession () Sess.run (Tf.initialize_all_variables ()) forIinchRange (20000): Batch= Mnist.train.next_batch (50)  ifi%100 = = 0:#Train 100 times, verify onceTRAIN_ACC = Accuracy.eval (Feed_dict={x:batch[0], y_actual:batch[1], keep_prob:1.0})    Print('Step'I'Training Accuracy', TRAIN_ACC) train_step.run (feed_dict={x:batch[0], y_actual:batch[1], keep_prob:0.5}) Test_acc=accuracy.eval (Feed_dict={x:mnist.test.images, Y_actual:mnist.test.labels, keep_prob:1.0})Print("Test Accuracy", TEST_ACC)
View Code

TensorFlow Study Note Five: mnist example-convolutional neural Network (CNN)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.