ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing
ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing

All Courses

Advanced Adversarial Machine Learning
Chapter 1: Foundations of Adversarial ML Security
Review of Machine Learning Security Vulnerabilities
Threat Models in Machine Learning
Attack Surfaces: Training vs. Inference
Mathematical Formulation of Adversarial Examples
Taxonomy of Adversarial Attacks
Overview of Defense Strategies
Chapter 2: Advanced Evasion Attacks
Gradient-Based Attacks: FGSM, BIM, PGD Analysis
Optimization-Based Attacks: Carlini & Wagner Methods
Score-Based Attack Techniques
Decision-Based Attack Techniques
Transferability of Adversarial Examples
Attacking Ensemble Models
Implementing Evasion Attacks: Hands-on Practical
Chapter 3: Data Poisoning and Backdoor Attacks
Poisoning Attack Strategies: Availability vs Integrity
Targeted Data Poisoning Techniques
Backdoor Attack Mechanisms and Trigger Design
Clean-Label Poisoning Attacks
Analyzing Poisoning Impact on Model Training
Crafting Data Poisoning Attacks: Hands-on Practical
Chapter 4: Model Inference and Privacy Attacks
Membership Inference Attacks: Theory and Methods
Attribute Inference Techniques
Model Inversion and Reconstruction Attacks
Model Stealing: Functionality Extraction Methods
Relationship to Differential Privacy
Implementing Membership Inference: Hands-on Practical
Chapter 5: Robust Defense Mechanisms
Adversarial Training: Principles and Variations
Certified Defenses: Randomized Smoothing
Input Transformation Defenses
Gradient Masking and Obfuscation Issues
Defending Against Poisoning and Backdoors
Implementing Adversarial Training: Hands-on Practical
Chapter 6: Evaluating Model Robustness
Metrics for Adversarial Robustness
Benchmarking Tools and Frameworks
Adaptive Attacks: Evaluating Defenses Properly
Security Evaluations under Different Threat Models
Interpreting Robustness Evaluation Results
Setting up Robustness Benchmarks: Hands-on Practical
Chapter 7: Adversarial Examples in Specific Domains
Adversarial Attacks on Computer Vision Models
Generating Adversarial Text for NLP Models
Attacks on Reinforcement Learning Agents
Physical Adversarial Attacks
Domain-Specific Attack Considerations
Generating Adversarial Text: Practice

Optimization-Based Attacks: Carlini & Wagner Methods

While gradient-based attacks like Projected Gradient Descent (PGD) provide a strong baseline for generating adversarial examples, they primarily focus on finding any adversarial example within a predefined perturbation budget (ϵ\epsilonϵ-ball). They don't necessarily find the example with the smallest possible perturbation that achieves misclassification. For situations where minimizing the perceptibility of the perturbation is critical, or when evaluating the absolute breaking point of a model, optimization-based attacks offer a more direct approach.

The Carlini & Wagner (C&W) attacks, introduced by Nicholas Carlini and David Wagner, represent a powerful class of optimization-based evasion attacks. Instead of just finding an adversarial example within a constraint, C&W methods frame the search as a formal optimization problem: find the minimum distortion δ\deltaδ such that xadv=x+δx_{adv} = x + \deltaxadv​=x+δ is misclassified and satisfies input constraints (e.g., pixel values remain valid).

Formulating the Optimization Problem

The core idea is to minimize the perturbation size, measured by an LpL_pLp​ norm, subject to the condition that the model misclassifies the perturbed input. Let xxx be the original input, x′x'x′ be the adversarial input (xadvx_{adv}xadv​), and fff be the model (often represented by its logits Z(x′)Z(x')Z(x′) before the final softmax). The goal is to find x′x'x′ that solves:

minimize ∣∣x′−x∣∣psubject to f(x′) is misclassified and x′∈[0,1]n\text{minimize } ||x' - x||_p \quad \text{subject to } f(x') \text{ is misclassified and } x' \in [0, 1]^nminimize ∣∣x′−x∣∣p​subject to f(x′) is misclassified and x′∈[0,1]n

Directly enforcing the "misclassified" constraint is difficult in standard optimization frameworks. C&W cleverly replace this constraint with a specially designed loss function, g(x′)g(x')g(x′), which is minimized when the model misclassifies x′x'x′. The optimization problem then becomes:

minimize ∣∣x′−x∣∣p+c⋅g(x′)\text{minimize } ||x' - x||_p + c \cdot g(x')minimize ∣∣x′−x∣∣p​+c⋅g(x′)

Here:

  • ∣∣x′−x∣∣p||x' - x||_p∣∣x′−x∣∣p​ is the distortion term, measuring the distance between the original and adversarial input using an LpL_pLp​ norm (commonly L0L_0L0​, L2L_2L2​, or L∞L_\inftyL∞​).
  • g(x′)g(x')g(x′) is the classification loss function.
  • ccc is a balancing constant, carefully chosen (often via binary search) to ensure the classification constraint is met while keeping the distortion low. A larger ccc places more emphasis on achieving misclassification.

The C&W Classification Loss

The genius of the C&W attack lies in the design of g(x′)g(x')g(x′). For a targeted attack, where the goal is to make the model classify x′x'x′ as a specific target class ttt, a common loss function is:

g(x′)=max⁡(max⁡i≠tZ(x′)i−Z(x′)t,−κ)g(x') = \max( \max_{i \neq t} Z(x')_i - Z(x')_t, -\kappa )g(x′)=max(i=tmax​Z(x′)i​−Z(x′)t​,−κ)

Let's break this down:

  • Z(x′)iZ(x')_iZ(x′)i​ is the logit value for class iii.
  • max⁡i≠tZ(x′)i\max_{i \neq t} Z(x')_imaxi=t​Z(x′)i​ finds the highest logit value among all classes except the target class ttt.
  • Z(x′)tZ(x')_tZ(x′)t​ is the logit value for the target class ttt.
  • The difference max⁡i≠tZ(x′)i−Z(x′)t\max_{i \neq t} Z(x')_i - Z(x')_tmaxi=t​Z(x′)i​−Z(x′)t​ measures how much "stronger" the most likely incorrect class is compared to the target class. We want this difference to be negative and large (in magnitude) for the target class ttt to be the most likely prediction.
  • The outer max⁡(…,−κ)\max(\dots, -\kappa)max(…,−κ) introduces a confidence margin κ≥0\kappa \ge 0κ≥0. The loss becomes negative only when Z(x′)tZ(x')_tZ(x′)t​ is greater than all other logits by at least κ\kappaκ. Minimizing this loss encourages the target logit to surpass the next highest logit by this margin, increasing the confidence of the misclassification. Setting κ=0\kappa = 0κ=0 simply requires the target class logit to be the highest.

For an untargeted attack, where the goal is simply to make the model misclassify x′x'x′ away from its original class ytruey_{true}ytrue​, the loss can be defined as:

g(x′)=max⁡(Z(x′)ytrue−max⁡i≠ytrueZ(x′)i,−κ)g(x') = \max( Z(x')_{y_{true}} - \max_{i \neq y_{true}} Z(x')_i, -\kappa )g(x′)=max(Z(x′)ytrue​​−i=ytrue​max​Z(x′)i​,−κ)

Here, the goal is to minimize the loss by making the true class logit Z(x′)ytrueZ(x')_{y_{true}}Z(x′)ytrue​​ smaller than the highest logit of any other class by at least κ\kappaκ.

Solving the Problem

C&W attacks typically use standard gradient-based optimizers like Adam to minimize the combined objective function ∣∣x′−x∣∣p+c⋅g(x′)||x' - x||_p + c \cdot g(x')∣∣x′−x∣∣p​+c⋅g(x′). However, a technical detail arises: the input x′x'x′ must often satisfy box constraints (e.g., pixel values in [0,1][0, 1][0,1]). Directly applying gradient descent and clipping x′x'x′ at each step can hinder convergence.

C&W proposed using a change of variables. For example, instead of optimizing x′x'x′ directly, they optimize a variable www where x′=12(tanh⁡(w)+1)x' = \frac{1}{2} (\tanh(w) + 1)x′=21​(tanh(w)+1). Since the output of tanh⁡\tanhtanh is always in (−1,1)(-1, 1)(−1,1), x′x'x′ is guaranteed to be in (0,1)(0, 1)(0,1), satisfying the box constraints naturally. The optimization is performed with respect to www, and the distortion ∣∣x′−x∣∣p||x' - x||_p∣∣x′−x∣∣p​ and classification loss g(x′)g(x')g(x′) are calculated based on the transformed x′x'x′.

C&W Attack Variants (L2L_2L2​, L∞L_\inftyL∞​, L0L_0L0​)

The C&W framework was developed for different LpL_pLp​ norms:

  • C&W L2L_2L2​ Attack: This is the most common variant. It minimizes the Euclidean distance ∣∣x′−x∣∣2||x' - x||_2∣∣x′−x∣∣2​. It typically uses the loss function described above and the tanh⁡\tanhtanh change-of-variables trick. It's known for finding very low-distortion L2L_2L2​ adversarial examples, often outperforming PGD in terms of perturbation magnitude, albeit usually slower.
  • C&W L∞L_\inftyL∞​ Attack: This variant aims to minimize the maximum change to any single feature, ∣∣x′−x∣∣∞||x' - x||_\infty∣∣x′−x∣∣∞​. It uses a slightly modified optimization approach, often involving iterative steps similar to PGD but incorporating the C&W loss function. It aims to find the smallest ϵ\epsilonϵ for which an L∞L_\inftyL∞​ attack succeeds.
  • C&W L0L_0L0​ Attack: This attack minimizes the number of pixels changed, ∣∣x′−x∣∣0||x' - x||_0∣∣x′−x∣∣0​. It's computationally harder. The approach typically involves iteratively fixing pixels that are not contributing significantly to the loss and only optimizing the remaining ones.

Difference between PGD and C&W (L2L_2L2​) approaches. PGD iteratively steps and projects to stay within a fixed-size LpL_pLp​ ball around the original input xxx, seeking any misclassification. C&W directly minimizes the L2L_2L2​ distance from xxx while satisfying a misclassification loss, effectively finding the closest point across the decision boundary.

Strengths and Trade-offs

Strengths:

  • Effectiveness: C&W attacks, particularly the L2L_2L2​ variant, are highly effective and often find adversarial examples with smaller perturbations compared to PGD for a given model.
  • Circumvents Gradient Masking: Because C&W uses an optimization objective that relies on logits and includes the distortion term, it can sometimes overcome defenses that attempt to mask or obfuscate gradients, which might fool simpler gradient-based methods.
  • Flexibility: The framework can be adapted for targeted/untargeted attacks and different LpL_pLp​ norms.

Trade-offs:

  • Computational Cost: C&W attacks are significantly slower than FGSM or PGD. They involve an iterative optimization process, and finding the optimal constant ccc typically requires a binary search, running the attack multiple times.
  • Complexity: The implementation is more involved than basic gradient-based methods, requiring careful handling of the optimization objective and constraints.

Despite their computational cost, C&W attacks serve as a critical benchmark for evaluating the robustness of machine learning models and defenses. Their ability to find near-minimal perturbations makes them a standard tool for security evaluations. If a defense holds up against a strong C&W attack, it provides greater confidence in its effectiveness compared to only testing against faster methods like FGSM or PGD.

  • Towards Evaluating the Robustness of Neural Networks, Nicholas Carlini, David Wagner, 2017 2017 IEEE Symposium on Security and Privacy (SP) (IEEE) DOI: 10.1109/SP.2017.49 - Describes the seminal Carlini & Wagner attacks (L0, L2, L∞) and their optimization framework for finding minimal perturbations.
  • Towards Deep Learning Models Resistant to Adversarial Attacks, Aleksander Madry, Aleksandar Makelov, Ludwig Schmidt, Dimitris Tsipras, Adrian Vladu, 2018 International Conference on Learning Representations (ICLR) DOI: 10.48550/arXiv.1706.06083 - While primarily known for PGD and adversarial training, this paper and its associated resources offer essential context for understanding strong gradient-based attacks and robustness, which is crucial for appreciating C&W's role as a benchmark.
  • Adversarial Attacks and Defenses in Machine Learning: A Survey, Xinyun Chen, Changxi Zheng, Li Erran Li, Dawn Song, 2020 ACM Computing Surveys, Vol. 53 (Association for Computing Machinery (ACM)) DOI: 10.1145/3371921 - A comprehensive survey that reviews various adversarial attack techniques, including Carlini & Wagner, and discusses their impact and the corresponding defenses.
  • Reliable Evaluation of Adversarial Robustness with an Ensemble of Diverse Attacks, Francesco Croce, Matthias Hein, 2020 International Conference on Machine Learning (ICML) DOI: 10.48550/arXiv.2003.01690 - Introduces AutoAttack, a robust benchmark suite that incorporates C&W L2 as a core component for thoroughly evaluating model robustness against strong adversarial examples.

© 2026 ApX Machine Learning