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 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:
3. In a KNN classifier using KD-Tree, which scenario would make a brute force approach more suitable?
medium
Solution
Step 1: Understand KD-Tree limitations
KD-Trees are inefficient in high-dimensional spaces due to the curse of dimensionality.Step 2: Evaluate scenarios
A: Large datasets benefit from KD-Trees. C: Small datasets are manageable with brute force. D: Categorical features require different handling.Final Answer:
Option A -> Option AQuick Check:
High dimensions favor brute force over KD-Tree [OK]
Hint: High dimensions favor brute force over KD-Tree [OK]
Common Mistakes:
4. How would you apply sinusoidal positional encoding in a transformer model for a sequence of variable length?
medium
Solution
Step 1: Consider variable sequence lengths
Each sequence should have positional encoding applied based on its actual length to maintain positional information.Step 2: Eliminate wrong options
Pad all sequences to the maximum length and apply encoding. is inefficient and may introduce unnecessary padding. Apply encoding only to the first few positions of each sequence. ignores the rest of the sequence. Use a fixed-length encoding for all sequences, regardless of length. loses positional information for longer sequences.Final Answer:
Option B -> Option BQuick Check:
Encode based on actual sequence length. [OK]
Hint: Encode based on actual sequence length. [OK]
Common Mistakes:
5. How can you optimize the computation of cosine similarity for a large dataset of vectors?
hard
Solution
Step 1: Identify computational bottlenecks
Looping over pairs is inefficient for large datasets.Step 2: Optimize using matrix operations
Precomputing norms and using matrix multiplication reduces redundant calculations.Final Answer:
Option C -> Option CQuick Check:
Matrix ops reduce redundancy. [OK]
Hint: Matrix ops reduce redundancy. [OK]
Common Mistakes:
