src.main.Viyog
- class src.main.Viyog(model, device=None)[source]
Bases:
objectWrapper that captures first-layer activations and computes an activation-norm based score.
The wrapper finds the first convolutional layer of model (prefers an attribute named
conv1if present) and registers a forward hook that saves the layer’s output for later inspection. Usefit()to compute a training baseline (mean infinity-norm of per-sample flattened activations). Then callscore()orscore_loader()to compute Viyog scores on new data.- Parameters:
model (torch.nn.Module) – The model to wrap. The forward pass must traverse at least one Conv{1,2,3}d module (or provide an attribute named
conv1).device (torch.device | str | None, optional) – Device to use for computation. If
None(default) the device is inferred from model parameters as needed.
- id_norm_scores_mean
Mean per-sample infinity norm computed by
fit().Noneuntil fit completes successfully.- Type:
float or None
- __init__(model, device=None)[source]
- Parameters:
model (torch.nn.Module)
device (Optional[torch.device | str])
Methods
Viyog_Score(num[, Temperature])Convert centered norms into bounded scores in approximately (-1, 1).
__init__(model[, device])close()Remove the hook (call when you no longer need the Viyog wrapper).
fit(self, trainloader)score(self, x[, Temperature])score_loader(loader[, Temperature])Helper that returns a single 1D tensor of scores for the whole loader.
- close()[source]
Remove the hook (call when you no longer need the Viyog wrapper).
- Return type:
None
- static Viyog_Score(num, Temperature=1000.0)[source]
Convert centered norms into bounded scores in approximately (-1, 1).
- Parameters:
num (torch.Tensor) – Tensor (any shape) of centered norms (i.e., per-sample norm minus training mean). Must be on the same device you want the result on.
Temperature (float, optional) – Temperature scaling factor. Default is 1000.0.
- Returns:
Tensor of same shape as
numwith values approx in (-1, 1).- Return type:
torch.Tensor
- fit(trainloader)
Compute the mean of per-sample infinity norms of the first-layer activations across the provided trainloader and store it as a Python float.
- Parameters:
trainloader (torch.utils.data.DataLoader) – DataLoader providing samples to estimate the mean activation norm. Each yielded batch should be (inputs, labels) or where inputs are the first item.
- Returns:
The computed mean per-sample infinity norm (stored in
self.id_norm_scores_mean).- Return type:
float
- score(x, Temperature=1000.0)
If x is a Tensor (batch): returns a 1D tensor of scores for that batch. If x is a DataLoader: processes entire loader and returns concatenated scores.
- Parameters:
x (torch.Tensor or torch.utils.data.DataLoader) – Input batch (tensor) or data loader to score.
Temperature (float, optional) – Temperature scaling factor passed through to
Viyog_Score().
- Returns:
1D tensor of scores (device equals inferred device).
- Return type:
torch.Tensor