Bird
Raised Fist0
TensorFlowml~20 mins

Multi-class classification model in TensorFlow - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Multi-class Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Model Choice
intermediate
2:00remaining
Choosing the correct output layer for multi-class classification

You want to build a neural network in TensorFlow to classify images into 5 different categories. Which output layer configuration is correct for this multi-class classification task?

ADense(5, activation='softmax')
BDense(1, activation='sigmoid')
CDense(1, activation='softmax')
DDense(5, activation='sigmoid')
Attempts:
2 left
💡 Hint

For multi-class classification, the output layer should have one neuron per class with a softmax activation.

Metrics
intermediate
2:00remaining
Correct loss function for multi-class classification

Which loss function should you use in TensorFlow when training a multi-class classification model with one-hot encoded labels?

Atf.keras.losses.SparseCategoricalCrossentropy()
Btf.keras.losses.BinaryCrossentropy()
Ctf.keras.losses.MeanSquaredError()
Dtf.keras.losses.CategoricalCrossentropy()
Attempts:
2 left
💡 Hint

One-hot encoded labels require a loss function that compares probability distributions.

Predict Output
advanced
2:00remaining
Output shape of model predictions

Given the following TensorFlow model for 4-class classification, what is the shape of the output predictions for a batch of 10 samples?

TensorFlow
import tensorflow as tf
model = tf.keras.Sequential([
    tf.keras.layers.Dense(16, activation='relu', input_shape=(8,)),
    tf.keras.layers.Dense(4, activation='softmax')
])
import numpy as np
sample_input = np.random.random((10, 8))
predictions = model(sample_input)
predictions_shape = predictions.shape
A(10, 4)
B(4, 10)
C(10, 1)
D(1, 4)
Attempts:
2 left
💡 Hint

The output shape matches the batch size and number of classes.

Hyperparameter
advanced
2:00remaining
Effect of batch size on training stability

In training a multi-class classification model, what is a common effect of increasing the batch size too much?

ATraining becomes more noisy and always improves generalization
BModel always overfits immediately
CTraining becomes less noisy but may converge to sharp minima causing worse generalization
DTraining speed decreases significantly without affecting accuracy
Attempts:
2 left
💡 Hint

Think about how batch size affects gradient noise and generalization.

🔧 Debug
expert
3:00remaining
Identifying the cause of poor multi-class model accuracy

You trained a multi-class classification model with 3 classes using one-hot encoded labels. The model's accuracy stays around 33% (random guess). Which of the following is the most likely cause?

AUsing CategoricalCrossentropy loss with one-hot encoded labels
BUsing SparseCategoricalCrossentropy loss with one-hot encoded labels
CUsing softmax activation in the output layer
DUsing Adam optimizer
Attempts:
2 left
💡 Hint

Check if the loss function matches the label format.

Practice

(1/5)
1.

What activation function is commonly used in the last layer of a multi-class classification model in TensorFlow?

easy
A. Sigmoid
B. ReLU
C. Softmax
D. Tanh

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Softmax -> Option C
  4. Quick Check:

    Softmax = last layer activation [OK]
Hint: Use softmax for multi-class output probabilities [OK]
Common Mistakes:
  • Using sigmoid which is for binary classification
  • Using ReLU which does not output probabilities
  • Using tanh which outputs values between -1 and 1
2.

Which loss function should you use in TensorFlow for a multi-class classification model with integer labels?

easy
A. binary_crossentropy
B. sparse_categorical_crossentropy
C. mean_squared_error
D. hinge

Solution

  1. Step 1: Identify the label format

    Labels are integer class IDs, not one-hot encoded vectors.
  2. 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.
  3. Final Answer:

    sparse_categorical_crossentropy -> Option B
  4. Quick Check:

    Integer labels = sparse_categorical_crossentropy [OK]
Hint: Use sparse_categorical_crossentropy for integer class labels [OK]
Common Mistakes:
  • Using binary_crossentropy which is for two classes
  • Using mean_squared_error which is for regression
  • Using hinge loss which is for SVMs
3.

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)
medium
A. (4, 5)
B. (8, 4)
C. (5, 10)
D. (5, 4)

Solution

  1. 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).
  2. Step 2: Determine output shape from last layer

    Output shape is (batch_size, number_of_classes) = (5, 4).
  3. Final Answer:

    (5, 4) -> Option D
  4. Quick Check:

    Batch size 5, classes 4 = (5, 4) [OK]
Hint: Output shape = (batch_size, number_of_classes) [OK]
Common Mistakes:
  • Confusing batch size and feature dimensions
  • Swapping rows and columns in output shape
  • Assuming output shape matches input feature size
4.

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'])
medium
A. Last layer activation should be softmax, not sigmoid
B. Loss function should be binary_crossentropy
C. Optimizer 'adam' is invalid
D. Dense layer units must be 1 for multi-class

Solution

  1. Step 1: Check last layer activation for multi-class

    Sigmoid outputs independent probabilities, not suitable for multi-class where classes are exclusive.
  2. Step 2: Correct activation for multi-class classification

    Softmax outputs probabilities summing to 1, appropriate for multi-class classification.
  3. Final Answer:

    Last layer activation should be softmax, not sigmoid -> Option A
  4. Quick Check:

    Multi-class needs softmax activation [OK]
Hint: Use softmax activation for multi-class last layer [OK]
Common Mistakes:
  • Using sigmoid activation for multi-class output
  • Confusing loss functions for classification types
  • Thinking optimizer name 'adam' is invalid
5.

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'])
hard
A. Option A
B. Option B
C. Option C
D. Option D

Solution

  1. Step 1: Check output layer units and activation

    For 5 classes, output units must be 5 with softmax activation to get class probabilities.
  2. Step 2: Check loss function matches label format

    Labels are integers, so sparse_categorical_crossentropy is correct loss.
  3. Step 3: Verify optimizer and metrics

    Adam optimizer and accuracy metric are appropriate choices.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Correct output units, activation, and loss for integer labels [OK]
Hint: Match output units and loss to label format [OK]
Common Mistakes:
  • Using sigmoid activation for multi-class output
  • Using binary_crossentropy loss for multi-class
  • Setting output units to 1 instead of number of classes