Practice
1. What does the following Python code output when applied to an image array
img?
import numpy as np
def flip_image(img):
return np.flip(img, axis=1)
img = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
flipped_img = flip_image(img)
print(flipped_img)easy
Solution
Step 1: Understand the flip operation
Thenp.flipfunction flips the array along the specified axis. Here, axis=1 means flipping horizontally.Step 2: Apply the flip to the array
Flipping horizontally changes the order of elements in each row.Final Answer:
Option A -> Option AQuick Check:
Horizontal flip reverses row elements. [OK]
Hint: Horizontal flip reverses row elements. [OK]
Common Mistakes:
2. 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:
3. What is the time complexity of calculating precision, recall, and F1-score from a confusion matrix?
easy
Solution
Step 1: Analyze the operations
Calculating precision, recall, and F1-score involves basic arithmetic operations on a fixed-size matrix.Step 2: Determine complexity
These operations are constant time, O(1), as they do not depend on the size of the input data.Final Answer:
Option A -> Option AQuick Check:
Fixed-size matrix operations are O(1). [OK]
Hint: Fixed-size matrix operations are O(1). [OK]
Common Mistakes:
4. A Random Forest implementation is producing poor results. The trees seem to be too similar. What is the most likely cause?
medium
Solution
Step 1: Identify the Cause of Similar Trees
Similar trees often result from insufficient randomness in feature selection.Step 2: Eliminate Other Causes
Too many trees (A) or high variance (D) don't cause similarity; too much data (C) affects overfitting, not similarity.Final Answer:
Option A -> Option AQuick Check:
Feature sampling adds diversity [OK]
Hint: Feature sampling adds diversity [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:
