BaseScore#
- class scio.scores.BaseScore(*, act_norm=None)[source]#
Bases:
ParamClass,GenericBase class for every scoring algorithms.
A score is defined through:
a calibration procedure, during which In-Distribution data and labels may be observed;
an inference procedure.
These should respectively be carried out in
calibrate()andget_conformity().Scores are paramclasses and hyperparameters are defined as their parameters. Optionally, scores may override
_on_param_will_be_set()and_check_params()(if so, it should callsuper()._check_params()). Refer to source code for examples.For development, the score should not assume anything about which layers are recorded. All that is known is that the
activations()method returns activations following the layers recording order (seeconcatenatekeyword for scores using only one layer).It is also possible to use on-the-fly activations postprocessing during inference (see
Recorder.forward()) and access the network’s named parameters for recorded layers withRecorder.recorded_params(see e.g.GradNorm).For usage, instances should be
fit()before being called:score = Score(param="param") score.fit(rnet, calib_data, calib_labels) out, conformity = score(test_data)
- Parameters:
act_norm (
float, optional) – If provided, the recorded activations are normalized for normact_norm. Should not be0ornan.
Useful methods defined here
__call__(inputs)Return bound net output with conformity estimate.
__repr__()Add fit/unfit information to paramclass repr.
activations(*[, concatenate])Return last recorded activations of bound net.
calibrate(calib_data, calib_labels)Calibrate the score with In-Distribution data.
fit(rnet, calib_data, calib_labels)Fit the score for given
rnetand In-Distribution data.get_conformity(inputs)Compute output and associated conformity at inference.
Reset
timer.unfit()Unfit the score instance.
Useful attributes defined here
The
ScoreTimerinstance forself.- __call__(inputs)[source]#
Return bound net output with conformity estimate.
- Parameters:
inputs (
Tensor) – Input samples to process.- Returns:
out (
Tensor) – The natural output of the bound neural network.conformities (
Conformities) – The associated conformity.
- Raises:
RuntimeError – If the instance is not fit.
- __repr__()[source]#
Add fit/unfit information to paramclass repr.
Example
>>> KNN() KNN[unfit](act_norm=2.0, mode='raw', k=?, index_metric='l2')
- activations(*, concatenate=False)[source]#
Return last recorded activations of bound net.
During a standard forward pass,
Recordernets record the requested activations. This method retrieves those, with additional per-sample normalization if specified byact_norm. The normalization occurs layer-wise, unlessconcatenate is True.- Parameters:
concatenate (
bool) – IfTrue, flattens every sample at every layer and concatenates across layers, in which caseoutis a \(1\)-tuple of oneTensorwith shape(n_samples, tot_data_dim). This option is handy for scoring techniques that only work with one latent space: concatenating across layers mimicks having recorded only one layer. Keep in mind that it might be preferable to only record one layer in the first place, if the technique is designed this way. Defaults toFalse.- Returns:
out (
tuple[Tensor, ...]) – The last recorded activations. It is a tuple of length the number of recorded layers ifnot contatenate, otherwise one. Ifnot concatenate, the shape of samples is untouched.
- abstractmethod calibrate(calib_data, calib_labels)[source]#
Calibrate the score with In-Distribution data.
- Parameters:
calib_data (
Tensor) – Batched calibration samples. Shape(n_calib, *sample_shape).calib_labels (
Labels) – Batched labels.
- abstractmethod get_conformity(inputs)[source]#
Compute output and associated conformity at inference.
- Parameters:
inputs (
Tensor) – Batched data to be processed by the bound neural network. Shape(n_sample, *sample_shape).- Returns:
out (
Tensor) – The neural network output. Shape(n_samples, *output_sample_shape).conformity (
Conformities) – Associated conformities.