Digital Recognition using keras model training, and the training output accuracy

from keras.datasets import mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
#train_images 和 train_labels 是训练集
train_images.shape # The first number indicates the number of pictures, behind the express image size, and different before I met on the OpenCV 
# in front represents the picture size on the opencv, behind the picture indicates the number of channels

Output:

(60000, 28, 28)

len(train_labels)

Output:
60,000

from keras import models
from keras import layers

Here begin construction neural network:

network = models.Sequential()
network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28,)))#果然shape是28*28!!!
network.add(layers.Dense(10, activation='softmax'))

Precompiled:

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

Start training model:

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

Output:

Epoch 1/5
60000/60000 [==============================] - 7s 111us/step - loss: 0.2523 - acc: 0.9274
Epoch 2/5
60000/60000 [==============================] - 7s 111us/step - loss: 0.1029 - acc: 0.9689 5s - loss: 0.1212
Epoch 3/5
60000/60000 [==============================] - 7s 116us/step - loss: 0.0677 - acc: 0.9795
Epoch 4/5
60000/60000 [==============================] - 8s 130us/step - loss: 0.0504 - acc: 0.9848
Epoch 5/5
60000/60000 [==============================] - 7s 119us/step - loss: 0.0374 - acc: 0.9886 2s - loss: 0.0370 -
Out[12]:
<keras.callbacks.History at 0x1c6e30c1828>

Thus the recognition accuracy can be obtained 98%

Test set validation:

 test_loss, test_acc = network.evaluate(test_images, test_labels)

Output accuracy:

 Print ( ' recognition accuracy: ' , test_acc)

Recognition accuracy:
0.9807

Guess you like

Origin www.cnblogs.com/geeksongs/p/11102118.html
Recommended