TensorFlow 2:使用神经网络对Fashion MNIST分类并进行比较分析

实验设置

  本实验采用不同激活函数的神经网络对MNIST数据集进行分类,同时探究隐藏层数量对实验结果的影响。实验的隐藏层主要为1层和2层,而激活函数分别为ReLU、Laaky ReLU和Softmax,输出函数不变均为Softmax。实验数据集为Fashion MNIST数据集,主要用于手写识别的多分类。

代码及实验结果

隐藏层为1层时的不同激活函数表现

# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras

# Helper libraries
import numpy as np

# Fashion MNIST input data
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

# preprocess
train_images = train_images / 255.0
test_images = test_images / 255.0

# build model, loss, and train op
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(196, activation='relu'), # change the activation
    keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=10)

test_loss, test_acc = model.evaluate(test_images, test_labels)
print('\nTest Accuracy:', test_acc)

  relu
在这里插入图片描述
  elu
在这里插入图片描述
  softmax
在这里插入图片描述

隐藏层为2层时不同激活函数的表现

# build model, loss, and train op
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(196, activation='relu'), # change activation
    keras.layers.Dense(98, activation='relu'), # change activation
    keras.layers.Dense(10, activation='softmax')
])

  relu

在这里插入图片描述
  elu

在这里插入图片描述

  softmax

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是使用卷积神经网络完成 Fashion MNIST 数据集图像分类任务的代码示例: ``` import tensorflow as tf from tensorflow import keras from tensorflow.keras import layers # 加载数据集 (x_train, y_train), (x_test, y_test) = keras.datasets.fashion_mnist.load_data() # 数据预处理 x_train = x_train.astype("float32") / 255.0 x_test = x_test.astype("float32") / 255.0 x_train = x_train.reshape(-1, 28, 28, 1) x_test = x_test.reshape(-1, 28, 28, 1) # 构建模型 model = keras.Sequential( [ layers.Conv2D(32, kernel_size=(3, 3), activation="relu", input_shape=(28, 28, 1)), layers.MaxPooling2D(pool_size=(2, 2)), layers.Conv2D(64, kernel_size=(3, 3), activation="relu"), layers.MaxPooling2D(pool_size=(2, 2)), layers.Flatten(), layers.Dropout(0.5), layers.Dense(10, activation="softmax"), ] ) # 编译模型 model.compile(loss="sparse_categorical_crossentropy", optimizer="adam", metrics=["accuracy"]) # 训练模型 model.fit(x_train, y_train, batch_size=128, epochs=10, validation_split=0.1) # 评估模型 test_loss, test_acc = model.evaluate(x_test, y_test) print("Test accuracy:", test_acc) ``` 在上面的代码中,我们首先加载了 Fashion MNIST 数据集,并对数据进行了预处理。然后我们构建了一个卷积神经网络模型,并编译了模型。接下来,我们使用训练集对模型进行训练,并在测试集上进行评估。最后输出了模型在测试集上的准确率。 这里的卷积神经网络模型采用了两层卷积层和一层全连接层,其中每个卷积层后面跟着一个最大池化层,用于降低数据维度,防止过拟合。同时,我们还添加了一层 Dropout 层,用于随机丢弃一定比例的神经元,以避免过拟合。最后,我们使用 softmax 激活函数对输出进行处理,得到每个类别的概率分布。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值