Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the implementation method of TensorFlow convolution neural network MNIST data set?

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the implementation method of TensorFlow convolution neural network MNIST data set". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "TensorFlow convolution neural network MNIST data set implementation method is what" it!

Here we use TensorFlow to implement a simple convolution neural network using MNIST data sets. The network structure is as follows: data input layer-convolution layer 1-pooling layer 1-convolution layer 2-pooling layer 2-full connection layer 1-full connection layer 2 (output layer). This is a simple but very representative convolution neural network.

Import tensorflow as tfimport numpy as npimport input_datamnist = input_data.read_data_sets ('data/', one_hot=True) print ("MNIST ready") sess = tf.InteractiveSession () # define initialization functions for reuse. Create some random noise for the weights to break the complete symmetry, using a truncated normal distribution, and the standard deviation is set to 0.1 ~ # and because of the use of relu Also add some small positive values to paranoia to avoid death (dead neurons) def weight_variable (shape): initial = tf.truncated_normal (shape, stddev=0.1) return tf.Variable (initial) def bias_variable (shape): initial = tf.constant (0.1shape=shape) return tf.Variable (initial) def conv2d (x, W): return tf.nn.conv2d (x, W, strides= [1,1,1,1] Padding='SAME') # parameter specifies the size of the convolution kernel, the number of channel and the number of filter, that is, the number of feature graphs generated # 2x2 maximum pool Reduce the pixel block of an 2x2 to the pixel of 1x1. Maximum pooling retains the pixel with the highest gray value in the original pixel block, that is, the most prominent feature. Def max_pool_2x2 (x): grayscale image of return tf.nn.max_pool (x, ksize= [1,2,2,1], strides= [1,2,2,1], padding='SAME') n_input = 784 # 28028, the number of pixels 784n_output = 10 # is a 10 classification problem # before designing the network structure, define the input placeholder,x as the feature Y is the real labelx = tf.placeholder (tf.float32, [None, n_input]) y = tf.placeholder (tf.float32, [None, n_output]) x_image = tf.reshape (x, [- 1,28,28,1]) # pre-process the image and convert the input vector of 1D into the 2D picture structure, that is, the structure of 1D to 2828, and-1 represents an unfixed number of samples. 1 represents the number of color channels # define the first convolution layer and initialize the parameters using the previously written function Include weight and biasW_conv1 = weight_variable ([3,3,1,32]) b_conv1 = bias_variable ([32]) h_conv1 = tf.nn.relu (conv2d (x_image, W_conv1) + b_conv1) h_pool1 = max_pool_2x2 (h_conv1) # define the second convolution layer W_conv2 = weight_variable ([3,3,32,64]) b_conv2 = bias_variable ([64]) h_conv2 = tf.nn.relu (conv2d (h_pool1) W_conv2) + b_conv2) h_pool2 = max_pool_2x2 (h_conv2) # fc1 After twice pooling, a total of 128feature graphs are converted into 1D vectors, and the implied node 1024 is defined by itself W_fc1 = weight_variable ([77064,1024]) b_fc1 = bias_variable ([1024]) h_pool2_flat = tf.reshape (h_pool2, [- 1,727,64]) h_fc1 = tf.nn.relu (tf.matmul (h_pool2_flat, W_fc1) + b_fc1) # in order to reduce overfitting Connect a Softmax layer using the Dropout layer keep _ prob = tf.placeholder (tf.float32) h_fc1_drop = tf.nn.dropout (h_fc1, keep_prob) # Dropout layer output to get the final probability output W_fc2 = weight_variable ([1024, 10]) b_fc2 = bias_variable ([10]) pred = tf.nn.softmax (tf.matmul (h_fc1_drop, W_fc2) + b_fc2) # forward propagation prediction Print ("CNN READY") # defines the loss function as the cross entropy loss function cost = tf.reduce_mean (- tf.reduce_sum (y*tf.log (pred), reduction_indices= [1])) # optimizer optm = tf.train.AdamOptimizer (0.001) .minimize (cost) # defines the operation of evaluation accuracy corr = tf.equal (tf.argmax (pred, 1), tf.argmax (y) 1)) # compare whether the predicted index is the same as the real label index Same return True, different return Falseaccuracy = tf.reduce_mean (tf.cast (corr, tf.float32)) # initialize all parameters tf.global_variables_initializer (). Run () print ("FUNCTIONS READY") training_epochs = 1000 # all sample iterations 1000 times batch_size = 100# each iteration selects 100 samples display_step = 1for i in range (training_epochs): avg_cost = 0. Total_batch = int (mnist.train.num_examples/batch_size) batch = mnist.train.next_batch (batch_size) optm.run (feed_dict= {x:batch [0], y:batch [1], keep_prob:0.7}) avg_cost + = sess.run (cost, feed_dict= {x:batch [0], y:batch [1], keep_prob:1.0}) / total_batch if I% display_step = = 0: # every 10 training sessions Test the accuracy once train_accuracy = accuracy.eval (feed_dict= {x:batch [0], y:batch [1], keep_prob:1.0}) test_accuracy = accuracy.eval (feed_dict= {x:mnist.test.images, y:mnist.test.labels, keep_prob:1.0}) print ("step:% d cost:% .9f TRAIN ACCURACY:% .3f TEST ACCURACY:% .3f"% (I Avg_cost, train_accuracy, test_accuracy) print ("DONE")

After 1000 training iterations, the correct rate of test classification reached 98.6%.

Step: 999 cost: 0.000048231 TRAIN ACCURACY: 0.990 TEST ACCURACY: 0.986

It reached 99.1% at 2000 times.

Step: 2004 cost: 0.000042901 TRAIN ACCURACY: 0.990 TEST ACCURACY: 0.991

Compared with the previous simple neural network, the effect of CNN is obviously better, in which the main performance improvement comes from the better network design, that is, the ability of convolution neural network to extract and abstract image features. Relying on the weight sharing of the convolution kernel, the number of parameters of CNN does not explode, which reduces the amount of calculation and reduces the overfitting, so the performance of the whole model is greatly improved.

At this point, I believe that everyone on the "TensorFlow convolution neural network MNIST data set implementation method is what" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report