Practice
1.
import numpy as np
from sklearn.metrics import confusion_matrix
y_true = [0, 1, 0, 1, 0, 1, 1]
y_pred = [0, 0, 0, 1, 1, 1, 1]
cm = confusion_matrix(y_true, y_pred)
print(cm)What does the code output?easy
Solution
Step 1: Understand confusion matrix structure
The confusion matrix is structured as [[TN, FP], [FN, TP]].Step 2: Calculate values
TN=2 (true negatives), FP=1 (false positives), FN=1 (false negatives), TP=3 (true positives).Final Answer:
Option A -> Option AQuick Check:
Confusion matrix is [[TN, FP], [FN, TP]]. [OK]
Hint: Confusion matrix is [[TN, FP], [FN, TP]]. [OK]
Common Mistakes:
2. What does the following Python code output when applied to an image array
img?
import numpy as np
def flip_image(img):
return np.flip(img, axis=1)
img = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
flipped_img = flip_image(img)
print(flipped_img)easy
Solution
Step 1: Understand the flip operation
Thenp.flipfunction flips the array along the specified axis. Here, axis=1 means flipping horizontally.Step 2: Apply the flip to the array
Flipping horizontally changes the order of elements in each row.Final Answer:
Option A -> Option AQuick Check:
Horizontal flip reverses row elements. [OK]
Hint: Horizontal flip reverses row elements. [OK]
Common Mistakes:
3. What is the time complexity of updating the learning rate using PyTorch's
StepLR scheduler for each epoch?easy
Solution
Step 1: Analyze StepLR update mechanism
StepLR updates the learning rate by multiplying it with a constant factor, which is a constant time operation.Step 2: Determine time complexity
Since the update is a simple multiplication, the time complexity is O(1).Final Answer:
Option C -> Option CQuick Check:
StepLR update is a constant time operation. [OK]
Hint: StepLR update is a constant time operation. [OK]
Common Mistakes:
4. What is the time complexity of the following non-max suppression implementation?
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 keepeasy
Solution
Step 1: Analyze the loop structure
The outer loop runs n times, and the inner loop (IoU calculation) also runs n times in the worst case.Step 2: Determine complexity
Each IoU calculation is O(1), making the overall complexity O(n^2).Final Answer:
Option B -> Option BQuick Check:
Nested loops suggest O(n^2) [OK]
Hint: Nested loops suggest O(n^2) [OK]
Common Mistakes:
5. What is the time complexity of a single update step in Mini-Batch SGD with Momentum for a model with
n parameters?easy
Solution
Step 1: Identify operations involved
Each update step involves computing the gradient and updating the velocity and parameters, all of which are linear operations with respect to the number of parameters.Step 2: Determine the complexity
Since each operation is linear, the overall complexity isO(n).Final Answer:
Option A -> Option AQuick Check:
Each parameter update is linear in complexity [OK]
Hint: Each parameter update is linear in complexity [OK]
Common Mistakes:
