Tensorflow2.0学习(2):基于fashion_mnist数据集的分类基本步骤

2024-01-13 09:08

本文主要是介绍Tensorflow2.0学习(2):基于fashion_mnist数据集的分类基本步骤,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  • 导入包、打印包的信息

  其中 %matplotlib inline 是IPython中的魔法函数,作用是:在利用matplotlib.pyplot作图或创建画布时不需要plt.show(),即可实现图像的显示。

import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import sklearn
import pandas as pd
import os
import sys
import time
import tensorflow as tf
from tensorflow import keras
print(tf.__version__)
print(sys.version_info)
for module in mpl, np ,pd, sklearn, tf, keras:print(module.__name__, module.__version__)
2.1.0
sys.version_info(major=3, minor=7, micro=4, releaselevel='final', serial=0)
matplotlib 3.1.1
numpy 1.16.5
pandas 0.25.1
sklearn 0.21.3
tensorflow 2.1.0
tensorflow_core.python.keras.api._v2.keras 2.2.4-tf
  • 下载、读取、分割数据集
# 读取keras中的进阶版mnist数据集
fashion_mnist = keras.datasets.fashion_mnist
# 加载数据集,切分为训练集和测试集
(x_train_all, y_train_all), (x_test, y_test) = fashion_mnist.load_data()
# 从训练集中将后五千张作为验证集,前五千张作为训练集
# [:5000]默认从头开始,从头开始取5000个
# [5000:]从第5001开始,结束位置默认为最后
x_valid, x_train = x_train_all[:5000], x_train_all[5000:]
y_valid, y_train = y_train_all[:5000], y_train_all[5000:]
# 打印这些数据集的大小
print(x_valid.shape, y_valid.shape)
print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz
26427392/26421880 [==============================] - 7s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz
8192/5148 [===============================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz
4423680/4422102 [==============================] - 1s 0us/step
(5000, 28, 28) (5000,)
(55000, 28, 28) (55000,)
(10000, 28, 28) (10000,)

  可以得出,训练集有55000张图片,每张图片为28*28;验证集有5000张图片;测试集有1000张图片。

  • 显示图片
def show_single_image(img_arr):plt.imshow(img_arr, cmap="binary")plt.show()
# 显示训练集第一张图片    
show_single_image(x_train[0])

在这里插入图片描述

# 设置n_rows行与n_cols列用来显示图像,共显示x_data个图像
#(y_data是其标签,class_names是其真实的类名)
def show_imgs(n_rows, n_cols, x_data, y_data, class_names):# 断言:不满足条件触发异常assert len(x_data) == len(y_data)assert n_rows * n_cols <len(x_data)plt.figure(figsize=(n_cols * 1.4, n_rows * 1.6))for row in range(n_rows):for col in range(n_cols):index = n_cols * row + col# 总的图像为n_rows * n_cols,当前图像位置为index+1plt.subplot(n_rows, n_cols, index+1)plt.imshow(x_data[index], cmap="binary",interpolation = 'nearest')plt.axis('off')plt.title(class_names[y_data[index]])plt.show()
class_names = ['T-shirt','Trouser','Pullover','Dress','Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag','Ankle boot']
# 显示训练集的十五张图片              
show_imgs(3, 5, x_train, y_train, class_names)

在这里插入图片描述

  • 构建模型
# tf.keras.models.Sequential() 构建模型的容器# 创建一个Sequential的对象,顺序模型,多个网络层的线性堆叠
# 可使用add方法将各层添加到模块中
model = keras.models.Sequential()# 添加层次
# 输入层:Flatten将28*28的图像矩阵展平成为一个一维向量
model.add(keras.layers.Flatten(input_shape=[28,28]))# 全连接层(上层所有单元与下层所有单元都连接):
# 第一层300个单元,第二层100个单元,激活函数为 relu:
# relu: y = max(0, x)
model.add(keras.layers.Dense(300,activation="relu"))          
model.add(keras.layers.Dense(100,activation="relu"))# 输出为长度为10的向量,激活函数为 softmax:
# softmax: 将向量变成概率分布,x = [x1, x2, x3],
# y = [e^x1/sum, e^x2/sum, e^x3/sum],sum = e^x1+e^x2+e^x3
model.add(keras.layers.Dense(10,activation="softmax"))# 目标函数的构建与求解方法
# 为什么使用sparse? : 
# y->是一个数,要用sparse_categorical_crossentropy
# y->是一个向量,直接用categorical_crossentropy
model.compile(loss="sparse_categorical_crossentropy",optimizer="adam",metrics = ["accuracy"])
"""
构建模型也可以这样:
model = keras.models.Sequential([keras.layers.Flatten(input_shape=[28,28]),keras.layers.Dense(300,activation="relu"),keras.layers.Dense(300,activation="relu"),keras.layers.Dense(10,activation="softmax")
])"""
  • 查看模型
# 看模型的层情况
model.layers
[<tensorflow.python.keras.layers.core.Flatten at 0x28a9c583088>,<tensorflow.python.keras.layers.core.Dense at 0x28a9c583108>,<tensorflow.python.keras.layers.core.Dense at 0x28a9c5cbec8>,<tensorflow.python.keras.layers.core.Dense at 0x28a9c925f88>]
# 看模型的概况
model.summary()# 参数量:[None,784] * w + b -> [None, 300]:w.shape=[784, 300],b = 300
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
flatten (Flatten)            (None, 784)               0         
_________________________________________________________________
dense (Dense)                (None, 300)               235500    
_________________________________________________________________
dense_1 (Dense)              (None, 100)               30100     
_________________________________________________________________
dense_2 (Dense)              (None, 10)                1010      
=================================================================
Total params: 266,610
Trainable params: 266,610
Non-trainable params: 0
_________________________________________________________________
  • 训练
# 开启训练
# epochs:训练集遍历10次
# validation_data:每个epoch就会用验证集验证
# 会发现loss和accuracy到后面一直不变,因为用sgd梯度下降法会导致陷入局部最小值点
# 因此将loss函数的下降方法改为 adam
history = model.fit(x_train, y_train, epochs=10,validation_data=(x_valid, y_valid))
Train on 55000 samples, validate on 5000 samples
Epoch 1/10
55000/55000 [==============================] - 4s 69us/sample - loss: 2.5882 - accuracy: 0.7635 - val_loss: 0.6161 - val_accuracy: 0.8122
Epoch 2/10
55000/55000 [==============================] - 3s 62us/sample - loss: 0.5384 - accuracy: 0.8169 - val_loss: 0.5430 - val_accuracy: 0.8268
Epoch 3/10
55000/55000 [==============================] - 3s 62us/sample - loss: 0.4813 - accuracy: 0.8317 - val_loss: 0.5800 - val_accuracy: 0.8166
Epoch 4/10
55000/55000 [==============================] - 3s 63us/sample - loss: 0.4575 - accuracy: 0.8381 - val_loss: 0.5061 - val_accuracy: 0.8276
Epoch 5/10
55000/55000 [==============================] - 3s 62us/sample - loss: 0.4363 - accuracy: 0.8447 - val_loss: 0.4295 - val_accuracy: 0.8612
Epoch 6/10
55000/55000 [==============================] - 3s 62us/sample - loss: 0.4133 - accuracy: 0.8530 - val_loss: 0.4093 - val_accuracy: 0.8602
Epoch 7/10
55000/55000 [==============================] - 3s 63us/sample - loss: 0.3913 - accuracy: 0.8603 - val_loss: 0.4328 - val_accuracy: 0.8506
Epoch 8/10
55000/55000 [==============================] - 3s 63us/sample - loss: 0.3798 - accuracy: 0.8635 - val_loss: 0.3907 - val_accuracy: 0.8564
Epoch 9/10
55000/55000 [==============================] - 4s 64us/sample - loss: 0.3694 - accuracy: 0.8681 - val_loss: 0.4227 - val_accuracy: 0.8564
Epoch 10/10
55000/55000 [==============================] - 4s 64us/sample - loss: 0.3625 - accuracy: 0.8696 - val_loss: 0.4136 - val_accuracy: 0.8630
  • 查看训练后的结果
type(history)
tensorflow.python.keras.callbacks.History
history.history
# loss是训练集的损失值,val_loss是测试集的损失值
{'loss': [2.5882461955070495,0.5384013248010115,0.48129710446704516,0.45748968857851896,0.43628633408329703,0.41328840144330803,0.3912581247546456,0.37976206094351683,0.3693601242488081,0.3625139371091669],'accuracy': [0.7634909,0.81685454,0.8316727,0.83805454,0.84467274,0.8529818,0.8602909,0.8634727,0.86814547,0.8695818],'val_loss': [0.6161495039701462,0.542988519859314,0.5799951359272003,0.506083318400383,0.4295428094863892,0.40931380726099015,0.4327934848666191,0.39065408419966696,0.42266337755918504,0.41358628759980204],'val_accuracy': [0.8122,0.8268,0.8166,0.8276,0.8612,0.8602,0.8506,0.8564,0.8564,0.863]}
def plot_learning_curves(history):# 将history.history转换为dataframe格式pd.DataFrame(history.history).plot(figsize=(8, 5 ))plt.grid(True)# gca:get current axes,gcf: get current figureplt.gca().set_ylim(0, 1)plt.show()
plot_learning_curves(history)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PzEQNq8y-1582528839439)(output_10_0.png)]

# 转换为dataframe格式进行查看
pd.DataFrame(history.history)
lossaccuracyval_lossval_accuracy
02.5882460.7634910.6161500.8122
10.5384010.8168550.5429890.8268
20.4812970.8316730.5799950.8166
30.4574900.8380550.5060830.8276
40.4362860.8446730.4295430.8612
50.4132880.8529820.4093140.8602
60.3912580.8602910.4327930.8506
70.3797620.8634730.3906540.8564
80.3693600.8681450.4226630.8564
90.3625140.8695820.4135860.8630

这篇关于Tensorflow2.0学习(2):基于fashion_mnist数据集的分类基本步骤的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/600942

相关文章

Java注解之超越Javadoc的元数据利器详解

《Java注解之超越Javadoc的元数据利器详解》本文将深入探讨Java注解的定义、类型、内置注解、自定义注解、保留策略、实际应用场景及最佳实践,无论是初学者还是资深开发者,都能通过本文了解如何利用... 目录什么是注解?注解的类型内置注编程解自定义注解注解的保留策略实际用例最佳实践总结在 Java 编程

一文教你Python如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

redis中使用lua脚本的原理与基本使用详解

《redis中使用lua脚本的原理与基本使用详解》在Redis中使用Lua脚本可以实现原子性操作、减少网络开销以及提高执行效率,下面小编就来和大家详细介绍一下在redis中使用lua脚本的原理... 目录Redis 执行 Lua 脚本的原理基本使用方法使用EVAL命令执行 Lua 脚本使用EVALSHA命令

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll

IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤及问题解决

《IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决》:本文主要介绍IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决,本文分步骤结合实例给大... 目录步骤 1:创建 Maven Web 项目步骤 2:添加 Spring MVC 依赖1、保存后执行2、将新的依赖

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Pandas使用AdaBoost进行分类的实现

《Pandas使用AdaBoost进行分类的实现》Pandas和AdaBoost分类算法,可以高效地进行数据预处理和分类任务,本文主要介绍了Pandas使用AdaBoost进行分类的实现,具有一定的参... 目录什么是 AdaBoost?使用 AdaBoost 的步骤安装必要的库步骤一:数据准备步骤二:模型