Practice
1. What is the time complexity of computing the numerical gradient for a function with n parameters using central difference?
easy
Solution
Step 1: Analyze the loop structure
The numerical gradient computation involves a loop over n parameters, each requiring constant time operations.Step 2: Determine the complexity
Each parameter requires a constant number of operations, leading to O(n) complexity.Final Answer:
Option D -> Option DQuick Check:
Loop over n parameters, each constant time. [OK]
Hint: Loop over n parameters, each constant time. [OK]
Common Mistakes:
2.
import numpy as np
def iou(box1, box2):
x1 = max(box1[0], box2[0])
y1 = max(box1[1], box2[1])
x2 = min(box1[2], box2[2])
y2 = min(box1[3], box2[3])
intersection = max(0, x2 - x1) * max(0, y2 - y1)
area1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
area2 = (box2[2] - box2[0]) * (box2[3] - box2[1])
union = area1 + area2 - intersection
return intersection / union
box1 = [1, 1, 3, 3]
box2 = [2, 2, 4, 4]
print(iou(box1, box2))easy
Solution
Step 1: Calculate intersection and union areas
Intersection area is 1, union area is 7.Step 2: Compute IoU
IoU = 1/7 = 0.14285714285714285.Final Answer:
Option C -> Option CQuick Check:
Intersection over Union formula [OK]
Hint: Intersection over Union formula [OK]
Common Mistakes:
3.
import numpy as np
from scipy.spatial import distance
X_train = np.array([[1, 2], [3, 4], [5, 6]])
X_test = np.array([[1, 2]])
# Calculate distances
D = distance.cdist(X_test, X_train, 'euclidean')
print(D)What does the above code output?easy
Solution
Step 1: Understand the function
Thecdistfunction computes the distance between each pair of the two collections of inputs.Step 2: Calculate the distances
The Euclidean distance from[1, 2]to each point inX_trainis calculated as[0., 2.828, 4.472].Final Answer:
Option D -> Option DQuick Check:
Euclidean distance formula: √((x2-x1)² + (y2-y1)²) [OK]
Hint: Euclidean distance formula: √((x2-x1)² + (y2-y1)²) [OK]
Common Mistakes:
4. 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:
5. How can you optimize a naive 2D convolution implementation for large images and kernels?
hard
Solution
Step 1: Identify optimization techniques
Using FFT can significantly reduce the computational complexity of convolution from O(N^2 * K^2) to O(N^2 log N) for large images and kernels.Step 2: Eliminate wrong options
Use a larger stride to reduce computation. reduces output size, not computation. Increase the kernel size to cover more area. increases computation. Use a smaller kernel to decrease computation. limits kernel effectiveness.Final Answer:
Option C -> Option CQuick Check:
FFT reduces convolution complexity. [OK]
Hint: FFT reduces convolution complexity. [OK]
Common Mistakes:
