Practice
1. What does the following Python code snippet output when implementing sinusoidal positional encoding?
import numpy as np
def positional_encoding(position, d_model):
angle_rads = np.arange(position)[:, np.newaxis] / np.power(10000, (2 * (np.arange(d_model)[np.newaxis, :] // 2)) / np.float32(d_model))
angle_rads[:, 0::2] = np.sin(angle_rads[:, 0::2])
angle_rads[:, 1::2] = np.cos(angle_rads[:, 1::2])
return angle_rads
pe = positional_encoding(4, 4)
print(pe)easy
Solution
Step 1: Understand the code logic
The code generates a matrix where each row corresponds to a position and each column to a dimension, filled with sinusoidal values.Step 2: Eliminate wrong options
A 4x4 matrix with all zeros is incorrect because the matrix is not filled with zeros. A 4x4 identity matrix is incorrect because the matrix is not an identity matrix. A 4x4 matrix with random values is incorrect because the values are not random but deterministic sinusoidal values.Final Answer:
Option A -> Option AQuick Check:
Sinusoidal encoding alternates sine/cosine. [OK]
Hint: Sinusoidal encoding alternates sine/cosine. [OK]
Common Mistakes:
2. What is the time complexity of calculating precision, recall, and F1-score from a confusion matrix?
easy
Solution
Step 1: Analyze the operations
Calculating precision, recall, and F1-score involves basic arithmetic operations on a fixed-size matrix.Step 2: Determine complexity
These operations are constant time, O(1), as they do not depend on the size of the input data.Final Answer:
Option A -> Option AQuick Check:
Fixed-size matrix operations are O(1). [OK]
Hint: Fixed-size matrix operations are O(1). [OK]
Common Mistakes:
3.
import numpy as np
def compute_confusion_matrix(y_true, y_pred):
cm = np.zeros((2, 2), dtype=int)
for t, p in zip(y_true, y_pred):
cm[t][p] += 1
return cm
# Test case
y_true = [0, 1, 0, 1, 0, 1, 1]
y_pred = [0, 0, 0, 1, 1, 1, 1]
cm = compute_confusion_matrix(y_true, y_pred)
print(cm)Why might this implementation produce incorrect results?medium
Solution
Step 1: Analyze indexing logic
The code uses cm[t][p] which is incorrect for confusion matrix indexing.Step 2: Identify correct logic
Correct indexing should be cm[p][t] for predictions vs actuals.Final Answer:
Option A -> Option AQuick Check:
Confusion matrix should index [pred][true]. [OK]
Hint: Confusion matrix should index [pred][true]. [OK]
Common Mistakes:
4. 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:
5. 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:
