Tensorflow实战:基于Fashion_MNIST时尚数据实现对十种类别商品的十分类任务
1实验环境
Windows系统
Tensorflow版本:2.0
Python版本:3.7
Anaconda版本:Anaconda3-2019.10-Windows-x86_64.sh
2实验目的
基于Fashion_MNIST时尚数据实现对十种类别商品的十分类任务
商品类别依次对应0-T恤(t-shirt)、1-牛仔裤(trouser)、2-套衫(pullover)、3-裙子(dress)、4-外套(coat)、5-凉鞋(sandal)、6-衬衫(shirt)、7-运动鞋(sneaker)、8-包(bag)、9-短靴(ankle boot)。
3实验要求
Python编程
卷积神经网络搭建
TensorFlow应用
4实验步骤
一 导入包及数据集
二 检查数据大小类型
三 数据预处理进行归一化操作
四 建立模型
本实验因为是多分类问题,采用全连接层方法,一共有四层,一个输入层,两个隐藏层,一个输出层.其中隐藏层激活函数为relu函数,输出层为softmax函数。为了防止过拟合的问题,采用正则化计数dropout,随机丢掉一些神经元。
五 配置训练模型
将测试集中60000个数据分为训练集和验证集,验证是否存在过拟合问题
进行模型优化,采用adam函数,损失函数采用交叉熵损失函数。
六 训练模型
七 绘制可视化图形
八 评估模型
九模型预测
十模型使用
5实验内容及结果展示
#导入所需的包
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras import layers
#导入数据集
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
#查看数据集的个数和形状大小
print(train_images.shape)
print(train_labels.shape)
print(test_images.shape)
print(test_labels.shape)
#查看数据类型
type(train_images),type(train_labels)
type(test_images),type(test_labels)
#数据的灰度值为(0,255)
train_images.min(),train_images.max()
#数据标签为0-9的数字
test_labels.min(),test_labels.max()
#对应标签0-9
class_names=[‘T-shirt/top’,‘Trouser’,‘Pullover’,‘Dress’,‘Coat’,
‘Sandal’,‘Shirt’,‘Sneaker’,‘Bag’,‘Ankle boot’]
#数据预处理
tf.keras.layers.Flatten()
#归一化
‘’‘train_images,test_images=tf.cast(train_images/255.0,tf.float32),tf.cast(test_images/255.0,tf.float32)
train_labels,test_labels=tf.cast(train_labels,tf.int16),tf.cast(test_labels,tf.int16)’’’
train_images=train_images/255.0
test_images=test_images/255.0
#画布大小宽度为 10,长度为10
plt.figure(figsize=(10,10))
显示训练集的30张图像
for i in range(30):
# 创建分布6*5的图形
plt.subplot(5, 5, i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
# 显示照片,以cm 为单位。
plt.imshow(train_images[i], cmap=plt.cm.binary)
plt.xlabel(class_names[train_labels[i]])
#建立模型
#采用四层神经网络结构
model = keras.Sequential()
model.add(layers.Flatten(input_shape=(28,28)))
model.add(layers.Dense(128,activation=“relu”))
model.add(layers.Dropout(0.25)) #防止过拟合,随机拿掉一些数据
model.add(layers.Dense(64, activation=“relu”))
model.add(layers.Dense(10,activation=“softmax”))
model.summary()
#配置训练方法
#x_val、y_val为验证集
x_val = train_images[:12000]
partial_train_images = train_images[12000:]
y_val = train_labels[:12000]
partial_train_labels = train_labels[12000:]
#模型优化
model.compile(optimizer=“adam”,
loss=“sparse_categorical_crossentropy”,
metrics=[‘accuracy’])
#训练模型
#第一次测试集60000条数据中50000条数据用来训练
#10000条数据作为验证集,验证是否存在过拟合问题
#迭代次数开始设置为20,发现准确率不高
#而且验证集中的损失函数准确率不收敛,存在过拟合问题
#反复修改迭代次数,仍然存在过拟合问题。
出现以下问题如图所示:loss逐渐收敛但是vel_loss却没有收敛,数值反而变大
#修改Dropout函数中取值为0.25,修改测试集和验证集的比例
#训练集为48000,验证集为12000,继续试验迭代次数为50,得到的结果较好
history=model.fit(partial_train_images,
partial_train_labels,
epochs=50,
validation_data=(x_val,y_val),
verbose=1)
#绘制可视化损失和准确率变化曲线
#history对象里有两种两个属性,分别为epoch和history,epoch为训练轮数。
#当某一次metrics=[‘accuracy’]时,显示history对象里的key值分别为’loss’, ‘accuracy’, ‘val_loss’, ‘val_accuracy’.
history_dict = history.history
history_dict.keys()
import matplotlib.pyplot as plt
acc = history.history[‘accuracy’]
val_acc = history.history[‘val_accuracy’]
loss = history.history[‘loss’]
val_loss = history.history[‘val_loss’]
epochs = range(1, len(acc) + 1)
plt.figure(figsize=(10,8))
plt.plot(epochs, acc, color=‘red’, marker=’+’, label=‘Training accuracy’)
plt.plot(epochs, loss, color=‘blue’, marker=’*’, label=‘Training loss’)
plt.plot(epochs, val_acc, color=‘black’, marker=‘p’, label=‘validation accuracy’)
plt.plot(epochs, val_loss, color=‘black’, marker=’>’, label=‘validation loss’)
plt.title(‘Training and validation accuracy/loss’)
plt.xlabel(‘Epochs’)
plt.ylabel(‘Accuracy’)
#plt.plot(color=“blue”,lebel=“loss”)
#plt.plot(color=“red”,lebel=“accuracy”)
plt.legend()
plt.show()
#评估模型
test_loss, test_acc=model.evaluate(test_images,test_labels)
print(‘测试损失:%f 测试准确率: %f’ % (test_loss, test_acc))
#预测模型
predictions = model.predict(test_images)
np.argmax(model.predict([[test_images[0]]]))
#测试具体数据,显示商品图片
plt.figure(figsize=(10, 40))
for i in range(50):
plt.subplot(10, 5, i + 1)
plt.xticks([])
plt.yticks([])
plt.grid(‘off’)
plt.imshow(test_images[i], cmap=plt.cm.binary)
predicted_label = np.argmax(predictions[i])
true_label = test_labels[i]
if predicted_label == true_label:
color = ‘green’
else:
color = ‘red’
plt.xlabel("{} ({})".format(class_names[predicted_label],
class_names[true_label]),
color=color)
plt.show()
最后结果可知:50个样本中识别结果为42个正确,8个错误。
完整代码:
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 20 15:39:18 2020
@author: SYM
"""
#导入所需的包
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras import layers
#导入数据集
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
#查看数据集的个数和形状大小
print(train_images.shape)
print(train_labels.shape)
print(test_images.shape)
print(test_labels.shape)
#查看数据类型
type(train_images),type(train_labels)
type(test_images),type(test_labels)
#数据的灰度值为(0,255)
train_images.min(),train_images.max()
#数据标签为0-9的数字
test_labels.min(),test_labels.max()
#对应标签0-9
class_names=['T-shirt/top','Trouser','Pullover','Dress','Coat',
'Sandal','Shirt','Sneaker','Bag','Ankle boot']
#数据预处理
tf.keras.layers.Flatten()
#归一化
'''train_images,test_images=tf.cast(train_images/255.0,tf.float32),tf.cast(test_images/255.0,tf.float32)
train_labels,test_labels=tf.cast(train_labels,tf.int16),tf.cast(test_labels,tf.int16)'''
train_images=train_images/255.0
test_images=test_images/255.0
#画布大小宽度为 10,长度为10
plt.figure(figsize=(10,10))
# 显示训练集的30张图像
for i in range(30):
# 创建分布6*5的图形
plt.subplot(5, 5, i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
# 显示照片,以cm 为单位。
plt.imshow(train_images[i], cmap=plt.cm.binary)
plt.xlabel(class_names[train_labels[i]])
#建立模型
#采用四层神经网络结构
model = keras.Sequential()
model.add(layers.Flatten(input_shape=(28,28)))
model.add(layers.Dense(128,activation="relu"))
model.add(layers.Dropout(0.25)) #防止过拟合,随机拿掉一些数据
model.add(layers.Dense(64, activation="relu"))
model.add(layers.Dense(10,activation="softmax"))
model.summary()
#配置训练方法
#x_val、y_val为验证集
x_val = train_images[:12000]
partial_train_images = train_images[12000:]
y_val = train_labels[:12000]
partial_train_labels = train_labels[12000:]
#模型优化
model.compile(optimizer="adam",
loss="sparse_categorical_crossentropy",
metrics=['accuracy'])
#训练模型
#第一次测试集60000条数据中50000条数据用来训练
#10000条数据作为验证集,验证是否存在过拟合问题
#迭代次数开始设置为20,发现准确率不高
#而且验证集中的损失函数准确率不收敛,存在过拟合问题
#反复修改迭代次数,仍然存在过拟合问题。
#修改Dropout函数中取值为0.25,修改测试集和验证集的比例
#训练集为48000,验证集为12000,继续试验迭代次数为50
history=model.fit(partial_train_images,
partial_train_labels,
epochs=50,
validation_data=(x_val,y_val),
verbose=1)
#绘制可视化损失和准确率变化曲线
#history对象里有两种两个属性,分别为epoch和history,epoch为训练轮数。
#当某一次metrics=['accuracy']时,运行如下部分代码我们可以看出,
#history字典类型,包含val_loss,val_acc,loss,acc四个key值。
#显示history对象里的key值分别为'loss', 'accuracy', 'val_loss', 'val_accuracy'
history_dict = history.history
history_dict.keys()
import matplotlib.pyplot as plt
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(1, len(acc) + 1)
plt.figure(figsize=(10,8))
plt.plot(epochs, acc, color='red', marker='+', label='Training accuracy')
plt.plot(epochs, loss, color='blue', marker='*', label='Training loss')
plt.plot(epochs, val_acc, color='black', marker='p', label='validation accuracy')
plt.plot(epochs, val_loss, color='black', marker='>', label='validation loss')
plt.title('Training and validation accuracy/loss')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
#plt.plot(color="blue",lebel="loss")
#plt.plot(color="red",lebel="accuracy")
plt.legend()
plt.show()
#评估模型
test_loss, test_acc=model.evaluate(test_images,test_labels)
print('测试损失:%f 测试准确率: %f' % (test_loss, test_acc))
#预测模型
predictions = model.predict(test_images)
np.argmax(model.predict([[test_images[0]]]))
#使用模型
#测试具体数据,显示商品图片
plt.figure(figsize=(10, 40))
for i in range(50):
plt.subplot(10, 5, i + 1)
plt.xticks([])
plt.yticks([])
plt.grid('off')
plt.imshow(test_images[i], cmap=plt.cm.binary)
predicted_label = np.argmax(predictions[i])
true_label = test_labels[i]
if predicted_label == true_label:
color = 'green'
else:
color = 'red'
plt.xlabel("{} ({})".format(class_names[predicted_label],
class_names[true_label]),
color=color)
plt.show()