识别结果
Show me the code
导包
# 顺序模型
from keras import Sequential
# 神经网络层,激活函数类
from keras.layers import Dense, Activation
# 随机梯度下降法
from keras.optimizers import SGD
# 转换“one hot”
from keras import utils
import numpy as np
import matplotlib.pyplot as plt
处理数据,请注意前面三行的注释。
# 导入并预处理手写字符集数据,注释的这一行有vpn的朋友可以很快下载
# 没有vpn请下载mnist.npz文件
# (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
with np.load('./mnist.npz') as f:
x_train, y_train, x_test, y_test = f["x_train"], f["y_train"], f["x_test"], f["y_test"]
# 打印显示原始数据维度
print("原始数据大小:")
print("x_train的大小为:", x_train.shape)
print("y_train的大小为:", y_train.shape)
print("x_test的大小为:", x_test.shape)
print("y_test的大小为:", y_test.shape)
# 将x_train与x_test转化为二维数据,保证一行是一副图像,并转化成float数
x_train = x_train.reshape(x_train.shape[0], -1).astype("float64")
x_test = x_test.reshape(x_test.shape[0], -1).astype("float64")
# 将图像除以255进行像素值归一化
x_train /= 255
x_test /= 255
# 将y_train与y_test转化为“one hot”形式
y_train = utils.to_categorical(y_train)
y_test = utils.to_categorical(y_test)
# 打印转化后数据大小
print("\n转化后数据大小:")
print("x_train的大小为:", x_train.shape)
print("y_train的大小为:", y_train.shape)
print("x_test的大小为:", x_test.shape)
print("y_test的大小为:", y_test.shape)
创建神经网络进行训练
n_samples = x_train.shape[0] # 样本数
n_features = x_train.shape[1] # 特征数
n_clases = y_train.shape[1] # 类别数
# 建立神经网络并训练
model = Sequential()
model.add(Dense(units= n_clases, input_dim= n_features))
model.add(Activation('softmax'))
# 整合模型
sgd = SGD(lr= 0.5)
# 第三个参数再后面用fit训练时可以打印出准确率
model.compile(optimizer= sgd,
loss= "categorical_crossentropy",
metrics=["accuracy"])
# 训练
print("\n开始训练:")
model.fit(x_train, y_train, batch_size=128, epochs=10)
# 开始评价模型
print("\n评价模型:")
final_loss, final_accuracy = model.evaluate(x_test, y_test)
# 结果
print("\n测试结果:")
print("测试集loss:", final_loss)
print("测试集accuracy:", final_accuracy)
print("\n模型信息:")
print(model.summary())
对模型进行测试
# 从测试集中随意挑选一张数字图像进行预测
pic_dex = np.random.randint(0, x_test.shape[0] - 1)
print("挑选训练集图像序号为:", pic_dex)
pic_img = x_test[pic_dex, :]
# 利用模型进行预测
predict_result = model.predict(pic_img.reshape(1, -1))
print("\n模型预测结果为数字:", np.argmax(predict_result))
# 显示实际图像
print("\n实际图像为:")
plt.imshow(pic_img.reshape(28, 28))
plt.show()