A multi-class classification model helps us teach a computer to tell which category something belongs to when there are more than two choices.
Multi-class classification model in TensorFlow
Start learning this pattern below
Jump into concepts and practice - no test required
model = tf.keras.Sequential([
tf.keras.layers.Dense(units, activation='relu', input_shape=(input_features,)),
tf.keras.layers.Dense(number_of_classes, activation='softmax')
])
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)The last layer uses softmax to give probabilities for each class.
Use sparse_categorical_crossentropy loss when labels are integers representing classes.
model = tf.keras.Sequential([
tf.keras.layers.Dense(16, activation='relu', input_shape=(4,)),
tf.keras.layers.Dense(3, activation='softmax')
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])model = tf.keras.Sequential([
tf.keras.layers.Dense(32, activation='relu', input_shape=(10,)),
tf.keras.layers.Dense(5, activation='softmax')
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])This program trains a simple multi-class model on small sample data with 3 classes. It then predicts classes for new samples.
import tensorflow as tf import numpy as np # Sample data: 6 samples, 4 features each X_train = np.array([ [5.1, 3.5, 1.4, 0.2], [7.0, 3.2, 4.7, 1.4], [6.3, 3.3, 6.0, 2.5], [5.0, 3.6, 1.4, 0.2], [6.7, 3.1, 4.4, 1.4], [7.6, 3.0, 6.6, 2.1] ]) # Labels: 3 classes (0, 1, 2) y_train = np.array([0, 1, 2, 0, 1, 2]) # Build model model = tf.keras.Sequential([ tf.keras.layers.Dense(10, activation='relu', input_shape=(4,)), tf.keras.layers.Dense(3, activation='softmax') ]) # Compile model model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) # Train model history = model.fit(X_train, y_train, epochs=10, verbose=0) # Predict on new data X_new = np.array([ [5.9, 3.0, 5.1, 1.8], [5.0, 3.4, 1.5, 0.2] ]) predictions = model.predict(X_new) predicted_classes = predictions.argmax(axis=1) print(f"Training accuracy after 10 epochs: {history.history['accuracy'][-1]:.2f}") print(f"Predicted classes for new samples: {predicted_classes.tolist()}")
Make sure your labels are integers starting from 0 for sparse categorical loss.
Softmax outputs probabilities that add up to 1 for each sample.
More epochs usually improve accuracy but watch out for overfitting.
Multi-class models classify inputs into more than two categories.
Use softmax activation in the last layer to get class probabilities.
Use sparse_categorical_crossentropy loss when labels are integer class IDs.
Practice
What activation function is commonly used in the last layer of a multi-class classification model in TensorFlow?
Solution
Step 1: Understand the purpose of the last layer in multi-class classification
The last layer outputs probabilities for each class, so the activation must convert raw scores to probabilities.Step 2: Identify the activation function that outputs probabilities summing to 1
Softmax converts logits into probabilities that sum to 1, suitable for multi-class classification.Final Answer:
Softmax -> Option CQuick Check:
Softmax = last layer activation [OK]
- Using sigmoid which is for binary classification
- Using ReLU which does not output probabilities
- Using tanh which outputs values between -1 and 1
Which loss function should you use in TensorFlow for a multi-class classification model with integer labels?
Solution
Step 1: Identify the label format
Labels are integer class IDs, not one-hot encoded vectors.Step 2: Choose loss function matching integer labels for multi-class
Sparse categorical crossentropy works with integer labels directly, unlike categorical crossentropy which needs one-hot labels.Final Answer:
sparse_categorical_crossentropy -> Option BQuick Check:
Integer labels = sparse_categorical_crossentropy [OK]
- Using binary_crossentropy which is for two classes
- Using mean_squared_error which is for regression
- Using hinge loss which is for SVMs
What will be the shape of the output tensor from the last layer of this TensorFlow model for multi-class classification with 4 classes?
model = tf.keras.Sequential([ tf.keras.layers.Dense(10, activation='relu'), tf.keras.layers.Dense(4, activation='softmax') ]) inputs = tf.random.uniform((5, 8)) outputs = model(inputs) print(outputs.shape)
Solution
Step 1: Understand input and output shapes
Input batch size is 5, each input has 8 features. The last Dense layer outputs 4 units (classes).Step 2: Determine output shape from last layer
Output shape is (batch_size, number_of_classes) = (5, 4).Final Answer:
(5, 4) -> Option DQuick Check:
Batch size 5, classes 4 = (5, 4) [OK]
- Confusing batch size and feature dimensions
- Swapping rows and columns in output shape
- Assuming output shape matches input feature size
Identify the error in this TensorFlow multi-class classification model code:
model = tf.keras.Sequential([ tf.keras.layers.Dense(16, activation='relu'), tf.keras.layers.Dense(3, activation='sigmoid') ]) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
Solution
Step 1: Check last layer activation for multi-class
Sigmoid outputs independent probabilities, not suitable for multi-class where classes are exclusive.Step 2: Correct activation for multi-class classification
Softmax outputs probabilities summing to 1, appropriate for multi-class classification.Final Answer:
Last layer activation should be softmax, not sigmoid -> Option AQuick Check:
Multi-class needs softmax activation [OK]
- Using sigmoid activation for multi-class output
- Confusing loss functions for classification types
- Thinking optimizer name 'adam' is invalid
You want to build a multi-class classification model with 5 classes. Your labels are integers from 0 to 4. Which of the following code snippets correctly defines and compiles the model?
Option A: model = tf.keras.Sequential([ tf.keras.layers.Dense(32, activation='relu'), tf.keras.layers.Dense(5, activation='softmax') ]) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) Option B: model = tf.keras.Sequential([ tf.keras.layers.Dense(32, activation='relu'), tf.keras.layers.Dense(5, activation='sigmoid') ]) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) Option C: model = tf.keras.Sequential([ tf.keras.layers.Dense(32, activation='relu'), tf.keras.layers.Dense(1, activation='softmax') ]) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) Option D: model = tf.keras.Sequential([ tf.keras.layers.Dense(32, activation='relu'), tf.keras.layers.Dense(5, activation='softmax') ]) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
Solution
Step 1: Check output layer units and activation
For 5 classes, output units must be 5 with softmax activation to get class probabilities.Step 2: Check loss function matches label format
Labels are integers, so sparse_categorical_crossentropy is correct loss.Step 3: Verify optimizer and metrics
Adam optimizer and accuracy metric are appropriate choices.Final Answer:
Option A -> Option AQuick Check:
Correct output units, activation, and loss for integer labels [OK]
- Using sigmoid activation for multi-class output
- Using binary_crossentropy loss for multi-class
- Setting output units to 1 instead of number of classes
