tensorflow2之fashion_minist測試

下面做的是一個多分類的經典例子,加載編號(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=128,activation='relu'))
model.add(tf.keras.layers.Dense(units=10, activation='softmax'))  # 多分類激活函數,將這10個類別變成概率輸出

model.compile(optimizer='adam',
             #   loss='categorical_crossentropy'
                loss='sparse_categorical_crossentropy',  # 使用了數字編碼類別用
                metrics=['acc'])

model.fit(train_image,train_label,epochs=5)

print(model.evaluate(test_image,test_label))  #

print(model.predict(train_image[:1])) # 對第一張圖片進行測試
# [[0.00200258 0.00096263 0.00356959 0.01052908 0.0028083  0.1917256 0.01329381 0.39358088 0.05934311 0.32218438]]

調試結果:

第一張訓練圖片

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章