Python 如何使用Python训练Fashion MNIST数据集的TensorFlow模型?
TensorFlow是一种非常流行的深度学习工具,在ImageNet数据集中尤其强大。但是,作为一种入门级别的练习,我们可以使用简单的服装图像数据集Fashion MNIST。本文将探讨如何使用Python和TensorFlow训练模型。
阅读更多:Python 教程
Fashion MNIST数据集
Fashion-MNIST是一个包含十种不同类型的服装图像的数据集。每个图像具有28×28的灰度版本。数据集包含60,000个训练图像和10,000个测试图像。以下是数据集中十种不同服装类型的图像:
标签 | 描述 |
---|---|
0 | T-shirt/top |
1 | Trouser |
2 | Pullover |
3 | Dress |
4 | Coat |
5 | Sandal |
6 | Shirt |
7 | Sneaker |
8 | Bag |
9 | Ankle boot |
下面是如何下载并加载数据集的示例代码:
import tensorflow as tf
# 下载Fashion MNIST数据集
fashion_mnist = tf.keras.datasets.fashion_mnist
# 将数据集分成训练集和测试集
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
# 将图像像素值标准化为[0,1]范围内的浮点数
train_images = train_images / 255.0
test_images = test_images / 255.0
在运行上面的代码后,将获得两个NumPy数组:train_images和train_labels,以及test_images和test_labels。
构建模型
我们将使用一个包含三个密集层的神经网络来训练Fashion MNIST数据集,最后一层具有10个输出节点,对应于十个label. 下面是如何定义该模型的示例代码:
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
该模型使用Flatten层将输入图像转换为大小为784的一维向量,然后通过两个密集层,分别有128个和64个神经元,最后输出一个包含10个神经元的sigmoid层。
编译和拟合模型
在定义模型后,我们需要编译模型并使用训练数据集训练模型。以下是如何编译和拟合模型的示例代码:
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=10)
上面的代码将使用’the adam optimizer’和’sparse categorical_crossentropy’损失函数编译模型,并使用train_images和train_labels拟合模型。模型将在10个epochs中进行训练。
评估模型
一旦我们训练了模型,我们将使用测试数据集对其进行评估。以下是如何评估模型的示例代码:
test_loss, test_acc = model.evaluate(test_images, test_labels)
print('Test accuracy:', test_acc)
在运行上述代码后,您可能会得到类似于以下输出:
10000/10000 [==============================] - 1s 52us/sample - loss: 0.3624 - acc: 0.8691
Test accuracy: 0.8691
绘制预测图像
我们可以绘制一些预测图像,以查看模型的预测结果。以下是如何绘制预测图像的示例代码:
import numpy as np
import matplotlib.pyplot as plt
# 预测图像标签
predictions = model.predict(test_images)
# 绘制前25个测试图像及其预测标签和实际标签
plt.figure(figsize=(10, 10))
for i in range(25):
plt.subplot(5, 5, i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(test_images[i], cmap=plt.cm.binary)
predicted_label = np.argmax(predictions[i])
true_label = test_labels[i]
if predicted_label == true_label:
color = 'green'
else:
color = 'red'
plt.xlabel("{} ({})".format(class_names[predicted_label],
class_names[true_label]),
color=color)
plt.show()
上面的代码将绘制前25个测试图像及其预测标签和实际标签的网络输出结果。
结论
在本文中,我们已经讨论了如何使用Python和TensorFlow训练Fashion MNIST数据集的模型。我们涉及了如何加载数据集,构建模型,编译和拟合模型,以及评估模型并绘制预测图像。 TensorFlow是一个强大的深度学习工具,我们希望该篇文章可以帮助您入门TensorFlow!