Practice
1.
import torch.optim as optim
model = ... # some PyTorch model
optimizer = optim.SGD(model.parameters(), lr=0.1)
scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=30, gamma=0.1)
for epoch in range(100):
# training loop
scheduler.step()
print(optimizer.param_groups[0]['lr'])What will be the learning rate after 100 epochs?easy
Solution
Step 1: Understand StepLR behavior
The learning rate is multiplied by gamma (0.1) every step_size (30 epochs).Step 2: Calculate the learning rate after 100 epochs
After 100 epochs, the learning rate is updated 3 times (at epochs 30, 60, and 90): 0.1 * 0.1 * 0.1 * 0.1 = 0.001.Final Answer:
Option B -> Option BQuick Check:
StepLR reduces LR by gamma every step_size epochs. [OK]
Hint: StepLR reduces LR by gamma every step_size epochs. [OK]
Common Mistakes:
2. What is the time complexity of computing the scaled dot-product attention for a single query with n keys?
easy
Solution
Step 1: Identify operations
For a single query, the main operations are dot product and softmax, both O(n).Step 2: Eliminate incorrect complexities
O(n^2) and O(n log n) are incorrect as they imply more complex operations than present.Final Answer:
Option C -> Option CQuick Check:
Single query, n keys -> O(n) [OK]
Hint: Single query, n keys -> O(n) [OK]
Common Mistakes:
3. What does the following Python code output?
import numpy as np
def softmax(x):
e_x = np.exp(x - np.max(x))
return e_x / e_x.sum(axis=0)
x = np.array([2.0, 1.0, 0.1])
print(softmax(x))easy
Solution
Step 1: Calculate softmax
Compute the exponentials of the input array after subtracting the max value for numerical stability.Step 2: Normalize
Divide each exponential by the sum of all exponentials to get probabilities.Final Answer:
Option A -> Option AQuick Check:
Softmax outputs sum to 1 [OK]
Hint: Softmax outputs sum to 1 [OK]
Common Mistakes:
4. In a 2D convolution implementation, how would you handle an input image with a single channel differently from a multi-channel image?
medium
Solution
Step 1: Understand multi-channel convolution
For multi-channel images, the same kernel is applied to each channel separately, and the results are summed to produce the final output.Step 2: Eliminate wrong options
Use a different kernel size for each channel. is incorrect as kernel size remains constant. Sum the channels before applying the kernel. loses channel information. Concatenate the channels before applying the kernel. is not a standard approach.Final Answer:
Option A -> Option AQuick Check:
Apply kernel to each channel, then sum. [OK]
Hint: Apply kernel to each channel, then sum. [OK]
Common Mistakes:
5. How can you optimize the following non-max suppression code for large datasets?
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 keephard
Solution
Step 1: Identify bottleneck
IoU calculations are the most computationally expensive part.Step 2: Optimize bottleneck
Parallelizing IoU calculations can significantly reduce runtime for large datasets.Final Answer:
Option A -> Option AQuick Check:
Parallelize expensive operations [OK]
Hint: Parallelize expensive operations [OK]
Common Mistakes:
