Practice
1.
import numpy as np
def dcg_at_k(r, k):
r = np.asfarray(r)[:k]
if r.size:
return np.sum(r / np.log2(np.arange(2, r.size + 2)))
return 0.0
relevance_scores = [3, 2, 3, 0, 1, 2]
k = 3
print(dcg_at_k(relevance_scores, k))What does this code output?easy
Solution
Step 1: Understand DCG Calculation
The DCG is calculated as the sum of relevance scores divided by log base 2 of their positions.Step 2: Calculate for k=3
For k=3, the scores are [3, 2, 3]. The calculation is 3/1 + 2/log2(3) + 3/log2(4).Final Answer:
Option A -> Option AQuick Check:
DCG uses log2 positions. [OK]
Hint: DCG uses log2 positions. [OK]
Common Mistakes:
2. What does the following Python code output?
import numpy as np
def softmax(x):
e_x = np.exp(x - np.max(x))
return e_x / e_x.sum(axis=0)
x = np.array([2.0, 1.0, 0.1])
print(softmax(x))easy
Solution
Step 1: Calculate softmax
Compute the exponentials of the input array after subtracting the max value for numerical stability.Step 2: Normalize
Divide each exponential by the sum of all exponentials to get probabilities.Final Answer:
Option A -> Option AQuick Check:
Softmax outputs sum to 1 [OK]
Hint: Softmax outputs sum to 1 [OK]
Common Mistakes:
3. You implemented stratified K-Fold cross-validation but notice class imbalance in some folds. What is the most likely cause?
medium
Solution
Step 1: Identify stratification issue
Stratification ensures class distribution is preserved in each fold.Step 2: Eliminate wrong options
Incorrect number of splits and D relate to fold count, not class distribution. Data shuffling before splitting affects randomness, not stratification.Final Answer:
Option A -> Option AQuick Check:
Check stratification parameter for class balance. [OK]
Hint: Check stratification parameter for class balance. [OK]
Common Mistakes:
4. You implemented early stopping in your training loop, but the model still overfits. What is the most likely reason?
medium
Solution
Step 1: Analyze early stopping behavior
Early stopping relies on validation performance to halt training.Step 2: Identify the root cause of overfitting
If the validation set is too small, it may not accurately reflect generalization, leading to overfitting despite early stopping.Final Answer:
Option A -> Option AQuick Check:
Small validation set can mislead early stopping. [OK]
Hint: Small validation set can mislead early stopping. [OK]
Common Mistakes:
5. In the context of implementing Skip-Gram with negative sampling, which approach is best for handling rare words?
medium
Solution
Step 1: Understand the problem with rare words
Rare words can be overshadowed by frequent words, leading to poor embeddings.Step 2: Evaluate solutions
Subsampling frequent words reduces their dominance, allowing rare words to have more influence during training.Final Answer:
Option A -> Option AQuick Check:
Subsampling frequent words helps rare words [OK]
Hint: Subsampling frequent words helps rare words [OK]
Common Mistakes:
