For language models, the main metric is Perplexity. It measures how well the model predicts the next word. A lower perplexity means the model is better at guessing the next word in a sentence, just like how a good friend can finish your sentence correctly. Perplexity is important because it directly shows how confident and accurate the model is in understanding language patterns.
Language modeling concept in NLP - Model Metrics & Evaluation
Language modeling usually predicts many possible next words, so a confusion matrix is not typical. Instead, we look at Perplexity, which is calculated from the probabilities the model assigns to the correct next words.
Perplexity = 2^(- (1/N) * sum(log2 P(w_i | context)))
Where:
- N is the number of words
- P(w_i | context) is the predicted probability of the actual next word
Example:
If the model predicts the next word with 0.5 probability, perplexity contribution is 2^(-log2(0.5)) = 2^1 = 2
Lower perplexity means better prediction.
Precision and recall are less common for language modeling because it predicts probabilities over many words. But if we think about next word prediction as a classification task, there is a tradeoff:
- Precision: How often the predicted word is actually correct. High precision means the model rarely guesses wrong words.
- Recall: How many of the correct next words the model can predict. High recall means the model covers many possible correct words.
For example, in autocomplete on your phone, high precision avoids annoying wrong suggestions, while high recall helps suggest many useful words. Language models balance these by assigning probabilities to many words.
Good: Perplexity close to 10 or lower on a test set means the model predicts next words well. It shows the model understands language patterns clearly.
Bad: Perplexity above 100 means the model is very confused and guesses poorly. It might be just picking words randomly or not learning from data.
Remember, perplexity depends on dataset size and complexity, so compare models on the same data.
- Overfitting: Very low perplexity on training data but high on test data means the model memorizes instead of learning language rules.
- Data leakage: If test sentences appear in training, perplexity looks artificially low, hiding true performance.
- Ignoring context length: Short context can make perplexity look better but model may fail on longer sentences.
- Comparing across datasets: Perplexity values vary by dataset size and vocabulary, so only compare models on the same data.
Your language model has a perplexity of 50 on training data but 200 on test data. Is it good? Why or why not?
Answer: This is not good. The model performs well on training but poorly on test data, showing it memorized training sentences and cannot generalize to new text. It needs better training or regularization.