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 binary cross-entropy loss for a logistic regression model with
n samples and m features?easy
Solution
Step 1: Identify operations involved
Binary cross-entropy involves computing predictions for each sample, which requires a dot product of weights and features.Step 2: Calculate complexity
Each dot product is O(m), and there are n samples, leading to O(nm) complexity.Final Answer:
Option D -> Option DQuick Check:
Dot product per sample, n samples -> O(nm) [OK]
Hint: Dot product per sample, n samples -> O(nm) [OK]
Common Mistakes:
3.
import numpy as np
from sklearn.model_selection import train_test_split
X = np.array([1, 2, 3, 4, 5, 6])
y = np.array([0, 0, 1, 1, 0, 1])
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, stratify=y)
print(y_train)easy
Solution
Step 1: Understand stratification
Stratification ensures that the class distribution in the train and test sets is similar to the original dataset.Step 2: Calculate class distribution
Original y has 3 zeros and 3 ones. With stratification, y_train should have 1 zero and 2 ones.Final Answer:
Option D -> Option DQuick Check:
Stratify maintains class distribution. [OK]
Hint: Stratify maintains class distribution. [OK]
Common Mistakes:
4. How can you optimize the computation of a confusion matrix for a large dataset with millions of predictions?
hard
Solution
Step 1: Identify bottleneck
Large datasets require efficient computation, often limited by CPU processing.Step 2: Choose optimization strategy
Parallel processing with Dask can distribute computation across cores.Final Answer:
Option B -> Option BQuick Check:
Parallel processing speeds up large data tasks. [OK]
Hint: Parallel processing speeds up large data tasks. [OK]
Common Mistakes:
5. How can you optimize the following data augmentation pipeline for memory efficiency?
def augment_image(img):
img = np.flip(img, axis=1)
img = img[10:110, 10:110]
img = (img - np.mean(img)) / np.std(img)
return imghard
Solution
Step 1: Identify memory inefficiencies
Intermediate assignments create unnecessary copies.Step 2: Evaluate optimization strategies
Chaining operations reduces memory usage by avoiding intermediate arrays.Final Answer:
Option A -> Option AQuick Check:
Chain operations to reduce memory usage. [OK]
Hint: Chain operations to reduce memory usage. [OK]
Common Mistakes:
