pulsatrix 1.1.0
Loading...
Searching...
No Matches
adversarial.hpp
Go to the documentation of this file.
1
27#pragma once
28
29#include <cstdint>
30#include <functional>
31#include <limits>
32#include <string>
33#include <vector>
34
35#include "pulsatrix/json.hpp"
36#include "pulsatrix/tensor.hpp"
37
38namespace pulsatrix {
39
40class DeviceBackend;
41class ExplainerContext;
42class Module;
43
50 std::function<Tensor(const Tensor&)> forward;
51 std::function<Tensor(const Tensor&)> backward;
52};
53
57
63
65enum class AttackNorm { Linf, L2 };
66
70 double epsilon = 0.03;
73 int64_t steps = 10;
75 double step_size = 0.0;
78 bool random_start = true;
79 uint64_t seed = 0;
82 double clip_min = -std::numeric_limits<double>::infinity();
83 double clip_max = std::numeric_limits<double>::infinity();
84};
85
91 std::vector<int64_t> clean_predicted;
92 std::vector<int64_t> adversarial_predicted;
95 std::vector<bool> success;
97 std::vector<double> clean_loss;
98 std::vector<double> adversarial_loss;
100 std::vector<double> perturbation;
102 double success_rate = 0.0;
104 double robust_accuracy = 0.0;
106 double clean_accuracy = 0.0;
107 bool targeted = false;
108};
109
120[[nodiscard]] AttackResult Fgsm(const DifferentiableClassifier& model, const Tensor& input, const std::vector<int64_t>& labels,
121 DeviceBackend* backend, const AttackOptions& options = {}, bool targeted = false);
122
129[[nodiscard]] AttackResult Pgd(const DifferentiableClassifier& model, const Tensor& input, const std::vector<int64_t>& labels,
130 DeviceBackend* backend, const AttackOptions& options = {}, bool targeted = false);
131
134 double epsilon = 0.0;
136 double robust_accuracy = 0.0;
138 double flip_rate = 0.0;
140 double mean_perturbation = 0.0;
141};
142
145 std::string attack;
147 int64_t n = 0;
148 int64_t steps = 0;
149 double clean_accuracy = 0.0;
150 std::vector<RobustnessPoint> points;
151};
152
161 const std::vector<int64_t>& labels, const std::vector<double>& epsilons,
162 DeviceBackend* backend, const AttackOptions& options = {});
163
165[[nodiscard]] JsonValue ToJson(const AttackResult& result, const AttackOptions& options);
166
168[[nodiscard]] JsonValue ToJson(const RobustnessCurve& curve);
169
170} // namespace pulsatrix
Vendor-agnostic compute/memory backend. CPUBackend, CUDABackend (Phase 1.5), and HIPBackend (Phase 1....
Definition device_backend.hpp:248
Wraps an ordered chain of Modules, running them via Module::forward_traced to build a real Computatio...
Definition explainer_context.hpp:67
One JSON value: null, a boolean, a number, a string, an array or an object.
Definition json.hpp:22
Base class for every layer type (LinearModule, Conv2DModule, activations, ...).
Definition module.hpp:78
N-dimensional tensor. Owns its data buffer exclusively; a DeviceBackend* is injected (not owned) – th...
Definition tensor.hpp:29
A small strict JSON value, parser and deterministic writer (RFC 8259).
Definition acquisition_functions.hpp:16
AttackResult 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 ar...
AttackNorm
The norm an attack's budget is measured in.
Definition adversarial.hpp:65
AttackResult 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 arou...
RobustnessCurve 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 ge...
JsonValue ToJson(const AttackResult &result, const AttackOptions &options)
The result as a pulsatrix.attack.v1 document, without the adversarial inputs.
DifferentiableClassifier ClassifierFrom(ExplainerContext &context)
Differentiates through an ExplainerContext's autograd, as the gradient explainers do; the model's par...
Settings for Fgsm() and Pgd().
Definition adversarial.hpp:68
AttackNorm norm
Definition adversarial.hpp:71
uint64_t seed
Definition adversarial.hpp:79
bool random_start
PGD: start at a uniformly random point of the ball (the L2 ball for L2), as Madry et al....
Definition adversarial.hpp:78
double clip_max
Definition adversarial.hpp:83
double epsilon
The budget: every adversarial input is within epsilon of its original.
Definition adversarial.hpp:70
double clip_min
The valid range of each input element, such as [0, 1] for pixels; every adversarial input is clipped ...
Definition adversarial.hpp:82
int64_t steps
PGD: the number of steps.
Definition adversarial.hpp:73
double step_size
PGD: the step size; 0 means 2.5 epsilon / steps, Madry et al.'s choice.
Definition adversarial.hpp:75
One attack's result.
Definition adversarial.hpp:87
std::vector< bool > success
Untargeted: the adversarial input isn't classified as its label. Targeted: it is classified as its ta...
Definition adversarial.hpp:95
bool targeted
Definition adversarial.hpp:107
double clean_accuracy
Untargeted: the fraction classified correctly before it.
Definition adversarial.hpp:106
std::vector< int64_t > adversarial_predicted
Definition adversarial.hpp:92
std::vector< double > perturbation
Each perturbation's size in the attack's norm: at most epsilon, up to rounding.
Definition adversarial.hpp:100
double robust_accuracy
Untargeted: the fraction classified correctly after the attack.
Definition adversarial.hpp:104
Tensor adversarial
The adversarial inputs, the input's shape, on the input's device.
Definition adversarial.hpp:89
double success_rate
The fraction of inputs the attack succeeded on.
Definition adversarial.hpp:102
std::vector< double > adversarial_loss
Definition adversarial.hpp:98
std::vector< double > clean_loss
The cross-entropy of each label (the target's, when targeted) before and after.
Definition adversarial.hpp:97
std::vector< int64_t > clean_predicted
Each input's predicted class before and after the attack.
Definition adversarial.hpp:91
A classifier the attacks can differentiate: forward maps an (N, ...) input to (N, C) logits; backward...
Definition adversarial.hpp:49
std::function< Tensor(const Tensor &)> backward
Definition adversarial.hpp:51
std::function< Tensor(const Tensor &)> forward
Definition adversarial.hpp:50
RobustAccuracyCurve()'s result.
Definition adversarial.hpp:144
int64_t steps
Definition adversarial.hpp:148
AttackNorm norm
Definition adversarial.hpp:146
int64_t n
Definition adversarial.hpp:147
std::vector< RobustnessPoint > points
Definition adversarial.hpp:150
double clean_accuracy
Definition adversarial.hpp:149
std::string attack
Definition adversarial.hpp:145
One budget's result on a robustness curve.
Definition adversarial.hpp:133
double robust_accuracy
The fraction of inputs still classified correctly after PGD.
Definition adversarial.hpp:136
double flip_rate
Of the inputs classified correctly before the attack, the fraction it flipped.
Definition adversarial.hpp:138
double mean_perturbation
The mean perturbation size of the successful attacks; 0 if there were none.
Definition adversarial.hpp:140
double epsilon
Definition adversarial.hpp:134
N-dimensional tensor – owns a buffer via DeviceBackend*, RAII (Rule of Five).