Simple Algorithm
Deep Learning · Emergence · 26 min

Mixup, CutMix & Vicinal Risk

Training on blends of examples, and what it does to boundaries and calibration.

Mixup trains on images that never existed: weighted averages of two training images, labeled with the same weighted average of their labels. It is a two-line change to a training loop, and it often improves accuracy, calibration and robustness. Why should training on blends help? This lesson derives what mixup actually optimizes: label-smoothed risk on data shrunk toward its mean, plus a data-dependent penalty on input gradients, a penalty that, surprisingly, does nothing at all for linear least squares with an intercept.

What you’ll learn

  • Define vicinal risk minimization, derive mixup and CutMix as vicinal distributions, and show that the mixed cross-entropy equals cross-entropy with the soft label.
  • Prove that mixup's risk is empirical risk on mean-shrunk, label-smoothed data plus zero-mean perturbations (Carratino et al., 2022), and compute the implied label smoothing for a given α\alpha.
  • State the second-order expansion of the mixup loss (Zhang et al., 2021), and prove that mixup leaves linear least squares with an intercept unchanged.
  • Explain mixup's effects on calibration and decision boundaries, manifold mixup and manifold intrusion, and what CutMix changes.

Vicinal risk minimization

Empirical risk minimization trusts the training points exactly: it puts all probability on the nn observed pairs. Chapelle, Weston, Bottou and Vapnik (2001) proposed instead to spread each training example into a vicinity distribution ν(x~,y~∣xi,yi)\nu(\tilde{\mathbf x},\tilde y\mid\mathbf x_i,y_i) and minimize the vicinal risk:

Rν(f)=1n∑i=1nE(x~,y~)∼ν(⋅∣xi,yi)[ℓ(f(x~),y~)]R_\nu(f)=\frac1n\sum_{i=1}^n\E_{(\tilde{\mathbf x},\tilde y)\sim\nu(\cdot\mid\mathbf x_i,y_i)}\big[\ell(f(\tilde{\mathbf x}),\tilde y)\big]
A Gaussian vicinity, x~=xi+ϵ\tilde{\mathbf x}=\mathbf x_i+\boldsymbol\epsilon with the label kept, is training with input noise, which for least squares is ridge regression in expectation (Bishop, 1995). Label-preserving augmentation is a vicinity along the transformations. Each choice of ν\nu is a statement about what the neighborhood of a training example looks like.

Mixup (Zhang, Cisse, Dauphin & Lopez-Paz, 2018) builds the vicinity from pairs of examples. Draw λ∼Beta(α,α)\lambda\sim\mathrm{Beta}(\alpha,\alpha) (a mixing weight, unrelated to weight decay) and a partner jj uniformly at random, and train on

x~=λxi+(1−λ)xj,y~=λyi+(1−λ)yj,λ∼Beta(α,α)\tilde{\mathbf x}=\lambda\mathbf x_i+(1-\lambda)\mathbf x_j,\qquad\tilde{\mathbf y}=\lambda\mathbf y_i+(1-\lambda)\mathbf y_j,\qquad\lambda\sim\mathrm{Beta}(\alpha,\alpha)
With one-hot y\mathbf y, the targets become soft. As α→0\alpha\to0, λ\lambda concentrates on 00 and 11 and mixup turns back into ERM. Zhang et al. found α∈[0.1,0.4]\alpha\in[0.1,0.4] improved ImageNet accuracy over ERM while large α\alpha underfit, and that on CIFAR-10 with 20% of labels randomized, mixup with α=8\alpha=8 reached a best test error of 5.9% against 12.7% for ERM.
python
import numpy as np
import torch
import torch.nn.functional as F

def mixup_loss(model, x, y, alpha=0.2):
    lam = float(np.random.beta(alpha, alpha))
    perm = torch.randperm(x.size(0))          # pair each example with another in the batch
    logits = model(lam * x + (1 - lam) * x[perm])
    # cross-entropy is linear in the target, so this IS cross-entropy with the soft label
    return lam * F.cross_entropy(logits, y) + (1 - lam) * F.cross_entropy(logits, y[perm])

Pairs are formed within the mini-batch, with one λ\lambda per batch, as Zhang et al. suggested. The last line uses −∑ky~klog⁡qk=λ(−∑kyiklog⁡qk)+(1−λ)(−∑kyjklog⁡qk)-\sum_k\tilde y_k\log q_k=\lambda\big(-\sum_ky_{ik}\log q_k\big)+(1-\lambda)\big(-\sum_ky_{jk}\log q_k\big): no soft-label tensor is ever built.

Mixup is label-smoothed, perturbed ERM

DerivationProof
  1. Rmix(f)=1n2∑i,jEθ[ℓ(f(θxi+(1−θ)xj), θyi+(1−θ)yj)],θ∼Beta[12,1](α,α)R_{\text{mix}}(f)=\frac{1}{n^2}\sum_{i,j}\E_{\theta}\Big[\ell\big(f(\theta\mathbf x_i+(1-\theta)\mathbf x_j),\ \theta\mathbf y_i+(1-\theta)\mathbf y_j\big)\Big],\qquad\theta\sim\mathrm{Beta}_{[\frac12,1]}(\alpha,\alpha)

    The mixup risk averages over ordered pairs (i,j)(i,j) and λ∼Beta(α,α)\lambda\sim\mathrm{Beta}(\alpha,\alpha). The Beta distribution is symmetric about 12\tfrac12, so the half of the mass with λ<12\lambda<\tfrac12 becomes the other half after renaming λ→1−λ\lambda\to1-\lambda and swapping i↔ji\leftrightarrow j. Hence one may always put the larger weight on the first example:

  2. θxi+(1−θ)xj=xˉ+θˉ(xi−xˉ)⏟x~i+(θ−θˉ)xi+(1−θ)xj−(1−θˉ)xˉ⏟δi\theta\mathbf x_i+(1-\theta)\mathbf x_j=\underbrace{\bar{\mathbf x}+\bar\theta(\mathbf x_i-\bar{\mathbf x})}_{\tilde{\mathbf x}_i}+\underbrace{(\theta-\bar\theta)\mathbf x_i+(1-\theta)\mathbf x_j-(1-\bar\theta)\bar{\mathbf x}}_{\boldsymbol\delta_i}

    Add and subtract to split each mixed input into its shrunk version and the rest; the identity is exact:

  3. Eθ,j[δi]=(θˉ−θˉ)xi+(1−θˉ)xˉ−(1−θˉ)xˉ=0\E_{\theta,j}[\boldsymbol\delta_i]=(\bar\theta-\bar\theta)\mathbf x_i+(1-\bar\theta)\bar{\mathbf x}-(1-\bar\theta)\bar{\mathbf x}=\mathbf 0

    Since θ\theta and jj are independent, E[θ]=θˉ\E[\theta]=\bar\theta and Ej[xj]=xˉ\E_j[\mathbf x_j]=\bar{\mathbf x}, the perturbation has mean zero; the labels work identically:

Read off the consequences. With one-hot labels and KK balanced classes, yˉ=1/K\bar{\mathbf y}=\mathbf 1/K, so the shrunk target is θˉ yi+(1−θˉ)/K\bar\theta\,\mathbf y_i+(1-\bar\theta)/K: label smoothing with ε=1−θˉ\varepsilon=1-\bar\theta. The inputs are shrunk toward the data mean by the same factor, so Carratino et al. recommend applying x↦xˉ+θˉ(x−xˉ)\mathbf x\mapsto\bar{\mathbf x}+\bar\theta(\mathbf x-\bar{\mathbf x}) to test inputs too, and undoing the label shrinkage on the outputs, a one-line change they found improves both accuracy and calibration. On top of that sit the zero-mean perturbations, correlated between input and label, which they show act as further regularizers, among them a reduction of the estimator's Lipschitz constant.

Predict before you look

With α=1\alpha=1, λ\lambda is uniform on [0,1][0,1], so θ\theta is uniform on [12,1][\tfrac12,1]. What label-smoothing strength ε=1−θˉ\varepsilon=1-\bar\theta does mixup imply for balanced classes?

Show the answer

0.25

θˉ=34\bar\theta=\tfrac34, so ε=14\varepsilon=\tfrac14: much stronger than the ε=0.1\varepsilon=0.1 typical of explicit label smoothing. Smaller α\alpha smooths less: numerically, θˉ≈0.84\bar\theta\approx0.84 for α=0.4\alpha=0.4 and ≈0.90\approx0.90 for α=0.2\alpha=0.2, i.e. ε≈0.16\varepsilon\approx0.16 and 0.100.10.

A second-order view, and a surprise

R1=E[1−λ~]n∑i(h′(f(xi))−yi) ∇f(xi)⊤Er[r−xi]R2=E[(1−λ~)2]2n∑ih′′(f(xi)) ∇f(xi)⊤Er[(r−xi)(r−xi)⊤]∇f(xi)R3=E[(1−λ~)2]2n∑i(h′(f(xi))−yi) Er[(r−xi)⊤∇2f(xi) (r−xi)]\begin{aligned}R_1&=\frac{\E[1-\tilde\lambda]}{n}\sum_i\big(h'(f(\mathbf x_i))-y_i\big)\,\nabla f(\mathbf x_i)^\top\E_{\mathbf r}[\mathbf r-\mathbf x_i]\\R_2&=\frac{\E[(1-\tilde\lambda)^2]}{2n}\sum_ih''(f(\mathbf x_i))\,\nabla f(\mathbf x_i)^\top\E_{\mathbf r}\big[(\mathbf r-\mathbf x_i)(\mathbf r-\mathbf x_i)^\top\big]\nabla f(\mathbf x_i)\\R_3&=\frac{\E[(1-\tilde\lambda)^2]}{2n}\sum_i\big(h'(f(\mathbf x_i))-y_i\big)\,\E_{\mathbf r}\big[(\mathbf r-\mathbf x_i)^\top\nabla^2f(\mathbf x_i)\,(\mathbf r-\mathbf x_i)\big]\end{aligned}
R2R_2 penalizes the input gradient along directions toward other training points, weighted by the loss curvature h′′h'': for the logistic loss h′′=q(1−q)h''=q(1-q), so confident predictions pay little, just as in dropout's Fisher-weighted penalty. R1R_1 and R3R_3 are weighted by the residual h′(f)−yh'(f)-y and fade as the fit improves. Zhang et al. also show that the mixup loss approximately upper-bounds an adversarial loss, consistent with mixup's robustness to single-step attacks such as FGSM.
DerivationMixup does nothing to linear least squares
  1. y~−f(x~)=λei+(1−λ)ej\tilde y-f(\tilde{\mathbf x})=\lambda e_i+(1-\lambda)e_j

    Take f(x)=w⊤x+bf(\mathbf x)=\mathbf w^\top\mathbf x+b with squared loss and residuals ei=yi−f(xi)e_i=y_i-f(\mathbf x_i). Because ff is affine and inputs and labels are mixed with the same weight, the mixed residual is exactly the mix of residuals:

  2. Lmix=12(1−2κ) e2‾+κ eˉ 2=(12−κ)Var⁡(e)+12 eˉ 2L_{\text{mix}}=\tfrac12(1-2\kappa)\,\overline{e^2}+\kappa\,\bar e^{\,2}=\big(\tfrac12-\kappa\big)\Var(e)+\tfrac12\,\bar e^{\,2}

    Average 12(λei+(1−λ)ej)2\frac12(\lambda e_i+(1-\lambda)e_j)^2 over independent uniform i,ji,j and λ∼Beta(α,α)\lambda\sim\mathrm{Beta}(\alpha,\alpha), using E[λ2]+E[(1−λ)2]=1−2κ\E[\lambda^2]+\E[(1-\lambda)^2]=1-2\kappa with κ=E[λ(1−λ)]=α2(2α+1)\kappa=\E[\lambda(1-\lambda)]=\frac{\alpha}{2(2\alpha+1)}, and write eˉ\bar e and Var⁡(e)\Var(e) for the mean and variance of the residuals over the training set:

  3. The intercept can make eˉ=0\bar e=0 without changing Var⁡(e)\Var(e), which depends on w\mathbf w only, so the minimizer is the ordinary least-squares solution: mixup merely rescales the objective, as a direct numerical check on simulated data confirms. In the expansion above, R1+R2R_1+R_2 collapse to −κVar⁡(e)-\kappa\Var(e) plus a constant. Mixup's regularization must therefore come from a curved loss (h′′h'' varying, as for cross-entropy) or a nonlinear ff, not from the blending of inputs as such.

Check your understanding

You fit a linear regression with an intercept by minimizing the mixup loss with α=1\alpha=1 instead of the ordinary squared loss. How does the fitted slope compare with ordinary least squares?

  1. It is shrunk toward zero, as in ridge regression
  2. It is shrunk by the factor θˉ=34\bar\theta=\tfrac34
  3. It depends on which pairs happened to be mixed
  4. It is exactly the least-squares slope
Show the answer

D. It is exactly the least-squares slope

The mixup loss equals (12−κ)Var⁡(e)+12eˉ2(\frac12-\kappa)\Var(e)+\frac12\bar e^2, minimized by the least-squares slope with the intercept setting eˉ=0\bar e=0. Shrinking inputs and labels by the same factor leaves a regression slope unchanged.

Boundaries, calibration and representations

Zhang et al. (2018) described mixup as encouraging the model "to behave linearly in-between training examples": predictions along a segment between two training points are pulled toward the straight-line blend of their labels, which smooths decision boundaries and lowers confidence away from the data.

Calibration. Thulasidasan et al. (2019) found networks trained with mixup significantly better calibrated than standard training, on benchmarks up to ImageNet, and less prone to confident predictions on out-of-distribution and random-noise inputs. Their ablations point at the labels: mixing the inputs while keeping hard labels did not give the same benefit, so the label smoothing inside mixup, which the Carratino decomposition makes explicit, plays a significant role.

Manifold mixup (Verma et al., 2019) mixes hidden representations at a randomly chosen layer instead of raw inputs. In their experiments it flattened class representations and smoothed boundaries further, and improved generalization and robustness to single-step adversarial attacks over input mixup, Cutout and dropout.

Manifold intrusion (Guo, Mao & Zhang, 2019) is the failure mode. A blend of two examples can land on, or very near, a real example of a third class, yet it carries a soft label that contradicts that example. Such conflicts over-regularize and cause underfitting; their AdaMixUp learns the mixing policy to avoid them. It is one reason a moderate α\alpha usually works best.

Check your understanding

Thulasidasan et al. (2019) found mixup-trained networks better calibrated. Their ablation mixed the inputs but kept hard (one-hot) labels. What did it show?

  1. Mixing inputs alone gave the full calibration benefit
  2. Mixing inputs alone did not give the same benefit: the soft, smoothed labels matter
  3. Calibration got worse with mixup in every setting
  4. Calibration depends only on the network architecture
Show the answer

B. Mixing inputs alone did not give the same benefit: the soft, smoothed labels matter

Without label mixing the calibration gains largely disappeared. That fits the decomposition: mixup includes label smoothing toward the mean label, and smoothed targets discourage overconfident logits.

CutMix, and mixing in practice

CutMix (Yun et al., 2019) mixes by cutting and pasting: a rectangle from xj\mathbf x_j replaces the same region of xi\mathbf x_i, and the label is mixed in proportion to area. With a binary mask MM that is 00 inside the box:

x~=M⊙xi+(1−M)⊙xj,y~=λyi+(1−λ)yj,λ=1−box areaW H\tilde{\mathbf x}=M\odot\mathbf x_i+(\mathbf 1-M)\odot\mathbf x_j,\qquad\tilde{\mathbf y}=\lambda\mathbf y_i+(1-\lambda)\mathbf y_j,\qquad\lambda=1-\frac{\text{box area}}{W\,H}
Yun et al. sampled λ\lambda uniformly (α=1\alpha=1), placed the box center uniformly in the image, and gave the box sides W1−λW\sqrt{1-\lambda} and H1−λH\sqrt{1-\lambda}; their reference algorithm recomputes λ\lambda from the area actually pasted after clipping at the image border. Unlike Cutout, no pixels are wasted on blanks; unlike mixup, every region stays a natural image patch.
Predict before you look

CutMix pastes a 12×1612\times16-pixel patch of a cat image into a 32×3232\times32 dog image. What weight does the cat label get in the soft target?

Show the answer

0.1875

The patch covers 12×16=19212\times16=192 of the 10241024 pixels, so the cat gets 192/1024=0.1875192/1024=0.1875 and the dog 0.81250.8125. The weight follows the area, whether or not the patch happens to contain much of the cat, a known weakness that later variants try to fix by weighting with saliency. Clipping has a second, quieter effect: since the box center is uniform and boxes near the border are cut off, the pasted area is on average smaller than 1−λ1-\lambda, and the source keeps about two thirds of the label rather than half (a Monte Carlo estimate in the continuous square gives 68%).

In practice the two are combined. DeiT trained vision transformers on ImageNet alone with a recipe that included RandAugment, mixup, CutMix and random erasing among its regularizers (Touvron et al., 2021). Mixing is most established for classification; for regression, structured outputs or text, what a "blend" of two examples means is less obvious, and mixing is usually done on embeddings or hidden states, if at all.

Key takeaways

  • Mixup is vicinal risk minimization with x~=λxi+(1−λ)xj\tilde{\mathbf x}=\lambda\mathbf x_i+(1-\lambda)\mathbf x_j, y~=λyi+(1−λ)yj\tilde{\mathbf y}=\lambda\mathbf y_i+(1-\lambda)\mathbf y_j, λ∼Beta(α,α)\lambda\sim\mathrm{Beta}(\alpha,\alpha); because cross-entropy is linear in the target, the mixed loss is the λ\lambda-weighted sum of two ordinary losses.
  • Mixup's risk equals ERM on data shrunk toward the mean by θˉ=E[θ]\bar\theta=\E[\theta], θ∼Beta[12,1](α,α)\theta\sim\mathrm{Beta}_{[\frac12,1]}(\alpha,\alpha), plus zero-mean perturbations: label smoothing with ε=1−θˉ\varepsilon=1-\bar\theta (14\tfrac14 at α=1\alpha=1) and input noise (Carratino et al., 2022).
  • To second order, mixup adds curvature-weighted penalties on input gradients and Hessians (Zhang et al., 2021); for linear least squares with an intercept it changes nothing, so its effect comes from curved losses and nonlinear models.
  • Mixup tends to improve calibration, largely through its soft labels (Thulasidasan et al., 2019); manifold mixup mixes hidden states, and manifold intrusion, blends colliding with real examples of other classes, can cause underfitting.
  • CutMix pastes a box and weights the labels by area; modern image recipes such as DeiT's combine it with mixup, RandAugment and random erasing.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

What happens to mixup as α→0\alpha\to0?

  1. λ\lambda concentrates on 00 and 11, and mixup reduces to ordinary empirical risk minimization
  2. λ\lambda concentrates near 12\tfrac12, so every example is an even blend
  3. The labels become uniform over the classes
  4. Mixup becomes equivalent to adding Gaussian input noise
Show the answer

A. λ\lambda concentrates on 00 and 11, and mixup reduces to ordinary empirical risk minimization

Beta(α,α)\mathrm{Beta}(\alpha,\alpha) with small α\alpha is U-shaped and piles its mass at the endpoints, so each "mixed" example is almost exactly one training example with its own label.

Question 2

According to Carratino et al. (2022), mixup with balanced one-hot labels trains on which targets, before the zero-mean perturbations?

  1. The original one-hot targets
  2. Uniform targets 1/K\mathbf 1/K
  3. Smoothed targets θˉ yi+(1−θˉ)/K\bar\theta\,\mathbf y_i+(1-\bar\theta)/K, with θˉ\bar\theta the mean of θ∼Beta[12,1](α,α)\theta\sim\mathrm{Beta}_{[\frac12,1]}(\alpha,\alpha)
  4. Targets drawn at random from the training labels
Show the answer

C. Smoothed targets θˉ yi+(1−θˉ)/K\bar\theta\,\mathbf y_i+(1-\bar\theta)/K, with θˉ\bar\theta the mean of θ∼Beta[12,1](α,α)\theta\sim\mathrm{Beta}_{[\frac12,1]}(\alpha,\alpha)

Shrinking every label toward the label mean by θˉ\bar\theta is label smoothing with ε=1−θˉ\varepsilon=1-\bar\theta; for α=1\alpha=1, ε=14\varepsilon=\tfrac14.

Question 3

What is "manifold intrusion" in mixup?

  1. Mixed examples that lie far from every training example
  2. Hidden representations collapsing onto a low-dimensional manifold
  3. Mixed examples leaking from the training set into the test set
  4. A blend of two examples that lands on or near a real example of another class, while carrying a soft label that contradicts it
Show the answer

D. A blend of two examples that lands on or near a real example of another class, while carrying a soft label that contradicts it

Guo, Mao and Zhang (2019) showed that such conflicting labels over-regularize and can cause underfitting, and proposed learning the mixing policy to avoid them.

Question 4

How does CutMix differ from Cutout?

  1. CutMix fills the removed region with a patch from another image and mixes the labels by area; Cutout blanks the region and keeps the label
  2. CutMix blends all pixels of two images; Cutout blends only a rectangle
  3. They are the same method under two names
  4. CutMix keeps the original label; Cutout mixes the labels
Show the answer

A. CutMix fills the removed region with a patch from another image and mixes the labels by area; Cutout blanks the region and keeps the label

Both remove a rectangle of the image. Cutout leaves a blank, so those pixels carry no information; CutMix fills them with another training image and makes the label say so, in proportion to the area.

Further reading

  • H. Zhang, M. Cisse, Y. N. Dauphin & D. Lopez-Paz, "mixup: beyond empirical risk minimization," ICLR (2018).
  • O. Chapelle, J. Weston, L. Bottou & V. Vapnik, "Vicinal risk minimization," NeurIPS (2000; proceedings 2001).
  • L. Carratino, M. Cissé, R. Jenatton & J.-P. Vert, "On mixup regularization," Journal of Machine Learning Research 23 (2022).
  • L. Zhang, Z. Deng, K. Kawaguchi, A. Ghorbani & J. Zou, "How does mixup help with robustness and generalization?," ICLR (2021).
  • S. Thulasidasan, G. Chennupati, J. Bilmes, T. Bhattacharya & S. Michalak, "On mixup training: improved calibration and predictive uncertainty for deep neural networks," NeurIPS (2019).
  • S. Yun, D. Han, S. J. Oh, S. Chun, J. Choe & Y. Yoo, "CutMix: regularization strategy to train strong classifiers with localizable features," ICCV (2019).