使用 TensorFlow 处理 MNIST 手写体数字识别问题

使用 TensorFlow 官方提供的一个例子,基于 MNIST 数据集,实现一个图片分类的应用,本文是基于 TensorFlow 2.0 版本来学习和实践的。 MNIST 数据集是一个非常出 名的手写体数字识别数据集,它包含了 60000 张图片作为训练集,10000 张图片作为测试集,每张图片中的手写体数字是 0~9 中的一个,图片是 28×28 像素大小,并且每个数字都是位于图片的正中间的。 使用 TensorFlow 对 MNIST 数据集进行分类,整个实现对应的完整的 Python 代码,如下所示: from __future__ import absolute_import, division, print_function, unicode_literals import tensorflow as tf # 下载 MNIST 数据集 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 # 创建 tf.keras.Sequential 模型 model = tf.keras.models.Sequential([ tf.keras.layers.Flatten(input_shape=(28, 28)), tf.keras.layers.Dense(128, activation='relu'), tf.keras.layers.D