Carlini–Wagner attack

Also known as: C&W attack · Carlini-Wagner attack

On this page 7

The Carlini–Wagner attack, often shortened to the C&W attack, is an optimization-based white-box adversarial attack that searches for the smallest perturbation of an input that causes a neural network classifier to output a chosen wrong label. Introduced by Nicholas Carlini and David Wagner in 2017, it reformulates the search for adversarial examples as a constrained optimization problem solved with gradient descent, and it produces perturbations that are typically far smaller than those of earlier attacks.

In AI security, the C&W attack became a standard benchmark for evaluating defenses. Its original publication showed that defensive distillation, then considered a strong protection, offered almost no robustness against a well-designed optimization attack, and it shaped how researchers test adversarial robustness today.

How the Carlini–Wagner attack formulates the problem

The attack minimizes a sum of two terms: the size of the perturbation and a penalty that becomes zero once the model prefers the target class. Formally, it minimizes the distance between the original input x and the adversarial input x plus delta, plus a constant c multiplied by an objective function f that measures how far the model still is from the desired misclassification.

The most effective objective function in the original work operates on logits rather than softmax probabilities. It compares the largest logit among the non-target classes with the logit of the target class and stops rewarding progress once the target logit exceeds the others by a confidence margin kappa. Working on logits avoids the vanishing gradients that the softmax produces near saturation.

Three technical choices make the attack strong and stable:

  • Change of variables with tanh: the perturbation is expressed through a tanh transform, so pixel values always stay inside the valid range without clipping.
  • Binary search over c: the constant that balances distance and misclassification is tuned automatically for each input.
  • Adam optimizer: an adaptive gradient method converges reliably over hundreds or thousands of iterations.

Distance metrics and attack variants

The C&W paper defined three attack variants, one for each common way of measuring perturbation size. Each norm captures a different notion of how noticeable a change is, and each variant needs a different optimization strategy.

VariantDistance measuredOptimization approachTypical perturbation
L2 attackEuclidean size of the changeDirect gradient descent with tanhSmall, spread over many pixels
L0 attackNumber of changed pixelsIteratively fixes unimportant pixelsFew pixels, larger changes
L-infinity attackLargest change to any pixelPenalizes values above a shrinking thresholdUniform, low-amplitude noise
The three Carlini–Wagner variants

The L2 variant is by far the most widely used and is usually what practitioners mean by the C&W attack. Its perturbations on standard image datasets are often imperceptible to human observers, while still driving the misclassification rate on undefended models close to 100 percent.

Comparison with other adversarial attacks

The C&W attack occupies the high-cost, high-precision end of the white-box attack spectrum. Faster attacks produce larger perturbations, whereas C&W spends far more computation to find a near-minimal one.

AttackStrategyIterationsPerturbation sizeMain use
Fast gradient sign methodSingle signed gradient step1Large, fixed budgetQuick tests, adversarial training
Projected gradient descentRepeated steps within a norm ball10 to 100Up to a fixed budgetRobustness evaluation and training
DeepFoolLinearizes decision boundaryA fewSmall, not always minimalMeasuring boundary distance
Carlini–WagnerPenalized optimization on logitsHundreds to thousandsNear minimalBenchmarking defenses
Carlini–Wagner compared with other evasion attacks

The key conceptual difference is that projected gradient descent asks for any successful perturbation within a fixed budget, while C&W asks for the smallest successful perturbation. That makes C&W well suited to measuring how close inputs lie to a decision boundary, but too slow for generating adversarial examples during every training step.

Effect on adversarial defenses

The C&W attack demonstrated that many defenses only hide gradients instead of removing vulnerability. Defensive distillation, for example, made softmax outputs extremely saturated so that gradients approached zero, but the logit-based objective of C&W bypassed this effect entirely.

The following table summarizes how common defense families have fared against optimization attacks such as C&W.

DefenseMechanismOutcome against C&W
Defensive distillationSoftens training labels, saturates softmaxBroken by logit-based loss
Gradient masking defensesObscure or shatter gradientsUsually broken by adapted attacks
Detection classifiersFlag suspicious inputsOften evaded when detector is attacked jointly
Adversarial trainingTrains on attacked examplesSubstantially raises required perturbation
Certified robustnessProvable bounds such as randomized smoothingGuaranteed within the certified radius
Defense families and their behavior against C&W

A central lesson is the need for adaptive attacks: a defense must be evaluated against an attacker who knows the defense and modifies the objective accordingly. Testing only against the default C&W configuration is no longer considered sufficient evidence of robustness.

Using the attack for robustness evaluation

Security teams use C&W mainly for measurement, not deployment-time attacks. Toolkits such as the Adversarial Robustness Toolbox, Foolbox and CleverHans provide implementations, and the typical parameters are a confidence margin kappa between 0 and about 40, nine or ten binary search steps for c, and 1,000 or more optimization iterations.

Increasing kappa produces high-confidence adversarial examples that transfer better to other models, which matters for estimating black-box risk. The main practical limitations are computational cost, since each input requires its own optimization, and the white-box assumption, which requires access to the model gradients.

A non-technical view

Picture a photo of a cat that an AI system labels correctly. The Carlini–Wagner attack searches for the tiniest possible edit that makes the system call it a dog. It changes pixels so slightly that a person sees no difference at all.

The method works like a careful forger. It tries again and again, each time making the edit smaller while keeping the fake convincing. Researchers use it to test protection. If a defense cannot stop it, the defense is weaker than it appears.

A concrete example

An automotive supplier evaluates a traffic sign classifier before integrating it into a driver assistance system. The security team runs the L2 Carlini–Wagner attack on 1,000 test images with kappa set to 0 and nine binary search steps. On the baseline model, the attack succeeds on every image with an average L2 distance below 0.5 on images scaled to the range 0 to 1, a change invisible on screen.

The team then applies adversarial training with projected gradient descent and repeats the evaluation, adding an adaptive version of the attack. The required perturbation grows several times, and the success rate within the agreed distance budget falls sharply. The measured distance becomes a release metric, and the report documents that the remaining risk applies to white-box attackers only.