esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • AI papersPages
  • Interview KitPages
  • Book a callContact
  • Download ResumeContact
Let’s talk

I’ll build the agent for it.

L1, L2 and Dropout Regularisation

What L1, L2 and dropout do to stop a model memorising its training data, and when to reach for each, explained simply for your AI interview.

In short

L1 and L2 regularisation add a penalty for large weights to the training loss, so the model stays simpler. L1 can push weights to exactly zero, while L2 shrinks them all. Dropout switches random units off during training so the network cannot lean on any single one.

13 min readIntermediateVery common · estimateFacts checked 26 November 2026

Say it out loud

Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.

L1 and L2 add a penalty for large weights to the training loss, so the model does not memorise its data. L2 shrinks every weight a little and never reaches zero. L1 can push weak weights to exactly zero, which also picks features. Dropout switches random units off during each training step, so no unit can lean on one partner. I tune the strength on validation data and pick the method that fits the problem.

What are L1, L2 and dropout, in plain words?

A model learns by adjusting its weights, the numbers that decide how much each input counts. Overfitting is when it learns the training examples so well, noise included, that it does badly on new ones.

Picture a fraud analyst who memorises last year's 200 flagged cases word for word. This year's scams differ in small ways, and the analyst misses them. Regularisation is a rule that makes the analyst learn the pattern instead. In a model, that rule is a penalty added to the training loss. The loss is the model's error score during training. Lower is better, and training tries to make it lower.

Key words

Weight
A number the model learns. It says how strongly one input pushes the answer.
Overfitting
A model that matches its training data too closely and fails on new data.
Penalty
An extra cost added to the training loss for large weights.
Lambda
The strength of the penalty. A higher lambda gives simpler models.
Validation data
Examples held back from training, used to check the model on data it has not learned from.

How does L2 (ridge) regularisation work?

L2 adds the sum of the squared weights to the training loss. A squared weight grows fast when the weight is large, so the penalty punishes the biggest weights most. Training then shrinks every weight towards zero. L2 never pushes a weight all the way to zero, so every input still counts a little.

Lambda sets how hard the penalty pushes. A lambda of zero switches the penalty off, which leaves the model free to overfit. A larger lambda gives simpler models, and a much larger one makes the model too simple to fit real patterns.

Worked example, with made-up numbers. A fraud model has three weights: 0.4 for the transaction amount, -0.3 for the hour of the day, and 6.0 for a noisy device code. The L2 penalty adds up 0.4 squared, -0.3 squared and 6.0 squared: 0.16 + 0.09 + 36 = 36.25. The device code weight makes up 36 of that 36.25, so training shrinks it hardest.

In PyTorch's SGD optimiser, the weight_decay setting is this L2 penalty.

How does L1 (lasso) regularisation work?

L1 adds the sum of the absolute values of the weights, which is each weight's size with its sign removed. The pull on a small weight is the same size as the pull on a big one, so weak weights can reach exactly zero.

A weight at exactly zero removes its input from the model. That is why L1 is used for feature selection, which means picking the inputs (features) that matter. With made-up numbers, a model that starts with 300 candidate inputs may end with 40 non-zero weights, and those 40 are the list you can explain to a risk team.

Here is a small run with made-up numbers. It applies only the penalty, for 10 steps and no data, so you can see what each one does to five weights.

Python
# A fraud model's five weights, after 10 steps of penalty alone (no data).
# Made-up numbers: ip_noise and device_age carry almost no signal.
FEATURES = ["amount", "new_device", "night_hour", "ip_noise", "device_age"]
START = [2.0, 1.2, -0.8, 0.05, 0.02]
LR, LAM = 0.1, 0.5

def l2_step(w):
    return w - LR * 2 * LAM * w  # L2: every weight shrinks by the same share

def l1_step(w):
    cut = LR * LAM  # L1: every weight shrinks by the same amount
    if abs(w) <= cut:
        return 0.0  # small weights land exactly on zero
    return w - cut if w > 0 else w + cut

def run(step):
    w = list(START)
    for _ in range(10):
        w = [step(x) for x in w]
    return w

for name, step in [("L2", l2_step), ("L1", l1_step)]:
    final = run(step)
    print(name)
    for f, x in zip(FEATURES, final):
        print(f"  {f:<11} {x:9.4f}")
    print(f"  exact zeros: {sum(1 for x in final if x == 0)}")

Output

L2
  amount         0.6974
  new_device     0.4184
  night_hour    -0.2789
  ip_noise       0.0174
  device_age     0.0070
  exact zeros: 0
L1
  amount         1.5000
  new_device     0.7000
  night_hour    -0.3000
  ip_noise       0.0000
  device_age     0.0000
  exact zeros: 2

L2 leaves the two noise weights tiny but not zero. L1 sets them to exactly zero, and it shrinks the strong weights by the same amount each step.

How does dropout work?

During each training step, dropout switches off a random share of the units (the small calculators that make up a neural network), along with their connections. The network trains a different thinned version every time.

  • “The key idea is to randomly drop units (along with their connections) from the neural network during training.”

    Srivastava, Hinton and colleagues, JMLR

The idea first appeared in a 2012 preprint by Hinton and colleagues. Srivastava, Hinton and colleagues then wrote the full version in JMLR. The paper says dropout prevents units from co-adapting too much, and that it significantly reduces overfitting.

Dropout adds no penalty term of its own. For generalized linear models, a family that includes linear and logistic regression, the paper shows it performs a form of adaptive regularization. For deep networks, it is not a plain L2 penalty.

Dropout: a different thinned network each step

Full network

Hidden units learn the patterns

Drop units at random

Each unit off with chance p

Thinned network

Only kept units learn this step

Less overfitting

Units lean on each other less

  1. 1Pick a random set of units to switch off for this batch.
  2. 2The kept units form a smaller network and learn this step.
  3. 3Over many batches the kept units learn, so none becomes the only support.
One training step in four stages. A random set of units is switched off, the thinned network learns, and repeated over many batches no unit becomes the only support. The result is less overfitting.

Why it helps: a unit cannot count on one partner being present, so it has to carry a useful signal on its own. The dropout rate (p) is the chance that a unit is switched off in one step. PyTorch's dropout layer uses a rate p, and its default is 0.5. During training it zeroes elements at random, and it picks a new random set each time it runs on a batch.

At prediction time, PyTorch's docs say you set the model to evaluation mode, and then the dropout layer does nothing. During training, PyTorch also scales the kept outputs up, so the output stays the same on average over many steps.

Tip

Dropout belongs to training. If you leave it on while scoring live transactions, the same input can get a different score on each call.

A worked example, with made-up numbers: an insurance claims model has a middle layer of 512 units (the layer between the inputs and the output) and a dropout rate of 0.5, so about half the units are off in each step. After 15 passes through the data, training loss keeps falling but validation loss starts to rise. Adding dropout to that layer holds the two curves closer together.

Which one should you pick?

  • L2

    Shrinks all weights

    Keeps every input a little. A common first try for linear models, and the weight_decay setting in PyTorch's SGD optimiser.

  • L1

    Picks inputs

    Sets weak weights to zero, so the model is smaller and you can see which inputs matter.

  • Dropout

    For neural networks

    Switches units off during training. It adds no penalty, but the model must switch to evaluation mode when scoring.

  • “L2 regularization encourages weights toward 0, but never pushes weights all the way to zero.”

    Google Machine Learning Crash Course

  • “The Lasso is a linear model that estimates sparse coefficients, i.e., it is able to set coefficients exactly to zero.”

    scikit-learn

All three reduce overfitting, but they act in different places. The two penalties act on the loss. Dropout acts on the network while it trains. When the model predicts a number, L2 is called ridge and L1 is called lasso.

Two penalties, two different results

Big model, many weights

Some weights only fit noise

L2 penalty

Squared weights

Adds the sum of w squared

L1 penalty

Absolute weights

Adds the sum of the size of w

Shrunk, none zero

Every weight smaller, none gone

Some exactly zero

Noise inputs switched off

  1. 1The big model's weights go into the squared penalty.
  2. 2The same weights go into the absolute-value penalty.
  3. 3Squared penalty: big weights lose the most size, small ones barely move.
  4. 4Absolute penalty: small weights can land exactly on zero.
The same big model goes into two penalties at once. L2 shrinks every weight a little and keeps them all. L1 can push weak weights to exactly zero, which switches their inputs off.

Two things hold for all three. You tune the strength on validation data, not the test set. None of them repairs bad or leaking data: a penalty only makes the model simpler.

How would you fix a fraud model that overfits?

Say a card-fraud model scores 99% on last quarter's data but 80% on this month's, with made-up numbers. Training loss is low, validation loss rises after a few passes, and the feature list has 300 inputs, many of them noise from a device feed.

  1. 1

    Check the gap first

    Compare training and validation scores. A wide gap points to overfitting. A drop on both may point to changed data.

  2. 2

    Cut the noise

    Use an L1 model to see which inputs it sets to zero. Drop the noisy device fields and check the gap again.

  3. 3

    Tune the penalty or dropout

    Search lambda or the dropout rate on validation data, never on the test set.

  4. 4

    Test on the newest month

    Fraud patterns change over time, so judge the model on the latest data it has not seen.

Finally, I check that a less overfit model still catches the fraud the team cares about. A model can look cleaner and still miss more real fraud.

What does the interviewer want to hear?

A strong answer covers

  • Plain definitions: L1 and L2 penalise big weights, and dropout switches units off while training.
  • The difference: L1 can reach zero and pick features, while L2 shrinks every weight.
  • Where each fits: L2 as a default, L1 for fewer inputs, dropout for neural networks.
  • Lambda and the dropout rate are tuned on validation data, not the test set.
  • Dropout is off at prediction time.
  • A real case with a gap between training and validation scores.

Common traps

  • Trap"Dropout is just another L2 penalty."

    BetterDropout changes the network while it trains. For deep nets it is not a plain L2 penalty.

  • Trap"L1 and L2 do the same thing, so pick either."

    BetterL1 can zero weights and pick features. L2 shrinks every weight and keeps all the inputs.

  • Trap"A bigger lambda always gives a better model."

    BetterToo big a penalty underfits, so the model misses real patterns. Tune it on validation data.

Can you spot the mistake?

Spot the mistake

Which snippet turns dropout off before scoring a live fraud batch?

Quick check

An insurance claims model gives two different risk scores for the same claim when the same trained model scores it twice. What is the most likely cause?

Can you answer these from memory?

Flash cards · tap a card to flip it

1 / 6

What will they ask next?

  1. Q412Bias vs variance?12 min
  2. Q413Overfitting vs underfitting?10 min
  3. Q415What is gradient descent?12 min
  4. Q416SGD vs Adam?Coming soon
  5. Q418What is backpropagation?11 min
  6. Q422Train/validation/test splits?11 min

Learn it in depth

Last updated on 26 November 2026

Building a model that has to work on next month's data?

I design and build AI products and agents, including the interface and the system behind them. Book a short call and tell me what your model needs to do.