Bird
Raised Fist0
TensorFlowml~20 mins

Sentiment analysis in TensorFlow - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Sentiment Analysis Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of model prediction on sample input
Given the following TensorFlow model for sentiment analysis, what is the output shape of the prediction for a batch of 3 samples?
TensorFlow
import tensorflow as tf

model = tf.keras.Sequential([
    tf.keras.layers.Embedding(input_dim=10000, output_dim=16, input_length=100),
    tf.keras.layers.GlobalAveragePooling1D(),
    tf.keras.layers.Dense(16, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
])

sample_input = tf.constant([[1]*100, [2]*100, [3]*100])
prediction = model(sample_input)
output_shape = prediction.shape
A(3, 1)
B(1, 3)
C(3,)
D(100, 1)
Attempts:
2 left
💡 Hint
The model outputs one value per input sample indicating sentiment probability.
❓ Model Choice
intermediate
2:00remaining
Best model layer for capturing word order in sentiment analysis
Which layer is best suited to capture the order of words in a sentence for sentiment analysis?
AEmbedding layer
BDense layer with ReLU activation
CGlobalAveragePooling1D layer
DLSTM (Long Short-Term Memory) layer
Attempts:
2 left
💡 Hint
Consider which layer can remember sequences over time.
❓ Hyperparameter
advanced
2:00remaining
Choosing the right batch size for training sentiment analysis model
You are training a sentiment analysis model on a dataset of 10,000 samples. Which batch size is likely to provide a good balance between training speed and model convergence?
A16
B5000
C1
D10000
Attempts:
2 left
💡 Hint
Very small batch sizes slow training; very large batch sizes may harm convergence.
❓ Metrics
advanced
2:00remaining
Interpreting accuracy and loss during sentiment analysis training
During training a binary sentiment analysis model, you observe that the training loss decreases steadily but the validation accuracy remains around 50%. What does this indicate?
AThe model is underfitting the training data
BThe model is overfitting the training data
CThe model is performing well on both training and validation data
DThere is a bug in the loss calculation
Attempts:
2 left
💡 Hint
Consider what it means when training improves but validation does not.
🔧 Debug
expert
2:00remaining
Identifying cause of NaN loss during sentiment analysis training
You train a sentiment analysis model and suddenly the loss becomes NaN after a few epochs. Which of the following is the most likely cause?
AUsing sigmoid activation in the output layer with binary crossentropy loss
BBatch size too small causing noisy gradients
CLearning rate set too high causing unstable training
DEmbedding layer input_dim smaller than vocabulary size
Attempts:
2 left
💡 Hint
NaN loss often happens when updates are too large and cause numerical instability.

Practice

(1/5)
1. What is the main goal of sentiment analysis in TensorFlow?
easy
A. To translate text into another language
B. To classify text as positive or negative
C. To generate new text automatically
D. To count the number of words in a sentence

Solution

  1. Step 1: Understand sentiment analysis purpose

    Sentiment analysis is used to find the feeling or opinion in text, usually positive or negative.
  2. Step 2: Match goal with options

    Among the options, only classifying text as positive or negative matches sentiment analysis.
  3. Final Answer:

    To classify text as positive or negative -> Option B
  4. Quick Check:

    Sentiment analysis = classify positive/negative [OK]
Hint: Sentiment analysis = positive or negative text classification [OK]
Common Mistakes:
  • Confusing sentiment analysis with translation
  • Thinking it generates new text
  • Believing it only counts words
2. Which TensorFlow layer is commonly used to convert words into numbers for sentiment analysis?
easy
A. Dense layer
B. Flatten layer
C. Dropout layer
D. Embedding layer

Solution

  1. Step 1: Identify layer for word representation

    Embedding layers convert words into numeric vectors that models can understand.
  2. Step 2: Compare with other layers

    Dense layers are for fully connected neurons, Dropout prevents overfitting, Flatten reshapes data, none convert words to numbers.
  3. Final Answer:

    Embedding layer -> Option D
  4. Quick Check:

    Embedding layer = word to number conversion [OK]
Hint: Embedding layer turns words into numbers for models [OK]
Common Mistakes:
  • Choosing Dense layer for word conversion
  • Confusing Dropout with embedding
  • Using Flatten layer incorrectly
3. What will be the output shape of the embedding layer if input shape is (None, 10) and embedding dimension is 16?
medium
A. (None, 10, 16)
B. (None, 16, 10)
C. (10, 16)
D. (None, 10)

Solution

  1. Step 1: Understand embedding input and output shapes

    Input shape (None, 10) means batch size unknown, sequence length 10. Embedding outputs a vector per word with size 16.
  2. Step 2: Calculate output shape

    Output shape is (batch_size, sequence_length, embedding_dim) = (None, 10, 16).
  3. Final Answer:

    (None, 10, 16) -> Option A
  4. Quick Check:

    Embedding output shape = (batch, seq_len, embed_dim) [OK]
Hint: Embedding output shape = input length x embedding size [OK]
Common Mistakes:
  • Swapping sequence length and embedding dimension
  • Ignoring batch size dimension
  • Confusing input and output shapes
4. Identify the error in this TensorFlow model snippet for sentiment analysis:
model = tf.keras.Sequential([
  tf.keras.layers.Embedding(input_dim=1000, output_dim=16, input_length=10),
  tf.keras.layers.Dense(1, activation='sigmoid')
])
medium
A. Dense layer activation should be 'relu' for binary output
B. Embedding layer input_dim should be output_dim
C. Missing Flatten or GlobalPooling layer before Dense
D. input_length parameter is not allowed in Embedding layer

Solution

  1. Step 1: Analyze model layer sequence

    Embedding outputs 3D tensor (batch, seq_len, embed_dim). Dense expects 2D input, so flattening or pooling is needed.
  2. Step 2: Identify missing layer

    Without Flatten or GlobalPooling, Dense layer will raise shape error.
  3. Final Answer:

    Missing Flatten or GlobalPooling layer before Dense -> Option C
  4. Quick Check:

    Embedding output must be flattened before Dense [OK]
Hint: Add Flatten or pooling before Dense after Embedding [OK]
Common Mistakes:
  • Confusing input_dim and output_dim in Embedding
  • Using wrong activation for binary classification
  • Thinking input_length is invalid parameter
5. You want to improve a sentiment analysis model's accuracy on short reviews. Which approach is best?
hard
A. Increase embedding dimension and add a Bidirectional LSTM layer
B. Reduce embedding dimension and remove all recurrent layers
C. Use only a Dense layer without embedding
D. Train on fewer examples to avoid overfitting

Solution

  1. Step 1: Consider model improvements for short text

    Increasing embedding dimension helps capture more word features; Bidirectional LSTM captures context from both directions.
  2. Step 2: Evaluate other options

    Reducing embedding or removing recurrent layers loses information; training on fewer examples reduces learning quality.
  3. Final Answer:

    Increase embedding dimension and add a Bidirectional LSTM layer -> Option A
  4. Quick Check:

    More embedding + Bidirectional LSTM = better short text accuracy [OK]
Hint: Use bigger embeddings and bidirectional LSTM for short texts [OK]
Common Mistakes:
  • Thinking smaller embeddings improve accuracy
  • Removing recurrent layers harms context understanding
  • Training on fewer examples reduces model learning
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