Keras deep learning record 1-starting from a simple mnist data set

First, get to know the neural network

This article uses the mnist data set to understand neural networks, learn Python's Keras library, and first understand the following concepts before learning.
1. MNIST data set : machine learning classic data set, this data set is handwritten digital grayscale images (28 pixels x 28 pixels), a total of ten categories (0-9), including 60,000 training images and 10,000 test images.
2. Class : A certain category in a machine learning classification problem is called a class. For example: ten numbers from 0 to 9, there are 10 categories.
3. Sample : Data points are called samples, which are all images in the mnist data set.
4. Label : The class corresponding to a sample is called label.
For example: the label corresponding to the figure below is 9.
Insert picture description here

2. Handwritten digit classification

The steps to build a neural network in Keras are as follows:
1. Load the mnist data set in Keras

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

train_iamges and train_labels constitute the training set of the model, the model learns through the training set;
test_images and test_labels constitute the test set of the model, the model is tested through the test set; the
image is encoded as a Numpy array with labels It is an array of numbers, with a value range of 0-9. The image and the label correspond one to one.

train_images.shape #(60000, 28, 28)
len(train_labels) #60000
test_images.shape #(10000, 28, 28)
len(test_labels) #10000

Through the above code, you can see that the training set contains 60,000 images and the size is 28x28.
2. Network architecture

from keras import models
from keras import layers

network = models.Sequential() #创建神经网络模型
network.add(layers.Dense(512, activation='relu', input_shape=(28*28,)))
network.add(layers.Dense(10, activation='softmax'))

Dense in the above code represents the fully connected layer of the neural network . The first layer has 512 neurons, the activation function is relu, and the second layer is a 10-channel softmax layer. Usually the output size of the last layer is the total number of data categories. For example: the handwritten digit set (0-9) in this article has 10 categories.
3. Compilation steps

network.compile(optimizer = 'rmsprop',
               loss = 'categorical_crossentropy',
               metrics = ['accuracy'])

Optimeizer stands for optimizer : this article uses the rmsprop optimizer;
loss is the loss function : this article uses the commonly used cross-entropy loss function;
metric is the monitoring indicator : for example, this article only cares about accuracy.
4. Preparing the image data
Before starting the training, first preprocess the data and convert the data into a float32 array with a shape of (60000, 28*28) and a value range of 0~1.

train_images = train_images.reshape((60000, 28*28))
train_images = train_images.astype('float32') / 255

test_images = test_images.reshape((10000, 28*28))
test_images = test_images.astype('float32') / 255

5. Prepare the label

from keras.utils import to_categorical

train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels

6. The model training
is accomplished by calling the data on the training method fit fitting (fit) Model

network.fit(train_images, train_labels, epochs = 50, batch_size = 128)

Epochs represents 50 rounds of training, which can be set according to the performance of your own machine.
batch_size represents the number of samples in each round of training, for example, 128 in this article, that is, 128 samples per training.

7. Model accuracy

test_loss, test_acc = network.evaluate(test_images, test_labels)
print('test_acc:', test_acc)

If only the accuracy of the training set is used as the evaluation result of the model performance, over-fitting may occur. Usually, deep learning will leave a part of the data set as a test set to evaluate the classification performance of the model.

Three, the code

Overall code list

from keras.datasets import mnist
from keras import models
from keras import layers
from keras.utils import to_categorical

(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
network = models.Sequential()
network.add(layers.Dense(512, activation='relu', input_shape=(28*28,)))
network.add(layers.Dense(10, activation='softmax'))

network.compile(optimizer = 'rmsprop',
               loss = 'categorical_crossentropy',
               metrics = ['accuracy'])

train_images = train_images.reshape((60000, 28*28))
train_images = train_images.astype('float32') / 255

test_images = test_images.reshape((10000, 28*28))
test_images = test_images.astype('float32') / 255


train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

network.fit(train_images, train_labels, epochs = 50, batch_size = 128)
test_loss, test_acc = network.evaluate(test_images, test_labels)
print('test_acc:', test_acc)

Guess you like

Origin blog.csdn.net/qq_40076022/article/details/108984897