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

204 阅读1分钟

实验设置

  本实验采用不同激活函数的神经网络对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

在这里插入图片描述