【Keras学习笔记】9:从MNIST⼿写数字识别中初识ANN超参数的选择读取数据
import keras
from keras import layers
import numpy as np
from matplotlib import pyplot as plt
import pandas as pd
%matplotlib inline
粉色的英文怎么写
Using TensorFlow backend.
from keras.datats import mnist as mn
# 读取训练数据和测试数据
(train_img, train_lab),(test_img, test_lab)= mn.load_data()
液晶膜Downloading data from /img-datats/mnist.npz
11493376/11490434 [==============================] - 893s 78us/step
train_img.shape, train_lab.shape, test_img.shape, test_lab.shape
祝福新人结婚的诗句
((60000, 28, 28), (60000,), (10000, 28, 28), (10000,))
# 绘制⼀张样本看⼀下
plt.imshow(train_img[0])
<matplotlib.image.AxesImage at 0x13d3c198>
# 看⼀下这个数是⼏
train_lab
array([5, 0, 4, ..., 5, 6, 8], dtype=uint8)
这⾥是28*28的数据,如果不⽤CNN⽽只是⽤普通的MLP,那要把它Flatten⼀下,让这两个维度合起来(784)。
建⽴模型
model = keras.Sequential()
# Flatten层会把除了第⼀维(样本数)之外的,也就是后⾯的维度摊平成同⼀个维度⾥
model.add(layers.Flatten())# (60000, 28, 28) => (60000, 28*28)
# 已经不是第⼀层了,所以不再需要input_dim了,会直接根据前⾯层的输出来设置
model.add(layers.Den(64, activation='relu'))
# Softmax多分类,因为⼀共10种数字,所以输出维度是10
model.add(layers.Den(10, activation='softmax'))
编译模型
哆啦a梦的女朋友pile(
optimizer='adam',
# 注意因为label是顺序编码的,这⾥⽤这个
loss='spar_categorical_crosntropy',
metrics=['acc']
)
训练模型
# 内存/显存容量是有限的,⼀般⼀次放⼀个batch的数据进去训练,⽽不是每次把整个样本集⼀起训练⼀遍
history = model.fit(train_img, train_lab, epochs=50, batch_size=512, validation_data=(test_img, test_lab), verbo=0)# 每批512张图⽚
WARNING:tensorflow:From E:\MyProgram\Anaconda\envs\krs\lib\site-packages\tensorflow\python\ops\math_ops.py:3066: to_int32 (from tensorflow.pytho n.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
U tf.cast instead.
查看Performance
model.evaluate(train_img, train_lab)
60000/60000 [==============================] - 2s 28us/step
[3.6218268792152406, 0.77455]
model.evaluate(test_img, test_lab)
10000/10000 [==============================] - 0s 26us/step
[3.6389780288696287, 0.7732]
plt.plot(history.epoch, ('val_acc'), c='g', label='validation acc')
plt.plot(history.epoch, ('acc'), c='b', label='train acc')
plt.legend()
<matplotlib.legend.Legend at 0x18578fd0>
可以看到没有发⽣过拟合,但预测效果不是很好,Acc不⾼。
谧怎么读音
使⽤模型做预测
奖学金评定标准# 得到的都是10维的概率向量,⽤argmax取每⾏最⼤值索引即是对应数字
np.argmax(model.predict(test_img[:10]), axis=1)
array([9, 2, 1, 0, 4, 1, 4, 9, 6, 9], dtype=int64)
# 查看⼀下实际label
test_lab[:10]
array([7, 2, 1, 0, 4, 1, 4, 9, 5, 9], dtype=uint8)
调整超参数以优化模型
总的原则:神经⽹络的容量⾜够拟合数据。
⽐较简单的数据⽤⽐较⼩容量的⽹络。
如果⽹络中间突然有容量⽐较⼩的层,信息就会丢失,容易引起⽋拟合。
先增⼤⽹络容量,直到过拟合。再采取措施抑制过拟合。再增⼤⽹络容量,直到过拟合。。如此反复。
model = keras.Sequential()
model.add(layers.Flatten())
model.add(layers.Den(64, activation='relu'))
# 多加两层隐藏层,以让它过拟合
model.add(layers.Den(64, activation='relu'))
model.add(layers.Den(64, activation='relu'))
model.add(layers.Den(10, activation='softmax'))
optimizer='adam',
loss='spar_categorical_crosntropy',
metrics=['acc']
)
history = model.fit(train_img, train_lab, epochs=50, batch_size=512, validation_data=(test_img, test_lab), verbo=0) # 训练集表现
model.evaluate(train_img, train_lab)
60000/60000 [==============================] - 2s 30us/step
[0.03728870131107869, 0.9931333333333333]
# 测试集表现
model.evaluate(test_img, test_lab)
10000/10000 [==============================] - 0s 29us/step
[0.24160608106338732, 0.9685]
plt.plot(history.epoch, ('val_acc'), c='g', label='validation acc')
plt.plot(history.epoch, ('acc'), c='b', label='train acc')
plt.legend()
<matplotlib.legend.Legend at 0x1bc82470>
盐酸氮卓斯汀发⽣了⼀定程度的过拟合,现在要采取措施去抑制过拟合。不妨添加两个Dropout层(实际上不是层)看⼀下。
model = keras.Sequential()
model.add(layers.Flatten())
model.add(layers.Den(64, activation='relu'))
model.add(layers.Den(64, activation='relu'))
model.add(layers.Dropout(0.5))# 添加Dropout
model.add(layers.Den(64, activation='relu'))
model.add(layers.Dropout(0.5))# 添加Dropout
model.add(layers.Den(10, activation='softmax'))
optimizer='adam',
loss='spar_categorical_crosntropy',
metrics=['acc']
)
关于牛顿的故事
# 前⾯添加了Dropout,在训练时随机断开连接,这样同等epochs下训练强度就变弱了
# 所以这⾥要增⼤epochs,考虑到0.5*0.5=0.25也就是1/4,这⾥不妨把epochs增⼤4倍到200
history = model.fit(train_img, train_lab, epochs=200, batch_size=512, validation_data=(test_img, test_lab), verbo=0)
WARNING:tensorflow:From E:\MyProgram\Anaconda\envs\krs\lib\site-packages\keras\backend\tensorflow_backend.py:3445: calling dropout (from tensorfl ow._ops) with keep_prob is deprecated and will be removed in a future version.
Instructions for updating:
Plea u `rate` instead of `keep_prob`. Rate should be t to `rate = 1 - keep_prob`.
# 训练集表现
model.evaluate(train_img, train_lab)
60000/60000 [==============================] - 2s 27us/step
[0.01952453022014009, 0.9959]
# 测试集表现
model.evaluate(test_img, test_lab)
10000/10000 [==============================] - 0s 30us/step
[0.21649456044882537, 0.9703]
plt.plot(history.epoch, ('val_acc'), c='g', label='validation acc')
plt.plot(history.epoch, ('acc'), c='b', label='train acc')
plt.legend()
<matplotlib.legend.Legend at 0x1c394cc0>
效果提升了!较好的抑制了过拟合,也提升了在测试集上的Performance。