For text classification using RNNs, the key metrics are accuracy, precision, recall, and F1-score. Accuracy tells us how many texts were correctly labeled overall. But if classes are uneven, precision and recall help us understand how well the model finds each class. F1-score balances precision and recall, giving a fair view when classes are imbalanced.
RNN for text classification in NLP - Model Metrics & Evaluation
Start learning this pattern below
Jump into concepts and practice - no test required
Actual \ Predicted | Positive | Negative
-------------------|----------|---------
Positive | 80 | 20
Negative | 10 | 90
Total samples = 200
TP = 80, FP = 10, TN = 90, FN = 20
From this matrix, we calculate:
- Precision = 80 / (80 + 10) = 0.89
- Recall = 80 / (80 + 20) = 0.80
- F1-score = 2 * (0.89 * 0.80) / (0.89 + 0.80) ≈ 0.84
- Accuracy = (80 + 90) / 200 = 0.85
Imagine a spam filter using an RNN:
- High precision: Few good emails are wrongly marked as spam. Users don't miss important messages.
- High recall: Most spam emails are caught, but some good emails might be wrongly flagged.
Depending on what matters more, you tune the model to favor precision or recall.
Good: Accuracy above 85%, precision and recall above 80%, and balanced F1-score. This means the model correctly classifies most texts and handles class imbalance well.
Bad: High accuracy but very low recall (e.g., 30%) means the model misses many positive cases. Or high recall but very low precision means many false alarms.
- Accuracy paradox: High accuracy can be misleading if classes are imbalanced.
- Data leakage: If test data leaks into training, metrics look unrealistically good.
- Overfitting: Very high training accuracy but low test accuracy means the model memorizes instead of learning.
Your RNN text classifier has 98% accuracy but only 12% recall on the positive class. Is it good for production? Why or why not?
Answer: No, it is not good. The model misses most positive cases (low recall), which is critical if positive detection matters. High accuracy is misleading because the negative class dominates.
Practice
Solution
Step 1: Understand RNN's role in text
RNNs process sequences of words one by one, keeping track of previous words to understand context.Step 2: Identify why order matters
Text meaning depends on word order, and RNNs remember this order, unlike simple models.Final Answer:
Because RNNs can remember the order of words and context in sentences -> Option DQuick Check:
RNN remembers sequence = D [OK]
- Thinking RNNs are faster than other models
- Believing RNNs don't need training data
- Confusing RNNs with image-only models
Solution
Step 1: Recall SimpleRNN syntax
SimpleRNN requires number of units and input shape for the first layer in a model.Step 2: Check options for correct usage
model.add(SimpleRNN(32, input_shape=(None, 100))) correctly specifies 32 units and input shape (sequence length unknown, 100 features).Final Answer:
model.add(SimpleRNN(32, input_shape=(None, 100))) -> Option AQuick Check:
SimpleRNN needs units and input shape first layer = A [OK]
- Omitting input_shape in first RNN layer
- Using activation='relu' instead of default tanh
- Passing units as keyword incorrectly
model = Sequential() model.add(Embedding(input_dim=5000, output_dim=16, input_length=10)) model.add(SimpleRNN(8)) model.add(Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) history = model.fit(X_train, y_train, epochs=2, batch_size=32) print(history.history['accuracy'][-1])
What does
history.history['accuracy'][-1] represent?Solution
Step 1: Understand Keras history object
history.history['accuracy'] stores training accuracy per epoch, so last element is final epoch training accuracy.Step 2: Differentiate training vs batch vs validation
It is training accuracy on all training data after last epoch, not batch or validation accuracy.Final Answer:
The accuracy of the model on the entire training data after the last epoch -> Option AQuick Check:
history.history['accuracy'][-1] = final training accuracy [OK]
- Confusing batch accuracy with epoch accuracy
- Mixing loss and accuracy values
- Assuming validation accuracy without validation data
model = Sequential() model.add(SimpleRNN(16)) model.add(Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
What is the most likely cause of the error?
Solution
Step 1: Check first layer requirements
The first RNN layer must know input shape to accept data; missing input_shape causes error.Step 2: Validate other options
Sigmoid activation in Dense is valid for binary classification; units can be any positive integer; binary_crossentropy is valid loss.Final Answer:
Missing input shape for the first SimpleRNN layer -> Option CQuick Check:
First RNN layer needs input_shape = B [OK]
- Assuming activation or loss function causes error
- Thinking units must be 32 or more
- Ignoring input shape requirement
Original: model = Sequential() model.add(SimpleRNN(16, input_shape=(10, 100))) model.add(Dense(1, activation='sigmoid')) Change: model = Sequential() model.add(Embedding(input_dim=5000, output_dim=100, input_length=10)) model.add(SimpleRNN(16)) model.add(Dense(1, activation='sigmoid'))
Solution
Step 1: Understand Embedding role
Embedding layer converts integer word indices into dense vectors, preparing input for RNN.Step 2: Check model order and shapes
Embedding outputs shape (batch, sequence_length, output_dim), matching SimpleRNN expected input shape, so no input_shape needed in SimpleRNN.Final Answer:
Correct: Embedding converts word indices to vectors, so SimpleRNN input shape changes automatically -> Option BQuick Check:
Embedding before RNN changes input shape correctly = C [OK]
- Placing Embedding after RNN
- Matching output_dim to RNN units incorrectly
- Adding activation to Embedding layer
