Keras-use Keras to build a classification neural network

1 Introduction

Today, Keras is used to build a classification neural network. The data set used is MNIST, which is a picture data set of several numbers from 0 to 9.

2. Use Keras to build a classification neural network

2.1. Import necessary modules

import numpy as np
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.optimizers import RMSprop
np.random.seed(42)

2.2. Data preprocessing

Keras has its own MNIST packet, which is further divided into a training set and a test set. x is a picture, y is the label corresponding to each picture, that is, which number it is.

The input x becomes 60,000 * 784 data, and then divided by 255 to normalize, because each pixel is between 0 and 255, after normalization, it becomes between 0 and 1.

For y, you need to use a numpy function np_utils.to_categorical modified by Keras to change y into one-hot form, that is, y was a value before, between 0-9, and now is a vector of size 10. , Whichever number it belongs to, it is 1 at all positions, and 0 at all other positions. np_utils.to_categorical maps category vectors (integer vectors from 0 to nb_classes) to binary category matrices for use in models with categorical_crossentropy as the objective function.

(X_train, y_train),(X_test,y_test) = mnist.load_data()  #返回两个元组
X_train = X_train.reshape(X_train.shape[0],-1)/255    #归一化
X_test = X_test.reshape(X_test.shape[0],-1)/255
y_train = np_utils.to_categorical(y_train,num_classes=10)   #to_categorical就是将类别向量转换为二进制(只有0和1)的矩阵类型表示。其表现为将原有的类别向量转换为独热编码的形式
y_test = np_utils.to_categorical(y_test,num_classes=10)  

2.3. Build the model

(1) models.Sequential, used to build a neural layer layer by layer;
(2) layers.Dense means that this neural layer is a fully connected layer.
(3) Layers.Activation excitation function.
(4) optimizers.RMSprop The optimizer uses RMSprop to accelerate the neural network training method.

What is used in the regression network is model.add to add neural layers layer by layer. Today's method is to add multiple neural layers directly inside the model. It's like a water pipe, section by section. The data falls from the upper section to the lower section, and then to the lower section.

The first paragraph is to join the Dense nerve layer. 32 is the output dimension, and 784 is the input dimension. The data from the first layer has 32 features, which are transmitted to the excitation unit. The excitation function uses the relu function. After the excitation function, it becomes a nonlinear data. Then pass this data to the next neural layer. For this Dense, we define the feature with 10 outputs. Similarly, there is no need to define the input dimension here, because it receives the output of the previous layer. Next, enter the following softmax function for classification.

Next, RMSprop is used as an optimizer, and its parameters include learning rate, etc. You can look at the effect of the model by modifying these parameters.

model = Sequential([
    Dense(32,input_dim=784),
    Activation('relu'),   #非线性化
    Dense(10),     #输入32,输出10
    Activation('softmax')
])
rmsprop = RMSprop(lr=0.001,rho=0.9,epsilon=1e-8,decay=0.0)   #RMSProp 优化器,建议使用优化器的默认参数 (除了学习率 lr,它可以被自由调节)

2.4. Activate the model

Next, use model.compile to stimulate the neural network.

The optimizer can be either the default or the one we defined in the previous step. The loss function, the classification and regression problems are different, and cross entropy is used. Metrics, you can put the cost, accuracy, score, etc. that need to be calculated.

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

2.5. Training + Testing

print('Training.................')  
model.fit(X_train, y_train, epochs=2, batch_size=32)   #训练
print('\nTesting')
loss, accuracy = model.evaluate(X_test, y_test)   #测试

print('test loss:',loss)
print('test accuracy:',accuracy)

Insert picture description here

Published 233 original articles · praised 645 · 30,000+ views

Guess you like

Origin blog.csdn.net/weixin_37763870/article/details/105594347