sagelearnai

Machine Learning Fundamentals: Concepts, part 1

The core vocabulary of machine learning. Interviewers use these to check that your foundations are precise, not just familiar.

What is machine learning, and how do supervised, unsupervised, semi-supervised, self-supervised and reinforcement learning differ?

Machine learning is learning a function from data rather than hand-coding rules: you choose a model family, a loss, and an optimization procedure that fits the model's parameters to data.

ParadigmDataObjectiveExample
SupervisedInputs with labelsPredict the labelSpam classification, price prediction
UnsupervisedInputs onlyFind structureClustering, dimensionality reduction
Semi-supervisedFew labels + many unlabeledUse unlabeled data to improve the supervised modelPseudo-labeling
Self-supervisedUnlabeled; labels created from the data itselfPredict a hidden part of the inputNext-token prediction (LLMs), masked language modeling, contrastive learning
Reinforcement learningInteraction with an environmentMaximize cumulative rewardGame playing, RLHF

Self-supervised learning is why LLMs scale: the internet provides virtually unlimited "labels" (the next token).

Explain the bias–variance trade-off.

Expected test error for squared loss decomposes into bias² + variance + irreducible noise.

  • Bias: error from wrong assumptions, a model too simple to capture the true function (underfitting). E.g., a linear model on a curved relationship.
  • Variance: error from sensitivity to the particular training sample, a model that fits noise (overfitting). E.g., a deep unpruned decision tree.

Increasing model complexity typically lowers bias and raises variance; the best test error is at the balance point. Diagnose with learning curves: high train error ≈ val error → high bias (bigger model, better features, less regularization); low train error ≪ val error → high variance (more data, regularization, simpler model, ensembling/bagging).

Modern nuance: very over-parameterized models (deep nets) can show double descent, where test error falls again past the interpolation threshold, so the classic U-curve isn't the whole story.

What is overfitting and how do you prevent it?

Overfitting is when a model learns patterns specific to the training data (including noise) that don't generalize, seen as a gap between training and validation performance.

Prevention, roughly in order of effectiveness:

  1. More (and more diverse) data, or data augmentation.
  2. Regularization: L2/L1 penalties, dropout, weight decay.
  3. Simpler model / fewer features / feature selection.
  4. Early stopping on validation loss.
  5. Ensembling (bagging reduces variance).
  6. Cross-validation to choose hyperparameters without overfitting to one split.

Also check for leakage: suspiciously good validation results often mean the validation data leaked into training.

What are training, validation and test sets for? Why not tune on the test set?
  • Training set: fit the model's parameters.
  • Validation set: choose hyperparameters, features, model type and early-stopping point.
  • Test set: a final, untouched estimate of generalization.

If you tune on the test set, it becomes part of model selection, and the reported score is optimistically biased: you've overfit to that specific sample. With many experiments, even random noise can look like improvement. Keep the test set locked until the end, or use nested cross-validation when data is scarce. In production ML, the best "test set" is often a future time period (temporal holdout).

L1 vs L2 regularization — what's the difference and why does L1 produce sparsity?

Both add a penalty on weights to the loss: L2 adds λ·Σw² (ridge), L1 adds λ·Σ|w| (lasso).

  • L2 shrinks all weights smoothly toward zero but rarely makes them exactly zero; it handles correlated features by spreading weight across them. Equivalent to a Gaussian prior on weights.
  • L1 drives many weights to exactly zero, which means implicit feature selection. Equivalent to a Laplace prior.

Why sparsity: geometrically, the L1 constraint region is a diamond with corners on the axes, and the loss contours typically first touch it at a corner, where some coordinates are 0. Analytically, L1's gradient has constant magnitude λ regardless of how small w is, so it can push a weight all the way to zero, while L2's gradient 2λw vanishes as w → 0.

Elastic Net combines both to get sparsity while handling correlated features.

How does gradient descent work? Compare batch, stochastic and mini-batch.

Gradient descent iteratively updates parameters in the direction of steepest decrease of the loss: θ ← θ − η·∇L(θ), where η is the learning rate.

VariantGradient computed onProsCons
BatchFull datasetExact gradient, stableSlow per step, memory-heavy
Stochastic (SGD)One exampleCheap steps; noise helps escape saddle pointsVery noisy, so it needs a decaying LR
Mini-batch32–4096 examplesGPU-efficient, moderate noiseBatch size is a hyperparameter

Mini-batch is the standard. The learning rate is the most important hyperparameter: too high diverges or oscillates, too low is slow and gets stuck. Schedules (warmup, cosine decay) and adaptive optimizers (Adam) address this.

What is a loss function vs an evaluation metric? Give examples where they differ.

The loss is what the optimizer minimizes during training; it must be (sub)differentiable and well-behaved. The metric is what you report and care about; it can be anything that reflects business value.

TaskTypical lossTypical metric
Binary classificationBinary cross-entropyPrecision/recall at a threshold, PR-AUC
RankingPairwise/listwise losses (or cross-entropy)nDCG@k, MRR
RegressionMSE or HuberMAE, MAPE, business cost
Object detectionClassification + box regression lossesmAP

They differ because metrics like accuracy, F1 or nDCG are non-differentiable (step functions of thresholds or ranks). You optimize a smooth surrogate and monitor the metric on validation data.

Generative vs discriminative models?
  • Discriminative models learn P(y|x) or a decision boundary directly: logistic regression, SVMs, most neural classifiers. Usually better accuracy for classification given enough data.
  • Generative models learn P(x, y) or P(x) (and get P(y|x) via Bayes): Naive Bayes, GMMs, HMMs, VAEs, diffusion models, and LLMs as models of P(text). They can generate samples, handle missing data, and often work better with little data.

Classic result (Ng & Jordan): Naive Bayes converges faster with few examples, while logistic regression has lower asymptotic error.

What is the "no free lunch" theorem and what does it mean in practice?

Averaged over all possible problems, no learning algorithm outperforms any other. Every algorithm's advantage comes from inductive biases that match the structure of the problems we actually care about (smoothness, locality, sparsity, hierarchy).

In practice: there is no universally best model. Choose models whose assumptions fit your data (gradient-boosted trees for tabular data, CNNs/ViTs for images, Transformers for sequences), and validate empirically. Always compare against simple baselines.

Last reviewed 2026-11-05.