Practice
1.
import numpy as np
class DropoutLayer:
def __init__(self, dropout_rate):
self.dropout_rate = dropout_rate
def forward(self, inputs, training=True):
if training:
self.mask = np.random.binomial(1, 1 - self.dropout_rate, size=inputs.shape)
return inputs * self.mask
else:
return inputs
inputs = np.array([1.0, 2.0, 3.0])
dropout_layer = DropoutLayer(0.5)
output = dropout_layer.forward(inputs, training=False)
print(output)easy
Solution
Step 1: Understand Inference Mode
During inference, dropout is not applied, so the input is returned unchanged.Step 2: Eliminate Wrong Options
[0.5, 1.0, 1.5] applies scaling, which is incorrect for inference. [0.0, 0.0, 0.0] assumes all zeros, which is not the case. [2.0, 4.0, 6.0] doubles the input, which is incorrect.Final Answer:
Option B -> Option BQuick Check:
Inference mode returns inputs unchanged. [OK]
Hint: Inference mode returns inputs unchanged. [OK]
Common Mistakes:
2. In the context of implementing linear regression with gradient descent, how would you handle a dataset with missing values?
medium
Solution
Step 1: Consider impact of missing values
Ignoring or zero-filling can bias results; removing samples reduces data.Step 2: Choose appropriate imputation
Mean imputation is a common strategy that maintains dataset size and reduces bias.Final Answer:
Option A -> Option AQuick Check:
Mean imputation balances bias and data retention. [OK]
Hint: Mean imputation balances bias and data retention. [OK]
Common Mistakes:
3. A Gaussian Naive Bayes model is giving poor performance on a dataset. Which of the following is the most likely cause?
medium
Solution
Step 1: Identify Gaussian Naive Bayes assumptions
Gaussian Naive Bayes assumes that features are normally distributed.Step 2: Evaluate each option
The features are not normally distributed. is correct because non-normal distribution violates the model's assumptions. The model has too many parameters. is incorrect as GaussianNB is simple. The dataset is too small. is less likely as small datasets affect all models. The features are highly correlated. is incorrect as GaussianNB can handle correlated features.Final Answer:
Option B -> Option BQuick Check:
GaussianNB assumes normal distribution. [OK]
Hint: GaussianNB assumes normal distribution. [OK]
Common Mistakes:
4. How would you apply sinusoidal positional encoding in a transformer model for a sequence of variable length?
medium
Solution
Step 1: Consider variable sequence lengths
Each sequence should have positional encoding applied based on its actual length to maintain positional information.Step 2: Eliminate wrong options
Pad all sequences to the maximum length and apply encoding. is inefficient and may introduce unnecessary padding. Apply encoding only to the first few positions of each sequence. ignores the rest of the sequence. Use a fixed-length encoding for all sequences, regardless of length. loses positional information for longer sequences.Final Answer:
Option B -> Option BQuick Check:
Encode based on actual sequence length. [OK]
Hint: Encode based on actual sequence length. [OK]
Common Mistakes:
5. How can you optimize the train-test split with stratification for a very large dataset to reduce memory usage?
hard
Solution
Step 1: Identify memory optimization techniques
Using a generator allows processing data in batches, reducing memory footprint.Step 2: Evaluate options
In-place operations and smaller data types help, but generators are more effective for large datasets.Final Answer:
Option C -> Option CQuick Check:
Generators reduce memory by processing in batches. [OK]
Hint: Generators reduce memory by processing in batches. [OK]
Common Mistakes:
