本文仅为本人的巩固与学习,例子源自Tensorflow的官方文档。
加载必要的库
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
导入自带的 Fashion MNIST 数据集
直接从 TensorFlow 中导入和加载 Fashion MNIST 数据
fashion_mnist = tf.keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
Fashion MNIST 数据集都相对较小,都用于验证某个算法是否按预期工作。对于代码的测试和调试,它们都是很好的起点。
每个图像都会被映射到一个标签。由于数据集不包括类名称,请将它们存储在下方,供稍后绘制图像时使用:
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
浏览数据
train_images.shape
# (60000, 28, 28) 训练集中有 60,000 个图像,每个图像由 28 x 28 的像素表示
len(train_labels)
# 60000 训练集中有 60,000 个标签(数据)
train_labels
# array([9, 0, 0, ..., 3, 0, 5], dtype=uint8) 每个标签都是一个 0 到 9 之间的整数
test_images.shape
# (10000, 28, 28) 测试集中有 10,000 个图像。同样,每个图像都由 28x28 个像素表示
len(test_labels)