Practice
1.
import numpy as np
def numerical_gradient(f, x):
h = 1e-4
grad = np.zeros_like(x)
for i in range(x.size):
x[i] += h
fxh1 = f(x)
x[i] -= 2 * h
fxh2 = f(x)
grad[i] = (fxh1 - fxh2) / (2 * h)
x[i] += h
return grad
# Test function
f = lambda x: x[0]**2 + x[1]**2
x = np.array([3.0, 4.0])
print(numerical_gradient(f, x))easy
Solution
Step 1: Understand the numerical gradient calculation
The function calculates the gradient using central difference approximation.Step 2: Calculate the gradient for each element
For x = [3.0, 4.0], the gradient is [6.0, 8.0] because the derivative of x^2 is 2x.Final Answer:
Option A -> Option AQuick Check:
Central difference gives exact gradient for quadratic. [OK]
Hint: Central difference gives exact gradient for quadratic. [OK]
Common Mistakes:
2.
import numpy as np
class DropoutLayer:
def __init__(self, dropout_rate):
self.dropout_rate = dropout_rate
def forward(self, inputs, training=True):
if training:
self.mask = np.random.binomial(1, 1 - self.dropout_rate, size=inputs.shape)
return inputs * self.mask
else:
return inputs * (1 - self.dropout_rate)
inputs = np.array([1.0, 2.0, 3.0])
dropout_layer = DropoutLayer(0.5)
output = dropout_layer.forward(inputs, training=True)
print(output)Why might this implementation produce unexpected results during training?medium
Solution
Step 1: Identify Training Mode Requirements
During training, the mask must be saved for backpropagation to correctly compute gradients.Step 2: Eliminate Wrong Options
The dropout rate is too high. is about hyperparameter tuning, not implementation. The mask is not applied correctly. is incorrect as the mask is applied correctly. The inputs are not scaled during training. is irrelevant during training.Final Answer:
Option C -> Option CQuick Check:
Mask must be saved for backpropagation. [OK]
Hint: Mask must be saved for backpropagation. [OK]
Common Mistakes:
3. In the context of logistic regression, which approach is correct for handling a dataset with highly imbalanced classes?
medium
Solution
Step 1: Identify the problem with imbalanced classes
Imbalanced classes can lead to biased predictions towards the majority class.Step 2: Choose a method to address imbalance
SMOTE (Synthetic Minority Over-sampling Technique) is effective for balancing datasets.Final Answer:
Option B -> Option BQuick Check:
SMOTE balances classes effectively [OK]
Hint: SMOTE balances classes effectively [OK]
Common Mistakes:
4. You implemented a train-test split with stratification, but the test set has a different class distribution than expected. What is the most likely cause?
medium
Solution
Step 1: Identify the role of stratification
Stratification ensures class distribution is maintained across splits.Step 2: Diagnose the issue
If the stratify parameter is not set correctly, class distribution will not be maintained.Final Answer:
Option B -> Option BQuick Check:
Check stratify parameter for distribution issues. [OK]
Hint: Check stratify parameter for distribution issues. [OK]
Common Mistakes:
5. How can you optimize the memory usage of a learned positional encoding implementation in PyTorch?
hard
Solution
Step 1: Consider memory optimization techniques
Using half-precision floating point reduces memory usage while maintaining sufficient precision for many applications.Step 2: Eliminate wrong options
Reduce the number of dimensions in the encoding. changes the model architecture. Use a sparse matrix for the encoding. is not applicable as the matrix is dense. Share the encoding matrix across multiple layers. may affect model performance and is not a direct memory optimization.Final Answer:
Option D -> Option DQuick Check:
Half-precision reduces memory without major precision loss. [OK]
Hint: Half-precision reduces memory without major precision loss. [OK]
Common Mistakes:
