下面做的是一个多分类的经典例子,加载编号(0-9)的10种商品的fashion_minist数据集进行测试。
FashionMNIST 是一个替代 MNIST 手写数字集 的图像数据集。 它是由 Zalando(一家德国的时尚科技公司)旗下的研究部门提供。其涵盖了来自 10 种类别的共 7 万个不同商品的正面图片。
https://github.com/zalandoresearch/fashion-mnist
创建 fashion_minist_tf.py
#encoding=utf-8
# 多分类问题
import pandas as pd
import matplotlib.pyplot as plt
# from matplotlib.font_manager import FontProperties
import tensorflow as tf
(train_image,train_label),(test_image,test_label) = tf.keras.datasets.fashion_mnist.load_data()
plt.imshow(train_image[0]) # 显示第一张图片
plt.show()
print(train_label[:10]) # [9 0 0 3 0 2 7 2 5 5]
train_image = train_image / 255
test_image = test_image /255
print(train_image.shape) # (60000, 28, 28)
model = tf.keras.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(28,28)))
model.add(tf.keras.layers.Dense(units=