pulsatrix 1.1.0
Loading...
Searching...
No Matches
adversarial.hpp File Reference

Adversarial attacks on a classifier (KS-4): the fast gradient sign method (Goodfellow, Shlens and Szegedy, ICLR 2015) and projected gradient descent (Madry et al., ICLR 2018), under the L∞ or L2 norm, untargeted or targeted, and the robust accuracy they leave. More...

#include <cstdint>
#include <functional>
#include <limits>
#include <string>
#include <vector>
#include "pulsatrix/json.hpp"
#include "pulsatrix/tensor.hpp"
Include dependency graph for adversarial.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  pulsatrix::DifferentiableClassifier
 A classifier the attacks can differentiate: forward maps an (N, ...) input to (N, C) logits; backward maps a gradient at those logits to the gradient at the input, for the input of the last forward. More...
 
struct  pulsatrix::AttackOptions
 Settings for Fgsm() and Pgd(). More...
 
struct  pulsatrix::AttackResult
 One attack's result. More...
 
struct  pulsatrix::RobustnessPoint
 One budget's result on a robustness curve. More...
 
struct  pulsatrix::RobustnessCurve
 RobustAccuracyCurve()'s result. More...
 

Namespaces

namespace  pulsatrix
 

Enumerations

enum class  pulsatrix::AttackNorm { pulsatrix::Linf , pulsatrix::L2 }
 The norm an attack's budget is measured in. More...
 

Functions

DifferentiableClassifier pulsatrix::ClassifierFrom (ExplainerContext &context)
 Differentiates through an ExplainerContext's autograd, as the gradient explainers do; the model's parameter gradients are left alone. The context must outlive the result.
 
DifferentiableClassifier pulsatrix::ClassifierFrom (Module &model)
 Differentiates through Module::forward and Module::backward. backward() also accumulates the parameters' gradients, so zero them before training again. The module must outlive the result, and should be in eval mode, so dropout doesn't change between the steps.
 
AttackResult pulsatrix::Fgsm (const DifferentiableClassifier &model, const Tensor &input, const std::vector< int64_t > &labels, DeviceBackend *backend, const AttackOptions &options={}, bool targeted=false)
 The fast gradient sign method: one step of size epsilon (options.steps, step_size and random_start are ignored).
 
AttackResult pulsatrix::Pgd (const DifferentiableClassifier &model, const Tensor &input, const std::vector< int64_t > &labels, DeviceBackend *backend, const AttackOptions &options={}, bool targeted=false)
 Projected gradient descent: random start, then steps steps, each projected onto the epsilon-ball around the input and into [clip_min, clip_max]. Returns the last iterate.
 
RobustnessCurve pulsatrix::RobustAccuracyCurve (const DifferentiableClassifier &model, const Tensor &input, const std::vector< int64_t > &labels, const std::vector< double > &epsilons, DeviceBackend *backend, const AttackOptions &options={})
 Robust accuracy against untargeted PGD at each budget: how fast the accuracy falls as the attacker gets more room. options.epsilon is ignored; options.step_size, when set, is a fraction of each epsilon (0 keeps 2.5 / steps).
 
JsonValue pulsatrix::ToJson (const AttackResult &result, const AttackOptions &options)
 The result as a pulsatrix.attack.v1 document, without the adversarial inputs.
 
JsonValue pulsatrix::ToJson (const RobustnessCurve &curve)
 The curve as a pulsatrix.robustness_curve.v1 document.
 

Detailed Description

Adversarial attacks on a classifier (KS-4): the fast gradient sign method (Goodfellow, Shlens and Szegedy, ICLR 2015) and projected gradient descent (Madry et al., ICLR 2018), under the L∞ or L2 norm, untargeted or targeted, and the robust accuracy they leave.

An adversarial example is an input changed by less than ε, in some norm, that the model classifies differently. How small an ε suffices says how robust the model is; which inputs fall first says where it is fragile. That answers the Control reason's question, what could make this model fail, with a number.

Both attacks follow the gradient of the cross-entropy loss with respect to the input:

  • FGSM takes one step of size ε: x + ε sign(∇ₓL) under L∞, x + ε ∇ₓL / ‖∇ₓL‖₂ under L2 (the fast gradient method).
  • PGD starts at a random point of the ε-ball, takes steps steps of size α the same way, and after each one projects back onto the ball and into the input's valid range.

A targeted attack descends the loss of a chosen class instead of ascending the true one's. The steps, the projection and the random start follow torchattacks' FGSM, PGD and PGDL2; tests/adversarial_test.cpp checks them against tools/generate_adversarial_reference_values.py, a numpy implementation of the same updates.

PGD is a lower bound on what an attacker can do: a model that resists it may fall to a stronger attack. Robust accuracy measured with it is an upper bound on the true robust accuracy.