如何使用 Python 和 TensorFlow 下載並探索 Fashion MNIST 資料集?
TensorFlow 是 Google 提供的一個機器學習框架。它是一個開源框架,與 Python 結合使用,可以實現演算法、深度學習應用程式等等。它用於研究和生產目的。
可以使用以下程式碼行在 Windows 上安裝 ‘tensorflow’ 包:
pip install tensorflow
‘Fashion MNIST’ 資料集包含各種服裝的影像。它包含超過 7 萬件屬於 10 個不同類別的服裝的灰度影像。這些影像是低解析度的 (28 x 28 畫素)。
我們使用 Google Colaboratory 來執行以下程式碼。Google Colab 或 Colaboratory 幫助在瀏覽器上執行 Python 程式碼,無需任何配置,並可免費訪問 GPU(圖形處理單元)。Colaboratory 建立在 Jupyter Notebook 之上。
以下是程式碼:
示例
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("The dimensions of training data ") 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("The dimensions of test data ") 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 The dimensions of training data (60000, 28, 28) The number of rows in the training data 60000 The column names of dataset [9 0 0 ... 3 0 5] The dimensions of test data (10000, 28, 28) The number of rows in the test data 10000
解釋
匯入了所需的包。
確定正在使用的 TensorFlow 版本。
載入 Fashion MNIST 資料集,可以直接從 TensorFlow 訪問 Fashion MNIST 資料集。
接下來,資料被分割成訓練資料集和測試資料集。
資料集總共有 70000 行,其中 60000 張影像用於訓練,10000 張影像用於評估模型將影像分類到不同標籤的學習效果。
這是一個分類問題,其中資料集中的每張影像都分配了一個特定的標籤。
這些影像是服裝,併為其分配了相應的標籤。
訓練和測試資料集的行數、形狀以及資料集的列名都顯示在控制檯上。
廣告