莫烦keras学习自修第四天分类问题

Posted liuzhiqaingxyz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了莫烦keras学习自修第四天分类问题相关的知识,希望对你有一定的参考价值。

1.代码实战

#!/usr/bin/env python
#! _*_ coding:UTF-8 _*_

# 导入numpy
import numpy as np
np.random.seed(1337)
# 导入验证码图片数据集
from keras.datasets import mnist
from keras.utils import np_utils
# 导入kearas的模型
from keras.models import Sequential
# 导入keras的层和激励函数
from keras.layers import Dense, Activation
# 导入keras的优化器
from keras.optimizers import RMSprop

(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)
y_test = np_utils.to_categorical(y_test, num_classes=10)

# 生成训练模型,传入每个层及激励函数构造训练模型
model = Sequential([
    Dense(32, input_dim=784),
    Activation(relu),
    Dense(10),
    Activation(softmax),
])

# 自定义优化器
rmsprop = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)

# 使用优化器,和误差函数等编译训练模型
model.compile(optimizer=rmsprop,
              loss=categorical_crossentropy,
              metrics=[accuracy])

# 开始训练神经网络
model.fit(X_train, y_train, epochs=2, batch_size=32)

# 开始测试神经网络
loss, accuracy = model.evaluate(X_test, y_test)

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

结果:

/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/keras_day03/classifier.py
Using Theano backend.
Downloading data from https://s3.amazonaws.com/img-datasets/mnist.npz

   16384/11490434 [..............................] - ETA: 0s
   24576/11490434 [..............................] - ETA: 9:33
   40960/11490434 [..............................] - ETA: 10:31
   57344/11490434 [..............................] - ETA: 10:12
   73728/11490434 [..............................] - ETA: 10:02
   90112/11490434 [..............................] - ETA: 9:04 
  106496/11490434 [..............................] - ETA: 9:07
  122880/11490434 [..............................] - ETA: 8:33
  139264/11490434 [..............................] - ETA: 8:37
  163840/11490434 [..............................] - ETA: 7:47
  180224/11490434 [..............................] - ETA: 7:22
  196608/11490434 [..............................] - ETA: 7:00
  212992/11490434 [..............................] - ETA: 6:46
  229376/11490434 [..............................] - ETA: 6:30
  245760/11490434 [..............................] - ETA: 6:16
  262144/11490434 [..............................] - ETA: 6:03
  278528/11490434 [..............................] - ETA: 5:52
  303104/11490434 [..............................] - ETA: 5:32
  319488/11490434 [..............................] - ETA: 5:22
  335872/11490434 [..............................] - ETA: 5:14
  352256/11490434 [..............................] - ETA: 5:02
  368640/11490434 [..............................] - ETA: 4:54
  385024/11490434 [>.............................] - ETA: 4:43
  401408/11490434 [>.............................] - ETA: 4:36
  417792/11490434 [>.............................] - ETA: 4:27
  442368/11490434 [>.............................] - ETA: 4:16
  458752/11490434 [>.............................] - ETA: 4:08
  475136/11490434 [>.............................] - ETA: 4:01
  491520/11490434 [>.............................] - ETA: 3:55
  524288/11490434 [>.............................] - ETA: 3:41
  540672/11490434 [>.............................] - ETA: 3:36
  557056/11490434 [>.............................] - ETA: 3:31
  598016/11490434 [>.............................] - ETA: 3:17
  614400/11490434 [>.............................] - ETA: 3:13
  630784/11490434 [>.............................] - ETA: 3:09
  647168/11490434 [>.............................] - ETA: 3:05
  679936/11490434 [>.............................] - ETA: 2:56
  720896/11490434 [>.............................] - ETA: 2:47
  753664/11490434 [>.............................] - ETA: 2:41
  786432/11490434 [=>............................] - ETA: 2:35
  819200/11490434 [=>............................] - ETA: 2:29
  851968/11490434 [=>............................] - ETA: 2:24
  860160/11490434 [=>............................] - ETA: 2:23
  892928/11490434 [=>............................] - ETA: 2:18
  942080/11490434 [=>............................] - ETA: 2:11
  958464/11490434 [=>............................] - ETA: 2:09
  999424/11490434 [=>............................] - ETA: 2:04
 1015808/11490434 [=>............................] - ETA: 2:03
 1081344/11490434 [=>............................] - ETA: 1:55
 1114112/11490434 [=>............................] - ETA: 1:52
 1138688/11490434 [=>............................] - ETA: 1:51
 1204224/11490434 [==>...........................] - ETA: 1:44
 1236992/11490434 [==>...........................] - ETA: 1:42
 1277952/11490434 [==>...........................] - ETA: 1:39
 1294336/11490434 [==>...........................] - ETA: 1:38
 1327104/11490434 [==>...........................] - ETA: 1:36
 1392640/11490434 [==>...........................] - ETA: 1:31
 1433600/11490434 [==>...........................] - ETA: 1:29
 1466368/11490434 [==>...........................] - ETA: 1:27
 1572864/11490434 [===>..........................] - ETA: 1:21
 1589248/11490434 [===>..........................] - ETA: 1:20
 1622016/11490434 [===>..........................] - ETA: 1:19
 1695744/11490434 [===>..........................] - ETA: 1:15
 1744896/11490434 [===>..........................] - ETA: 1:13
 1761280/11490434 [===>..........................] - ETA: 1:13
 1810432/11490434 [===>..........................] - ETA: 1:11
 1900544/11490434 [===>..........................] - ETA: 1:07
 1974272/11490434 [====>.........................] - ETA: 1:05
 2007040/11490434 [====>.........................] - ETA: 1:04
 2113536/11490434 [====>.........................] - ETA: 1:00
 2179072/11490434 [====>.........................] - ETA: 58s 
 2195456/11490434 [====>.........................] - ETA: 58s
 2244608/11490434 [====>.........................] - ETA: 57s
 2301952/11490434 [=====>........................] - ETA: 55s
 2424832/11490434 [=====>........................] - ETA: 52s
 2441216/11490434 [=====>........................] - ETA: 52s
 2490368/11490434 [=====>........................] - ETA: 51s
 2564096/11490434 [=====>........................] - ETA: 49s
 2613248/11490434 [=====>........................] - ETA: 48s
 2703360/11490434 [======>.......................] - ETA: 46s
 2752512/11490434 [======>.......................] - ETA: 45s
 2826240/11490434 [======>.......................] - ETA: 44s
 2875392/11490434 [======>.......................] - ETA: 43s
 2949120/11490434 [======>.......................] - ETA: 42s
 3014656/11490434 [======>.......................] - ETA: 41s
 3121152/11490434 [=======>......................] - ETA: 39s
 3137536/11490434 [=======>......................] - ETA: 39s
 3186688/11490434 [=======>......................] - ETA: 38s
 3276800/11490434 [=======>......................] - ETA: 37s
 3383296/11490434 [=======>......................] - ETA: 36s
 3448832/11490434 [========>.....................] - ETA: 35s
 3522560/11490434 [========>.....................] - ETA: 34s
 3588096/11490434 [========>.....................] - ETA: 33s
 3645440/11490434 [========>.....................] - ETA: 33s
 3710976/11490434 [========>.....................] - ETA: 32s
 3801088/11490434 [========>.....................] - ETA: 31s
 3883008/11490434 [=========>....................] - ETA: 30s
 3956736/11490434 [=========>....................] - ETA: 29s
 4038656/11490434 [=========>....................] - ETA: 29s
 4145152/11490434 [=========>....................] - ETA: 28s
 4202496/11490434 [=========>....................] - ETA: 27s
 4284416/11490434 [==========>...................] - ETA: 27s
 4390912/11490434 [==========>...................] - ETA: 26s
 4440064/11490434 [==========>...................] - ETA: 25s
 4530176/11490434 [==========>...................] - ETA: 25s
 4636672/11490434 [===========>..................] - ETA: 24s
 4685824/11490434 [===========>..................] - ETA: 23s
 4775936/11490434 [===========>..................] - ETA: 23s
 4792320/11490434 [===========>..................] - ETA: 23s
 4898816/11490434 [===========>..................] - ETA: 22s
 4931584/11490434 [===========>..................] - ETA: 22s
 4997120/11490434 [============>.................] - ETA: 21s
 5038080/11490434 [============>.................] - ETA: 21s
 5103616/11490434 [============>.................] - ETA: 21s
 5177344/11490434 [============>.................] - ETA: 20s
 5242880/11490434 [============>.................] - ETA: 20s
 5292032/11490434 [============>.................] - ETA: 20s
 5398528/11490434 [=============>................] - ETA: 19s
 5455872/11490434 [=============>................] - ETA: 19s
 5505024/11490434 [=============>................] - ETA: 18s
 5554176/11490434 [=============>................] - ETA: 18s
 5660672/11490434 [=============>................] - ETA: 18s
 5726208/11490434 [=============>................] - ETA: 17s
 5767168/11490434 [==============>...............] - ETA: 17s
 5816320/11490434 [==============>...............] - ETA: 17s
 5922816/11490434 [==============>...............] - ETA: 16s
 5971968/11490434 [==============>...............] - ETA: 16s
 6029312/11490434 [==============>...............] - ETA: 16s
 6062080/11490434 [==============>...............] - ETA: 16s
 6094848/11490434 [==============>...............] - ETA: 15s
 6111232/11490434 [==============>...............] - ETA: 15s
 6266880/11490434 [===============>..............] - ETA: 15s
 6324224/11490434 [===============>..............] - ETA: 14s
 6340608/11490434 [===============>..............] - ETA: 14s
 6512640/11490434 [================>.............] - ETA: 14s
 6545408/11490434 [================>.............] - ETA: 13s
 6586368/11490434 [================>.............] - ETA: 13s
 6602752/11490434 [================>.............] - ETA: 13s
 6758400/11490434 [================>.............] - ETA: 13s
 6807552/11490434 [================>.............] - ETA: 12s
 6848512/11490434 [================>.............] - ETA: 12s
 6864896/11490434 [================>.............] - ETA: 12s
 6930432/11490434 [=================>............] - ETA: 12s
 7086080/11490434 [=================>............] - ETA: 11s
 7127040/11490434 [=================>............] - ETA: 11s
 7143424/11490434 [=================>............] - ETA: 11s
 7225344/11490434 [=================>............] - ETA: 11s
 7380992/11490434 [==================>...........] - ETA: 10s
 7421952/11490434 [==================>...........] - ETA: 10s
 7438336/11490434 [==================>...........] - ETA: 10s
 7544832/11490434 [==================>...........] - ETA: 10s
 7659520/11490434 [==================>...........] - ETA: 9s 
 7733248/11490434 [===================>..........] - ETA: 9s
 7766016/11490434 [===================>..........] - ETA: 9s
 7823360/11490434 [===================>..........] - ETA: 9s
 7921664/11490434 [===================>..........] - ETA: 8s
 8077312/11490434 [====================>.........] - ETA: 8s
 8118272/11490434 [====================>.........] - ETA: 8s
 8167424/11490434 [====================>.........] - ETA: 8s
 8290304/11490434 [====================>.........] - ETA: 7s
 8429568/11490434 [=====================>........] - ETA: 7s
 8478720/11490434 [=====================>........] - ETA: 7s
 8536064/11490434 [=====================>........] - ETA: 7s
 8675328/11490434 [=====================>........] - ETA: 6s
 8814592/11490434 [======================>.......] - ETA: 6s
 8863744/11490434 [======================>.......] - ETA: 6s
 8970240/11490434 [======================>.......] - ETA: 5s
 9003008/11490434 [======================>.......] - ETA: 5s
 9216000/11490434 [=======================>......] - ETA: 5s
 9265152/11490434 [=======================>......] - ETA: 5s
 9748480/11490434 [========================>.....] - ETA: 3s
 9822208/11490434 [========================>.....] - ETA: 3s
 9912320/11490434 [========================>.....] - ETA: 3s
 9945088/11490434 [========================>.....] - ETA: 3s
10027008/11490434 [=========================>....] - ETA: 3s
10100736/11490434 [=========================>....] - ETA: 2s
10190848/11490434 [=========================>....] - ETA: 2s
10272768/11490434 [=========================>....] - ETA: 2s
10354688/11490434 [==========================>...] - ETA: 2s
10395648/11490434 [==========================>...] - ETA: 2s
10502144/11490434 [==========================>...] - ETA: 2s
10551296/11490434 [==========================>...] - ETA: 1s
10625024/11490434 [==========================>...] - ETA: 1s
10690560/11490434 [==========================>...] - ETA: 1s
10764288/11490434 [===========================>..] - ETA: 1s
10846208/11490434 [===========================>..] - ETA: 1s
10919936/11490434 [===========================>..] - ETA: 1s
11337728/11490434 [============================>.] - ETA: 0s
11403264/11490434 [============================>.] - ETA: 0s
11493376/11490434 [==============================] - 23s 2us/step

11501568/11490434 [==============================] - 23s 2us/step
Epoch 1/2

   32/60000 [..............................] - ETA: 26s - loss: 2.4741 - acc: 0.0625
 1568/60000 [..............................] - ETA: 2s - loss: 1.6006 - acc: 0.5504 
 3264/60000 [>.............................] - ETA: 1s - loss: 1.2046 - acc: 0.6863
 4704/60000 [=>............................] - ETA: 1s - loss: 1.0104 - acc: 0.7428
 6368/60000 [==>...........................] - ETA: 1s - loss: 0.8834 - acc: 0.7747
 8000/60000 [===>..........................] - ETA: 1s - loss: 0.7862 - acc: 0.7997
 9728/60000 [===>..........................] - ETA: 1s - loss: 0.7161 - acc: 0.8150
11488/60000 [====>.........................] - ETA: 1s - loss: 0.6655 - acc: 0.8258
13376/60000 [=====>........................] - ETA: 1s - loss: 0.6203 - acc: 0.8360
15040/60000 [======>.......................] - ETA: 1s - loss: 0.5930 - acc: 0.8415
16928/60000 [=======>......................] - ETA: 1s - loss: 0.5626 - acc: 0.8487
18880/60000 [========>.....................] - ETA: 1s - loss: 0.5363 - acc: 0.8555
20832/60000 [=========>....................] - ETA: 1s - loss: 0.5138 - acc: 0.8611
22368/60000 [==========>...................] - ETA: 1s - loss: 0.5025 - acc: 0.8642
24288/60000 [===========>..................] - ETA: 1s - loss: 0.4874 - acc: 0.8675
26208/60000 [============>.................] - ETA: 0s - loss: 0.4728 - acc: 0.8708
28128/60000 [=============>................] - ETA: 0s - loss: 0.4608 - acc: 0.8732
30016/60000 [==============>...............] - ETA: 0s - loss: 0.4503 - acc: 0.8757
31616/60000 [==============>...............] - ETA: 0s - loss: 0.4406 - acc: 0.8779
33504/60000 [===============>..............] - ETA: 0s - loss: 0.4309 - acc: 0.8807
35296/60000 [================>.............] - ETA: 0s - loss: 0.4251 - acc: 0.8816
37120/60000 [=================>............] - ETA: 0s - loss: 0.4171 - acc: 0.8838
38976/60000 [==================>...........] - ETA: 0s - loss: 0.4101 - acc: 0.8855
40416/60000 [===================>..........] - ETA: 0s - loss: 0.4060 - acc: 0.8868
42240/60000 [====================>.........] - ETA: 0s - loss: 0.4000 - acc: 0.8884
44064/60000 [=====================>........] - ETA: 0s - loss: 0.3941 - acc: 0.8900
45888/60000 [=====================>........] - ETA: 0s - loss: 0.3875 - acc: 0.8920
47680/60000 [======================>.......] - ETA: 0s - loss: 0.3813 - acc: 0.8935
49184/60000 [=======================>......] - ETA: 0s - loss: 0.3773 - acc: 0.8945
51008/60000 [========================>.....] - ETA: 0s - loss: 0.3723 - acc: 0.8959
52832/60000 [=========================>....] - ETA: 0s - loss: 0.3669 - acc: 0.8973
54656/60000 [==========================>...] - ETA: 0s - loss: 0.3621 - acc: 0.8986
56480/60000 [===========================>..] - ETA: 0s - loss: 0.3577 - acc: 0.8998
57920/60000 [===========================>..] - ETA: 0s - loss: 0.3540 - acc: 0.9007
59744/60000 [============================>.] - ETA: 0s - loss: 0.3492 - acc: 0.9020
60000/60000 [==============================] - 2s 29us/step - loss: 0.3488 - acc: 0.9021
Epoch 2/2

   32/60000 [..............................] - ETA: 6s - loss: 0.0511 - acc: 0.9688
 1856/60000 [..............................] - ETA: 1s - loss: 0.2283 - acc: 0.9316
 3584/60000 [>.............................] - ETA: 1s - loss: 0.2271 - acc: 0.9350
 5216/60000 [=>............................] - ETA: 1s - loss: 0.2180 - acc: 0.9356
 6752/60000 [==>...........................] - ETA: 1s - loss: 0.2226 - acc: 0.9347
 8416/60000 [===>..........................] - ETA: 1s - loss: 0.2237 - acc: 0.9351
10080/60000 [====>.........................] - ETA: 1s - loss: 0.2214 - acc: 0.9350
11744/60000 [====>.........................] - ETA: 1s - loss: 0.2173 - acc: 0.9367
13248/60000 [=====>........................] - ETA: 1s - loss: 0.2177 - acc: 0.9372
14848/60000 [======>.......................] - ETA: 1s - loss: 0.2171 - acc: 0.9372
16640/60000 [=======>......................] - ETA: 1s - loss: 0.2116 - acc: 0.9391
18432/60000 [========>.....................] - ETA: 1s - loss: 0.2092 - acc: 0.9402
20032/60000 [=========>....................] - ETA: 1s - loss: 0.2078 - acc: 0.9406
21856/60000 [=========>....................] - ETA: 1s - loss: 0.2068 - acc: 0.9407
23584/60000 [==========>...................] - ETA: 1s - loss: 0.2067 - acc: 0.9407
25408/60000 [===========>..................] - ETA: 1s - loss: 0.2036 - acc: 0.9413
27200/60000 [============>.................] - ETA: 0s - loss: 0.2058 - acc: 0.9404
28800/60000 [=============>................] - ETA: 0s - loss: 0.2050 - acc: 0.9407
30592/60000 [==============>...............] - ETA: 0s - loss: 0.2046 - acc: 0.9407
32224/60000 [===============>..............] - ETA: 0s - loss: 0.2045 - acc: 0.9408
34016/60000 [================>.............] - ETA: 0s - loss: 0.2034 - acc: 0.9413
35744/60000 [================>.............] - ETA: 0s - loss: 0.2031 - acc: 0.9413
37248/60000 [=================>............] - ETA: 0s - loss: 0.2025 - acc: 0.9414
38880/60000 [==================>...........] - ETA: 0s - loss: 0.2007 - acc: 0.9419
40448/60000 [===================>..........] - ETA: 0s - loss: 0.1982 - acc: 0.9425
42080/60000 [====================>.........] - ETA: 0s - loss: 0.1987 - acc: 0.9428
43744/60000 [====================>.........] - ETA: 0s - loss: 0.1977 - acc: 0.9431
45216/60000 [=====================>........] - ETA: 0s - loss: 0.1973 - acc: 0.9433
46848/60000 [======================>.......] - ETA: 0s - loss: 0.1973 - acc: 0.9434
48320/60000 [=======================>......] - ETA: 0s - loss: 0.1968 - acc: 0.9434
49984/60000 [=======================>......] - ETA: 0s - loss: 0.1967 - acc: 0.9435
51712/60000 [========================>.....] - ETA: 0s - loss: 0.1961 - acc: 0.9438
53312/60000 [=========================>....] - ETA: 0s - loss: 0.1955 - acc: 0.9438
55072/60000 [==========================>...] - ETA: 0s - loss: 0.1954 - acc: 0.9438
56864/60000 [===========================>..] - ETA: 0s - loss: 0.1954 - acc: 0.9439
58688/60000 [============================>.] - ETA: 0s - loss: 0.1942 - acc: 0.9440
60000/60000 [==============================] - 2s 30us/step - loss: 0.1937 - acc: 0.9440

   32/10000 [..............................] - ETA: 0s
 6784/10000 [===================>..........] - ETA: 0s
10000/10000 [==============================] - 0s 7us/step
(test loss: , 0.18316557179167867)
(test accuracy: , 0.9466)

Process finished with exit code 0

 

以上是关于莫烦keras学习自修第四天分类问题的主要内容,如果未能解决你的问题,请参考以下文章

莫烦scikit-learn学习自修第四天内置训练数据集

莫烦sklearn学习自修第八天过拟合问题

莫烦sklearn学习自修第九天过拟合问题处理

莫烦theano学习自修第二天激励函数

莫烦theano学习自修第五天定义神经层

莫烦scikit-learn学习自修第三天通用训练模型