Exploring the neural network algorithm in Tensorflow2 - based on the Mnist data set (1)

Estimated reading time for this article: 10 minutes

Mainly explain how to build a neural network in Tensorflow2 based on the Mnist dataset


This column mainly explores the use of Tensorflow2.2 on 64-bit mac. In the first article of this column, the author listed several learning websites that are relatively clear for Tensorflow users. Interested learners can explore by themselves. There are many ways to learn Tensorflow from different angles, so I won't go into details here.


  • quotation

If we want to use Tensorflow2 to construct a neural network to analyze the data in the Mnist dataset, then we need to do the following steps: prepare the dataset, build and evaluate the model. The author will explain how to use Tensorflow2 to construct a neural network in two parts, namely the data set and the model. This article mainly mines the difficulties encountered in preparing the data set under the mac system, and then lays the foundation for the construction model.

  • in-depth

1.1 Prepare the dataset

First, before preparing the dataset, we need to import the following modules:

import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

The keras here is an open source artificial neural network library written in Python, which can be used as a high-level application program interface such as Tensorflow, and then can be used for the design, debugging, evaluation, application and visualization of deep learning models.

The Mnist handwritten digit set has been included in keras as an example of a neural network, so we can import it directly using the keras module.

#读取数据集
mnist = keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

* If you are using a domestic network proxy, then due to the proxy problem, a timeout error will eventually appear when downloading the dataset from the external network.

Therefore, we choose to download the Mnist dataset directly and put it in the local keras.datasets folder.

The Mnist download URL is as follows:

Mnist dataset storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz

Since the keras folder is hidden in the mac system, it is difficult to find. So we go into Finder, type �������+�ℎ���+� to trace the path to the folder, and enter ~/.keras in it.

After entering the keras folder, if there is no datasets folder in it, then we need to create a folder named datasets inside, and then put the previously downloaded .npz Mnist dataset into it. At this point, our work of preparing the dataset is complete.

At this point, we can use the previous code to verify the preparation of the data set, and after confirming that it is correct, go to the next step.

mnist = keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

1. 2 Feature Processing

After preparing the data set, we need to process the data to build the neural network. Feature processing is one of the major focuses of machine learning. Feature processing is the process of converting raw data attributes into data features through processing and processing of raw data. To some extent, good data and features are often the basis of a model with excellent performance.

We know that in the Mnist dataset, the train set has been marked with numbers from 1-9, then we can classify each picture in the train set into the class defined by 1-9. This way, each image will be mapped to a label, and since the class names are not included in the dataset, the class will store them here for use when drawing the image:

#定义数据集图片的class
class_names = ['0', '1', '2', '3', '4','5', '6', '7', '8', '9']

Next, we need to normalize each image before we can pass it into the neural network. Normalize pixel values ​​from , [0, 255] to , [−1, 1] to make the neural network easier to train.

#调整图片pixel
train_images = train_images / 255.0
test_images = test_images / 255.0

So far, we have preliminarily classified and processed the data, and preprocessed the data of the test set and train set in the Mnist dataset, laying the foundation for generating a high-performance neural network.

Finally, we can view some of the data we have prepared for building the neural network through matplotlib.

#展示训练集中的数据
plt.figure(figsize=(10,10))
for i in range(10):
    plt.subplot(5,5,i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(train_images[i], cmap=plt.cm.binary)
    plt.xlabel(class_names[train_labels[i]])
plt.show()

  • epilogue

This article mainly explains the basic steps of building a neural network in Tensorflow2 based on the Mnist dataset, and also explains how to prepare the dataset under the mac system and process the data with Tensorflow2. In the next article, the author will mainly explain how to build a neural network through processed data, and then complete the reproduction of the neural network algorithm in Tensorflow2.

PS: I am not talented, welcome to correct me! If you think this chapter is helpful to you, please pay attention, comment, and praise!

Reference URL:

Basic classification: Classify images of clothing | TensorFlow Core

Guess you like

Origin blog.csdn.net/zhaomengsen/article/details/130914860