Practice
1.
import torch
import torch.nn.functional as F
query = torch.tensor([[1.0, 0.0]])
key = torch.tensor([[0.5, 0.5], [1.0, 0.0]])
value = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
attention_scores = torch.matmul(query, key.T)
attention_weights = F.softmax(attention_scores, dim=-1)
output = torch.matmul(attention_weights, value)
print(output)What does this code output?easy
Solution
Step 1: Calculate attention scores.
Query-key dot product gives scores: [[0.5, 1.0]].Step 2: Apply softmax to scores.
Softmax gives weights: [[0.2689, 0.7311]].Step 3: Compute weighted sum of values.
Output = [[0.2689*1.0 + 0.7311*3.0, 0.2689*2.0 + 0.7311*4.0]] = [[1.5, 3.0]].Final Answer:
Option A -> Option AQuick Check:
Softmax weights applied to values. [OK]
Hint: Softmax weights applied to values. [OK]
Common Mistakes:
2.
import numpy as np
from sklearn.neighbors import NearestNeighbors
X = np.array([[1, 2], [3, 4], [5, 6]])
knn = NearestNeighbors(n_neighbors=2)
knn.fit(X)
distances, indices = knn.kneighbors([[1, 2]])
print(indices)What does this code output?easy
Solution
Step 1: Understand kneighbors output
The code finds the 2 nearest neighbors of [1, 2] in X.Step 2: Calculate distances
[1, 2] is closest to itself (index 0) and then to [3, 4] (index 1).Final Answer:
Option C -> Option CQuick Check:
Self is always nearest. [OK]
Hint: Self is always nearest. [OK]
Common Mistakes:
3. In the context of why machine learning exists, which of the following problems is most suitable for a machine learning approach rather than a rule-based system?
easy
Solution
Step 1: Identify the nature of the problem.
Predicting the next word in a sentence involves understanding context and patterns, which are complex and not easily defined by rules.Step 2: Eliminate rule-based problems.
Options A, C, and D are deterministic and can be solved with explicit algorithms.Final Answer:
Option A -> Option AQuick Check:
ML for patterns, rules for logic. [OK]
Hint: ML excels at pattern recognition, not deterministic logic. [OK]
Common Mistakes:
4. For a large streaming dataset where you need to maintain a representative sample, which sampling strategy is most appropriate and why?
medium
Solution
Step 1: Identify the need for streaming data handling.
Streaming data requires a strategy that can update samples dynamically.Step 2: Eliminate non-streaming strategies.
Random (A) and Stratified (B) don't handle streaming efficiently. Weighted (D) is for prioritization, not streaming.Final Answer:
Option B -> Option BQuick Check:
Reservoir = Streaming data handling [[check]]
Hint: Reservoir = Streaming data handling [[check]]
Common Mistakes:
5. You are tasked with improving a model's performance on a highly imbalanced dataset. Which approach is most appropriate to prevent overfitting while addressing class imbalance?
hard
Solution
Step 1: Address class imbalance and overfitting
Class weights help balance the dataset, and early stopping prevents overfitting by halting training when validation performance stops improving.Step 2: Eliminate wrong options
Use SMOTE to oversample the minority class and increase model complexity. increases complexity, risking overfitting, Downsample the majority class and increase the learning rate. may lead to instability, and Use a simpler model with no regularization. lacks regularization, increasing overfitting risk.Final Answer:
Option C -> Option CQuick Check:
Class weights + early stopping = balanced, robust model [OK]
Hint: Class weights + early stopping = balanced, robust model [OK]
Common Mistakes:
