Practice
1.
import numpy as np
def cosine_similarity(a, b):
dot_product = np.dot(a, b)
norm_a = np.linalg.norm(a)
norm_b = np.linalg.norm(b)
return dot_product / (norm_a * norm_b)
vector1 = np.array([1, 2, 3])
vector2 = np.array([4, 5, 6])
result = cosine_similarity(vector1, vector2)
print(result)easy
Solution
Step 1: Calculate dot product and norms
Dot product = 32, norm_a = 3.7416573867739413, norm_b = 8.774964387392123.Step 2: Compute cosine similarity
Cosine similarity = 32 / (3.7416573867739413 * 8.774964387392123) = 0.9746318461970762.Final Answer:
Option A -> Option AQuick Check:
Dot product over product of norms. [OK]
Hint: Dot product over product of norms. [OK]
Common Mistakes:
2. 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:
3. What is the time complexity of updating the embeddings for a single word pair in the Skip-Gram model with negative sampling?
easy
Solution
Step 1: Identify the operations involved
Updating embeddings involves computing gradients and updating weights for the target word and k negative samples.Step 2: Determine the complexity
Each update involves a constant number of operations per sample, leading to O(k) complexity where k is the number of negative samples.Final Answer:
Option A -> Option AQuick Check:
Complexity is O(k) due to k negative samples [OK]
Hint: Complexity is O(k) due to k negative samples [OK]
Common Mistakes:
4. How would you modify the scaled dot-product attention mechanism to handle variable-length sequences efficiently?
medium
Solution
Step 1: Identify the challenge
Variable-length sequences require handling of different sequence lengths.Step 2: Choose the correct approach
Padding with masking is efficient and maintains sequence integrity.Final Answer:
Option A -> Option AQuick Check:
Pad and mask for variable lengths [OK]
Hint: Pad and mask for variable lengths [OK]
Common Mistakes:
5. How can you optimize the following logistic regression implementation for faster computation?
import numpy as np
# Initialize weights
weights = np.zeros((m, 1))
# Training loop
for epoch in range(epochs):
# Compute predictions
predictions = 1 / (1 + np.exp(-np.dot(X, weights)))
# Compute gradient
gradient = np.dot(X.T, (predictions - y)) / n
# Update weights
weights -= learning_rate * gradienthard
Solution
Step 1: Identify bottlenecks
Matrix operations can be slow if X is dense and large.Step 2: Optimize matrix operations
Using a sparse matrix representation for X reduces memory usage and speeds up computations.Final Answer:
Option A -> Option AQuick Check:
Sparse matrices optimize large, sparse datasets [OK]
Hint: Sparse matrices optimize large, sparse datasets [OK]
Common Mistakes:
