Data mining algorithm and practice (12): Use tf.keras to implement fashion_MNIST image classification

fashion_MNIST is a handwritten image classification data set, mainly for some clothing matching charts. The data set is relatively small, and it is a data set suitable for hands-on practice. It is also the first multi-classification example. It uses the softmax loss function for training; softmax guarantees the output Classification is a probability, and so the probability of classification results add up to 1;

This is the first time to use an image data set. Generally, a convolutional neural network is required to extract features of the data. The data set itself is some data with features (the boundary of the image is relatively clear). It is direct training, such as the following high heels, the feature boundary of the data set is obvious:

 The implementation is as follows:

from __future__ import absolute_import, division, print_function
import tensorflow as tf
import numpy as np
rng = np.random
import matplotlib.pyplot as plt
%matplotlib inline

(train_image,train_lable),(test_image,test_lable)=tf.keras.datasets.fashion_mnist.load_data()
# type(train_image)

 # image show 查看图像
plt.imshow(train_image[0])

train_image=train_image/255 # 归一化
test_image=test_image/255

# 简历顺序模型
model=tf.keras.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(28,28))) # 28*28向量
model.add(tf.keras.layers.Dense(128,activation="relu"))
model.add(tf.keras.layers.Dense(10,activation="softmax"))
model.compile(optimizer="adam",loss="sparse_categorical_crossentropy",metrics=["acc"])


# X = np.array([[1.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,7.042,10.791,5.313,7.997,5.654,9.27,3.1],[2]])
# Y= np.array([1.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,7.042,10.791,5.313,7.997,5.654,9.27,3.1])
# Y[0:2]

#  softmax的损失函数用sparse_categorical_crossentropy 和categorical_crossentropy
#  独热编码时候用categorical_crossentropy:
# train_lable_onehot=tf.keras.utils.to_categorical(train_lable)

history=model.fit(train_image,train_lable,epochs=5)
model.evaluate(test_image,test_lable)
# 查看损失函数图像
plt.plot(history.epoch,history.history.get('loss'))
# 查看准确率图像
plt.plot(history.epoch,history.history.get('acc'))
#train_lable_onehot

You can directly view the image observation data, plt.imshow(train_image[0]) is a shoe,

Changes in the loss function:

Changes in accuracy rate:

Guess you like

Origin blog.csdn.net/yezonggang/article/details/106396244