这段代码使用 TensorFlow 构建了一个简单的神经网络模型,用于分类 Fashion MNIST 数据集。模型包含三层,包括:

  1. Flatten 层:将输入图像数据转换为一维向量。
  2. Dense 层:包含 128 个神经元,使用 ReLU 激活函数。
  3. Dense 层:包含 10 个神经元,使用 Softmax 激活函数,用于输出每个类别的概率。

代码使用 Adam 优化器和 SparseCategoricalCrossentropy 损失函数进行训练,并使用 ModelCheckpoint 回调函数保存训练过程中最佳的模型参数。

model.summary() 输出的模型摘要显示,该模型共有 3 层

以下是代码的详细解释:

import tensorflow as tf
import os

fashion = tf.keras.datasets.fashion_mnist
(x_train, y_train), (x_test, y_test) = fashion.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
              metrics=['sparse_categorical_accuracy'])

checkpoint_save_path = './checkpoint/fashion.ckpt'  # 指定保存文件的地址
if os.path.exists(checkpoint_save_path + '.index'):
    print('-------------load the model-----------------')
    model.load_weights(checkpoint_save_path)

cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path,  # 给出模型保存路径
                                                 save_weights_only=True,  # 只保留模型参数
                                                 save_best_only=True)  # 只保留最优模型

history = model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1,
                    callbacks=[cp_callback])
model.summary()

这段代码展示了一个简单的神经网络模型的构建和训练过程,可以作为 TensorFlow 初学者的入门示例。

TensorFlow Fashion MNIST 分类模型 - 3层神经网络构建与训练

原文地址: https://www.cveoy.top/t/topic/ccj4 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录