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. In a pipeline that includes flipping, cropping, and normalizing images, where should normalization be applied for best results?
medium
Solution
Step 1: Understand the impact of operations
Normalization should be the last step to ensure consistent scaling.Step 2: Evaluate each option
Applying normalization last ensures it accounts for all transformations.Final Answer:
Option D -> Option DQuick Check:
Normalize last for consistent scaling. [OK]
Hint: Normalize last for consistent scaling. [OK]
Common Mistakes:
3.
import numpy as np
# Backpropagation for a single neuron
inputs = np.array([1.0, 2.0, 3.0])
weights = np.array([0.2, 0.8, -0.5])
bias = 2.0
learning_rate = 0.01
d_output = np.array([1.0]) # Gradient of loss w.r.t output
# Incorrect gradient calculation
weights_gradient = d_output * inputs
weights -= learning_rate * weights_gradient
print(weights)medium
Solution
Step 1: Identify error in gradient calculation
The code incorrectly applies the gradient directly without considering the chain rule.Step 2: Correct approach
Should multiply by the derivative of the activation function.Final Answer:
Option D -> Option DQuick Check:
Always apply chain rule in backpropagation [OK]
Hint: Always apply chain rule in backpropagation [OK]
Common Mistakes:
4. In the following non-max suppression code, the function sometimes returns an empty list even when there are valid boxes. What is the likely cause?
def non_max_suppression(boxes, scores, iou_threshold):
indices = np.argsort(scores)[::-1]
keep = []
while len(indices) > 0:
current = indices[0]
keep.append(current)
indices = indices[1:]
indices = [i for i in indices if iou(boxes[current], boxes[i]) < iou_threshold]
return keepmedium
Solution
Step 1: Analyze the condition for keeping boxes
If the IoU threshold is too high, no boxes will be kept.Step 2: Eliminate other causes
Sorting and empty boxes would cause different errors; incorrect IoU would not consistently return empty.Final Answer:
Option D -> Option DQuick Check:
High IoU threshold filters all boxes [OK]
Hint: High IoU threshold filters all boxes [OK]
Common Mistakes:
5.
import numpy as np
def ndcg_at_k(r, k):
dcg_max = dcg_at_k(sorted(r, reverse=True), k)
if not dcg_max:
return 0.
return dcg_at_k(r, k) / dcg_max
relevance_scores = [3, 2, 3, 0, 1, 2]
k = 3
print(ndcg_at_k(relevance_scores, k))The output is unexpectedly low. What is the most likely cause?medium
Solution
Step 1: Check Ideal DCG Calculation
The ideal DCG should be calculated using the sorted relevance scores.Step 2: Identify the Error
If the ideal DCG is not calculated correctly, the NDCG will be incorrect.Final Answer:
Option D -> Option DQuick Check:
Check ideal DCG calculation. [OK]
Hint: Check ideal DCG calculation. [OK]
Common Mistakes:
