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.
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:
Python1from data_preprocessing import load_preprocessed_data 2 3X_train, X_test, y_train, y_test = load_preprocessed_data()
Here's a brief recap of the data_preprocessing.py
:
Python1import numpy as np 2from sklearn.datasets import load_iris 3from sklearn.model_selection import train_test_split 4from sklearn.preprocessing import StandardScaler, OneHotEncoder 5 6def load_preprocessed_data(): 7 # Load the Iris dataset 8 iris = load_iris() 9 X, y = iris.data, iris.target 10 11 # Split the dataset into training and testing sets 12 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, stratify=y, random_state=42) 13 14 # Scale the features 15 scaler = StandardScaler().fit(X_train) 16 X_train_scaled = scaler.transform(X_train) 17 X_test_scaled = scaler.transform(X_test) 18 19 # One-hot encode the targets 20 encoder = OneHotEncoder(sparse_output=False).fit(y_train.reshape(-1, 1)) 21 y_train_encoded = encoder.transform(y_train.reshape(-1, 1)) 22 y_test_encoded = encoder.transform(y_test.reshape(-1, 1)) 23 24 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.
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.
Python1import tensorflow as tf 2 3model = tf.keras.Sequential([ 4 tf.keras.layers.Input(shape=(4,)), 5 tf.keras.layers.Dense(10, activation='relu'), 6 tf.keras.layers.Dense(10, activation='relu'), 7 tf.keras.layers.Dense(3, activation='softmax') 8])
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.
Before we can train our model, we need to compile it. Compiling the model signifies that we're set with the architecture and ready to smooth the weights to better predict our data.
Let's compile our model:
Python1model.compile(optimizer='adam', 2 loss='categorical_crossentropy', 3 metrics=['accuracy'])
We use the adam
optimizer — a combination of RMSprop and Stochastic Gradient Descent with momentum. It adapts the parameter learning rates based on the average of recent weight updates. For the loss function, we're using categorical_crossentropy
, which suits multi-class classification problems. And finally, we're using accuracy as our evaluation metric.
Now comes the moment of truth — training our model on our preprocessed Iris dataset. Training involves the iterative process of feeding our data through the model (forward propagation), calculating an error (loss), and back-propagating through the model to adjust the weights.
We will use our test set as a validation set to monitor the performance of the model on unseen data during training. The validation set is not used to train the model; it is only used to evaluate the model's performance each epoch to ensure it generalizes well to new data.
Let's train our model:
Python1history = model.fit(X_train, y_train, epochs=150, batch_size=5, validation_data=(X_test, y_test))
We use the fit
method for training. We're training for 150 epochs, meaning the entire dataset will pass through the model 150 times. We've set a batch size of 5, which means that after the model has seen 5 samples, it will update the weights. We also pass in our test data for validation.
You might have noticed that model's fit
method returned an object called history
. But what exactly is it? history
is an object that TensorFlow provides out of the box and contains a record of training loss values and metrics values at successive epochs, along with validation loss values and validation metrics values (if applicable). Analyzing its values can help us understand the training process and possibly diagnose issues (such as overfitting). Let's see how to extract the data
from this object:
Python1print(history.history)
The output of the above code will have following format:
Plain text1{'accuracy': [0.3523809611797333, ...], 'loss': [1.6941570043563843, ...], 'val_accuracy': [0.2888889014720917, ...], 'val_loss': [1.6415398120880127, ...]}
This output summarises the model's performance over 150 epochs, showing accuracy and loss for both training and validation data sets. This kind of data is crucial for understanding how well our model is learning and generalizing to new data.
Great job! You've learned how to build, compile, and train a TensorFlow multi-class classification model and how to interpret training results through the history
object. Continue practicing these steps with the following coding exercises to solidify what you learned. Stay tuned for more lessons that will expand your TensorFlow and machine learning capabilities. Keep up the great work!