Practice
1.
import numpy as np
def cosine_similarity(a, b):
dot_product = np.dot(a, b)
norm_a = np.linalg.norm(a)
norm_b = np.linalg.norm(b)
return dot_product / (norm_a * norm_b)
vector1 = np.array([1, 2, 3])
vector2 = np.array([4, 5, 6])
result = cosine_similarity(vector1, vector2)
print(result)easy
Solution
Step 1: Calculate dot product and norms
Dot product = 32, norm_a = 3.7416573867739413, norm_b = 8.774964387392123.Step 2: Compute cosine similarity
Cosine similarity = 32 / (3.7416573867739413 * 8.774964387392123) = 0.9746318461970762.Final Answer:
Option A -> Option AQuick Check:
Dot product over product of norms. [OK]
Hint: Dot product over product of norms. [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. What is the time complexity of computing the softmax function for an input array of size n?
easy
Solution
Step 1: Analyze operations
Softmax involves computing exponentials and a sum, both linear in n.Step 2: Determine complexity
Each operation is O(n), so overall complexity is O(n).Final Answer:
Option D -> Option DQuick Check:
Softmax is linear in input size [OK]
Hint: Softmax is linear in input size [OK]
Common Mistakes:
4. You implemented a normalization function for image data, but the model's performance dropped. What is the most likely cause?
medium
Solution
Step 1: Identify normalization issues
Incorrect mean and std values can skew the data distribution.Step 2: Eliminate other options
Normalization was applied after data augmentation. is valid; Normalization was applied only to the training set. would cause a mismatch; Normalization was applied using the global mean and std. is standard practice.Final Answer:
Option C -> Option CQuick Check:
Check mean/std values for normalization. [OK]
Hint: Check mean/std values for normalization. [OK]
Common Mistakes:
5. A candidate implemented the cross-entropy loss function but it returns negative values. What is the most likely cause?
medium
Solution
Step 1: Identify negative values cause
Cross-entropy loss should be non-negative; negative values suggest log issues.Step 2: Check log application
Log applied to values outside (0,1) can cause negatives.Final Answer:
Option B -> Option BQuick Check:
Log of non-probabilities causes negatives [OK]
Hint: Log of non-probabilities causes negatives [OK]
Common Mistakes:
