0
0
NLPml~20 mins

N-gram language models in NLP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
N-gram Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding the purpose of N-gram models
What is the main purpose of using an N-gram language model in natural language processing?
ATo generate images from text descriptions using neural networks
BTo translate text from one language to another using deep learning
CTo cluster documents based on their topics using unsupervised learning
DTo predict the next word in a sequence based on the previous N-1 words
Attempts:
2 left
💡 Hint
Think about what N-gram models estimate about word sequences.
Predict Output
intermediate
2:00remaining
Output of a bigram probability calculation
Given the sentence: 'I love machine learning', and the bigram counts: {('I', 'love'): 3, ('love', 'machine'): 2, ('machine', 'learning'): 4}, what is the bigram probability P('machine'|'love') using maximum likelihood estimation?
NLP
bigram_counts = {('I', 'love'): 3, ('love', 'machine'): 2, ('machine', 'learning'): 4}
unigram_counts = {'I': 3, 'love': 4, 'machine': 4}
prob = bigram_counts[('love', 'machine')] / unigram_counts['love']
print(prob)
A0.5
B0.33
C0.67
D1.0
Attempts:
2 left
💡 Hint
Divide the count of the bigram by the count of the first word in the bigram.
Hyperparameter
advanced
2:00remaining
Choosing the value of N in N-gram models
Which of the following is a common trade-off when increasing the value of N in an N-gram language model?
AHigher N always improves model accuracy without any drawbacks
BHigher N leads to better context capture but requires more data and increases sparsity
CHigher N reduces model size and speeds up training
DHigher N eliminates the need for smoothing techniques
Attempts:
2 left
💡 Hint
Think about how longer sequences affect data requirements and sparsity.
Metrics
advanced
2:00remaining
Evaluating N-gram language models with perplexity
If an N-gram language model has a perplexity of 50 on a test set, what does this indicate about the model's performance?
AThe model is uncertain and predicts the test data poorly
BThe model has zero error on the test data
CThe model is very confident and predicts the test data well
DThe model's predictions are random and unrelated to the test data
Attempts:
2 left
💡 Hint
Lower perplexity means better prediction; higher means worse.
🔧 Debug
expert
2:00remaining
Identifying the error in smoothing implementation
Consider this code snippet for add-one smoothing in a bigram model: ```python bigram_counts = {('the', 'cat'): 3, ('cat', 'sat'): 2} unigram_counts = {'the': 5, 'cat': 4} vocab_size = 10 word1 = 'cat' word2 = 'sat' prob = (bigram_counts.get((word1, word2), 0) + 1) / (unigram_counts[word1] + vocab_size) print(prob) ``` What error will this code raise when calculating the probability for the bigram ('sat', 'on')?
AZeroDivisionError because unigram_counts[word1] is zero
BTypeError because bigram_counts.get returns None
CKeyError because 'sat' is not in unigram_counts
DNo error, code runs correctly
Attempts:
2 left
💡 Hint
Check if all words used as keys exist in unigram_counts.