MNIST手写数字识别实践

from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf

mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
# 加载数据

x_train, x_test = x_train / 255.0, x_test / 255.0
# /255:将RGB数值归一化

#%%

print(y_train.shape)

#%%
model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),  # 张量扁平化,将28*28的图片矩阵转化为784的向量
  tf.keras.layers.Dense(128, activation='relu'),  # 全连接层,128个神经元,激活函数为relu(The Rectified Linear Unit)
    # y=max(0,x)
  tf.keras.layers.Dropout(0.2),  # 防止过拟合,drop out 80% of inputs,使用0.2*cell_size个神经元进行训练
  tf.keras.layers.Dense(10, activation='softmax')  # 全连接层输出 softmax激活函数
])


model.compile(optimizer='adam',  # 梯度下降策略 adam
              loss='sparse_categorical_crossentropy',  # 损失函数 sparse:y独热化编码 交叉熵
              metrics=['accuracy'])

# metrics 列表,包含评估模型在训练和测试时的性能的指标,典型用法是metrics=[‘accuracy’]。
# 如果要在多输出模型中为不同的输出指定不同的指标,可向该参数传递一个字典,例如metrics={‘output_a’: ‘accuracy’}

model.fit(x_train, y_train, epochs=5)  # 训练5轮

model.evaluate(x_test,  y_test, verbose=1)
# Verbosity mode. 0 = silent, 1 = progress bar.
# fit()用于使用给定输入训练模型.
# evaluate()用于评估已经训练过的模型.返回损失值&模型的度量值.

留下评论

您的电子邮箱地址不会被公开。