Practice
1. What does the following Python code snippet output when implementing linear regression using gradient descent?
import numpy as np
X = np.array([[1, 2], [3, 4], [5, 6]])
y = np.array([1, 2, 3])
weights = np.zeros(X.shape[1])
learning_rate = 0.01
for _ in range(1):
predictions = X.dot(weights)
errors = predictions - y
gradient = X.T.dot(errors) / len(y)
weights -= learning_rate * gradient
print(weights)easy
Solution
Step 1: Calculate initial predictions and errors
Initial predictions are [0, 0, 0] since weights are zeros. Errors are [-1, -2, -3].Step 2: Compute gradient and update weights
Gradient is [-3, -6]. Updated weights are [0.01, 0.02].Final Answer:
Option A -> Option AQuick Check:
Initial weights zero, single update step. [OK]
Hint: Initial weights zero, single update step. [OK]
Common Mistakes:
2.
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:
3. Your mAP implementation is returning unusually high values. What is the most likely cause?
medium
Solution
Step 1: Identify the Symptom
High mAP values suggest too many matches are considered correct.Step 2: Analyze Each Option
Incorrect IoU threshold for positive matches: A low IoU threshold can cause false positives to be counted as true positives, inflating mAP. Using precision instead of recall in calculations: Would affect recall, not mAP directly. Not normalizing the precision values: Would affect the scale, not the value. Including false negatives in the precision calculation: Would lower precision, not increase it.Final Answer:
Option B -> Option BQuick Check:
Check IoU threshold for false positives [OK]
Hint: Check IoU threshold for false positives [OK]
Common Mistakes:
4. In a multi-class classification problem, how would you modify the softmax function to handle very large input values?
medium
Solution
Step 1: Address numerical stability
Subtracting the max input value prevents overflow in exponentials.Step 2: Ensure correct probabilities
This adjustment maintains the relative differences between inputs.Final Answer:
Option A -> Option AQuick Check:
Subtract max for stability in softmax [OK]
Hint: Subtract max for stability in softmax [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:
