Practice
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))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]
Solution
Step 1: Identify the metric for node impurity.
Gini Impurity is specifically used to measure the impurity of a node in decision trees.Step 2: Eliminate wrong options.
Entropy is used for information gain, Mean Squared Error is for regression tasks, and Information Gain is a criterion for splitting, not impurity.Final Answer:
Option B -> Option BQuick Check:
Gini measures impurity in nodes. [OK]
import torch
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
for epoch in range(10):
for data, target in dataloader:
optimizer.zero_grad()
output = model(data)
loss = loss_fn(output, target)
loss.backward()
optimizer.step()
print(optimizer.param_groups[0]['lr'])What will be printed after the loop?Solution
Step 1: Understand the code.
The learning rate is set to 0.1 and not changed in the loop.Step 2: Check for learning rate updates.
SGD without a scheduler keeps the learning rate constant.Final Answer:
Option C -> Option CQuick Check:
SGD without scheduler keeps lr constant. [check]
Solution
Step 1: Identify LSTM-specific issues.
Incorrect initialization of forget gate biases can lead to vanishing gradients.Step 2: Eliminate wrong options.
Too many layers can cause vanishing gradients, but it's not LSTM-specific. High learning rates cause exploding gradients, and dropout affects overfitting, not gradient flow.Final Answer:
Option A -> Option AQuick Check:
Forget gate bias affects gradient flow. [OK]
Solution
Step 1: Identify symptoms of vanishing gradient.
Early plateauing of loss often indicates vanishing gradients.Step 2: Eliminate other causes.
High learning rate (A) would cause erratic loss, overfitting (B) would show low training loss, and small dataset (D) would not cause early plateau.Final Answer:
Option C -> Option CQuick Check:
Early plateau = vanishing gradient. [check]
