Practice
1. What does the following Python code snippet output when implementing the K-Means clustering algorithm from scratch?
import numpy as np
# Sample data
X = np.array([[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]])
# Initial centroids
centroids = np.array([[1, 2], [10, 2]])
# Calculate distances
for i in range(len(X)):
distances = np.linalg.norm(X[i] - centroids, axis=1)
print(distances)
easy
Solution
Step 1: Understand the code logic
The code calculates the Euclidean distance from each data point to both centroids.Step 2: Eliminate wrong options
Prints the distances between each point and the first centroid only. and C are incorrect because the code calculates distances to both centroids, not just one. Prints the sum of distances for each point to both centroids. is incorrect because it doesn't sum the distances.Final Answer:
Option B -> Option BQuick Check:
Distances to all centroids, not just one. [OK]
Hint: Distances to all centroids, not just one. [OK]
Common Mistakes:
2. In a Random Forest implementation, how would you apply feature sampling to handle high-dimensional data effectively?
medium
Solution
Step 1: Understand Feature Sampling
Feature sampling involves selecting a random subset of features for each tree to reduce dimensionality.Step 2: Evaluate Options
Using all features (A) or a fixed subset (C) doesn't reduce dimensionality; correlation-based selection (D) isn't random.Final Answer:
Option C -> Option CQuick Check:
Random subsets reduce dimensionality [OK]
Hint: Random subsets reduce dimensionality [OK]
Common Mistakes:
3. In the following implementation of scaled dot-product attention, the output is incorrect. What is the likely cause?
import torch
import torch.nn.functional as F
queries = torch.randn(2, 3)
keys = torch.randn(2, 3)
values = torch.randn(2, 3)
scores = torch.matmul(queries, keys.T) / torch.sqrt(torch.tensor(3.0))
attention_weights = F.softmax(scores, dim=0)
output = torch.matmul(attention_weights, values)medium
Solution
Step 1: Identify the issue
Softmax should be applied on the last dimension, not the first.Step 2: Eliminate other causes
Scaling factor and tensor shapes are correct; normalization is handled by softmax.Final Answer:
Option B -> Option BQuick Check:
Softmax on last dim for attention [OK]
Hint: Softmax on last dim for attention [OK]
Common Mistakes:
4. You implemented a Skip-Gram model with negative sampling, but the loss is not decreasing. What is the most likely cause?
medium
Solution
Step 1: Analyze the symptoms
A non-decreasing loss often indicates issues with learning rate or gradient updates.Step 2: Evaluate each option
High learning rates can cause oscillations, preventing loss reduction. Random initialization and sample randomness are less likely to cause this specific issue.Final Answer:
Option D -> Option DQuick Check:
High learning rate can cause loss oscillation [OK]
Hint: High learning rate can cause loss oscillation [OK]
Common Mistakes:
5. How can you optimize the backward pass of batch normalization to improve computational efficiency?
hard
Solution
Step 1: Identify optimization techniques
Fusing operations can reduce computational overhead.Step 2: Evaluate each option
Fusing batch normalization with linear layers reduces redundant computations.Final Answer:
Option A -> Option AQuick Check:
Fuse layers to optimize batch norm [OK]
Hint: Fuse layers to optimize batch norm [OK]
Common Mistakes:
