Practice
1.
import numpy as np
def entropy(y):
unique, counts = np.unique(y, return_counts=True)
probabilities = counts / counts.sum()
return -np.sum(probabilities * np.log2(probabilities))
labels = np.array([1, 1, 0, 0, 1])
print(entropy(labels))What does this code output?easy
Solution
Step 1: Calculate probabilities
For labels [1, 1, 0, 0, 1], probabilities are [0.4, 0.6].Step 2: Calculate entropy
Entropy = -[0.4*log2(0.4) + 0.6*log2(0.6)] = 0.9709505944546686.Final Answer:
Option B -> Option BQuick Check:
Entropy formula: -Σ(p*log2(p)) [OK]
Hint: Entropy formula: -Σ(p*log2(p)) [OK]
Common Mistakes:
2. What is the time complexity of computing the numerical gradient for a function with n parameters using central difference?
easy
Solution
Step 1: Analyze the loop structure
The numerical gradient computation involves a loop over n parameters, each requiring constant time operations.Step 2: Determine the complexity
Each parameter requires a constant number of operations, leading to O(n) complexity.Final Answer:
Option D -> Option DQuick Check:
Loop over n parameters, each constant time. [OK]
Hint: Loop over n parameters, each constant time. [OK]
Common Mistakes:
3. What is the time complexity of training a Random Forest with bagging and feature sampling, assuming each tree is trained on a subset of m features from n samples?
easy
Solution
Step 1: Analyze Tree Training Complexity
Each decision tree in a Random Forest is trained on a subset of features and samples.Step 2: Determine Complexity
Training a decision tree typically involves sorting, leading to O(n * m * log(n)) complexity.Final Answer:
Option D -> Option DQuick Check:
Tree training involves sorting [OK]
Hint: Tree training involves sorting [OK]
Common Mistakes:
4. In the context of implementing backpropagation using NumPy, which of the following is a correct approach to handle vanishing gradients?
medium
Solution
Step 1: Identify vanishing gradient issue
Vanishing gradients occur when gradients become too small to update weights effectively.Step 2: Correct approach
Gradient clipping prevents gradients from becoming too small or too large.Final Answer:
Option C -> Option CQuick Check:
Clip gradients to prevent vanishing [OK]
Hint: Clip gradients to prevent vanishing [OK]
Common Mistakes:
5. How can you optimize the dropout layer implementation to reduce computational overhead during training?
hard
Solution
Step 1: Identify Optimization Opportunities
In-place operations reduce memory usage and computational overhead by modifying data directly.Step 2: Eliminate Wrong Options
Use a fixed mask for all training iterations. reduces randomness, harming model performance. Precompute masks for all layers before training. increases memory usage. Reduce the dropout rate to minimize mask application. changes model behavior, not efficiency.Final Answer:
Option A -> Option AQuick Check:
In-place operations save memory and time. [OK]
Hint: In-place operations save memory and time. [OK]
Common Mistakes:
