Keras教程
多层感知机进行手写数字识别训练与预测:
# -*- coding: UTF-8 -*- import numpy as np from keras.datasets import mnist from keras.layers import Dense from keras.models import Sequential, load_model # 加载数据 local_path = '/home/data/keras/mnist.npz' (x_train, y_train), (x_test, y_test) = mnist.load_data(path=local_path); print(x_train.shape, y_train.shape, x_test.shape, y_test.shape) # (60000, 28, 28) (60000,) (10000, 28, 28) (10000,) # 将二维的图片矩阵转换成一维数组,每个像素数值除以255归一化到0-1的数值 dim = x_train.shape[1] * x_train.shape[2] x_train = x_train.reshape((x_train.shape[0], dim)) / 255 x_test = x_test.reshape((x_test.shape[0], dim)) / 255 print(x_train.shape, x_test.shape) # (60000, 784) (10000, 784) # one-hot 编码 y_train_new = np.zeros((y_train.shape[0], 10)) for i, num in enumerate(y_train): y_train_new[i][num] = 1 y_test_new = np.zeros((y_test.shape[0], 10)) for i, num in enumerate(y_test): y_test_new[i][num] = 1 def train(): # 构造多层感知机模型 model = Sequential() model.add(Dense(256, input_dim=dim, activation='relu')) model.add(Dense(10, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) print(model.summary()) # Model: "sequential_1" # _________________________________________________________________ # Layer (type) Output Shape Param # # ================================================================= # dense_1 (Dense) (None, 256) 200960 # _________________________________________________________________ # dense_2 (Dense) (None, 10) 2570 # ================================================================= # Total params: 203,530 # Trainable params: 203,530 # Non-trainable params: 0 # _________________________________________________________________ # None # 训练模型 model.fit(x_train, y_train_new, epochs=10, batch_size=50) # 模型评估 score = model.evaluate(x_test, y_test_new) print(score) # [0.07887125950443005, 0.9782999753952026] model.save("../data/mnist.h5") def predict(): model = load_model("../data/mnist.h5") y = model.predict(x_test[:5]) print("predict=>", np.argmax(y, axis=1)) print("real=>", y_test[:5]) # predict = > [7 2 1 0 4] # real = > [7 2 1 0 4] train() predict()