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

How to use Tensorflow to complete handwritten digit recognition

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces you how to use Tensorflow to complete handwritten digit recognition, the content is very detailed, interested friends can refer to, hope to be helpful to you.

The most classic task problem of deep learning is classification. Through classification, we can classify the numbers, faces, animals and plants in the photo into the category it belongs to, and complete the recognition. Next, I will take you to complete a simple program to achieve handwritten digit recognition of classic problems.

Data set

The first step is to collect a bunch of handwritten data and label each handwritten number into a dataset. For deep learning, the average dataset size is at least tens of thousands. Therefore, the work of collecting data is still relatively tedious. However, someone has already helped us with the dataset, which is the famous MNIST dataset.

The MNIST dataset is a standard handwritten dataset. As shown in the figure above, there are 60, 000 handwritten digits in the dataset and all of them are fully marked. Of these, 50,000 handwritten numbers are used as training sets and another 10,000 as test sets.

Here is a portal:

Http://yann.lecun.com/exdb/mnist/

We don't need to download the MNIST dataset in advance, and Tensorflow can do it with a few lines of code:

Build a network

With the dataset ready, we began to build a neural network model using Tensorflow:

1. Input and output

Tf.placeholder means placeholder. Fill in the hole first, and then the data will be filled in. Where y _ is the correct digital label and x is the handwritten digital photo.

two。 Network subject

We have established a four-layer fully connected network, with a width of 400 for each layer. Because the digital photos of the MNIST dataset are all 28028, the shape of the weight of the first layer network is [784400]. Notice that we use Dropout technology, so there is tf.nn.dropout in the code. For the last layer, we use softmax technology to normalize the prediction of 0-9 numbers into a probability.

3. Loss function and optimizer

For the loss function, we choose the square error function, which is actually linear programming. And optimizer we chose Adam, which is the mainstream optimizer at present.

Training network

1. Initialization

We do two things here, one is to initialize the variables in the network, and the other is to set up a memory to store some variables of the training process.

two。 Training

The loop in the first line is to control the number of cycles. We use random gradient training, that is, instead of stuffing 50, 000 photos together every time we update the parameters, we choose them randomly to train as a batch. The advantage of this is that the amount of computation can be greatly reduced. We need to train on the training set at each step to update the parameters of the network, and then we will see the effect of our training on the test set after a certain step.

3. Executive program

At the beginning, the accuracy on the training set and test set is around 10%, because when the parameters of the network are not updated, all the parameters are random, which is equivalent to our wild guess. There are ten numbers, so the probability of guessing right is 1/10. After that, with the progress of the training, the accuracy of both the training set and the test set increases. We observe the accuracy of both the training set and the test set to prevent network overfitting from deceiving us.

When the training reaches a certain step, we find that the accuracy of the training set is close to 100%, and the accuracy of the test set is more than 97%. Simple four layers can achieve such a high accuracy, we can see the magic of the neural network!

On how to use Tensorflow to complete handwritten digit recognition is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Internet Technology

Wechat

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

12
Report