tensorflow2.3实现Fashion_mnist数据分类(一)

tensorflow2.3实现Fashion_mnist数据分类

  • Fashion-MNIST克隆了MNIST的所有外在特征:
    • 60000张训练图像和对应Label;10000张测试图像和对应Label;
      10个类别;
      每张图像28x28的分辨率;Fashion-MNIST不再是抽象符号,而是更加具象化的人类必需品——服装
  • 共10大类:
    Label Description 0 T恤(T-shirt/top) 1 裤子(Trouser) 2 套头衫(Pullover)
    3 连衣裙(Dress) 4 外套(Coat) 5 凉鞋(Sandal) 6 衬衫(Shirt) 7 运动鞋(Sneaker) 8 包(Bag) 9 靴子(Ankle boot)

导入包

import tensorflow as tf
import numpy as np
import pathlib

查看tensorflow的版本

print('Tensorflow version: {}'.format(tf.__version__))
Tensorflow version: 2.3

读取数据

(train_image, train_label),(test_image, test_label) = tf.keras.datasets.fashion_mnist.load_data()

查看数据图像的shape(维度)

train_image.shape,test_image.shape
((60000, 28, 28), (10000, 28, 28))

查看数据图像标签的shape(维度)

train_label.shape,test_label.shape
((60000,), (10000,))

测试标签的输出格式

train_label
array([9, 0, 0, ..., 3, 0, 5], dtype=uint8)

显示其中一张图像

plt.imshow(test_image[0])

tensorflow2.3实现Fashion_mnist数据分类(一)_第1张图片
数据归一化

train_image = train_image/255
test_image = test_image/255

归一化后的数据图像的shape(维度)

train_image.shape,test_image.shape
((60000, 28, 28), (10000, 28, 28))

第一种模型,目标值是顺序编码

建立模型

model = tf.keras.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(28,28)))
model.add(tf.keras.layers.Dense(128, activation='relu'))
model.add(tf.keras.layers.Dense(10, activation='softmax'))

模型概述

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
flatten (Flatten)            (None, 784)               0         
_________________________________________________________________
dense (Dense)                (None, 128)               100480    
_________________________________________________________________
dense_1 (Dense)              (None, 10)                1290      
=================================================================
Total params: 101,770
Trainable params: 101,770
Non-trainable params: 0

模型编译:目标值是顺序编码,使用sparse_categorical_crossentropy损失函数

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['acc'])

模型训练

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

模型评估

model.evaluate(test_image,test_label)
10000/10000 [==============================] - 1s 78us/sample - loss: 0.3496 - acc: 0.8767
[0.3496406935095787, 0.8767]

第二种模型目标值是独热编码,使用categorical_crossentropy损失函数

标签顺序值转换为杜热编码型

train_label_onehot = tf.keras.utils.to_categorical(train_label)
train_label_onehot[0],train_label[0]
(array([0., 0., 0., 0., 0., 0., 0., 0., 0., 1.], dtype=float32), 9)

测试集标签转换为独热编码型

test_label_onehot = tf.keras.utils.to_categorical(test_label)

建立模型

model = tf.keras.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(28,28)))
model.add(tf.keras.layers.Dense(128, activation='relu'))
model.add(tf.keras.layers.Dense(10, activation='softmax'))

模型概述

model.summary()
Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
flatten_1 (Flatten)          (None, 784)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 128)               100480    
_________________________________________________________________
dense_3 (Dense)              (None, 10)                1290      
=================================================================
Total params: 101,770
Trainable params: 101,770
Non-trainable params: 0

模型编译:目标值是独热编码,使用categorical_crossentropy损失函数

model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), loss='categorical_crossentropy', metrics=['acc'])

模型训练

history = model.fit(train_image, train_label_onehot, epochs=5, validation_data=(test_image, test_label_onehot))
history.history.keys()
dict_keys(['loss', 'acc', 'val_loss', 'val_acc'])

训练过程结果显示

plt.plot(history.epoch, history.history.get('loss'))
plt.plot(history.epoch, history.history.get('val_loss'))

tensorflow2.3实现Fashion_mnist数据分类(一)_第2张图片

plt.plot(history.epoch, history.history.get('acc'))
plt.plot(history.epoch, history.history.get('val_acc'))

tensorflow2.3实现Fashion_mnist数据分类(一)_第3张图片

你可能感兴趣的:(tensorflow2.3,tensorflow,深度学习)