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 snippet output when implementing the Skip-Gram model with negative sampling?
import numpy as np
vocab_size = 5
embedding_dim = 3
# Initialize random embeddings
embeddings = np.random.rand(vocab_size, embedding_dim)
# Select a word index
word_index = 2
# Get the embedding for the word
word_embedding = embeddings[word_index]
print(word_embedding.shape)easy
Solution
Step 1: Understand the code
The code initializes a random embedding matrix of shape (vocab_size, embedding_dim) and retrieves the embedding for a specific word index.Step 2: Determine the shape of the retrieved embedding
The embedding for a single word is a 1D array with length equal to embedding_dim, hence the shape is (3,).Final Answer:
Option C -> Option CQuick Check:
Single word embedding shape is (embedding_dim,) [OK]
Hint: Single word embedding shape is (embedding_dim,) [OK]
Common Mistakes:
3.
import numpy as np
# Backpropagation for a single neuron
inputs = np.array([1.0, 2.0, 3.0])
weights = np.array([0.2, 0.8, -0.5])
bias = 2.0
learning_rate = 0.01
d_output = np.array([1.0]) # Gradient of loss w.r.t output
# Incorrect gradient calculation
weights_gradient = d_output * inputs
weights -= learning_rate * weights_gradient
print(weights)medium
Solution
Step 1: Identify error in gradient calculation
The code incorrectly applies the gradient directly without considering the chain rule.Step 2: Correct approach
Should multiply by the derivative of the activation function.Final Answer:
Option D -> Option DQuick Check:
Always apply chain rule in backpropagation [OK]
Hint: Always apply chain rule in backpropagation [OK]
Common Mistakes:
4. You implemented gradient checking but the numerical and analytical gradients differ significantly. What is the most likely cause?
medium
Solution
Step 1: Identify potential causes for gradient mismatch
Significant differences often indicate errors in the analytical gradient.Step 2: Eliminate other options
The step size for numerical gradient is too large. would cause small numerical errors, not significant differences. The learning rate is too high. and C are unrelated to gradient checking.Final Answer:
Option C -> Option CQuick Check:
Mismatch often means analytical error. [OK]
Hint: Mismatch often means analytical error. [OK]
Common Mistakes:
5. In a Random Forest implementation, how would you apply feature sampling to handle high-dimensional data effectively?
medium
Solution
Step 1: Understand Feature Sampling
Feature sampling involves selecting a random subset of features for each tree to reduce dimensionality.Step 2: Evaluate Options
Using all features (A) or a fixed subset (C) doesn't reduce dimensionality; correlation-based selection (D) isn't random.Final Answer:
Option C -> Option CQuick Check:
Random subsets reduce dimensionality [OK]
Hint: Random subsets reduce dimensionality [OK]
Common Mistakes:
