Building a Multi-Class Classification Model with TensorFlow

Lesson Overview

Hello, there! You have done a fantastic job with preprocessing the Iris dataset. Now, it's time to apply what you've learned by building a multi-class classification model using TensorFlow. We will guide you through each step of constructing and training this model specifically for our problem. We'll also unpack the history object returned by the model's training process. You're in good hands. Let's get started.

Loading the Preprocessed Dataset

Before we start building our multi-class classification model, we need to load our preprocessed dataset. To maintain modular code, we use the load_preprocessed_data function from our previous lesson, stored in data_preprocessing.py. This function handles loading, splitting, scaling, and one-hot encoding the Iris dataset, providing the data in a format that is ready to train our model.

Load the preprocessed dataset:

Python
from data_preprocessing import load_preprocessed_data

X_train, X_test, y_train, y_test = load_preprocessed_data()

Here's a brief recap of the data_preprocessing.py:

Python
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, OneHotEncoder

def load_preprocessed_data():
    # Load the Iris dataset
    iris = load_iris()
    X, y = iris.data, iris.target

    # Split the dataset into training and testing sets
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, stratify=y, random_state=42)

    # Scale the features
    scaler = StandardScaler().fit(X_train)
    X_train_scaled = scaler.transform(X_train)
    X_test_scaled = scaler.transform(X_test)

    # One-hot encode the targets
    encoder = OneHotEncoder(sparse_output=False).fit(y_train.reshape(-1, 1))
    y_train_encoded = encoder.transform(y_train.reshape(-1, 1))
    y_test_encoded = encoder.transform(y_test.reshape(-1, 1))

    return X_train_scaled, X_test_scaled, y_train_encoded, y_test_encoded

With the dataset loaded, we're ready to build our multi-class classification model.

Building the Model

The next step is building our multi-class classification model. TensorFlow makes it easy for us to construct models using the Sequential API which allows us to create models layer-by-layer.

Python
import tensorflow as tf

model = tf.keras.Sequential([
    tf.keras.layers.Input(shape=(4,)),
    tf.keras.layers.Dense(10, activation='relu'),
    tf.keras.layers.Dense(10, activation='relu'),
    tf.keras.layers.Dense(3, activation='softmax')
])

Our input shape aligns with the four features (sepal length, sepal width, petal length, petal width) in our Iris dataset. The model includes two dense layers, each having 10 neurons and ReLU (Rectified Linear Unit) as the activation function, which will help us introduce non-linearity into our model. Finally, we have an output layer with three neurons representing our three Iris species.

Our output layer uses the softmax activation function, which helps in multi-class classification problems by converting the raw output scores (logits) into probabilities. Softmax takes the output of each neuron and turns it into a probability between 0 and 1, with all probabilities adding up to 100%. Essentially, it tells us the likelihood that the input data point belongs to each of the three classes (Iris species) we have in our dataset. The class with the highest probability is selected as the model's prediction.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal