【 small house press 】 Learning the theoretical knowledge of machine learning will inevitably feel boring, may wish to quickly implement a simple neural network. Let part of the network run first, and finally master the theoretical knowledge.

Here we choose the handwritten digital MNIST dataset. First, we import the dataset

from keras.datasets import mnist
import matplotlib.pyplot as plt

(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
print(train_images.shape)Copy the code

Here you can see the size of the MNIST dataset

Mnist has divided the data set into training set and test set. The training set has 60,000 images, and the test set has 10,000 images, each image is composed of 28*28 matrix. We first visualized the matrix of one image.

digit = test_images[0]
plt.imshow(digit, cmap=plt.cm.binary)
plt.show()Copy the code

Test_images [0] Data visualization

In order to deepen our understanding, we choose a sample from 0 to 9 for visualization

num = 0 for i in range(len(train_images)): if train_labels[i] == num and num < 10: Num += 1 plt.subplot(3,4,num) plt.axis('off') plt.imshow(train_images[I], cmap='gray', interpolation='none') plt.title("Class {}".format(train_labels[i]))Copy the code

The mnIST dataset 0-9 is shown here

Mnist data set was imported successfully. Next, we used Keras to quickly build the neural network.

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'))
network.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])Copy the code

Keras is perfect for building networks quickly, models.sequential () means that we want to connect each data processing layer, and layers.dense (…). Is to construct a data processing layer. Input_shape (28*28,) indicates that the current processing layer must receive data in a two-dimensional array of length and width 28.

The code above builds such a network, which you can see has two layers

So far, our network has been set up. We need to input data into the network, and now we preprocess the data set. The two-dimensional matrix with both length and width of 28 was changed into a one-dimensional matrix of 28*28. As the value range of each pixel was 0-255, it was not easy to calculate, so we carried out simple normalization for the data.

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') / 25Copy the code

It is not easy for us to use the label of the image directly, so we convert the label to the unique thermal encoding format.

from keras.utils import to_categorical
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
print("before change:" ,test_labels[0])
print("after change: ", test_labels[0])Copy the code

It’s easy to see a comparison of the data before and after

After data preprocessing, we input the data into the network for training.

network.fit(train_images, train_labels, epochs=10, batch_size = 128)Copy the code

In the above code, train_images are handwritten digital images used for training, train_labels correspond to the labels of images, and batCH_size means that the network randomly selects 128 images from the input image array as a group for calculation, and the calculation cycle is 10 times.

The training process

It can be seen that after the training, ACC is closer and closer to 1, and finally the network gets a class object. We use the training results of test set team to test and verify the accuracy of the model.

test_loss, test_acc = network.evaluate(test_images, test_labels, verbose=1)
print(test_loss) 
print('test_acc', test_acc)Copy the code

When the trained network predicted the test set, the accuracy rate reached 98.27%

Finally, we input a picture to visually verify the recognition effect of the model.

from keras.datasets import mnist
i= 4
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
digit = test_images[i]
plt.imshow(digit, cmap=plt.cm.binary)
plt.show()
test_images = test_images.reshape((10000, 28*28))
res = network.predict(test_images)
print(res[i])
for k in range(res[i].shape[0]):
    if (res[i][k] == 1):
        print("the number for the picture is : ", k)
        breakCopy the code

Recognition effect

We showed the fifth image recognized, and judged that it should be number 4 by the naked eye, and the result given by the neural network after recognition was also number 4. It can be seen that the network has the ability of handwritten digital image recognition after training.


For more exciting content, please click on the top right corner to follow the small house


Source: Huawei Cloud community original article simultaneously published in Li Siyuan’s blog “Machine learning”