如何使用TensorFlow使用Python下载和探索Fashion MNIST数据集?
Tensorflow是Google提供的一种机器学习框架。它是一个开放源代码框架,与Python结合使用以实现算法,深度学习应用程序等等。它用于研究和生产目的。
可以使用下面的代码行在Windows上安装'tensorflow'软件包-
pip install tensorflow
“时尚MNIST”数据集包含各种服装的图像。它包含超过10万个类别的7万多件衣服的灰度图像。这些图像的分辨率较低(28x28像素)。
我们正在使用Google合作实验室来运行以下代码。GoogleColab或Colaboratory可帮助在浏览器上运行Python代码,并且需要零配置并免费访问GPU(图形处理单元)。合作已建立在JupyterNotebook的基础上。
以下是代码-
示例
import tensorflow as tf import numpy as np import matplotlib.pyplot as plt print("The tensorflow version used is ") print(tf.__version__) print("The dataset is being loaded") fashion_mnist = tf.keras.datasets.fashion_mnist print("The dataset is being classified into training and testing data ") (train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data() class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot'] print("训练数据的维度 ") print(train_images.shape) print("The number of rows in the training data") print(len(train_labels)) print("The column names of dataset") print(train_labels) print("测试数据的维度 ") print(test_images.shape) print("The number of rows in the test data") print(len(test_labels))
代码信用-https://www.tensorflow.org/tutorials/keras/classification
输出结果
The tensorflow version used is 2.4.0 The dataset is being loaded The dataset is being classified into training and testing data Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz 32768/29515 [=================================] - 0s 0us/step Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz 26427392/26421880 [==============================] - 0s 0us/step Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz 8192/5148 [===============================================] - 0s 0us/step Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz 4423680/4422102 [==============================] - 0s 0us/step 训练数据的维度 (60000, 28, 28) The number of rows in the training data 60000 The column names of dataset [9 0 0 ... 3 0 5] 测试数据的维度 (10000, 28, 28) The number of rows in the test data 10000
解释
所需的软件包已导入。
确定所使用的Tensorflow的版本。
加载了FashionMNIST数据集,并且可以直接从TensorFlow访问FashionMNIST数据集。
接下来,将数据分为训练和测试数据集。
数据集中总共有70000行,其中60k张图像用于训练,10k张用于评估模型学习将图像分类到不同标签的程度。
这是分类问题,其中来自数据集的每个图像都被赋予特定的标签。
这些图像是衣服,并为其分配了相应的标签。
形状,训练和测试数据集中的行数以及数据集中的列名都显示在控制台上。