基于keras的fashion_mnist模型训练过程

2024-04-26 03:32

本文主要是介绍基于keras的fashion_mnist模型训练过程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.下载fashion_mnist数据

使用以下三行代码下载fashion mnist数据

from tensorflow import keras
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

查看数据信息:

构建网络:

import tensorflow as tf
from tensorflow import keras
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()model = keras.Sequential()
model.add(keras.layers.Flatten(input_shape=(28,28)))
model.add(keras.layers.Dense(128,activation=tf.nn.relu))
model.add(keras.layers.Dense(10,activation=tf.nn.softmax))
model.summary()

模型参数

Model: "sequential_2"
_________________________________________________________________
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

 这是一个全链接的神经网络结构,结构如下:

每一层均是全连接

中间层的参数计算过程为:

784 * 128(weight) + 128(bias) = 100480.

最终层的参数计算过程为:

128 * 10(weight) + 10(bias) = 1290.

 计算结果符合summary方法给出的统计数据。

训练模型

model.compile(optimizer=tf.optimizers.Adam(),loss=tf.losses.sparse_categorical_crossentropy,metrics=['accuracy'])
model.fit(train_images,train_labels,epochs=50)

Epoch 1/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.5283 - accuracy: 0.8235
Epoch 2/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.5076 - accuracy: 0.8300
Epoch 3/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4954 - accuracy: 0.8316
Epoch 4/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4892 - accuracy: 0.8350
Epoch 5/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4853 - accuracy: 0.8364
Epoch 6/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4847 - accuracy: 0.8388
Epoch 7/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4769 - accuracy: 0.8414
Epoch 8/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4733 - accuracy: 0.8407
Epoch 9/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4796 - accuracy: 0.8410
Epoch 10/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4935 - accuracy: 0.8381: 0s - loss: 0.4940 - accura
Epoch 11/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4756 - accuracy: 0.8408
Epoch 12/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4745 - accuracy: 0.8424
Epoch 13/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4707 - accuracy: 0.8434
Epoch 14/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4693 - accuracy: 0.8434
Epoch 15/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4614 - accuracy: 0.8463
Epoch 16/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4617 - accuracy: 0.8468
Epoch 17/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4481 - accuracy: 0.8492
Epoch 18/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4638 - accuracy: 0.8464
Epoch 19/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4624 - accuracy: 0.8481
Epoch 20/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4500 - accuracy: 0.8501
Epoch 21/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4546 - accuracy: 0.8479
Epoch 22/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4560 - accuracy: 0.8495
Epoch 23/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4552 - accuracy: 0.8490
Epoch 24/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4492 - accuracy: 0.8501
Epoch 25/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4497 - accuracy: 0.8525
Epoch 26/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4489 - accuracy: 0.8495
Epoch 27/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4464 - accuracy: 0.8515
Epoch 28/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4529 - accuracy: 0.8507
Epoch 29/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4495 - accuracy: 0.8526
Epoch 30/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4454 - accuracy: 0.8508
Epoch 31/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4578 - accuracy: 0.8513
Epoch 32/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4434 - accuracy: 0.8534
Epoch 33/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4387 - accuracy: 0.8541
Epoch 34/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4481 - accuracy: 0.8536
Epoch 35/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4448 - accuracy: 0.8536: 
Epoch 36/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4459 - accuracy: 0.8540
Epoch 37/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4397 - accuracy: 0.8537
Epoch 38/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4399 - accuracy: 0.8559
Epoch 39/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4528 - accuracy: 0.8530
Epoch 40/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4404 - accuracy: 0.8561
Epoch 41/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4433 - accuracy: 0.8540
Epoch 42/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4354 - accuracy: 0.8555
Epoch 43/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4319 - accuracy: 0.8572
Epoch 44/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4408 - accuracy: 0.8557
Epoch 45/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4708 - accuracy: 0.8440
Epoch 46/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4461 - accuracy: 0.8542
Epoch 47/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4337 - accuracy: 0.8584
Epoch 48/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4346 - accuracy: 0.8577
Epoch 49/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4386 - accuracy: 0.8569
Epoch 50/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4505 - accuracy: 0.8560
<keras.callbacks.History at 0x7f5c3f065430>

可以看到,经过50次的训练,正确率在%85左右徘徊 ,中间经过几次周旋倒退,最终总体上还是在正确率上升的轨道上进行,稳定副在%85.6

加入normalization,提升正确率

说白了,正规化就是这样一个过程:

将数据正规化,将每个像素的灰度值转换为[0,1]区间的数据,

train_images = train_images/255
model.compile(optimizer=tf.optimizers.Adam(),loss=tf.losses.sparse_categorical_crossentropy,metrics=['accuracy'])
model.fit(train_images,train_labels,epochs=50)
Epoch 1/50
1875/1875 [==============================] - 2s 1ms/step - loss: 1.0806 - accuracy: 0.6693
Epoch 2/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.6354 - accuracy: 0.7707
Epoch 3/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.5593 - accuracy: 0.8002
Epoch 4/50
1875/1875 [==============================] - 3s 1ms/step - loss: 0.5169 - accuracy: 0.8167
Epoch 5/50
1875/1875 [==============================] - 3s 1ms/step - loss: 0.4904 - accuracy: 0.8270
Epoch 6/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4709 - accuracy: 0.8342
Epoch 7/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4553 - accuracy: 0.8400
Epoch 8/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4430 - accuracy: 0.8442
Epoch 9/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4333 - accuracy: 0.8471
Epoch 10/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4246 - accuracy: 0.8502
Epoch 11/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4172 - accuracy: 0.8526
Epoch 12/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4102 - accuracy: 0.8553
Epoch 13/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4045 - accuracy: 0.8586
Epoch 14/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3987 - accuracy: 0.8605
Epoch 15/50
1875/1875 [==============================] - 3s 1ms/step - loss: 0.3939 - accuracy: 0.8608
Epoch 16/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3904 - accuracy: 0.8626
Epoch 17/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3858 - accuracy: 0.8647
Epoch 18/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3818 - accuracy: 0.8654
Epoch 19/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3778 - accuracy: 0.8677
Epoch 20/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3745 - accuracy: 0.8683
Epoch 21/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3718 - accuracy: 0.8690
Epoch 22/50
1875/1875 [==============================] - 3s 1ms/step - loss: 0.3680 - accuracy: 0.8701
Epoch 23/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3653 - accuracy: 0.8717
Epoch 24/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3620 - accuracy: 0.8724
Epoch 25/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3594 - accuracy: 0.8736
Epoch 26/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3569 - accuracy: 0.8743
Epoch 27/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3543 - accuracy: 0.8750
Epoch 28/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3521 - accuracy: 0.8763
Epoch 29/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3492 - accuracy: 0.8766
Epoch 30/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3467 - accuracy: 0.8773
Epoch 31/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3445 - accuracy: 0.8788
Epoch 32/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3424 - accuracy: 0.8783
Epoch 33/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3403 - accuracy: 0.8801
Epoch 34/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3379 - accuracy: 0.8795
Epoch 35/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3360 - accuracy: 0.8816
Epoch 36/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3338 - accuracy: 0.8817
Epoch 37/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3317 - accuracy: 0.8824
Epoch 38/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3296 - accuracy: 0.8832
Epoch 39/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3271 - accuracy: 0.8837
Epoch 40/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3257 - accuracy: 0.8842
Epoch 41/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3236 - accuracy: 0.8846
Epoch 42/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3211 - accuracy: 0.8864
Epoch 43/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3196 - accuracy: 0.8859
Epoch 44/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3174 - accuracy: 0.8868
Epoch 45/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3162 - accuracy: 0.8874
Epoch 46/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3144 - accuracy: 0.8877
Epoch 47/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3124 - accuracy: 0.8884
Epoch 48/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3108 - accuracy: 0.8894
Epoch 49/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3086 - accuracy: 0.8895
Epoch 50/50
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3074 - accuracy: 0.8897
<keras.callbacks.History at 0x7f5c4011ddc0>

可以看到,最终的正确率比正规化之前有所提升。 

利用模型进行推理

test_images_scaled=test_images/255
model.evaluate(test_images_scaled,test_labels)

验证非正规化的参数推理: 

import numpy as np
demo = tf.reshape(test_images[0],(1,28,28))
print(np.argmax(model.predict(demo)))
print(test_labels[0])

如果训练的时候,输入经过了正规化,则推理也需要正规化:

 通过上面几次的训练过程输出的准确率差异我们可以总结出,同一个网络模型,不同的时间进行训练,可以得到完全不同的正确率,这也说明模型本身的复杂性和时变性,至于为何会发生, 模型的大量参数组成一个高维的数据空间,我们用梯度下降法寻找最优解的时候,依赖于初始参数权重和BIAS在高维空间中的位置,不同次的训练过程,初始化参数在空间中的位置不同,优化的方向则不同,最终的最优解也不相同,所以可以看出,模型训练是一个非常复杂的数学计算过程,并且很可能寻找到的最优解是局部最优解,而并非全局最优解。

调试

得到每层的名称:

得到权重和BIAS

可以看到,权重的维度和上文中的计算是相符合的。

参考排错:

ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape_善良995的博客-CSDN博客


结束!

这篇关于基于keras的fashion_mnist模型训练过程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

大模型研发全揭秘:客服工单数据标注的完整攻略

在人工智能(AI)领域,数据标注是模型训练过程中至关重要的一步。无论你是新手还是有经验的从业者,掌握数据标注的技术细节和常见问题的解决方案都能为你的AI项目增添不少价值。在电信运营商的客服系统中,工单数据是客户问题和解决方案的重要记录。通过对这些工单数据进行有效标注,不仅能够帮助提升客服自动化系统的智能化水平,还能优化客户服务流程,提高客户满意度。本文将详细介绍如何在电信运营商客服工单的背景下进行

作业提交过程之HDFSMapReduce

作业提交全过程详解 (1)作业提交 第1步:Client调用job.waitForCompletion方法,向整个集群提交MapReduce作业。 第2步:Client向RM申请一个作业id。 第3步:RM给Client返回该job资源的提交路径和作业id。 第4步:Client提交jar包、切片信息和配置文件到指定的资源提交路径。 第5步:Client提交完资源后,向RM申请运行MrAp

Andrej Karpathy最新采访:认知核心模型10亿参数就够了,AI会打破教育不公的僵局

夕小瑶科技说 原创  作者 | 海野 AI圈子的红人,AI大神Andrej Karpathy,曾是OpenAI联合创始人之一,特斯拉AI总监。上一次的动态是官宣创办一家名为 Eureka Labs 的人工智能+教育公司 ,宣布将长期致力于AI原生教育。 近日,Andrej Karpathy接受了No Priors(投资博客)的采访,与硅谷知名投资人 Sara Guo 和 Elad G

Retrieval-based-Voice-Conversion-WebUI模型构建指南

一、模型介绍 Retrieval-based-Voice-Conversion-WebUI(简称 RVC)模型是一个基于 VITS(Variational Inference with adversarial learning for end-to-end Text-to-Speech)的简单易用的语音转换框架。 具有以下特点 简单易用:RVC 模型通过简单易用的网页界面,使得用户无需深入了

【机器学习】高斯过程的基本概念和应用领域以及在python中的实例

引言 高斯过程(Gaussian Process,简称GP)是一种概率模型,用于描述一组随机变量的联合概率分布,其中任何一个有限维度的子集都具有高斯分布 文章目录 引言一、高斯过程1.1 基本定义1.1.1 随机过程1.1.2 高斯分布 1.2 高斯过程的特性1.2.1 联合高斯性1.2.2 均值函数1.2.3 协方差函数(或核函数) 1.3 核函数1.4 高斯过程回归(Gauss

透彻!驯服大型语言模型(LLMs)的五种方法,及具体方法选择思路

引言 随着时间的发展,大型语言模型不再停留在演示阶段而是逐步面向生产系统的应用,随着人们期望的不断增加,目标也发生了巨大的变化。在短短的几个月的时间里,人们对大模型的认识已经从对其zero-shot能力感到惊讶,转变为考虑改进模型质量、提高模型可用性。 「大语言模型(LLMs)其实就是利用高容量的模型架构(例如Transformer)对海量的、多种多样的数据分布进行建模得到,它包含了大量的先验

图神经网络模型介绍(1)

我们将图神经网络分为基于谱域的模型和基于空域的模型,并按照发展顺序详解每个类别中的重要模型。 1.1基于谱域的图神经网络         谱域上的图卷积在图学习迈向深度学习的发展历程中起到了关键的作用。本节主要介绍三个具有代表性的谱域图神经网络:谱图卷积网络、切比雪夫网络和图卷积网络。 (1)谱图卷积网络 卷积定理:函数卷积的傅里叶变换是函数傅里叶变换的乘积,即F{f*g}

秋招最新大模型算法面试,熬夜都要肝完它

💥大家在面试大模型LLM这个板块的时候,不知道面试完会不会复盘、总结,做笔记的习惯,这份大模型算法岗面试八股笔记也帮助不少人拿到过offer ✨对于面试大模型算法工程师会有一定的帮助,都附有完整答案,熬夜也要看完,祝大家一臂之力 这份《大模型算法工程师面试题》已经上传CSDN,还有完整版的大模型 AI 学习资料,朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费

【生成模型系列(初级)】嵌入(Embedding)方程——自然语言处理的数学灵魂【通俗理解】

【通俗理解】嵌入(Embedding)方程——自然语言处理的数学灵魂 关键词提炼 #嵌入方程 #自然语言处理 #词向量 #机器学习 #神经网络 #向量空间模型 #Siri #Google翻译 #AlexNet 第一节:嵌入方程的类比与核心概念【尽可能通俗】 嵌入方程可以被看作是自然语言处理中的“翻译机”,它将文本中的单词或短语转换成计算机能够理解的数学形式,即向量。 正如翻译机将一种语言