Practice
1.
import numpy as np
def convolve2d(image, kernel):
kernel_height, kernel_width = kernel.shape
image_height, image_width = image.shape
output_height = image_height - kernel_height + 1
output_width = image_width - kernel_width + 1
output = np.zeros((output_height, output_width))
for i in range(output_height):
for j in range(output_width):
output[i, j] = np.sum(image[i:i+kernel_height, j:j+kernel_width] * kernel)
return output
image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
kernel = np.array([[1, 0], [0, -1]])
print(convolve2d(image, kernel))easy
Solution
Step 1: Understand the convolution operation
The function performs a 2D convolution by sliding the kernel over the image and computing the sum of element-wise products.Step 2: Calculate the output
For each position, calculate the sum of products:
Top-left: (1*1 + 2*0 + 4*0 + 5*-1) = 0
Top-right: (2*1 + 3*0 + 5*0 + 6*-1) = 1
Bottom-left: (4*1 + 5*0 + 7*0 + 8*-1) = 1
Bottom-right: (5*1 + 6*0 + 8*0 + 9*-1) = -1Final Answer:
Option D -> Option DQuick Check:
Sum of element-wise products for each position. [OK]
Hint: Sum of element-wise products for each position. [OK]
Common Mistakes:
2. What does the following Python code snippet output when implementing linear regression using gradient descent?
import numpy as np
X = np.array([[1, 2], [3, 4], [5, 6]])
y = np.array([1, 2, 3])
weights = np.zeros(X.shape[1])
learning_rate = 0.01
for _ in range(1):
predictions = X.dot(weights)
errors = predictions - y
gradient = X.T.dot(errors) / len(y)
weights -= learning_rate * gradient
print(weights)easy
Solution
Step 1: Calculate initial predictions and errors
Initial predictions are [0, 0, 0] since weights are zeros. Errors are [-1, -2, -3].Step 2: Compute gradient and update weights
Gradient is [-3, -6]. Updated weights are [0.01, 0.02].Final Answer:
Option A -> Option AQuick Check:
Initial weights zero, single update step. [OK]
Hint: Initial weights zero, single update step. [OK]
Common Mistakes:
3.
import numpy as np
def euclidean_distance(a, b):
return np.sqrt(np.sum((a - b) ** 2))
vector1 = np.array([1, 2, 3])
vector2 = np.array([4, 5])
result = euclidean_distance(vector1, vector2)
print(result)medium
Solution
Step 1: Check vector shapes
vector1 has shape (3,), vector2 has shape (2,).Step 2: Identify operation failure
Subtraction fails due to shape mismatch, raising a ValueError.Final Answer:
Option B -> Option BQuick Check:
Shape mismatch leads to ValueError. [OK]
Hint: Shape mismatch leads to ValueError. [OK]
Common Mistakes:
4. In a pipeline that includes flipping, cropping, and normalizing images, where should normalization be applied for best results?
medium
Solution
Step 1: Understand the impact of operations
Normalization should be the last step to ensure consistent scaling.Step 2: Evaluate each option
Applying normalization last ensures it accounts for all transformations.Final Answer:
Option D -> Option DQuick Check:
Normalize last for consistent scaling. [OK]
Hint: Normalize last for consistent scaling. [OK]
Common Mistakes:
5. In a multi-class classification problem, how would you modify the softmax function to handle very large input values?
medium
Solution
Step 1: Address numerical stability
Subtracting the max input value prevents overflow in exponentials.Step 2: Ensure correct probabilities
This adjustment maintains the relative differences between inputs.Final Answer:
Option A -> Option AQuick Check:
Subtract max for stability in softmax [OK]
Hint: Subtract max for stability in softmax [OK]
Common Mistakes:
