DkNN#
- class scio.scores.DkNN(*, act_norm=None, mode='raw', k=?, per_class=False, per_layer=False, aggregation='mean', index_metric='l2')[source]#
Bases:
BaseScoreClassifDkNN for classification.
Vanilla
DkNNis equivalent to nearest neighbors class counting.- Parameters:
k (
int) – Number of nearest neighbors to consider.per_class (
bool) – IfTrue, the quantiles are computed relatively to calibration scores from the candidate class. Defaults toFalse.per_layer (
bool) – IfTrue, scores are separated for each layer, then aggregated at the end, usingaggregationscheme. Defaults toFalse.aggregation (
AggrNameLike | float) – Seeper_layerandAggrName. Defaults to"mean".index_metric (
IndexMetricLike) – Kind of metric to use for nearest neighbors search. SeeIndexMetric. 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 oftest_scoresagainstcalib_scores. Ifcalib_labelswas provided, computed class-wise. Shape(n_test, n_classes). Whennan, returned0.