Three questions from the bank, with answers. The practice test draws different ones each time.
Question 1 · regularization · mid
What is the primary mathematical effect of increasing the alpha parameter in Ridge Regression?
from sklearn.linear_model import Ridge
ridge = Ridge(alpha=100.0)
ridge.fit(X, y)
A.It penalizes large coefficients by adding their squared sum to the loss function, reducing model variance.
B.It converts a linear regression model into a non-linear classifier.
C.It performs automatic feature selection by setting coefficients exactly to zero.
D.It eliminates the need for scaling features.
Show answer
A. It penalizes large coefficients by adding their squared sum to the loss function, reducing model variance.
L2 regularization (Ridge) adds a penalty proportional to the square of the coefficient magnitudes (L2 norm). This shrinks the coefficients towards zero, reducing model variance and preventing overfitting without driving them strictly to zero (unlike L1/Lasso).
Question 2 · models · mid
When using PyTorch's nn.CrossEntropyLoss for multi-class classification, what format must the model outputs take?
import torch.nn as nn
loss_fn = nn.CrossEntropyLoss()
# model output: logits of shape (batch_size, num_classes)
# targets: tensor of shape (batch_size,) containing class indices
A.model outputs must be negative values.
B.model outputs should be raw logits, as nn.CrossEntropyLoss applies log-softmax internally.
C.model outputs must be passed through a softmax activation before entering the loss function.
D.targets must be one-hot encoded vectors of shape (batch_size, num_classes).
Show answer
B. model outputs should be raw logits, as nn.CrossEntropyLoss applies log-softmax internally.
PyTorch's nn.CrossEntropyLoss expects raw, unnormalized scores (logits) as inputs, not probabilities or log-probabilities. It internally applies LogSoftmax followed by the Negative Log Likelihood loss (NLLLoss). Passing softmax probabilities to it will result in incorrect gradients and poor training.
Question 3 · training · mid
How does RandomizedSearchCV differ fundamentally from GridSearchCV in hyperparameter optimization?
# Hyperparameter tuning setup
from sklearn.model_selection import RandomizedSearchCV
# param_distributions has wide continuous ranges for learning rate and regularization
search = RandomizedSearchCV(estimator=model, param_distributions=param_dist, n_iter=50, cv=5)
A.It samples a fixed number of random parameter combinations from specified distributions, offering a faster and often more effective search than exhaustive grid search.
B.It exhaustively evaluates every possible combination of hyperparameters in the grid.
C.It guarantees finding the absolute global minimum of the hyperparameter validation loss.
D.It trains the model only once using default parameters.
Show answer
A. It samples a fixed number of random parameter combinations from specified distributions, offering a faster and often more effective search than exhaustive grid search.
RandomizedSearchCV searches a subset of specified parameter values randomly (controlled by n_iter), making it much more computationally efficient than GridSearchCV when tuning high-dimensional continuous or discrete hyperparameter spaces.