What Is Multiclass Classification in TensorFlow?
Multiclass classification is a supervised learning task that assigns each input to one of three or more possible classes, in contrast to binary classification, which chooses between only two. In Machine Learning, the classification problem is categorising input data
into different classes. For instance, we can categorise email messages into two
groups: spam or not spam. In this case, we have two classes, we talk about binary
classification. When we have more than two classes, we talk about multiclass
classification. In this post, I address multiclass classification
on the example of categorising clothing items into clothing types based on the Fashion
MNIST dataset. The code and general concepts are adopted from
TensorFlow Developer Certificate in 2022: Zero to Mastery.
Below is a concise summary of the key steps: loading the data, preprocessing it, building and tuning a model, and evaluating its predictions.
Loading the Fashion MNIST Dataset in Keras
The [Fashion-MNIST dataset built by Zalando Research][5] — 60,000 training and 10,000 test greyscale images across 10 clothing classes — is available directly in the tf.keras.datasets module. With the
following code, we download the dataset into training and testing datasets,
and create human-readable labels.
First of all, we need to import all required libraries.
import tensorflow as tf
import pandas as pd
import numpy as np
from sklearn.metrics import confusion_matrix
import itertools
import random
import matplotlib.pyplot as plt
Next, we load the Fashion MNIST dataset from keras.
# Fashion dataset
fashion_mnist = tf.keras.datasets.fashion_mnist
# Get the training and testing data
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
# Create human-readable labels
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz
32768/29515 [=================================] - 0s 0us/step
40960/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
26435584/26421880 [==============================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz
16384/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
4431872/4422102 [==============================] - 0s 0us/step
We see the shapes of downloaded training and testing datasets with their labels.
print(f"Train images shape: {train_images.shape}")
print(f"Test images shape: {test_images.shape}")
print(f"Train labels shape: {train_labels.shape}")
print(f"Test labels shape: {test_labels.shape}")
Train images shape: (60000, 28, 28)
Test images shape: (10000, 28, 28)
Train labels shape: (60000,)
Test labels shape: (10000,)
Exploring the Fashion MNIST Image Data
🔒 Subscribe to keep reading.
Preprocessing Fashion MNIST Pixel Values for a Neural Network
🔒 Subscribe to keep reading.
Building a Multiclass Classification Model in TensorFlow
🔒 Subscribe to keep reading.
Generating Prediction Probabilities with Softmax Output
🔒 Subscribe to keep reading.
Inspecting Learned Weights and Biases in Model Layers
🔒 Subscribe to keep reading.
Key Takeaways: Multiclass Classification with TensorFlow
🔒 Subscribe to keep reading.
References
🔒 Subscribe to keep reading.
Subscribe to unlock the full article ❤️
Log in to unlock
The form below signs you up for the newsletter. It does not log you into the app — log in afterwards (same email) to unlock this article and download your subscriber gifts. New subscribers get an inbox mail: Set a password to unlock articles.
Full content temporarily unavailable — refresh in a moment