一、进行数据预处理
步骤1:导入所需模块
import tensorflow as tf
import tensorflow.keras
from tensorflow.keras.datasets import mnist
import numpy as np
import pandas as pd
from tensorflow.keras.utils import to_categorical
np.random.seed(10)
步骤2:读取MNIST数据
(x_train, y_train), \
(x_test, y_test) = mnist.load_data()
步骤3:将features(数字图像特征值)转换为四维矩阵
将features(数字图像特征值)以reshape转换为600002828*1的四维矩阵
x_train4d=x_train.reshape(x_train.shape[0],28,28,1).astype('float32')
x_test4d=x_test.reshape(x_test.shape[0],28,28,1).astype('float32')
步骤4:将features(数字图像特征值)标准化
x_train4d_normalize = x_train4d/255
x_test4d_normalize = x_test4d/255
步骤5:label(数字真实的值)以One-Hot Encoding进行转换
y_trainonehot =tf.keras.utils. to_categorical(y_train)
y_testonehot = tf.keras.utils.to_categorical(y_test)
二、 建立模型
1.导入所需模块
from tensorflow.keras.models impor