Practice
n samples and m features?Solution
Step 1: Identify operations involved
Binary cross-entropy involves computing predictions for each sample, which requires a dot product of weights and features.Step 2: Calculate complexity
Each dot product is O(m), and there are n samples, leading to O(nm) complexity.Final Answer:
Option D -> Option DQuick Check:
Dot product per sample, n samples -> O(nm) [OK]
position x d_model?Solution
Step 1: Analyze the operations
The code iterates over each position and each dimension, performing constant-time operations.Step 2: Eliminate wrong options
O(position^2) is incorrect because the complexity is not quadratic in position. O(d_model^2) is incorrect because the complexity is not quadratic in d_model. O(1) is incorrect because the complexity is not constant.Final Answer:
Option C -> Option CQuick Check:
Iterate over position and dimension. [OK]
import numpy as np
def compute_confusion_matrix(y_true, y_pred):
cm = np.zeros((2, 2), dtype=int)
for t, p in zip(y_true, y_pred):
cm[t][p] += 1
return cm
# Test case
y_true = [0, 1, 0, 1, 0, 1, 1]
y_pred = [0, 0, 0, 1, 1, 1, 1]
cm = compute_confusion_matrix(y_true, y_pred)
print(cm)Why might this implementation produce incorrect results?Solution
Step 1: Analyze indexing logic
The code uses cm[t][p] which is incorrect for confusion matrix indexing.Step 2: Identify correct logic
Correct indexing should be cm[p][t] for predictions vs actuals.Final Answer:
Option A -> Option AQuick Check:
Confusion matrix should index [pred][true]. [OK]
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]
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)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]
