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])

在这里插入图片描述
数据归一化

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'))

loss与val_loss

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

acc yu val_acc

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值