DkNN#

class scio.scores.DkNN(*, act_norm=None, mode='raw', k=?, per_class=False, per_layer=False, aggregation='mean', index_metric='l2')[source]#

Bases: BaseScoreClassif

DkNN for classification.

Vanilla DkNN is equivalent to nearest neighbors class counting.

Parameters:
  • k (int) – Number of nearest neighbors to consider.

  • per_class (bool) – If True, the quantiles are computed relatively to calibration scores from the candidate class. Defaults to False.

  • per_layer (bool) – If True, scores are separated for each layer, then aggregated at the end, using aggregation scheme. Defaults to False.

  • aggregation (AggrNameLike | float) – See per_layer and AggrName. Defaults to "mean".

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

  • mode – See BaseScoreClassif.

  • act_norm – See BaseScore.

References

[PM18]

Nicolas Papernot and Patrick McDaniel. Deep k-nearest neighbors: towards confident, interpretable and robust Deep Learning. arXiv, 2018. URL: https://arxiv.org/abs/1803.04765.

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

quantiles(calib_scores, test_scores[, ...])

Left quantile of test_scores, potentially class-wise.

static quantiles(calib_scores, test_scores, calib_labels=None)[source]#

Left quantile of test_scores, potentially class-wise.

Parameters:
  • calib_scores (Tensor) – MUST BE SORTED. Population to compute quantiles against. Shape (n_calib,).

  • test_scores (Tensor) – Query scores for quantile computation. There is one score per sample and per candidate class. Shape (n_test, n_classes).

  • calib_labels (Tensor, optional) – If provided, labels of calibration samples, in which case the quantiles are computed class-wise. Shape (n_calib,).

Returns:

out (Tensor) – Left quantile of test_scores against calib_scores. If calib_labels was provided, computed class-wise. Shape (n_test, n_classes). When nan, returned 0.