将以最简单的MNIST数据集展示如何使用Keras框架训练神经网络。
- 最简单的神经网络,在测试集上的精度达到了88.5%
- 使用全连接的神经网络,精度达到97.2%。
最简单的神经网络(输入784维,输出10维)
1.加载和处理MNIST数据集
1.1 加载数据集
from keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
print('shape of x_train:' + str(x_train.shape))
print('shape of x_test:' + str(x_test.shape))
print('shape of y_train:' + str(y_train.shape))
print('shape of y_test:' + str(y_test.shape))
输出mnist训练集、测试集的数据和标签的shape:
shape of x_train:(60000, 28, 28)
shape of x_test:(10000, 28, 28)
shape of y_train:(60000,)
shape of y_test:(10000,)
1.2 将28×28的images转换成784维的向量
x_train_vec = x_train.reshape(60000,784)
x_test_vec = x_test.reshape(10000,784)
print('shape of x_train_vec is' + str(x_train_vec.shape))
输出训练集数据的向量shape:
shape of x_train_vec is(60000, 784)
1.3 使用One-hot编码,将0-9的整数数字用10维的向量表示
import numpy as np
def to_one_hot(labels,dimension = 10):
results = np.zeros((len(labels),dimension))
for i, label in enumerate(labels):
results[i, label] =1.
return results
y_train_vec = to_one_hot(y_train)
y_test_vec = to_one_hot