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 calculating the entropy for a dataset with n samples?
easy
Solution
Step 1: Analyze entropy calculation
Entropy calculation involves counting occurrences and computing probabilities, both O(n).Step 2: Eliminate wrong options
O(n^2) and O(n log n) are incorrect as they overestimate the operations needed.Final Answer:
Option D -> Option DQuick Check:
Entropy calculation is linear in n [OK]
Hint: Entropy calculation is linear in n [OK]
Common Mistakes:
3.
import numpy as np
def ndcg_at_k(r, k):
dcg_max = dcg_at_k(sorted(r, reverse=True), k)
if not dcg_max:
return 0.
return dcg_at_k(r, k) / dcg_max
relevance_scores = [3, 2, 3, 0, 1, 2]
k = 3
print(ndcg_at_k(relevance_scores, k))The output is unexpectedly low. What is the most likely cause?medium
Solution
Step 1: Check Ideal DCG Calculation
The ideal DCG should be calculated using the sorted relevance scores.Step 2: Identify the Error
If the ideal DCG is not calculated correctly, the NDCG will be incorrect.Final Answer:
Option D -> Option DQuick Check:
Check ideal DCG calculation. [OK]
Hint: Check ideal DCG calculation. [OK]
Common Mistakes:
4. In the following learned positional encoding implementation, the model is not learning as expected. What is the most likely cause?
import torch
import torch.nn as nn
class LearnedPositionalEncoding(nn.Module):
def __init__(self, max_len, d_model):
super().__init__()
self.positional_encoding = nn.Parameter(torch.zeros(max_len, d_model))
def forward(self, x):
return x + self.positional_encoding[:x.size(1), :]
model = LearnedPositionalEncoding(100, 512)
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)medium
Solution
Step 1: Identify the initialization issue
The positional encoding is initialized to zeros, which may not provide a useful starting point for learning.Step 2: Eliminate wrong options
The learning rate is too low. is incorrect because the learning rate is standard. The optimizer is not suitable for this task. is incorrect because Adam is generally suitable. The positional encoding is not being updated. is incorrect because the parameter is part of the model and should be updated.Final Answer:
Option A -> Option AQuick Check:
Check initialization of learned parameters. [OK]
Hint: Check initialization of learned parameters. [OK]
Common Mistakes:
5. How can you optimize the numerical gradient computation for a large neural network?
hard
Solution
Step 1: Identify optimization strategies for numerical gradient computation
Parallelizing the computation can significantly reduce time for large networks.Step 2: Eliminate ineffective strategies
Options A, C, and D do not directly optimize the gradient computation process.Final Answer:
Option B -> Option BQuick Check:
Parallelize for large networks. [OK]
Hint: Parallelize for large networks. [OK]
Common Mistakes:
