使用MNIST数据集做手写数字识别
参考:https://www.bilibili.com/video/BV13x411v7US/?p=19&t=78
基础知识:
https://keras-cn.readthedocs.io/en/latest/getting_started/sequential_model/
导入数据:
# -*- coding: utf-8 -*-
import keras
# from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop
from keras.datasets import mnist
import matplotlib.pyplot as plt
import numpy as np
num_classes = 10
path='/data/MNIST/keras/mnist.npz'
f = np.load(path)
x_train, y_train = f['x_train'], f['y_train']
x_test, y_test = f['x_test'], f['y_test']
f.close()
x_train = x_train.reshape(60000, 784).astype('float32')
x_test = x_test.reshape(10000, 784).astype('float32')
x_train /= 255
x_test /= 255
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
# label为0~9共10个类别,keras要求格式为binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
先随便建一个模型:
model = Sequential()
model.add(Dense(input_dim=28*28,units=633,activation='sigmoid'))
model.add(Dense(units=633,activation='sigmoid'))
model.add(Dense(units=633,activation='sigmoid'))
model.add(Dense(units=10,activation='softmax'))
model.compile(loss='mse',optimizer='sgd',metrics=['accuracy'])
model.fit(x_train,y_train,batch_size=100,epochs=20)
result = model.evaluate(x_test,y_test)
print ('\nTest Acc:',result[1])
结果很不理想,准确率为0.1135
我们改变网络结构,让层数变深一点
model = Sequential()
model.add(Dense(input_dim=28*28,units=633,activation='sigmoid'))
for i in range(10):
model.add(Dense(units=633,activation='sigmoid'))
#model.add(Dense(units=633,activation='sigmoid'))
model.add(Dense(units=10,activation='softmax'))
model.compile(loss='mse',optimizer='sgd',metrics=['accuracy'])
model.fit(x_train,y_train,batch_size=100,epochs=20)
result = model.evaluate(x_test,y_test)
print ('\nTest Acc:',result[1])
发现训练时间增加,但结果并未改观,仍为0.1135
把激励函数有sigmoid改为relu,每个epoch的准确率在提高,可见训练起来了
准确率为0.8858
同样改为10层:
层数增加后,学习的速度变慢,最终结果不理想。10层会不会太多了,改成5层,准确率为0.7585
12层:0.1744
7层:0.7585
6层:0.8489
5层:0.8522
4层:0.8806
3层:0.8784
2层:0.87
4层的时候效果最好,那么在这种情况下增加单元的数量会如何呢
633个单元:0.8806
1000个单元:0.8786
2000个单元:0.8829
5000个单元:0.8925
10000个单元:0.8981
由于之前的损失函数用的是mse,多用于回归问题,而手写数字识别应当那是一个分类问题,考虑采用Cross Entropy作为损失函数。
model = Sequential()
model.add(Dense(input_dim=28*28,units=689,activation='relu'))
for i in range(1):
model.add(Dense(units=689,activation='relu'))
model.add(Dense(units=10,activation='softmax'))
model.compile(loss='categorical_crossentropy',optimizer='sgd',metrics=['accuracy'])
model.fit(x_train,y_train,batch_size=100,epochs=20)
result = model.evaluate(x_test,y_test)
print ('\nTest Acc:',result[1])
result = model.evaluate(x_train,y_train)
print('\nTrain Acc:',result[1])
采用三层,激励函数为relu,损失函数为交叉熵,准确率为
0.9633
3层:0.9691
4层:0.9684
10层:0.9748
20层:0.9663
把optimizer换成adam,层数仍旧是3层,损失函数用relu,准确率为0.9817
下面给测试集故意加上噪声:
x_test = np.random.normal(x_test)
出现的情况是,在训练集上跑的很好,但在测试集上结果很糟,出现了过拟合的情况。
为了防止过拟合,我们加入dropout
model = Sequential()
model.add(Dense(input_dim=28*28,units=689,activation='relu'))
model.add(Dropout(0.7))
model.add(Dense(units=689,activation='relu'))
model.add(Dropout(0.7))
model.add(Dense(units=689,activation='relu'))
model.add(Dropout(0.7))
model.add(Dense(units=10,activation='softmax'))
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
model.fit(x_train,y_train,batch_size=100,epochs=20)
结果是,train的结果变差,但是test的结果变好
Dropout的参数改为0.5:
test acc:0.5344
train acc:0.9955
Dropout的参数改为0.8:
test acc:0.586
train acc:0.9708
Dropout的参数改为0.9:
test acc:0.3978
train acc:0.4735