Search

Search papers and lessons

mixup: Beyond Empirical Risk Minimization

Hongyi Zhang, Moustapha Cisse, Yann N. Dauphin et al. · 2017

Zhang and colleagues (2017) train a network on random blends of pairs of examples, mixing inputs and labels with one weight, which improves accuracy and robustness without domain knowledge.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Discrete math
  • Learning basics
  • Losses & regularization
  • Inference
  • ReadReady to readNot yet

TL;DR

Mixup is a training rule proposed by Hongyi Zhang, Moustapha Cisse, Yann Dauphin and David Lopez-Paz in 2017. Instead of training a network on the examples exactly as given, it trains on new examples made by blending two real examples: their inputs are averaged with a random weight, and their labels are averaged with the same weight. The paper shows that this simple change improves test accuracy on image, speech and tabular data, and makes the network less fragile to adversarial examples and less able to memorize random labels. It needs no knowledge of the data, only a few lines of code.

Builds on: Empirical Risk Minimization, Data Augmentation, Vicinal Risk Minimization, One-Hot Encoding, Beta Distribution, Linear Interpolation · Leads to: the robustness questions in Adversarial Examples and Out-of-Distribution Generalization, which the paper tests directly

Why it mattered

The method is cheap: it works with many architectures, and the change is a few lines in a training loop. The paper reports gains on image, speech and tabular benchmarks with the same recipe. The paper's central idea, that a model should behave in a simple, straight-line way between its training examples, also gave the field a clear target to test other methods against. The paper itself compares mixup with dropout, label smoothing, and a family of nearest-neighbour interpolation schemes, which made the trade-offs between these methods easier to see.

The problem

Training a model by empirical risk minimization (Empirical Risk Minimization, ERM) means choosing the model that does best on the training examples. The paper states the premise plainly: a large neural network trained this way can simply memorize its training set. ERM also gives no guidance about what the model should do between the examples it has seen.

Two symptoms follow. First, networks memorize: on classification problems where labels are assigned at random, large networks still fit the training data, so a low training error says nothing about generalization. Second, networks are fragile: small, deliberately chosen changes to an input can flip the prediction. Such inputs are called Adversarial Examples. The paper's diagnosis is that ERM does not say anything about the space between training points, and that the space in between is where the trouble sits.

The standard remedy is Data Augmentation: make new training examples by transforming old ones, such as flipping or cropping an image. The paper points to two limits. Augmentation needs expert knowledge about which transformations keep the label, and it assumes that neighbours share a class, so it cannot describe a neighbourhood that crosses two classes.

How it works

Take a tiny two-class problem. Each input is a vector of two numbers, and each label is a one-hot vector: class A is (1, 0) and class B is (0, 1) (see One-Hot Encoding). Two training examples are:

  • Example i: input x_i = (1, 0), label y_i = (1, 0) (class A).
  • Example j: input x_j = (0, 1), label y_j = (0, 1) (class B).

Mixup builds one new example from them.

  1. Draw a weight. Sample λ between 0 and 1. The paper draws it from a Beta Distribution with both parameters equal to α, written Beta(α, α). Say λ = 0.7. Check: 0.7 is between 0 and 1.
  2. Blend the inputs. Compute x̃ = λ·x_i + (1 − λ)·x_j. Here that is 0.7·(1, 0) + 0.3·(0, 1) = (0.7, 0.3). Check: the result lies on the straight line between (1, 0) and (0, 1), 70% of the way toward the first.
  3. Blend the labels with the same weight. Compute ỹ = λ·y_i + (1 − λ)·y_j, which gives (0.7, 0.3). Check: the entries still add up to 1, so the label is still a valid probability vector.
  4. Train on the new pair. The network sees x̃ and is asked to predict ỹ. A network that predicts class A with probability 0.7 and class B with 0.3 on this input has done exactly what was asked.
  5. Repeat with a fresh draw. Choose another partner j at random, and another λ. If λ = 0.2, the new example is x̃ = (0.2, 0.8) with label (0.2, 0.8). Check: the input and the label move together.

The paper's observation is that the network now learns the same relationship for every blend: the label moves along the same line as the input does. Between two training points, the model is asked to change its answer gradually rather than jump.

flowchart LR
  A[Draw two training pairs] --> B[Draw lambda from Beta a,a]
  B --> C[Mix inputs: lambda x_i + 1-lambda x_j]
  B --> D[Mix labels with the same lambda]
  C --> E[Forward pass on the mixed input]
  D --> F[Loss against the mixed label]
  E --> F
  F --> G[Gradient step]

The paper also says what happens at the extreme: as α goes to 0, the Beta distribution puts almost all of its mass at 0 and 1, so almost every blend is just one of the two original examples, and mixup reduces to ordinary ERM.

00.20.40.60.8100.511.5λdensity
  • α = 1 (flat)
  • α = 2
The density of λ for two values of α. At α = 1 every blend weight is equally likely; at α = 2 blends near 0.5 are more common. Smaller α pushes the mass toward 0 and 1, where mixup changes the example least.

The math

Start from the quantity we want to minimize. For a model f, a loss ℓ that scores a prediction against a target, and the data distribution P, the expected risk is R(f) = ∫ ℓ(f(x), y) dP(x, y). We never know P, so we replace it with the empirical distribution of our n training pairs, which puts mass 1/n on each pair. This gives the empirical risk R_δ(f) = (1/n) Σ ℓ(f(x_i), y_i), which is ERM (Empirical Risk Minimization).

Step 2 in symbols: a vicinity around each example

Vicinal risk minimization (Vicinal Risk Minimization) replaces the point mass at each training pair with a distribution ν over nearby pairs, P_ν(x̃, ỹ) = (1/n) Σ ν(x̃, ỹ | x_i, y_i). A common choice adds Gaussian noise to the input and keeps the label. Training then uses m sampled virtual pairs (x̃_k, ỹ_k) and minimizes the average of ℓ(f(x̃_k), ỹ_k).

Step 3 in symbols: the mixup vicinity

The paper's vicinity moves an example toward a second random example and blends the label too. For a pair (x_i, y_i),

x̃ = λ x_i + (1 − λ) x_j, and ỹ = λ y_i + (1 − λ) y_j,

where (x_j, y_j) is drawn at random from the training set and λ ∼ Beta(α, α) with α in (0, ∞). Every symbol in this pair is a vector: x is the input, y the one-hot label, and λ a single number in [0, 1]. Because λ and 1 − λ add to 1, the mixed label is still a probability vector whenever both labels are one-hot.

Step 4 in symbols: the training loss

For a classifier with softmax outputs p = f(x̃) and the cross-entropy loss (Cross-Entropy Loss), the mixed example contributes

ℓ(f(x̃), ỹ) = − Σ_k ỹ_k log p_k.

Substitute ỹ = λ y_i + (1 − λ) y_j and the loss becomes λ · (− Σ_k y_{i,k} log p_k) + (1 − λ) · (− Σ_k y_{j,k} log p_k). That is a weighted sum of the two ordinary cross-entropies, with the same weights as the blend. Each step of the optimizer therefore pushes the model toward both labels in the proportions λ and 1 − λ.

Why α controls the strength

Let α be small. Then Beta(α, α) puts most of its mass near 0 and 1, so usually λ is close to 0 or 1 and the blend is nearly one of the original examples. Let α be large. Then λ is near 0.5 most of the time, and the blends are far from any single training example. The paper uses α = 1 for CIFAR, which makes λ uniform on [0, 1].

Worked examples

Example
1. Tiny: one blend by hand
  1. Inputs: x_i = 2 (class A, one-hot label (1, 0)) and x_j = 6 (class B, one-hot label (0, 1)), one number each.
  2. Draw λ = 0.25.
  3. Blended input: x̃ = 0.25·2 + 0.75·6 = 0.5 + 4.5 = 5.
  4. Blended label: ỹ = (0.25, 0.75), meaning 25% class A and 75% class B.
  5. Check: 0.25 + 0.75 = 1, so the label is valid.
  6. Interpretation: the new input sits three quarters of the way from 2 toward 6, and its label leans toward the class of the second example in the same proportion.
Example
2. Standard: a two-dimensional blend with α = 1
  1. Inputs: x_i = (2, 4) (class A, label (1, 0)) and x_j = (6, 0) (class B, label (0, 1)).
  2. Draw λ = 0.25 from Beta(1, 1), which is uniform on [0, 1], so every value of λ is equally likely.
  3. Blended input, first coordinate: 0.25·2 + 0.75·6 = 0.5 + 4.5 = 5.
  4. Blended input, second coordinate: 0.25·4 + 0.75·0 = 1 + 0 = 1.
  5. So x̃ = (5, 1).
  6. Blended label: ỹ = 0.25·(1, 0) + 0.75·(0, 1) = (0.25, 0.75).
  7. Suppose the network currently predicts p = (0.5, 0.5). Its loss on this mixed example is −(0.25·ln 0.5 + 0.75·ln 0.5) = −ln 0.5 ≈ 0.693.
  8. Check: 0.25 + 0.75 = 1, and the loss is ln 2 ≈ 0.693, which is the loss of a uniform guess over two classes. A uniform guess is the right baseline here, so the number is easy to sanity-check.
  9. Interpretation: the network is rewarded for leaning toward class B, in proportion to how much of the blend came from the B example, and is penalized for being unsure about the blend.
Example
3. Realistic: a pixel in two CIFAR-style images

Two training images from a ten-class image set are mixed. Image i shows a cat, image j shows a dog. The pixel at one position has value 200 in image i and 40 in image j, on a 0 to 255 scale. Draw λ = 0.6.

  1. Blended pixel: x̃ = 0.6·200 + 0.4·40 = 120 + 16 = 136.
  2. Blended label: ỹ gives weight 0.6 to "cat" and 0.4 to "dog", and 0 to the other eight classes.
  3. Check: 0.6 + 0.4 = 1, and the other entries are all 0, so the label is still a probability vector.
  4. The network sees one image whose pixels are the 60/40 blend of the two. A model that gives "cat" probability 0.6 and "dog" 0.4 on that image has the loss that a correct soft answer would give.
  5. If the network instead gives "cat" probability 0.9 and "dog" 0.1, the loss is −(0.6 ln 0.9 + 0.4 ln 0.1) = −(0.6·(−0.105) + 0.4·(−2.303)) ≈ 0.063 + 0.921 = 0.984. The network is penalized for being overconfident about a blend that was 40% dog.
  6. Interpretation: the model is not allowed to become certain between the two classes, which is the behaviour the paper calls linear behaviour between training points.

Try it: a three-class example. Input x_i = 0.9 (label A, (1, 0, 0)) and x_j = 0.3 (label B, (0, 1, 0)), with λ = 0.4. What are x̃ and ỹ? Answer: x̃ = 0.4·0.9 + 0.6·0.3 = 0.36 + 0.18 = 0.54, and ỹ = (0.4, 0.6, 0).

Common mistakes and tradeoffs

It is tempting to think mixup is just noise added to the inputs. In fact the label is blended too, and that is the part that matters. The paper's ablation table compares mixup with Gaussian noise added to inputs: noise at σ = 0.05 gives a median test error of 5.53 with weight decay 10⁻⁴, while mixup over all classes with random pairs gives 4.24 with the same setting. Noise changes the input without changing the target, which is a different thing.

It is tempting to think a bigger α is always better. In fact the paper finds α between 0.1 and 0.4 improves ImageNet accuracy over ERM, while large α causes underfitting on that task. The right value depends on the model and the training length.

It is tempting to think mixup works only for images. In fact the paper also reports gains on speech spectrograms, on tabular data from the UCI repository, and on stabilizing GAN training. The method uses no domain-specific transformation, so it applies wherever the inputs can be averaged sensibly.

When to use it: classifiers where ordinary augmentation is hard to design, or where the model is overconfident and memorizes noisy labels. When not to use it: regression and structured outputs. The paper says generalizing to regression is "straightforward" but does not test it, and calls application to structured prediction such as segmentation "less obvious". Alternatives include Label Smoothing, which shrinks targets toward uniform without mixing inputs, and classic augmentation when a domain-specific transformation is known to keep the label.

History

Before 2017, the standard way to make a network robust to small changes was augmentation with transformations chosen by hand: flipping, cropping and rotating images, as the paper notes. The vicinal risk framework by Chapelle and colleagues in 2000 gave this a formal name. The paper cites Szegedy et al. (2014) for small input changes flipping a network's answer, and Zhang et al. (2017) for networks fitting random labels; both motivated methods beyond augmentation. Mixup was posted in 2017 as an arXiv preprint by Zhang, Cissé, Dauphin and Lopez-Paz. It caught on because the method is one line long, needs no domain knowledge, and improved results on several benchmarks the community was already tracking.

Results

The paper reports its numbers as errors, so lower is better.

  • ImageNet-2012, 90 epochs (Table 1). ResNet-50 top-1 error: ERM 23.5, mixup (α = 0.2) 23.3. ResNeXt-101 64×4d: ERM 20.4 at 100 epochs, mixup (α = 0.4) 19.8 at 90 epochs. The gains at 90 epochs are small, about 0.2 to 0.6 points.
  • ImageNet-2012, 200 epochs (Table 1). ResNet-50 top-1 error: ERM 23.6, mixup 22.1. ResNet-101: ERM 22.0, mixup 20.8. The paper notes that longer training and higher-capacity models benefit more.
  • CIFAR-10 and CIFAR-100 (Figure 3). The paper says mixup "significantly outperform[s]" ERM for the three architectures tested. The exact values are in its figure, not in the text, so this lesson does not quote them.
  • Corrupted labels (Table 2, CIFAR-10, best test error). At 20% random labels: ERM 12.7, mixup (α = 8) 5.9. At 50%: ERM 18.8, mixup (α = 32) 11.3. At 80%: ERM 36.5, mixup (α = 32) 25.3. Adding dropout to mixup gives the lowest best-test error at 50% and 80% noise (10.9 and 24.0).
  • Adversarial examples (Table 3, ResNet-101, FGSM white-box). Top-1 error: ERM 90.7, mixup 75.2. Under the stronger I-FGSM attack both models fail (top-1 errors 99.9 and 99.6). The paper's text calls the FGSM white-box result 2.7 times more robust, a ratio of errors rather than a difference of 15.5 points.
  • Tabular data (Table 4). Arcene: ERM 57.6, mixup 48.0. Arrhythmia: ERM 56.6, mixup 46.3. Abalone: ERM 74.0, mixup 73.6.
  • Ablations (Table 5, CIFAR-10, median test error of the last 10 epochs). ERM 5.53; mixup over all classes with random pairs 4.24 at weight decay 10⁻⁴; mixing inputs without mixing labels 5.17 at the same setting; Gaussian noise (σ = 0.05) 5.53. The paper concludes that mixing both inputs and labels is what matters.

The limits are the paper's own. Its theory of why the training error on real data rises with α is not yet developed, and the authors say so. Its experiments use fixed hyperparameters from prior work, so the gains are relative to those baselines.

Reading guide

The paper is short; read it in this order.

Check yourself

QuestionRecall: what are the two quantities that mixup blends, and with what weight?Show answer

Mixup blends the inputs x_i and x_j and the labels y_i and y_j. Both use the same weight λ, which is drawn from a Beta(α, α) distribution. The blended input is λ x_i + (1 − λ) x_j, and the blended label is λ y_i + (1 − λ) y_j.

QuestionComputation: an input `x_i = 10` with label `(1, 0)` and `x_j = 4` with label `(0, 1)` are mixed with `λ = 0.5`. What is the blended input, and what is the blended label?Show answer

The blended input is 0.5·10 + 0.5·4 = 5 + 2 = 7. The blended label is (0.5, 0.5). Both entries add to 1, so the label is still a valid probability vector.

QuestionWhy: the paper says mixup behaves like ERM as α goes to 0. Why does that happen?Show answer

As α goes to 0, Beta(α, α) puts almost all of its probability at 0 and 1. A draw of λ near 1 makes the blend almost equal to x_i with label y_i, and a draw near 0 makes it almost equal to x_j with label y_j. Both are ordinary training examples, so the procedure reduces to training on the original data, which is ERM.

QuestionApplied: a team trains a classifier on sensor readings that are hard to transform in any meaningful way, and the model memorizes a noisy set of labels. Would mixup be a reasonable first thing to try, and what would they measure?Show answer

Mixup needs no domain knowledge, so it fits a setting where hand-designed augmentation is hard. The team should measure the same things the paper does: test error on held-out data, and training error on the real labels against a model trained on the noisy labels. A large gap between the two is the memorization the paper describes. They should also tune α, since the paper finds small values help and very large values can underfit.

Sources

Read the full paper

Hongyi Zhang, Moustapha Cisse, Yann N. Dauphin et al., 2017. arXiv:1710.09412