Search

Search papers and lessons

Randomized Smoothing

Level 4 · 12 min read

Randomized smoothing turns any classifier into one that votes over noisy copies of its input, and the vote margin gives a radius inside which no small change can flip the answer.

On this page

  • Colour = topic mix
  • Probability
  • Inference
  • Discrete math
  • Calculus
  • Linear algebra
  • Neural network basics
  • ReadReady to readNot yet

TL;DR

A classifier can be fooled by an input changed so slightly that a person would not notice, and most defences only make that harder to do. Randomized smoothing wraps any classifier in a vote: to classify an input, add random Gaussian noise many times, ask the base classifier about each noisy copy, and return the most common answer. The share of votes for the winning answer gives a guarantee: every input within a computed radius gets the same answer.

Builds on: Adversarial Examples, Gaussian (Normal) Distribution, Monte Carlo Method, Neyman–Pearson Lemma · Leads to: Adversarial Training

Where it's used

Certifying image classifiers. Cohen, Rosenfeld and Kolter (2019) used randomized smoothing to give certified robustness guarantees for classifiers on large image datasets, including ImageNet-scale models. A certificate is a statement such as "no change of size at most this radius in pixel space changes this prediction", which is stronger than the usual test-set accuracy under attack.

Safety reviews that need a guarantee. A team building a camera system that reads traffic signs may need to state how much sensor noise the output can tolerate. Smoothing turns that question into a number that can be checked, not a claim based on the attacks the team happened to try.

Making a model stable under noise. Smoothing is also a simple way to make predictions less sensitive to small random errors in the input, such as sensor noise, because the vote averages over many perturbed copies.

How it works

Take a one-dimensional classifier. Its input is a single number xx, and the base classifier ff answers class 1 when x≥0x \geq 0 and class 0 otherwise. Its boundary is at 0.

  1. Start with the base classifier. For the input x=0.3x = 0.3, ff answers class 1. An attacker who can move the input by 0.4 can reach x=−0.1x = -0.1, and then ff answers class 0. The base classifier has no safety margin built in.
  2. Add noise to the input. Draw a random number δ\delta from a Gaussian with spread σ=1\sigma = 1, and look at the noisy input x+δx + \delta. Repeat this many times.
  3. Let the copies vote. Each noisy copy gets the base classifier's answer. The smoothed classifier returns the class that wins the most votes. For x=0.3x = 0.3, a copy votes class 1 exactly when 0.3+δ≥00.3 + \delta \geq 0, which happens with probability Φ(0.3)=0.618\Phi(0.3) = 0.618, where Φ\Phi is the standard normal cumulative distribution, the chance that a standard normal value is at most the number given.
  4. Measure the vote share. Monte Carlo sampling estimates the share: about 62 percent of the copies vote class 1, so the smoothed answer is class 1 and the runner-up gets about 38 percent.
  5. Turn the vote share into a radius. Here Φ−1\Phi^{-1} runs the lookup backwards: Φ−1(0.618)=0.3\Phi^{-1}(0.618) = 0.3, because Φ(0.3)=0.618\Phi(0.3) = 0.618. With pA=0.618p_A = 0.618 for the winning class and pB=0.382p_B = 0.382 for the runner-up, the certified radius is R=12(Φ−1(pA)−Φ−1(pB))=12(0.3+0.3)=0.3R = \frac{1}{2}\left(\Phi^{-1}(p_A) - \Phi^{-1}(p_B)\right) = \frac{1}{2}(0.3 + 0.3) = 0.3. Every input within 0.3 of x=0.3x = 0.3 keeps the answer class 1. Check: the distance from 0.3 to the boundary at 0 is 0.3, so the radius here is exactly the distance to the boundary.

The certificate is honest but limited. The attacker's point at x=−0.1x = -0.1 is 0.4 away, outside the radius, and the smoothed answer there really is class 0, because Φ(−0.1)=0.46\Phi(-0.1) = 0.46 is below one half. The radius only promises what it can prove.

−3−2−1012300.20.40.60.81input xshare of votes for class 1
  • smoothed probability of class 1, sigma = 1
The vote share crosses one half at the old boundary, and the margin at x = 0.3 is what the certificate uses.

The math

Definition: Randomized smoothing

Given a base classifier ff that maps an input to a class, the smoothed classifier is

g(x)=arg⁡max⁡c  Pδ(f(x+δ)=c),δ∼N(0,σ2I)g(x) = \arg\max_{c} \; P_{\delta}\left(f(x + \delta) = c\right), \qquad \delta \sim \mathcal{N}(0, \sigma^2 I)

It returns the class that ff outputs most often on Gaussian-noised copies of xx. The spread σ\sigma is chosen in advance.

Step 2 in symbols: the vote shares

Write pc(x)=Pδ(f(x+δ)=c)p_c(x) = P_{\delta}\left(f(x + \delta) = c\right) for the probability that a noisy copy gets class cc. These numbers add to 1 over all classes. Let cAc_A be the class with the largest pc(x)p_c(x), with value pAp_A, and let cBc_B be the runner-up, with value pBp_B.

Step 3 in symbols: smoothing is a convolution

The probability above is an average of the base classifier's answers over a Gaussian bump centred at xx. That is the Convolution of the indicator "ff answers cc" with the Gaussian density (a convolution is a weighted average of one function, slid along another), so the smoothed probability pc(x)p_c(x) is a smoothed version of the base classifier's decision regions. Sharp boundaries become gradual, and the width of the smoothing is σ\sigma.

Step 4 in symbols: the Monte Carlo estimate

Draw nn noisy copies, count how many vote for each class, and estimate p^A\hat{p}_A. These counts follow a binomial distribution. To make a guarantee, use a one-sided lower confidence bound p‾A\underline{p}_A on pAp_A and an upper bound p‾B\overline{p}_B on pBp_B, such as a Clopper–Pearson confidence interval, which is an exact interval for a proportion computed from the vote counts. The bounds must hold with high probability, which is why the estimate is an estimate with a stated confidence and not a single number. The Monte Carlo Method supplies the samples, and the bound supplies the safety margin.

Step 5 in symbols: the certified radius

If p‾A>p‾B\underline{p}_A > \overline{p}_B, then g(x+ε)=cAg(x + \varepsilon) = c_A for every attack change ε\varepsilon with ∥ε∥2<R\lVert \varepsilon \rVert_2 < R, where

R=σ2(Φ−1(p‾A)−Φ−1(p‾B))R = \frac{\sigma}{2} \left( \Phi^{-1}(\underline{p}_A) - \Phi^{-1}(\overline{p}_B) \right)

Here Φ\Phi is the standard normal cumulative distribution and Φ−1\Phi^{-1} its inverse, and ∥ε∥2\lVert \varepsilon \rVert_2 is the Euclidean size of the change, its straight-line length (see Vector Norm). The letter δ\delta stays reserved for the random noise. The guarantee is for changes measured in the Euclidean sense, and the radius grows with σ\sigma and with the gap between the two vote shares.

The reason is a most-powerful-test argument. Shifting a Gaussian by a vector of length rr moves its probability mass across a half-space (the region on one side of a flat boundary) by at most Φ(Φ−1(p)+r/σ)\Phi(\Phi^{-1}(p) + r/\sigma), where pp is the mass before the shift. That bound is tight for half-spaces, so the worst case for the winning class is at least Φ(Φ−1(pA)−r/σ)\Phi(\Phi^{-1}(p_A) - r/\sigma), and the worst case for the runner-up is at most Φ(Φ−1(pB)+r/σ)\Phi(\Phi^{-1}(p_B) + r/\sigma). The winning class stays ahead as long as Φ−1(pA)−r/σ>Φ−1(pB)+r/σ\Phi^{-1}(p_A) - r/\sigma > \Phi^{-1}(p_B) + r/\sigma, and solving for rr gives the formula above.

Worked examples

Example
Example 1: one certificate with round numbers

Take σ=1\sigma = 1, p‾A=0.9\underline{p}_A = 0.9 and p‾B=0.1\overline{p}_B = 0.1. Find the certified radius.

  1. Look up the inverse normal values: Φ−1(0.9)=1.2816\Phi^{-1}(0.9) = 1.2816 and Φ−1(0.1)=−1.2816\Phi^{-1}(0.1) = -1.2816. They are equal and opposite, because 0.1 and 0.9 are symmetric around one half.
  2. Take the difference: 1.2816−(−1.2816)=2.56311.2816 - (-1.2816) = 2.5631.
  3. Multiply by σ/2=0.5\sigma / 2 = 0.5: R=0.5×2.5631=1.2816R = 0.5 \times 2.5631 = 1.2816.
  4. Check the condition: p‾A=0.9>p‾B=0.1\underline{p}_A = 0.9 > \overline{p}_B = 0.1, so the certificate applies.

Interpretation: with 90 percent of the votes for the winning class and 10 percent for the runner-up, every input within a Euclidean distance of about 1.28 keeps its answer.

Example
Example 2: the standard case with a smaller spread

Take σ=0.5\sigma = 0.5, p‾A=0.99\underline{p}_A = 0.99 and p‾B=0.01\overline{p}_B = 0.01. Find the certified radius and compare it with Example 1.

  1. Look up the inverse normal values: Φ−1(0.99)=2.3263\Phi^{-1}(0.99) = 2.3263 and Φ−1(0.01)=−2.3263\Phi^{-1}(0.01) = -2.3263.
  2. Take the difference: 2.3263−(−2.3263)=4.65272.3263 - (-2.3263) = 4.6527.
  3. Multiply by σ/2=0.25\sigma / 2 = 0.25: R=0.25×4.6527=1.1632R = 0.25 \times 4.6527 = 1.1632.
  4. Compare: R=1.16R = 1.16 is smaller than the 1.28 of Example 1, even though the vote share is more lopsided.

Interpretation: a smaller spread gives a more lopsided vote but a smaller radius, because the radius scales with σ\sigma. The vote share cannot make up for a small spread.

Example
Example 3: choosing the spread on a realistic classifier

A team tests three spreads on the same image classifier, with the Monte Carlo lower bound on the winning class's share reported for each. Assume p‾B=1−p‾A\overline{p}_B = 1 - \underline{p}_A, as in a two-class vote. The results are: σ=0.25\sigma = 0.25 with p‾A=0.99\underline{p}_A = 0.99, σ=0.5\sigma = 0.5 with p‾A=0.90\underline{p}_A = 0.90, and σ=1.0\sigma = 1.0 with p‾A=0.60\underline{p}_A = 0.60. Which spread gives the largest certified radius?

  1. For σ=0.25\sigma = 0.25, with p‾B=1−0.99=0.01\overline{p}_B = 1 - 0.99 = 0.01: R=σΦ−1(0.99)=0.25×2.3263=0.5816R = \sigma \Phi^{-1}(0.99) = 0.25 \times 2.3263 = 0.5816.
  2. For σ=0.5\sigma = 0.5: R=0.5×1.2816=0.6408R = 0.5 \times 1.2816 = 0.6408.
  3. For σ=1.0\sigma = 1.0: R=1.0×Φ−1(0.60)=1.0×0.2533=0.2533R = 1.0 \times \Phi^{-1}(0.60) = 1.0 \times 0.2533 = 0.2533.
  4. Compare: the middle spread gives the largest radius, 0.64, the small spread gives 0.58, and the large spread gives 0.25.
  5. Interpret: a large spread blurs the classifier so much that the vote share falls, and a small spread keeps the vote lopsided but gives little radius to work with. The best spread sits between the two extremes, and the team tunes it on held-out data.
  6. Try it: suppose a later run with σ=2.0\sigma = 2.0 reports p‾A=0.75\underline{p}_A = 0.75. What is the certified radius? Answer: R=2.0×Φ−1(0.75)=2.0×0.6745=1.349R = 2.0 \times \Phi^{-1}(0.75) = 2.0 \times 0.6745 = 1.349, so about 1.35, which beats all three spreads above.

Common mistakes and tradeoffs

It is tempting to think that adding noise at test time makes a classifier robust. In fact one noisy forward pass gives no guarantee. The certificate needs the vote over many copies, the lower bound on the winning share, and the upper bound on the runner-up. Without those, the answer is only a guess that happens to be stable.

It is tempting to think that the radius protects against every attack. In fact it covers only changes measured in the Euclidean sense, of size below RR. Attacks that are larger, or that use a different norm such as the largest single pixel change, fall outside the guarantee, and a different analysis is needed for them.

It is tempting to think that a larger spread is always safer. In fact Example 3 shows the tradeoff: a larger spread can raise the radius at first and then destroy the vote share, and the clean accuracy of the smoothed classifier usually drops as the spread grows, since the base classifier sees heavily distorted inputs.

It is tempting to think that the certificate is cheap. In fact each prediction needs many base-classifier calls, and the bounds get tighter only with more samples. That cost is the main practical limit.

When to use it: when a provable Euclidean robustness radius is worth the extra computation and some loss of clean accuracy, and when the base classifier is accurate under moderate noise. When not to use it: when attacks come from a different norm or the cost of many forward passes is too high. Alternatives include Adversarial Training, which trains on attacked inputs and gives empirical rather than certified robustness, and methods that bound the model's Lipschitz constant, which give certificates but often at a cost in accuracy.

History

Researchers had known since 2013 that small, carefully chosen changes can flip a neural network's answer, and most early defences were tested only against the attacks their authors tried, which later attacks broke. Certified guarantees were the goal, but exact bounds for deep networks were hard to compute.

Work in 2018 and 2019 showed that adding noise to the input could give guarantees for the smoothed classifier, borrowing ideas from differential privacy, a framework for limiting what a noisy output reveals. Cohen, Rosenfeld and Kolter (2019) gave the tight Euclidean radius formula above, and their method caught on because it scaled to large image models and was simple to implement: wrap any classifier, sample, and bound.

Recap

  • An adversarial example is a small change that flips a classifier's answer, so a defence needs a guarantee, not a test score.
  • The smoothed classifier votes over Gaussian-noised copies of the input, so its decision regions are smooth.
  • The vote share, with a confidence bound on it, gives a radius R=σ2(Φ−1(p‾A)−Φ−1(p‾B))R = \frac{\sigma}{2}(\Phi^{-1}(\underline{p}_A) - \Phi^{-1}(\overline{p}_B)) inside which the answer cannot change.
  • The radius grows with the spread σ\sigma up to a point, after which the vote share collapses, so the spread is a tradeoff.
  • Read next: Adversarial Training for the empirical alternative, and Lipschitz Continuity for the bound-based alternative.

Check yourself

QuestionWhat two things must be known about the vote to certify a radius, and which quantity does the radius grow with?Show answer

The lower bound on the winning class's vote share p‾A\underline{p}_A and the upper bound on the runner-up's share p‾B\overline{p}_B. The condition p‾A>p‾B\underline{p}_A > \overline{p}_B must hold. The radius R=σ2(Φ−1(p‾A)−Φ−1(p‾B))R = \frac{\sigma}{2}(\Phi^{-1}(\underline{p}_A) - \Phi^{-1}(\overline{p}_B)) grows with the spread σ\sigma and with the gap between the two shares, measured in standard normal units.

QuestionWith sigma = 1, a lower bound of 0.8 for the winning class and an upper bound of 0.2 for the runner-up, what is the certified radius?Show answer

Φ−1(0.8)=0.8416\Phi^{-1}(0.8) = 0.8416 and Φ−1(0.2)=−0.8416\Phi^{-1}(0.2) = -0.8416. The difference is 1.68331.6833. Multiply by σ/2=0.5\sigma / 2 = 0.5 to get R=0.8416R = 0.8416, so about 0.84.

QuestionWhy does a larger spread not always give a larger radius?Show answer

A larger spread blurs the base classifier more, so the votes move toward a mix of classes and the winning share p‾A\underline{p}_A falls. The radius is σ\sigma times a term that depends on the share. Once the share drops, that term shrinks faster than σ\sigma grows, and the radius falls, as Example 3 shows.

QuestionA team reports that its smoothed classifier is 99 percent accurate, but they used one noisy copy per input. What is wrong with the claim?Show answer

One noisy copy is a single random draw, so the answer may change from run to run, and there is no vote share and no bound. The 99 percent is an accuracy number on noisy inputs, not a certificate, and it says nothing about inputs within a radius. A certificate needs many samples, a confidence bound on the winning share and the runner-up share, and a radius computed from them.

Sources

Randomized Smoothing — explained simply · ungoon.me