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 output?
import numpy as np
def softmax(x):
e_x = np.exp(x - np.max(x))
return e_x / e_x.sum(axis=0)
x = np.array([2.0, 1.0, 0.1])
print(softmax(x))easy
Solution
Step 1: Calculate softmax
Compute the exponentials of the input array after subtracting the max value for numerical stability.Step 2: Normalize
Divide each exponential by the sum of all exponentials to get probabilities.Final Answer:
Option A -> Option AQuick Check:
Softmax outputs sum to 1 [OK]
Hint: Softmax outputs sum to 1 [OK]
Common Mistakes:
3. In a custom dropout layer implementation, how should you modify the forward pass for inference mode to ensure correct scaling?
medium
Solution
Step 1: Understand Inference Scaling
During inference, inputs should be scaled by dividing by (1 - dropout_rate) to maintain expected values.Step 2: Eliminate Wrong Options
Apply the same dropout mask as in training. incorrectly applies dropout during inference. Multiply inputs by (1 - dropout_rate). scales incorrectly. Use a different dropout rate for inference. suggests changing dropout rate, which is not standard practice.Final Answer:
Option A -> Option AQuick Check:
Scale by dividing during inference. [OK]
Hint: Scale by dividing during inference. [OK]
Common Mistakes:
4. You implemented Robust Scaling but your model's performance dropped. What is the most likely cause?
medium
Solution
Step 1: Understand Robust Scaling
Robust Scaling uses the median and IQR, which reduces the influence of outliers.Step 2: Identify the Issue
If outliers are important, reducing their influence can harm model performance.Final Answer:
Option D -> Option DQuick Check:
Robust Scaling reduces outlier impact. [OK]
Hint: Robust Scaling reduces outlier impact. [OK]
Common Mistakes:
5.
def calculate_f1(confusion_matrix):
tp = confusion_matrix[0, 0]
fp = confusion_matrix[1, 0]
fn = confusion_matrix[0, 1]
precision = tp / (tp + fp)
recall = tp / (tp + fn)
f1_score = 2 * (precision * recall) / (precision + recall)
return f1_score
confusion_matrix = np.array([[50, 10], [5, 35]])
print(calculate_f1(confusion_matrix))medium
Solution
Step 1: Calculate precision and recall
Precision = 0.9091, Recall = 0.8333.Step 2: Calculate F1-score
F1 = 2 * (0.9091 * 0.8333) / (0.9091 + 0.8333) = 0.8696.Final Answer:
Option B -> Option BQuick Check:
F1 combines precision and recall. [OK]
Hint: F1 combines precision and recall. [OK]
Common Mistakes:
