Bird
Raised Fist0
NLPml~8 mins

BERT pre-training concept in NLP - Model Metrics & Evaluation

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
Metrics & Evaluation - BERT pre-training concept
Which metric matters for BERT pre-training and WHY

BERT pre-training uses two main tasks: Masked Language Modeling (MLM) and Next Sentence Prediction (NSP). For MLM, the key metric is cross-entropy loss, which measures how well the model predicts missing words. Lower loss means better understanding of language context.

For NSP, accuracy is important because it shows how well the model predicts if one sentence follows another. High accuracy means the model learns sentence relationships well.

These metrics matter because BERT learns language patterns without labeled data. Good MLM loss and NSP accuracy mean BERT can understand words and sentence order, which helps later tasks like question answering.

Confusion matrix for Next Sentence Prediction (NSP)
    | Predicted Yes | Predicted No |
    |---------------|--------------|
    | True Yes  (TP)| False No (FN)|
    | False Yes (FP)| True No  (TN)|

    Example:
    TP = 8000, FP = 2000
    FN = 1500, TN = 8500

    Total samples = 8000 + 2000 + 1500 + 8500 = 20000
    Accuracy = (TP + TN) / Total = (8000 + 8500) / 20000 = 0.825 or 82.5%
    

This matrix helps check how well BERT predicts if one sentence follows another during pre-training.

Precision vs Recall tradeoff in NSP task

In NSP, precision means when BERT says "sentence B follows sentence A," how often it is correct.

Recall means how many actual sentence pairs BERT correctly identifies as following each other.

High precision but low recall means BERT is careful but misses many true pairs.

High recall but low precision means BERT finds most pairs but makes many mistakes.

For BERT pre-training, balanced precision and recall are best to learn good sentence relations.

What good vs bad metrics look like for BERT pre-training
  • Good MLM loss: Low cross-entropy loss (e.g., below 1.0) means BERT predicts masked words well.
  • Bad MLM loss: High loss (e.g., above 2.0) means poor word prediction.
  • Good NSP accuracy: Above 80% accuracy shows BERT understands sentence order well.
  • Bad NSP accuracy: Around 50% means random guessing, no learning.
Common pitfalls in BERT pre-training metrics
  • Ignoring MLM loss: Focusing only on NSP accuracy can miss poor word understanding.
  • Data leakage: Using test data in pre-training can inflate metrics falsely.
  • Overfitting: Very low MLM loss but poor downstream task performance means BERT memorized training data.
  • Accuracy paradox: High NSP accuracy on unbalanced data can be misleading if one class dominates.
Self-check question

Your BERT pre-training shows 98% NSP accuracy but MLM loss is very high (3.5). Is this good?

Answer: No, because high NSP accuracy alone is not enough. The high MLM loss means BERT is not learning to predict masked words well. This hurts its language understanding. Both tasks must have good metrics for effective pre-training.

Key Result
BERT pre-training success depends on low MLM loss and balanced NSP accuracy to learn language context and sentence order.

Practice

(1/5)
1. What are the two main tasks used during BERT pre-training?
easy
A. Text Classification and Named Entity Recognition
B. Masked Language Model and Next Sentence Prediction
C. Part-of-Speech Tagging and Dependency Parsing
D. Sentiment Analysis and Machine Translation

Solution

  1. Step 1: Understand BERT pre-training tasks

    BERT is trained to predict missing words and the order of sentences, which correspond to Masked Language Model (MLM) and Next Sentence Prediction (NSP).
  2. Step 2: Match tasks to options

    Only Masked Language Model and Next Sentence Prediction lists MLM and NSP, the two key pre-training tasks of BERT.
  3. Final Answer:

    Masked Language Model and Next Sentence Prediction -> Option B
  4. Quick Check:

    BERT pre-training tasks = MLM + NSP [OK]
Hint: Remember BERT guesses missing words and sentence order [OK]
Common Mistakes:
  • Confusing fine-tuning tasks with pre-training tasks
  • Mixing up NLP tasks unrelated to BERT pre-training
  • Thinking BERT uses only one pre-training task
2. Which of the following is the correct way to describe the Masked Language Model (MLM) task in BERT pre-training?
easy
A. Predict randomly masked words in a sentence
B. Predict the next sentence given the current sentence
C. Classify the sentiment of a sentence
D. Translate a sentence to another language

Solution

  1. Step 1: Define Masked Language Model (MLM)

    MLM involves randomly masking some words in a sentence and training the model to predict those masked words.
  2. Step 2: Match definition to options

    Predict randomly masked words in a sentence correctly describes MLM as predicting masked words, while others describe different tasks.
  3. Final Answer:

    Predict randomly masked words in a sentence -> Option A
  4. Quick Check:

    MLM = predict masked words [OK]
Hint: MLM means guessing hidden words in sentences [OK]
Common Mistakes:
  • Confusing MLM with Next Sentence Prediction
  • Thinking MLM predicts entire sentences
  • Mixing MLM with classification tasks
3. Consider the following simplified code snippet for BERT pre-training MLM task:
sentence = ['The', 'cat', 'sat', 'on', 'the', 'mat']
masked_sentence = ['The', '[MASK]', 'sat', 'on', 'the', 'mat']
predicted_word = model.predict(masked_sentence)
print(predicted_word)
If the model works correctly, what should predicted_word be?
medium
A. 'cat'
B. 'mat'
C. 'dog'
D. 'sat'

Solution

  1. Step 1: Identify the masked word in the sentence

    The original sentence is ['The', 'cat', 'sat', 'on', 'the', 'mat'], and the masked sentence replaces 'cat' with '[MASK]'.
  2. Step 2: Predict the masked word

    The model should predict the missing word 'cat' to correctly fill the mask.
  3. Final Answer:

    'cat' -> Option A
  4. Quick Check:

    Masked word prediction = 'cat' [OK]
Hint: Masked word is replaced by [MASK], predict original word [OK]
Common Mistakes:
  • Choosing a word from the sentence but not the masked one
  • Confusing masked word with next sentence prediction
  • Assuming model predicts random words
4. In BERT pre-training, a common error is mixing up the Next Sentence Prediction (NSP) task. Which of the following statements is a mistake in NSP implementation?
medium
A. Feeding two sentences and predicting if the second follows the first
B. Randomly pairing sentences for negative examples
C. Using a binary classifier to decide sentence order
D. Predicting masked words inside a single sentence

Solution

  1. Step 1: Understand NSP task

    NSP involves feeding two sentences and predicting if the second sentence logically follows the first.
  2. Step 2: Identify incorrect statement

    Predicting masked words inside a single sentence describes predicting masked words, which is MLM, not NSP, so it is a mistake in NSP implementation.
  3. Final Answer:

    Predicting masked words inside a single sentence -> Option D
  4. Quick Check:

    NSP ≠ masked word prediction [OK]
Hint: NSP predicts sentence order, not masked words [OK]
Common Mistakes:
  • Confusing NSP with MLM
  • Not using sentence pairs for NSP
  • Skipping negative examples in NSP
5. You want to improve BERT's understanding of sentence relationships by modifying the Next Sentence Prediction (NSP) task. Which approach would best enhance NSP during pre-training?
hard
A. Increase the percentage of masked words in MLM to 50%
B. Replace NSP with a sentiment classification task
C. Add more negative sentence pairs that are unrelated
D. Train only on single sentences without pairs

Solution

  1. Step 1: Understand NSP goal

    NSP aims to teach the model to distinguish if one sentence follows another logically by using positive and negative sentence pairs.
  2. Step 2: Choose best enhancement

    Adding more negative sentence pairs (unrelated sentences) improves the model's ability to learn sentence relationships, enhancing NSP.
  3. Final Answer:

    Add more negative sentence pairs that are unrelated -> Option C
  4. Quick Check:

    More negative pairs = better NSP learning [OK]
Hint: More unrelated sentence pairs improve NSP task [OK]
Common Mistakes:
  • Confusing MLM changes with NSP improvements
  • Removing sentence pairs breaks NSP
  • Replacing NSP with unrelated tasks