首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >[Tensorflow2.X][原创]入门基础之fashion_mnist分类

[Tensorflow2.X][原创]入门基础之fashion_mnist分类

作者头像
云未归来
发布2025-07-18 14:14:10
发布2025-07-18 14:14:10
950
举报

[Tensorflow2.X][原创]入门基础之fashion_mnist分类

import tensorflow as tf

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import sklearn

import os

import sys

def plot_learning_curves(history):

    pd.DataFrame(history.history).plot(figsize=(8, 5))

    plt.grid(True)

    plt.gca().set_ylim(0, 1)

    plt.show()

fashion_mnist = tf.keras.datasets.fashion_mnist # fashion_mnist数据集

(x_train_all, y_train_all), (x_test, y_test) = fashion_mnist.load_data() # 加载数据集

x_valid, x_train = x_train_all[:5000], x_train_all[5000:] # 从训练集中前5000个做为验证集,后面的作为训练集

y_valid, y_train = y_train_all[:5000], y_train_all[5000:]

model = tf.keras.models.Sequential() # 构建模型

model.add(tf.keras.layers.Flatten(input_shape=[28, 28])) #将输入扁平化,即转为一维向量[28x28,1]

model.add(tf.keras.layers.Dense(300, activation='sigmoid')) # 全连接层,300为卷积核大小WX=b

model.add(tf.keras.layers.Dense(100, activation='sigmoid')) # 全连接层,100为卷积核大小

model.add(tf.keras.layers.Dense(10, activation='softmax')) # 全连接层,输出10个预测值

# 另外一种网络模型搭建的写法

# model = tf.keras.models.Sequential([

# #     tf.keras.layers.Dense(300,activation='relu'),  # relu ==> max(0,x)

# #     tf.keras.layers.Dense(100,activation='relu'),  # softmax将向量转为概率分布

# #     tf.keras.layers.Dense(10,activation='softmax')

# # ])

print(model.summary()) #打印层结构

model.compile(loss='sparse_categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])

history = model.fit(x_train, y_train, epochs=10, validation_data=(x_valid, y_valid))

plot_learning_curves(history)

model.summary()结构:

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

参数第2行计算出来是根据WX+b算出来的即

[None,784][784,300]+300=784x300+300=235500

参数第3行计算出来是根据WX+b算出来的即

[None,300][300,100]+100=300x100+100=30100其他依次类推

利用history打印训练变化参数的曲线:

最终结果:

loss: 0.5496 - accuracy: 0.8094 - val_loss: 0.5803 - val_accuracy: 0.7990

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-07-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档