Callbacks

BaseCallback

class a2pm.callbacks.BaseCallback(verbose=0)

Bases: object

Base Attack Callback.

A callback records and/or prints specific values of each attack iteration of the generate method. This base class cannot be directly utilized.

It must be either a function or a class implementing the __call__ method, according to one of the following signatures:

__call__(self, **kwargs)

__call__(self, X, iteration, samples_left, samples_misclassified, nanoseconds)

It can receive five parameters:

  • the current data (input data at iteration 0, and then adversarial data);

  • the current attack iteration;

  • the number of samples left to be misclassified;

  • the number of samples misclassified in the current iteration;

  • the number of nanoseconds consumed in the current iteration.

For example, a simple function to print each iteration can be:

def callback(**kwargs): print(kwargs[“iteration”])

Parameters

verbose (int, in {0, 1, 2} (default 0)) – Verbosity level of the callback.

Set to 2 to enable a complete printing of the values and their descriptions, to 1 to enable a simple printing of the values, or to 0 to disable verbosity.

Variables

values (list of values) – The values recorded at each iteration by an inheriting class. Empty list before that class is called.

MetricCallback

class a2pm.callbacks.MetricCallback(classifier, y, scorers=[('Macro-averaged F1-Score', 'f1_macro')], verbose=0)

Bases: a2pm.callbacks.base_callback.BaseCallback

Metric Attack Callback.

Records the score of one or more metrics at each iteration.

The metrics are measured according to their respective scorer functions.

Parameters
  • classifier (object with a predict method) – Fitted classifier to be evaluated, which should be the same classifier being attacked.

  • y (array-like in the (n_samples, ) shape or None (default None)) – Ground truth classes that the classifier should predict.

  • scorers (list of tuples of ‘description, scorer’) – Tuples of custom metric descriptions and respective scorer functions.

    Besides an actual scorer function, a Scikit-learn compatible description is also supported.

    The default scorer is the following:

    (“Macro-averaged F1-Score”, “f1_macro”)

  • verbose (int, in {0, 1, 2} (default 0)) – Verbosity level of the callback.

    Set to 2 to enable a complete printing of the values and their descriptions, to 1 to enable a simple printing of the values, or to 0 to disable verbosity.

Variables

values (list of tuples of values) – The tuples of evaluation scores, one per metric, of each iteration. Empty list before this callback is called.

TimeCallback

class a2pm.callbacks.TimeCallback(verbose=0)

Bases: a2pm.callbacks.base_callback.BaseCallback

Time Attack Callback.

Records the time consumption of each iteration.

It is measured as nanoseconds per created example, according to the total samples that could be misclassified at each iteration.

Parameters

verbose (int, in {0, 1, 2} (default 0)) – Verbosity level of the callback.

Set to 2 to enable a complete printing of the values and their descriptions, to 1 to enable a simple printing of the values, or to 0 to disable verbosity.

Variables

values (list of values) – The time consumption of each iteration. Empty list before this callback is called.