Keras学习(三)——分类classification

本文主要介绍使用keras搭建神经网络并对手写数字进行分类。

代码:

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(1337)

# 下载数据集
# X_shape(60000 28x28),y shape(10000)
(X_train, y_train), (X_test, y_test) = mnist.load_data()

# 预处理数据
'''
X_train.reshape(X_train.shape[0], -1) 将60000个28x28的数据变为60000x784
/255:把数据标准化到[0,1]
'''
X_train = X_train.reshape(X_train.shape[0], -1) / 255  # 标准化
X_test = X_test.reshape(X_test.shape[0], -1) / 255  # 标准化
# 将标签变为one-hot形式
y_train = np_utils.to_categorical(y_train, num_classes=10)
y_test = np_utils.to_categorical(y_test, num_classes=10)

# 搭建神经网络
model = Sequential([
    Dense(32, input_dim=784),
    Activation('relu'),
    Dense(10),   # 默认上一层的输出为本层输入
    Activation('softmax'),
])

# 定义Optimizer
rmsprop = RMSprop(lr=0.001, rho=0.9, epsilon=1e-8, decay=0.0)

# 激活模型
model.compile(
    optimizer=rmsprop,   # optimizer='rmsprop'使用默认的rmsprop优化函数
    loss='categorical_crossentropy',
    metrics=['accuracy'],
)

# 训练
print('Training......')
model.fit(X_train, y_train, nb_epoch=2, batch_size=32)

# 测试
print('\nTesting......')
loss, accuracy = model.evaluate(X_test, y_test)

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

训练结果:

Training......
Epoch 1/2
2018-10-29 19:44:58.332022: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2

   32/60000 [..............................] - ETA: 28:08 - loss: 2.4464 - acc: 0.0625
  448/60000 [..............................] - ETA: 2:06 - loss: 2.0239 - acc: 0.3482 
 1216/60000 [..............................] - ETA: 48s - loss: 1.6656 - acc: 0.5403 
 1856/60000 [..............................] - ETA: 33s - loss: 1.4600 - acc: 0.6158
 2656/60000 [>.............................] - ETA: 23s - loss: 1.2610 - acc: 0.6803
 3392/60000 [>.............................] - ETA: 19s - loss: 1.1350 - acc: 0.7134
 4160/60000 [=>............................] - ETA: 16s - loss: 1.0305 - acc: 0.7411
 4832/60000 [=>............................] - ETA: 14s - loss: 0.9582 - acc: 0.7581
 5440/60000 [=>............................] - ETA: 13s - loss: 0.9133 - acc: 0.7691
 6112/60000 [==>...........................] - ETA: 12s - loss: 0.8695 - acc: 0.7806
 6848/60000 [==>...........................] - ETA: 10s - loss: 0.8205 - acc: 0.7935
 7424/60000 [==>...........................] - ETA: 10s - loss: 0.7875 - acc: 0.8019
 8192/60000 [===>..........................] - ETA: 9s - loss: 0.7532 - acc: 0.8104 
 8928/60000 [===>..........................] - ETA: 8s - loss: 0.7209 - acc: 0.8185
 9600/60000 [===>..........................] - ETA: 8s - loss: 0.6998 - acc: 0.8227
10432/60000 [====>.........................] - ETA: 7s - loss: 0.6741 - acc: 0.8275
11200/60000 [====>.........................] - ETA: 7s - loss: 0.6544 - acc: 0.8313
11872/60000 [====>.........................] - ETA: 7s - loss: 0.6361 - acc: 0.8351
12448/60000 [=====>........................] - ETA: 6s - loss: 0.6232 - acc: 0.8386
12992/60000 [=====>........................] - ETA: 6s - loss: 0.6110 - acc: 0.8409
13600/60000 [=====>........................] - ETA: 6s - loss: 0.5990 - acc: 0.8430
14016/60000 [======>.......................] - ETA: 6s - loss: 0.5916 - acc: 0.8450
14528/60000 [======>.......................] - ETA: 6s - loss: 0.5817 - acc: 0.8476
15008/60000 [======>.......................] - ETA: 6s - loss: 0.5771 - acc: 0.8483
15552/60000 [======>.......................] - ETA: 6s - loss: 0.5683 - acc: 0.8502
16160/60000 [=======>......................] - ETA: 5s - loss: 0.5590 - acc: 0.8528
16768/60000 [=======>......................] - ETA: 5s - loss: 0.5504 - acc: 0.8548
17184/60000 [=======>......................] - ETA: 5s - loss: 0.5452 - acc: 0.8559
17760/60000 [=======>......................] - ETA: 5s - loss: 0.5365 - acc: 0.8581
18368/60000 [========>.....................] - ETA: 5s - loss: 0.5280 - acc: 0.8601
18976/60000 [========>.....................] - ETA: 5s - loss: 0.5219 - acc: 0.8616
19616/60000 [========>.....................] - ETA: 5s - loss: 0.5150 - acc: 0.8630
20192/60000 [=========>....................] - ETA: 5s - loss: 0.5079 - acc: 0.8649
20832/60000 [=========>....................] - ETA: 4s - loss: 0.5018 - acc: 0.8662
21376/60000 [=========>....................] - ETA: 4s - loss: 0.4973 - acc: 0.8673
22016/60000 [==========>...................] - ETA: 4s - loss: 0.4935 - acc: 0.8684
22592/60000 [==========>...................] - ETA: 4s - loss: 0.4891 - acc: 0.8693
23168/60000 [==========>...................] - ETA: 4s - loss: 0.4852 - acc: 0.8702
23744/60000 [==========>...................] - ETA: 4s - loss: 0.4808 - acc: 0.8711
24352/60000 [===========>..................] - ETA: 4s - loss: 0.4765 - acc: 0.8722
24928/60000 [===========>..................] - ETA: 4s - loss: 0.4718 - acc: 0.8731
25568/60000 [===========>..................] - ETA: 4s - loss: 0.4672 - acc: 0.8741
26208/60000 [============>.................] - ETA: 3s - loss: 0.4630 - acc: 0.8750
26848/60000 [============>.................] - ETA: 3s - loss: 0.4597 - acc: 0.8757
27424/60000 [============>.................] - ETA: 3s - loss: 0.4558 - acc: 0.8765
28128/60000 [=============>................] - ETA: 3s - loss: 0.4518 - acc: 0.8774
28736/60000 [=============>................] - ETA: 3s - loss: 0.4488 - acc: 0.8782
29376/60000 [=============>................] - ETA: 3s - loss: 0.4454 - acc: 0.8789
29984/60000 [=============>................] - ETA: 3s - loss: 0.4416 - acc: 0.8797
30688/60000 [==============>...............] - ETA: 3s - loss: 0.4370 - acc: 0.8808
31424/60000 [==============>...............] - ETA: 3s - loss: 0.4328 - acc: 0.8819
32256/60000 [===============>..............] - ETA: 3s - loss: 0.4292 - acc: 0.8828
32960/60000 [===============>..............] - ETA: 2s - loss: 0.4261 - acc: 0.8838
33696/60000 [===============>..............] - ETA: 2s - loss: 0.4217 - acc: 0.8845
34464/60000 [================>.............] - ETA: 2s - loss: 0.4193 - acc: 0.8851
35200/60000 [================>.............] - ETA: 2s - loss: 0.4175 - acc: 0.8851
35744/60000 [================>.............] - ETA: 2s - loss: 0.4146 - acc: 0.8859
36480/60000 [=================>............] - ETA: 2s - loss: 0.4116 - acc: 0.8868
37248/60000 [=================>............] - ETA: 2s - loss: 0.4085 - acc: 0.8876
38016/60000 [==================>...........] - ETA: 2s - loss: 0.4065 - acc: 0.8881
38720/60000 [==================>...........] - ETA: 2s - loss: 0.4038 - acc: 0.8887
39488/60000 [==================>...........] - ETA: 2s - loss: 0.4014 - acc: 0.8892
40192/60000 [===================>..........] - ETA: 2s - loss: 0.3992 - acc: 0.8898
40960/60000 [===================>..........] - ETA: 1s - loss: 0.3969 - acc: 0.8905
41728/60000 [===================>..........] - ETA: 1s - loss: 0.3947 - acc: 0.8911
42432/60000 [====================>.........] - ETA: 1s - loss: 0.3928 - acc: 0.8916
43040/60000 [====================>.........] - ETA: 1s - loss: 0.3907 - acc: 0.8921
43776/60000 [====================>.........] - ETA: 1s - loss: 0.3880 - acc: 0.8929
44544/60000 [=====================>........] - ETA: 1s - loss: 0.3867 - acc: 0.8931
45152/60000 [=====================>........] - ETA: 1s - loss: 0.3844 - acc: 0.8938
45856/60000 [=====================>........] - ETA: 1s - loss: 0.3818 - acc: 0.8945
46560/60000 [======================>.......] - ETA: 1s - loss: 0.3797 - acc: 0.8952
47328/60000 [======================>.......] - ETA: 1s - loss: 0.3772 - acc: 0.8957
48032/60000 [=======================>......] - ETA: 1s - loss: 0.3748 - acc: 0.8963
48672/60000 [=======================>......] - ETA: 1s - loss: 0.3734 - acc: 0.8967
49312/60000 [=======================>......] - ETA: 1s - loss: 0.3715 - acc: 0.8972
49856/60000 [=======================>......] - ETA: 0s - loss: 0.3696 - acc: 0.8977
50432/60000 [========================>.....] - ETA: 0s - loss: 0.3685 - acc: 0.8980
51072/60000 [========================>.....] - ETA: 0s - loss: 0.3672 - acc: 0.8985
51712/60000 [========================>.....] - ETA: 0s - loss: 0.3653 - acc: 0.8989
52384/60000 [=========================>....] - ETA: 0s - loss: 0.3634 - acc: 0.8995
53024/60000 [=========================>....] - ETA: 0s - loss: 0.3617 - acc: 0.9000
53504/60000 [=========================>....] - ETA: 0s - loss: 0.3610 - acc: 0.9003
54048/60000 [==========================>...] - ETA: 0s - loss: 0.3593 - acc: 0.9006
54624/60000 [==========================>...] - ETA: 0s - loss: 0.3577 - acc: 0.9010
55232/60000 [==========================>...] - ETA: 0s - loss: 0.3561 - acc: 0.9015
55808/60000 [==========================>...] - ETA: 0s - loss: 0.3545 - acc: 0.9019
56352/60000 [===========================>..] - ETA: 0s - loss: 0.3528 - acc: 0.9023
56992/60000 [===========================>..] - ETA: 0s - loss: 0.3514 - acc: 0.9027
57696/60000 [===========================>..] - ETA: 0s - loss: 0.3499 - acc: 0.9031
58368/60000 [============================>.] - ETA: 0s - loss: 0.3484 - acc: 0.9035
58848/60000 [============================>.] - ETA: 0s - loss: 0.3468 - acc: 0.9039
59520/60000 [============================>.] - ETA: 0s - loss: 0.3454 - acc: 0.9044
60000/60000 [==============================] - 6s 94us/step - loss: 0.3443 - acc: 0.9046
Epoch 2/2

   32/60000 [..............................] - ETA: 15s - loss: 0.0667 - acc: 1.0000
  704/60000 [..............................] - ETA: 4s - loss: 0.2199 - acc: 0.9375 
 1184/60000 [..............................] - ETA: 5s - loss: 0.2214 - acc: 0.9358
 1728/60000 [..............................] - ETA: 5s - loss: 0.2245 - acc: 0.9358
 2368/60000 [>.............................] - ETA: 5s - loss: 0.2321 - acc: 0.9375
 3136/60000 [>.............................] - ETA: 4s - loss: 0.2355 - acc: 0.9362
 3808/60000 [>.............................] - ETA: 4s - loss: 0.2261 - acc: 0.9380
 4384/60000 [=>............................] - ETA: 4s - loss: 0.2267 - acc: 0.9368
 5088/60000 [=>............................] - ETA: 4s - loss: 0.2225 - acc: 0.9373
 5856/60000 [=>............................] - ETA: 4s - loss: 0.2181 - acc: 0.9377
 6624/60000 [==>...........................] - ETA: 4s - loss: 0.2224 - acc: 0.9372
 7232/60000 [==>...........................] - ETA: 4s - loss: 0.2223 - acc: 0.9367
 7712/60000 [==>...........................] - ETA: 4s - loss: 0.2227 - acc: 0.9363
 8416/60000 [===>..........................] - ETA: 4s - loss: 0.2259 - acc: 0.9360
 9216/60000 [===>..........................] - ETA: 3s - loss: 0.2211 - acc: 0.9365
 9760/60000 [===>..........................] - ETA: 3s - loss: 0.2211 - acc: 0.9363
10432/60000 [====>.........................] - ETA: 3s - loss: 0.2185 - acc: 0.9370
11200/60000 [====>.........................] - ETA: 3s - loss: 0.2182 - acc: 0.9378
11936/60000 [====>.........................] - ETA: 3s - loss: 0.2172 - acc: 0.9384
12320/60000 [=====>........................] - ETA: 3s - loss: 0.2176 - acc: 0.9386
13056/60000 [=====>........................] - ETA: 3s - loss: 0.2170 - acc: 0.9390
13792/60000 [=====>........................] - ETA: 3s - loss: 0.2159 - acc: 0.9392
14464/60000 [======>.......................] - ETA: 3s - loss: 0.2156 - acc: 0.9390
15040/60000 [======>.......................] - ETA: 3s - loss: 0.2161 - acc: 0.9391
15776/60000 [======>.......................] - ETA: 3s - loss: 0.2128 - acc: 0.9403
16512/60000 [=======>......................] - ETA: 3s - loss: 0.2106 - acc: 0.9405
17152/60000 [=======>......................] - ETA: 3s - loss: 0.2098 - acc: 0.9407
17824/60000 [=======>......................] - ETA: 3s - loss: 0.2089 - acc: 0.9411
18560/60000 [========>.....................] - ETA: 3s - loss: 0.2096 - acc: 0.9415
19296/60000 [========>.....................] - ETA: 3s - loss: 0.2086 - acc: 0.9414
19936/60000 [========>.....................] - ETA: 3s - loss: 0.2074 - acc: 0.9419
20576/60000 [=========>....................] - ETA: 3s - loss: 0.2076 - acc: 0.9417
21216/60000 [=========>....................] - ETA: 3s - loss: 0.2063 - acc: 0.9422
21984/60000 [=========>....................] - ETA: 2s - loss: 0.2070 - acc: 0.9418
22560/60000 [==========>...................] - ETA: 2s - loss: 0.2061 - acc: 0.9421
23232/60000 [==========>...................] - ETA: 2s - loss: 0.2057 - acc: 0.9422
23840/60000 [==========>...................] - ETA: 2s - loss: 0.2059 - acc: 0.9420
24512/60000 [===========>..................] - ETA: 2s - loss: 0.2046 - acc: 0.9422
25184/60000 [===========>..................] - ETA: 2s - loss: 0.2042 - acc: 0.9423
25824/60000 [===========>..................] - ETA: 2s - loss: 0.2039 - acc: 0.9423
26464/60000 [============>.................] - ETA: 2s - loss: 0.2040 - acc: 0.9422
27072/60000 [============>.................] - ETA: 2s - loss: 0.2054 - acc: 0.9417
27648/60000 [============>.................] - ETA: 2s - loss: 0.2054 - acc: 0.9416
28160/60000 [=============>................] - ETA: 2s - loss: 0.2048 - acc: 0.9418
28640/60000 [=============>................] - ETA: 2s - loss: 0.2051 - acc: 0.9418
29216/60000 [=============>................] - ETA: 2s - loss: 0.2054 - acc: 0.9417
29696/60000 [=============>................] - ETA: 2s - loss: 0.2057 - acc: 0.9415
30304/60000 [==============>...............] - ETA: 2s - loss: 0.2053 - acc: 0.9418
30880/60000 [==============>...............] - ETA: 2s - loss: 0.2050 - acc: 0.9419
31488/60000 [==============>...............] - ETA: 2s - loss: 0.2044 - acc: 0.9420
32128/60000 [===============>..............] - ETA: 2s - loss: 0.2048 - acc: 0.9420
32736/60000 [===============>..............] - ETA: 2s - loss: 0.2048 - acc: 0.9419
33344/60000 [===============>..............] - ETA: 2s - loss: 0.2043 - acc: 0.9420
34016/60000 [================>.............] - ETA: 2s - loss: 0.2035 - acc: 0.9422
34688/60000 [================>.............] - ETA: 2s - loss: 0.2037 - acc: 0.9421
35360/60000 [================>.............] - ETA: 1s - loss: 0.2035 - acc: 0.9419
35936/60000 [================>.............] - ETA: 1s - loss: 0.2031 - acc: 0.9420
36576/60000 [=================>............] - ETA: 1s - loss: 0.2030 - acc: 0.9419
37216/60000 [=================>............] - ETA: 1s - loss: 0.2027 - acc: 0.9419
37856/60000 [=================>............] - ETA: 1s - loss: 0.2018 - acc: 0.9421
38528/60000 [==================>...........] - ETA: 1s - loss: 0.2014 - acc: 0.9422
39072/60000 [==================>...........] - ETA: 1s - loss: 0.2009 - acc: 0.9423
39744/60000 [==================>...........] - ETA: 1s - loss: 0.1998 - acc: 0.9425
40448/60000 [===================>..........] - ETA: 1s - loss: 0.1987 - acc: 0.9428
41120/60000 [===================>..........] - ETA: 1s - loss: 0.1986 - acc: 0.9429
41760/60000 [===================>..........] - ETA: 1s - loss: 0.1983 - acc: 0.9431
42336/60000 [====================>.........] - ETA: 1s - loss: 0.1985 - acc: 0.9431
43008/60000 [====================>.........] - ETA: 1s - loss: 0.1979 - acc: 0.9432
43776/60000 [====================>.........] - ETA: 1s - loss: 0.1978 - acc: 0.9432
44576/60000 [=====================>........] - ETA: 1s - loss: 0.1975 - acc: 0.9434
45216/60000 [=====================>........] - ETA: 1s - loss: 0.1976 - acc: 0.9434
45952/60000 [=====================>........] - ETA: 1s - loss: 0.1976 - acc: 0.9434
46752/60000 [======================>.......] - ETA: 1s - loss: 0.1973 - acc: 0.9435
47520/60000 [======================>.......] - ETA: 0s - loss: 0.1970 - acc: 0.9436
48192/60000 [=======================>......] - ETA: 0s - loss: 0.1971 - acc: 0.9434
48960/60000 [=======================>......] - ETA: 0s - loss: 0.1966 - acc: 0.9434
49664/60000 [=======================>......] - ETA: 0s - loss: 0.1970 - acc: 0.9434
50464/60000 [========================>.....] - ETA: 0s - loss: 0.1973 - acc: 0.9433
51200/60000 [========================>.....] - ETA: 0s - loss: 0.1970 - acc: 0.9435
51968/60000 [========================>.....] - ETA: 0s - loss: 0.1963 - acc: 0.9437
52608/60000 [=========================>....] - ETA: 0s - loss: 0.1961 - acc: 0.9437
53376/60000 [=========================>....] - ETA: 0s - loss: 0.1964 - acc: 0.9436
54144/60000 [==========================>...] - ETA: 0s - loss: 0.1967 - acc: 0.9434
54912/60000 [==========================>...] - ETA: 0s - loss: 0.1967 - acc: 0.9433
55680/60000 [==========================>...] - ETA: 0s - loss: 0.1963 - acc: 0.9435
56512/60000 [===========================>..] - ETA: 0s - loss: 0.1968 - acc: 0.9435
57280/60000 [===========================>..] - ETA: 0s - loss: 0.1962 - acc: 0.9437
57984/60000 [===========================>..] - ETA: 0s - loss: 0.1959 - acc: 0.9436
58720/60000 [============================>.] - ETA: 0s - loss: 0.1954 - acc: 0.9438
59488/60000 [============================>.] - ETA: 0s - loss: 0.1952 - acc: 0.9438
60000/60000 [==============================] - 5s 77us/step - loss: 0.1950 - acc: 0.9439

Testing......

   32/10000 [..............................] - ETA: 16s
 1472/10000 [===>..........................] - ETA: 0s 
 3040/10000 [========>.....................] - ETA: 0s
 4576/10000 [============>.................] - ETA: 0s
 6016/10000 [=================>............] - ETA: 0s
 7360/10000 [=====================>........] - ETA: 0s
 8384/10000 [========================>.....] - ETA: 0s
 9408/10000 [===========================>..] - ETA: 0s
10000/10000 [==============================] - 0s 44us/step
test loss 0.17443157320916652
test accuracy 0.9512

 

你可能感兴趣的:(Keras)