import numpy #导入数据库
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.utils import to_categorical #进行one-shot编码
seed = 7 #设置随机种子,保证每次随机的数据都相同(在随机种子相同的情况下)
numpy.random.seed(seed)
(X_train, y_train), (X_test, y_test) = mnist.load_data() #加载数据,测试数据,训练数据
num_pixels = X_train.shape[1] * X_train.shape[2]#训练数据的行列数
X_train = X_train.reshape(X_train.shape[0], num_pixels).astype('float32')
X_test = X_test.reshape(X_test.shape[0], num_pixels).astype('float32')
#数据集是3维的向量(instance length,width,height).对于多层感知机,模型的输入是二维的向量,因此这
#里需要将数据集reshape,即将28*28的向量转成784长度的数组。可以用numpy的reshape函数轻松实现这个过
#程。
#给定的像素的灰度值在0-255,为了使模型的训练效果更好,通常将数值归一化映射到0-1。
X_train = X_train / 255#归一化
X_test = X_test / 255
#最后,模型的输出是对每个类别的打分预测,对于分类结果从
mnist手写识别数据集(Keras实现)
最新推荐文章于 2023-04-10 13:38:08 发布