Bird
Raised Fist0
TensorFlowml~20 mins

Text classification model in TensorFlow - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Text Classification Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Model Choice
intermediate
2:00remaining
Choosing the right model for text classification

You want to classify short text messages into categories. Which TensorFlow model architecture is best suited for this task?

AA simple feedforward neural network with dense layers only
BA recurrent neural network (RNN) or LSTM model that processes sequences
CA convolutional neural network (CNN) designed for image data
DA linear regression model
Attempts:
2 left
💡 Hint

Think about models that understand sequences of words.

❓ Predict Output
intermediate
2:00remaining
Output shape after embedding layer

Given this TensorFlow code snippet for text classification, what is the shape of embedded_sequences?

TensorFlow
import tensorflow as tf
vocab_size = 10000
embedding_dim = 16
max_length = 20
model = tf.keras.Sequential([
    tf.keras.layers.Embedding(vocab_size, embedding_dim, input_length=max_length)
])
input_example = tf.constant([[1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
embedded_sequences = model(input_example)
print(embedded_sequences.shape)
A(1, 16)
B(20, 16)
C(16, 20)
D(1, 20, 16)
Attempts:
2 left
💡 Hint

The embedding layer outputs one vector per input word, preserving batch size and sequence length.

❓ Hyperparameter
advanced
2:00remaining
Choosing batch size for training text classification

You are training a text classification model on a large dataset. Which batch size choice is likely to improve training stability and speed without using too much memory?

ABatch size of 32 or 64 (moderate size)
BBatch size of 1024 (very large batch)
CBatch size equal to the entire dataset
DBatch size of 1 (stochastic gradient descent)
Attempts:
2 left
💡 Hint

Consider a balance between memory use and stable gradient estimates.

❓ Metrics
advanced
2:00remaining
Evaluating multi-class text classification accuracy

You trained a text classification model with 5 categories. After evaluation, your accuracy is 0.85. What does this accuracy mean?

A85% of the model's predictions matched the true category labels
BThe model predicted the correct category for 85% of the words in each text
CThe model's loss value is 0.85
DThe model's predictions are 85% confident
Attempts:
2 left
💡 Hint

Accuracy measures how many predictions match the true labels.

🔧 Debug
expert
2:00remaining
Identifying error in TensorFlow text classification model code

What error will this TensorFlow code raise when run?

import tensorflow as tf
model = tf.keras.Sequential([
    tf.keras.layers.Embedding(input_dim=5000, output_dim=16),
    tf.keras.layers.GlobalAveragePooling1D(),
    tf.keras.layers.Dense(1, activation='softmax')
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
ATypeError because GlobalAveragePooling1D requires 3D input
BNo error, code runs fine
CValueError due to softmax activation with one output unit
DRuntimeError due to missing input shape
Attempts:
2 left
💡 Hint

Check if the output layer activation matches the number of classes.

Practice

(1/5)
1. What is the main purpose of a text classification model in TensorFlow?
easy
A. To sort text into different categories based on content
B. To generate new text from a given input
C. To translate text from one language to another
D. To detect objects in images

Solution

  1. Step 1: Understand the task of text classification

    Text classification means assigning labels or categories to text data based on its content.
  2. Step 2: Match the task to the options

    To sort text into different categories based on content describes sorting text into categories, which matches text classification.
  3. Final Answer:

    To sort text into different categories based on content -> Option A
  4. Quick Check:

    Text classification = sorting text [OK]
Hint: Text classification means sorting text into groups [OK]
Common Mistakes:
  • Confusing classification with text generation
  • Thinking classification is for images
  • Mixing up translation with classification
2. Which of the following is the correct way to add an embedding layer in a TensorFlow Sequential model for text classification?
easy
A. model.add(tf.keras.layers.Dense(64, input_dim=1000))
B. model.add(tf.keras.layers.Conv2D(64, kernel_size=3))
C. model.add(tf.keras.layers.Embedding(input_dim=1000, output_dim=64))
D. model.add(tf.keras.layers.LSTM(64, input_shape=(1000,)))

Solution

  1. Step 1: Identify the embedding layer syntax

    The embedding layer in TensorFlow is added with tf.keras.layers.Embedding and requires input_dim and output_dim parameters.
  2. Step 2: Check each option

    model.add(tf.keras.layers.Embedding(input_dim=1000, output_dim=64)) correctly uses Embedding with input_dim=1000 and output_dim=64. Others use Dense, Conv2D, or LSTM which are not embedding layers.
  3. Final Answer:

    model.add(tf.keras.layers.Embedding(input_dim=1000, output_dim=64)) -> Option C
  4. Quick Check:

    Embedding layer uses tf.keras.layers.Embedding [OK]
Hint: Embedding layer uses tf.keras.layers.Embedding with input/output dims [OK]
Common Mistakes:
  • Using Dense or Conv2D instead of Embedding
  • Missing input_dim or output_dim parameters
  • Confusing LSTM with embedding layer
3. Given the following TensorFlow model code for text classification, what will be the shape of the output layer's predictions for a batch of 32 samples if there are 5 classes?
model = tf.keras.Sequential([
  tf.keras.layers.Embedding(input_dim=1000, output_dim=16, input_length=10),
  tf.keras.layers.GlobalAveragePooling1D(),
  tf.keras.layers.Dense(5, activation='softmax')
])

predictions = model(tf.random.uniform((32, 10), maxval=1000, dtype=tf.int32))
medium
A. (10, 5)
B. (32, 5)
C. (32, 10)
D. (5, 32)

Solution

  1. Step 1: Analyze model output layer

    The last Dense layer has 5 units with softmax, so output shape per sample is (5,).
  2. Step 2: Consider batch size

    Input batch size is 32, so output shape is (32, 5).
  3. Final Answer:

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

    Batch size 32 x 5 classes = (32, 5) [OK]
Hint: Output shape = (batch size, number of classes) [OK]
Common Mistakes:
  • Confusing sequence length with batch size
  • Mixing up input and output shapes
  • Ignoring batch dimension in output
4. You wrote this TensorFlow model for text classification but get a shape error during training:
model = tf.keras.Sequential([
  tf.keras.layers.Embedding(input_dim=5000, output_dim=32),
  tf.keras.layers.Dense(10, activation='relu'),
  tf.keras.layers.Dense(3, activation='softmax')
])

What is the likely cause of the error?
medium
A. Dense layers cannot follow Embedding layers
B. Using wrong activation function in the last layer
C. input_dim in Embedding is too small
D. Missing a Flatten or GlobalAveragePooling1D layer after Embedding

Solution

  1. Step 1: Understand Embedding output shape

    Embedding outputs 3D tensor (batch, sequence_length, embedding_dim). Dense expects 2D input.
  2. Step 2: Identify missing layer

    Without Flatten or GlobalAveragePooling1D, Dense layer gets wrong input shape causing error.
  3. Final Answer:

    Missing a Flatten or GlobalAveragePooling1D layer after Embedding -> Option D
  4. Quick Check:

    Embedding output must be flattened before Dense [OK]
Hint: Add pooling or flatten after Embedding before Dense layers [OK]
Common Mistakes:
  • Assuming Dense accepts 3D input
  • Changing activation won't fix shape error
  • Believing Dense can't follow Embedding at all
5. You want to build a text classification model in TensorFlow that handles variable-length input sentences and classifies them into 4 categories. Which model design is best?
hard
A. Use Embedding layer, GlobalAveragePooling1D, then Dense with 4 units and softmax activation
B. Use Embedding layer, Flatten, then Dense with 4 units and sigmoid activation
C. Use Embedding layer, Conv2D, then Dense with 1 unit and softmax activation
D. Use Dense layers only without Embedding

Solution

  1. Step 1: Handle variable-length input

    GlobalAveragePooling1D can convert variable-length sequences to fixed-size vectors.
  2. Step 2: Choose output layer for multi-class classification

    Dense with 4 units and softmax activation outputs probabilities for 4 classes.
  3. Final Answer:

    Use Embedding layer, GlobalAveragePooling1D, then Dense with 4 units and softmax activation -> Option A
  4. Quick Check:

    Pooling + softmax = best for variable-length multi-class [OK]
Hint: Use pooling + softmax for variable-length multi-class text [OK]
Common Mistakes:
  • Using Flatten breaks variable-length input handling
  • Using sigmoid for multi-class instead of softmax
  • Using Conv2D for text sequences incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard