Practice
1. 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:
2. In which scenario is gradient checking most useful?
medium
Solution
Step 1: Identify scenarios where gradient checking is applicable
Gradient checking is crucial when implementing new components to ensure correctness.Step 2: Eliminate unrelated scenarios
Options A, C, and D are not directly related to verifying gradient correctness.Final Answer:
Option A -> Option AQuick Check:
Use gradient check for new components. [OK]
Hint: Use gradient check for new components. [OK]
Common Mistakes:
3. A logistic regression model is not converging during training. Which line in the code is most likely causing the issue?
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 * gradientmedium
Solution
Step 1: Identify potential issues
Convergence issues often arise from incorrect weight updates or learning rate problems.Step 2: Analyze weight update line
If learning_rate is too high, weights update too aggressively, causing divergence.Final Answer:
Option A -> Option AQuick Check:
Check learning rate in weight updates [OK]
Hint: Check learning rate in weight updates [OK]
Common Mistakes:
4. You implemented an ROC curve but the AUC is unexpectedly low. What is the most likely cause?
medium
Solution
Step 1: Analyze AUC behavior
AUC measures the ability to distinguish between classes. Random predictions result in an AUC of 0.5.Step 2: Eliminate wrong options
The model predictions are perfectly calibrated. would result in a high AUC, Options C and D would lead to undefined or trivial ROC curves.Final Answer:
Option B -> Option BQuick Check:
Random predictions lead to low AUC [OK]
Hint: Random predictions lead to low AUC [OK]
Common Mistakes:
5. How can you optimize a KD-Tree implementation for a KNN classifier to handle large datasets more efficiently?
hard
Solution
Step 1: Identify optimization strategies
Approximate nearest neighbors can significantly speed up queries in large datasets.Step 2: Evaluate other options
A: Balancing helps but isn't enough for large datasets. B: Increasing dimensions worsens performance. D: Reducing data points isn't always feasible.Final Answer:
Option B -> Option BQuick Check:
Approximate neighbors speed up KD-Tree [OK]
Hint: Approximate neighbors speed up KD-Tree [OK]
Common Mistakes:
