Practice
1. Given the following Python code, what will be the output?
import numpy as np
from sklearn.model_selection import train_test_split
X = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
y = np.array([1, 1, 1, 1, 0, 0, 0, 0, 0, 0])
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
print(len(X_train), len(X_test))easy
Solution
Step 1: Calculate the split sizes
With a test_size of 0.2, 20% of 10 samples go to the test set, i.e., 2 samples.Step 2: Determine the training set size
The remaining 80% (8 samples) go to the training set.Final Answer:
Option B -> Option BQuick Check:
80% train, 20% test split. [OK]
Hint: 80% train, 20% test split. [OK]
Common Mistakes:
2. Given the following PyTorch code snippet for an LSTM layer, what is the shape of the output
h_n?
import torch
lstm = torch.nn.LSTM(input_size=10, hidden_size=20, num_layers=2)
input = torch.randn(5, 3, 10)
output, (h_n, c_n) = lstm(input)easy
Solution
Step 1: Understand LSTM output shapes.
Theh_nshape is (num_layers, batch_size, hidden_size).Step 2: Apply the given parameters.
Here, num_layers=2, batch_size=3, hidden_size=20, so the shape is (2, 3, 20).Final Answer:
Option B -> Option BQuick Check:
h_n shape: (layers, batch, hidden). [OK]
Hint: h_n shape: (layers, batch, hidden). [OK]
Common Mistakes:
3. Given the following PyTorch code snippet, what will be the output shape of the model's output after fine-tuning?
import torch
import torch.nn as nn
class SimpleModel(nn.Module):
def __init__(self):
super(SimpleModel, self).__init__()
self.features = nn.Sequential(
nn.Linear(512, 256),
nn.ReLU(),
nn.Linear(256, 128)
)
self.classifier = nn.Linear(128, 10)
def forward(self, x):
x = self.features(x)
x = self.classifier(x)
return x
model = SimpleModel()
input_data = torch.randn(32, 512)
output = model(input_data)
print(output.shape)easy
Solution
Step 1: Trace the forward pass.
The input shape is (32, 512). After passing through the features, it becomes (32, 128). The classifier then outputs (32, 10).Step 2: Eliminate wrong options.
torch.Size([32, 512]) is incorrect because the output is not the same as the input. torch.Size([32, 256]) and C are intermediate shapes, not the final output.Final Answer:
Option C -> Option CQuick Check:
Final layer determines output shape. [check]
Hint: Final layer determines output shape. [check]
Common Mistakes:
4. A model trained on a dataset is performing exceptionally well on the test set but poorly on new unseen data. What is the most likely cause?
medium
Solution
Step 1: Analyze the symptoms
High test performance but poor real-world performance suggests leakage.Step 2: Eliminate other causes
The model is overfitting due to too many parameters. would affect both test and real-world data, The model has not been trained for enough epochs. would not cause high test performance, The feature scaling was not applied correctly. would affect all data equally.Final Answer:
Option B -> Option BQuick Check:
High test, low real-world = leakage [OK]
Hint: High test, low real-world = leakage [OK]
Common Mistakes:
5. A model using wrapper methods for feature selection is overfitting. What is the most likely cause?
medium
Solution
Step 1: Identify overfitting causes.
Data leakage during feature selection can lead to overfitting as the model sees information from the validation set.Step 2: Eliminate wrong options.
Too few features usually lead to underfitting. An inappropriate scoring function affects feature ranking, not directly overfitting. High variance can cause overfitting but is not specific to wrapper methods.Final Answer:
Option A -> Option AQuick Check:
Data leakage = overfitting risk. [OK]
Hint: Data leakage causes overfitting. [OK]
Common Mistakes:
