JTLA#

class scio.scores.JTLA(*, act_norm=2, mode='raw', test=?, layer_aggregation='fisher', layer_aggregation_consecutive=False, class_aggregation='nat', pred_conditional=True, index_metric='ip')[source]#

Bases: BaseScoreClassif

JTLA for classification.

The proposed framework essentially works in 3 steps:

  1. Statistical tests, conditional on both layer and class (true and predicted);

  2. Layer aggregation;

  3. Class aggregation.

Parameters:
  • test (JTLATestMultinomial) – Defines the statistical test to use. Currently, only multinomial tests are supported. For possible values, refer to JTLATestMultinomial.

  • layer_aggregation (str) –

    Defines layers aggregation scheme. The following values are supported.

    • "lpe": multi-layer normalization scheme using aK-LPE. Uses the same distance as nearest neighbors search. This approach requires a hyperparameter k for nearest neighbors search. By default, it uses test["k"], which can be overriden by specifying a suffix :k. Examples: "lpe", "lpe:20".

    • Any value from AggrName: \(1\)D aggregation of \(p\)-values. These may be appended ":" followed by a coma-separated list of integers n, indicating that combined \(p\)-values for n-tuples of layers must be computed. Appending ":1" is a no-op. Examples: "sum", "harmonic:2", "fisher:1,2,3".

  • layer_aggregation_consecutive (bool) – Whether n-tuples for layer aggregation are restricted to consecutive layers. Ignored for "lpe" aggregation. Defaults to False.

  • class_aggregation (ClassAggrLike) – See ClassAggr. Defaults to "nat".

  • pred_conditional (bool) – Whether to use predicted-class-conditional scores as described in [RCJB21]. Note that if the classifier is perfect on calibration samples, this has no effect. If False, every predicted-class-conditional object from [RCJB21] is replaced with its true-class-conditional counterpart. Defaults to True.

  • index_metric (IndexMetricLike) – Kind of metric to use for nearest neighbors search. See IndexMetric. Defaults to "ip".

  • mode – See BaseScoreClassif.

  • act_norm – See BaseScore. Now defaults to 2.

Notes

Here are a few key differences between our implementation and the authors’:

  • No bootstraping for \(p\)-values estimation;

  • No dimensionality reduction of latent spaces before neighbors search. Since they use neighborhood preserving projection, it should not impact neighbor search results and this is only seen as a computational acceleration;

  • New option mode="dcm" for MultinomialTestMode;

  • The layer aggregation can be done on n-tuples with arbitrary sets of n, and with or without restricting to consecutive layers.

  • It is possible to skip resorting to pred-class-conditional tests.

References

[RCJB21] (1,2,3,4)

Jayaram Raghuram, Varun Chandrasekaran, Somesh Jha, and Suman Banerjee. A general framework for detecting anomalous inputs to DNN classifiers. In Proceedings of the 38th International Conference on Machine Learning, volume 139, 8764–8775. 2021. URL: https://proceedings.mlr.press/v139/raghuram21a.html.

Hint

Below this point, the documentation is meant for development purposes only. Manual use of any listed member is highly discouraged. For usage, see Inferring with Confidence.

Useful methods defined here

aggregate_classes(q_values)

Aggregate \(q\)-values accross classes.

aggregate_layers(tests)

Aggregate tests results accross layers.

compute_p_values(reference_tests, query_tests)

Compute \(p\)-values for query samples.

parse_layer_aggregation()

Store layer aggregation method and n-tuples to use.

prepare_lpe()

For lpe layer aggregation prepare indexes and query calib.

prepare_tests(all_activations, n_classes)

Multinomial only for now.

run_tests(all_activations)

Run class-conditional statistical tests.

aggregate_classes(q_values)[source]#

Aggregate \(q\)-values accross classes.

Differences with [RCJB21, section 4.4]: no log scale (unimportant) and inverse ratio (convention).

Parameters:

q_values (tuple[Tensor, Tensor | None]) – Output of aggregate_layers(). It’s a \(2\)-tuple with shapes (when Tensor) (n_samples, n_classes).

Returns:

conformity (Tensor) – Shape (n_samples, n_classes). Result of \(q\)-values aggregation.

Raises:

ValueError – If self.class_aggregation value is unsupported.

aggregate_layers(tests)[source]#

Aggregate tests results accross layers.

Parameters:

tests (tuple[Tensor, Tensor | None]) – Output of run_tests() on query samples. Shapes (n_classes, n_samples, n_layers).

Returns:

  • q_values_true (Tensor) – Shape (n_samples, n_classes). True-class-conditional aggregated \(q\)-values.

  • q_values_pred (Tensor | None) – Not None only if self.pred_conditional. In this case, same shape as q_values_true. Pred-class-conditional aggregated \(q\)-values.

Note

High \(q\)-value \(\longleftrightarrow\) high conformity.

compute_p_values(reference_tests, query_tests)[source]#

Compute \(p\)-values for query samples.

Following [RCJB21, eq. (8)], for test values \((t_1, ..., t_n)\) from \(n\) layers, we define \(p := \mathbb{P}(T_1\geqslant t_1, ..., T_n\geqslant t_n)\) where \(n\)-tuples \((T_1, ..., T_n)\) are sampled across calibrations samples. This method computes and stacks all such \(p\)-values across all the n-tuples of layers, for n in self.layer_aggregation_tuples.

This implemention is a trivial counting approach. See orthogonal range searching for more efficient methods.

Parameters:
  • reference_tests (Tensor) – Reference tests results, against which to compute \(p\)-values. Shape (n_reference_samples, n_layers).

  • query_tests (Tensor) – Query tests results, for which to compute \(p\)-values. Shape (n_query_samples, n_layers).

Returns:

p_values (Tensor) – Shape (n_query_samples, n_p_values). The number of \(p\)-values can be expressed as sum(num_n_tuples for n in required_tuples).

parse_layer_aggregation()[source]#

Store layer aggregation method and n-tuples to use.

Handles special case of "lpe" aggregation.

prepare_lpe()[source]#

For lpe layer aggregation prepare indexes and query calib.

prepare_tests(all_activations, n_classes)[source]#

Multinomial only for now. Creates indexes and stats tests.

Note

For multinomial test: Sets attributes indexes, all_llrs_true and all_llrs_pred. The last two are nested tuples of shape (n_layers, n_classes), unless not self.pred_conditional, in which case self.all_llrs_true is set to itertools.repeat(None).

run_tests(all_activations)[source]#

Run class-conditional statistical tests.

Parameters:

all_activations (tuple[Tensor]) – Layers’ batched activations, for which to run statistical tests.

Returns:

  • tests_true (Tensor) – Shape (n_classes, n_samples, n_layers). True-class-conditional tests results.

  • tests_pred (Tensor | None) – Not None only if self.pred_conditional. In this case, same shape as tests_true. Predicted-class-conditional tests results.

Note

High test result \(\longleftrightarrow\) high deviation.