MINIST手写数字识别——04.多层感知器(MLP)
加载 MNIST 数据集
import tensorflow as tf
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
print(x_train.shape, type(x_train))
print(y_train.shape, type(y_train))
(60000, 28, 28) <class ‘numpy.ndarray’>
(60000,) <class ‘numpy.ndarray’>
数据处理:规范化
# 将图像本身从[28,28]转换为[784,]
X_train = x_train.reshape(60000, 784)
X_test = x_test.reshape(10000, 784)
print(X_train.shape, type(X_train))
print(X_test.shape, type(X_test))
(60000, 784) <class ‘numpy.ndarray’>
(10000, 784) <class ‘numpy.ndarray’>
# 将数据类型转换为float32
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
# 数据归一化
X_train /= 255
X_test /= 255
统计训练数据中各标签数量
import numpy as np
import matplotlib.pyplot as plt
label, count = np.unique(y_train, return_counts=True)
print(label, count)
[0 1 2 3 4 5 6 7 8 9] [5923 6742 5958 6131 5842 5421 5918 6265 5851 5949]
fig = plt.figure()
plt.bar(label, count, width = 0.7