Tensorflow2.0笔记 - FashionMnist数据集训练

本笔记使用FashionMnist数据集,搭建一个5层的神经网络进行训练,并统计测试集的精度。

本笔记中FashionMnist数据集是直接下载到本地加载的方式,不涉及用梯子。

关于FashionMnist的介绍,请自行百度。

复制代码
#Fashion Mnist数据集本地下载和加载(不用梯子)
#https://blog.csdn.net/scar2016/article/details/115361245 (百度网盘)
#https://blog.csdn.net/weixin_43272781/article/details/110006990 (github)
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import datasets, layers, optimizers, Sequential, metrics

tf.__version__

#加载fashion mnist数据集
def load_mnist(path, kind='train'):
    import os
    import gzip
    import numpy as np

    """Load MNIST data from `path`"""
    labels_path = os.path.join(path,
                               '%s-labels-idx1-ubyte.gz'
                               % kind)
    images_path = os.path.join(path,
                               '%s-images-idx3-ubyte.gz'
                               % kind)
    with gzip.open(labels_path, 'rb') as lbpath:
        labels = np.frombuffer(lbpath.read(), dtype=np.uint8,
                               offset=8)
    with gzip.open(images_path, 'rb') as imgpath:
        
        images = np.frombuffer(imgpath.read(), dtype=np.uint8,
                               offset=16).reshape(len(labels), 784)
    return images, labels

#预处理数据
def preprocess(x, y):
    x = tf.cast(x, dtype=tf.float32)
    x = tf.convert_to_tensor(x, dtype=tf.float32) / 255.
    y = tf.cast(y, dtype=tf.int32)
    y = tf.convert_to_tensor(y, dtype=tf.int32)
    return x, y
#训练数据
train_data, train_labels = load_mnist("./datasets")
print(train_data.shape, train_labels.shape)
#测试数据
test_data, test_labels = load_mnist("./datasets", "t10k")
print(test_data.shape, test_labels.shape)

batch_size = 128

train_db = tf.data.Dataset.from_tensor_slices((train_data, train_labels))
train_db = train_db.map(preprocess).shuffle(10000).batch(batch_size)

test_db = tf.data.Dataset.from_tensor_slices((test_data, test_labels))
test_db = test_db.map(preprocess).batch(batch_size)

train_db_iter = iter(train_db)
sample = next(train_db_iter)
print('Batch:', sample[0].shape, sample[1].shape)


#定义网络模型
model = Sequential([
    #Layer 1: [b, 784] => [b, 256]
    layers.Dense(256, activation=tf.nn.relu),
    #Layer 2: [b, 256] => [b, 128]
    layers.Dense(128, activation=tf.nn.relu),
    #Layer 3: [b, 128] => [b, 64]
    layers.Dense(64, activation=tf.nn.relu),
    #Layer 4: [b, 64] => [b, 32]
    layers.Dense(32, activation=tf.nn.relu),
    #Layer 5: [b, 32] => [b, 10], 输出类别结果
    layers.Dense(10)
])

#编译网络
model.build(input_shape=[None, 28*28])
model.summary()

#进行训练
total_epoches = 30
learn_rate = 0.01

optimizer = optimizers.Adam(learning_rate = learn_rate)
for epoch in range(total_epoches):
    for step, (x,y) in enumerate(train_db):
        with tf.GradientTape() as tape:
            logits = model(x)
            y_onehot = tf.one_hot(y, depth=10)
            #使用交叉熵作为loss
            loss_ce = tf.reduce_mean(tf.losses.categorical_crossentropy(y_onehot, logits, from_logits=True))
        #计算梯度
        grads = tape.gradient(loss_ce, model.trainable_variables)
        #更新梯度
        optimizer.apply_gradients(zip(grads, model.trainable_variables))

        if step % 100 == 0:
            print("Epoch[", epoch, "]: step-", step, "\tloss: CrossEntropy-", loss_ce.numpy())

#使用测试集进行验证
total_correct = 0
total_num = 0
for x,y in test_db:
    logits = model(x)
    #使用softmax得到各个类别的概率
    prob = tf.nn.softmax(logits, axis=1)
    #求出概率最大的结果参数位置,作为预测的分类结果
    pred = tf.cast(tf.argmax(prob, axis=1), dtype=tf.int32)
    #比较结果
    correct = tf.equal(pred, y)
    correct = tf.reduce_sum(tf.cast(correct, dtype=tf.int32))
    #计算精度
    total_correct += int(correct)
    total_num += x.shape[0]

acc = total_correct / total_num
print("Accuracy:", acc)

运行结果:

相关推荐
3DVisionary5 分钟前
3D-DIC与机器学习协同模拟材料应力-应变本构行为研究
人工智能·机器学习·3d·3d-dic技术 机器学习·应力-应变本构行为·卷积神经网络(ecnn)·数字图像相关法(dic)
神经星星7 分钟前
无需预对齐即可消除批次效应,东京大学团队开发深度学习框架STAIG,揭示肿瘤微环境中的详细基因信息
人工智能·深度学习·机器学习
神经星星7 分钟前
【vLLM 学习】调试技巧
人工智能·机器学习·编程语言
呵呵哒( ̄▽ ̄)"7 分钟前
线性代数:同解(1)
python·线性代数·机器学习
RedMery8 分钟前
论文阅读笔记:Denoising Diffusion Implicit Models (4)
论文阅读·笔记
SweetCode13 分钟前
裴蜀定理:整数解的奥秘
数据结构·python·线性代数·算法·机器学习
程序员Linc25 分钟前
写给新人的深度学习扫盲贴:向量与矩阵
人工智能·深度学习·矩阵·向量
CryptoPP26 分钟前
springboot 对接马来西亚数据源API等多个国家的数据源
spring boot·后端·python·金融·区块链
xcLeigh33 分钟前
OpenCV从零开始:30天掌握图像处理基础
图像处理·人工智能·python·opencv
大乔乔布斯34 分钟前
AttributeError: module ‘smtplib‘ has no attribute ‘SMTP_SSL‘ 解决方法
python·bash·ssl