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 method of creating MNIST data set of multi-layer perceptron by TensorFlow neural network?

2025-04-19 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 method of TensorFlow neural network to create multi-layer perceptron MNIST data set". The content of the article is simple and clear, and it is easy to learn and understand. please follow the editor's train of thought to study and learn "what is the method of TensorFlow neural network to create multi-layer perceptron MNIST data set".

Previously, we implemented a complete Softmax Regression using TensorFlow, and achieved about 92% accuracy on MNIST data.

Previous Portal: TensorFlow tutorial Softmax logical regression recognition handwritten numerals MNIST dataset

Now build a neural network model (multi-layer perceptron) with a hidden layer.

Import tensorflow as tfimport numpy as npimport input_datamnist = input_data.read_data_sets ('data/', one_hot=True) n_hidden_1 = 256n_input = 784n_classes = 1 occupied INPUTS AND OUTPUTSx = tf.placeholder (tf.float32, [None, n_input]) # pre-occupied space with placeholder, the number of samples is uncertain as Noney = tf.placeholder (tf.float32, [None, n_classes]) # pre-occupied space with placeholder The number of samples is uncertain as None# NETWORK PARAMETERSweights = {'w1: tf.Variable (tf.random_normal ([n_input, n_hidden_1], stddev=0.1)), 'out': tf.Variable (tf.zeros ([n_hidden_1, n_classes]))} biases = {' b1: tf.Variable (tf.zeros ([n_hidden_1]) 'out': tf.Variable (tf.zeros ([n_classes]))} print ("NETWORK READY") def multilayer_perceptron (_ X, _ weights, _ biases): # forward propagation L1 and L2 are followed by relu activation function layer_1 = tf.nn.relu (tf.add (tf.matmul (_ X, _ weights ['W1']), _ biases [' b1'])) # Hidden layer return (tf.matmul (layer_1, _ weights ['out']) + _ biases [' out']) # returns the result of the output layer Ten categories of score values pred = multilayer_perceptron (x, weights, biases) # predicted values of forward propagation cost = tf.reduce_mean (tf.nn.softmax_cross_entropy_with_logits (pred, y)) # Cross Entropy loss function are obtained. The parameters are the predicted value pred and the actual Label value y, respectively. Reduce_mean returns True to average lossoptm = tf.train.GradientDescentOptimizer (0.01). Minimize (cost) # gradient descent optimizer corr = tf.equal (tf.argmax (pred, 1), tf.argmax (y, 1)) # tf.equal () to compare whether the predicted index is the same as the actual label index Different return Falseaccr = tf.reduce_mean (tf.cast (corr, tf.float32)) # convert pred, that is, True or False to 1 or 0, and calculate the mean of all judgment results init = tf.global_variables_initializer () print ("FUNCTIONS READY") # after the above neural network structure is defined The following defines some super parameters training_epochs = 100 # all sample iterations batch_size = 100 # Select 100 samples per iteration display_step = examples LAUNCH THE GRAPHsess = tf.Session () # define a Sessionsess.run (init) # run the initialization operation # OPTIMIZEfor epoch in range (training_epochs): avg_cost = 0 in sess. Total_batch = int (mnist.train.num_examples/batch_size) # Loop over all batches for i in range (total_batch): batch_xs, batch_ys = mnist.train.next_batch (batch_size) # fetch data sess.run (optm, feed_dict= {x: batch_xs, y: batch_ys}) avg_cost + = sess.run (cost, feed_dict= {x: batch_xs) one by one Y: batch_ys}) / total_batch # Display logs per epoch step if epoch% display_step = 0: train_acc = sess.run (accr, feed_dict= {x: batch_xs, y: batch_ys}) test_acc = sess.run (accr, feed_dict= {x: mnist.test.images Y: mnist.test.labels}) print ("Epoch: d cost:% .9f TRAIN ACCURACY:% .3f TEST ACCURACY:% .3f"% (epoch, training_epochs, avg_cost, train_acc, test_acc)) print ("DONE")

After 100 iterations to see the effect, the running result of the program is as follows:

Epoch: 095/100 cost: 0.076462782 TRAIN ACCURACY: 0.990 TEST ACCURACY: 0.970

Finally, the accuracy on the test set reaches 97%, and the accuracy will increase with the increase of the number of iterations. Compared with the previous Softmax, our error rate has been reduced from 8% to 3% after 100 training iterations, which can be said to be a leaping improvement in scenarios that require high accuracy in identifying bank bills. This promotion is achieved only by adding a hidden layer, which shows how significant the effect of multi-layer neural network is.

Softmax Regression without hidden layer can only infer which number it is directly from the pixels of the image, and there is no process of feature abstraction. Depending on the hidden layer, the multi-layer neural network can combine high-order features, such as horizontal lines, vertical lines, circles and so on, and then these high-order features or components can be combined into numbers to achieve accurate matching and classification.

However, the use of fully connected neural networks is also limited, even if we use very deep networks, many hidden nodes, and large iterations, it is difficult to achieve more than 99% accuracy on MNIST data sets.

Thank you for your reading, the above is the content of "what is the method of TensorFlow neural network to create multilayer perceptron MNIST data set". After the study of this article, I believe you have a deeper understanding of what is the method of TensorFlow neural network to create multilayer perceptron MNIST data set, and the specific use still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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