Practice
1. What does the following Python code snippet output when implementing Mini-Batch SGD with Momentum?
import numpy as np
velocity = np.array([0.0, 0.0])
gradient = np.array([0.1, 0.2])
learning_rate = 0.01
momentum = 0.9
velocity = momentum * velocity - learning_rate * gradient
print(velocity)easy
Solution
Step 1: Calculate the velocity update
The formula for velocity update isvelocity = momentum * velocity - learning_rate * gradient. Given the initial velocity is zero, the update becomesvelocity = -learning_rate * gradient.Step 2: Substitute the values
Substitutelearning_rate = 0.01andgradient = [0.1, 0.2]into the formula:velocity = -0.01 * [0.1, 0.2] = [-0.001, -0.002].Final Answer:
Option C -> Option CQuick Check:
Velocity update is negative of learning rate times gradient [OK]
Hint: Velocity update is negative of learning rate times gradient [OK]
Common Mistakes:
2.
import numpy as np
def dcg_at_k(r, k):
r = np.asfarray(r)[:k]
if r.size:
return np.sum(r / np.log2(np.arange(2, r.size + 2)))
return 0.0
relevance_scores = [3, 2, 3, 0, 1, 2]
k = 3
print(dcg_at_k(relevance_scores, k))What does this code output?easy
Solution
Step 1: Understand DCG Calculation
The DCG is calculated as the sum of relevance scores divided by log base 2 of their positions.Step 2: Calculate for k=3
For k=3, the scores are [3, 2, 3]. The calculation is 3/1 + 2/log2(3) + 3/log2(4).Final Answer:
Option A -> Option AQuick Check:
DCG uses log2 positions. [OK]
Hint: DCG uses log2 positions. [OK]
Common Mistakes:
3. How can you optimize the implementation of linear regression using gradient descent for large datasets?
hard
Solution
Step 1: Identify scalability issues
Full-batch gradient descent is computationally expensive for large datasets.Step 2: Choose an optimization strategy
Mini-batch gradient descent reduces computation per iteration and can converge faster.Final Answer:
Option D -> Option DQuick Check:
Mini-batch reduces computation, speeds convergence. [OK]
Hint: Mini-batch reduces computation, speeds convergence. [OK]
Common Mistakes:
4. How can you optimize the implementation of a Gaussian Naive Bayes classifier for a large dataset?
hard
Solution
Step 1: Identify optimization needs
For large datasets, memory efficiency is crucial.Step 2: Evaluate optimization strategies
Using a sparse matrix (Use a sparse matrix representation for input data.) reduces memory usage and speeds up computation. Use a more complex model like SVM. increases complexity unnecessarily. Implement batch processing for training. is less relevant as GaussianNB is already efficient. Increase the number of features by adding polynomial terms. increases computational load.Final Answer:
Option A -> Option AQuick Check:
Use sparse matrices for memory efficiency. [OK]
Hint: Use sparse matrices for memory efficiency. [OK]
Common Mistakes:
5. How can you optimize the computation of the ROC curve for a large dataset?
hard
Solution
Step 1: Identify optimization strategies
Parallel processing can speed up computation by handling different thresholds simultaneously.Step 2: Eliminate wrong options
Use a smaller batch size for computation. doesn't address the core computation, Use a subset of data to approximate the ROC curve. may reduce accuracy, Use a more efficient sorting algorithm for the scores. is limited by sorting algorithm constraints.Final Answer:
Option D -> Option DQuick Check:
Parallel processing speeds up ROC computation [OK]
Hint: Parallel processing speeds up ROC computation [OK]
Common Mistakes:
