这一节我们主要学习
- 利用 Keras 搭建一个简单的手写数字识别网络
# 导入相应的工具包
import numpy as np
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.layers import Conv2D, MaxPooling2D, Flatten
from keras.optimizers import SGD, Adam
from keras.utils import np_utils
from keras.datasets import mnist
# 定义一个函数:从 MNIST 数据集中导入数据并处理好数据格式
def load_data():
(x_train, y_train) , (x_test, y_test) = mnist.load_data()
number = 10000
x_train = x_train[0:number]
y_train = y_train[0:number]
x_train = x_train.reshape(number, 28*28)
x_test = x_test.reshape(x_test.shape[0], 28*28)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
# convert class vectors to binary class matrices
y_train = np_utils.to_categorical(y_train,10)
y_test = np_utils.to_categorical(y_test,10)
x_train = x_train
x_test = x_test
# x_test = np.random.normal(x_test)
x_train = x_train / 255
x_test = x_test / 255
return (x_train, y_train), (x_test, y_test)
# 导入数据
(x_train,y_train), (x_test,y_test) = load_data()
# 观察一下数据输出格式
print(x_train.shape)
print(y_train.shape)
print(x_test.shape)
print(y_test.shape)
# 构建网络模型并训练数据
model = Sequential()
# construct the network
model.add(Dense(units=633,activation='sigmoid'))
model.add(Dense(units=633,activation='sigmoid'))
model.add(Dense(units=10,activation='softmax'))
# configuration and train
model.compile(loss='mse',optimizer=SGD(lr=0.1),metrics=['accuracy'])
model.fit(x_train,y_train,batch_size=100,epochs=20)
# validation set can be used in fit function to evaluate
# 对模型进行评估,并输出准确率
result = model.evaluate(x_test, y_test)
print("Test Acc:"+"{}".format(result[1]))
# 你会发现训练出的模型效果并不好,接下来可以对网络进行调整
# 可以调整每层神经元的数目
model.add(Dense(units=689,activation='sigmoid'))
# 可以增加网络的深度
for i in range(10):
model.add(Dense(units=689,activation='sigmoid'))
# 效果依然不大好,接下来可以学习下一节课—— DNN 训练技巧,根据课程内容来优化你的网络,让它表现的更好。