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 is the time complexity of computing the binary cross-entropy loss for a logistic regression model with
n samples and m features?easy
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]
Hint: Dot product per sample, n samples -> O(nm) [OK]
Common Mistakes:
3. In the context of implementing backpropagation using NumPy, which of the following is a correct approach to handle vanishing gradients?
medium
Solution
Step 1: Identify vanishing gradient issue
Vanishing gradients occur when gradients become too small to update weights effectively.Step 2: Correct approach
Gradient clipping prevents gradients from becoming too small or too large.Final Answer:
Option C -> Option CQuick Check:
Clip gradients to prevent vanishing [OK]
Hint: Clip gradients to prevent vanishing [OK]
Common Mistakes:
4. How can you optimize the computation of a confusion matrix for a large dataset with millions of predictions?
hard
Solution
Step 1: Identify bottleneck
Large datasets require efficient computation, often limited by CPU processing.Step 2: Choose optimization strategy
Parallel processing with Dask can distribute computation across cores.Final Answer:
Option B -> Option BQuick Check:
Parallel processing speeds up large data tasks. [OK]
Hint: Parallel processing speeds up large data tasks. [OK]
Common Mistakes:
5. Which optimization would most effectively reduce the computational cost of the scaled dot-product attention mechanism for very large input sequences?
hard
Solution
Step 1: Identify the bottleneck
Computational cost is high due to dense matrix operations.Step 2: Choose the best optimization
Sparse matrices reduce computation by focusing on non-zero elements.Final Answer:
Option D -> Option DQuick Check:
Sparse matrices for large inputs [OK]
Hint: Sparse matrices for large inputs [OK]
Common Mistakes:
