mlvbench.probes¶
Probes.
Probes are small models that readout information from intermediate features of a pretrained vision model.
LinearProbe
¶
Bases: Probe
Linear probe.
A linear projection of the input features to a scalar, optionally with a learnable bias:
name
property
¶
Name of the probe type ("linear").
__init__(layer, input_dim, bias='scalar', num_patches=None, metadata=None)
¶
Initialize the probe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layer
|
str
|
Name of the model layer the probe reads from. |
required |
input_dim
|
int
|
Number of dimensions of the input features. |
required |
bias
|
Literal['none', 'scalar', 'spatial'] | Tensor
|
The kind of learnable bias added to the output:
|
'scalar'
|
num_patches
|
int | None
|
Number of patch tokens of the input features. Required when
using a |
None
|
metadata
|
dict[str, Any] | None
|
Free-form metadata to attach to the probe. |
None
|
forward(features)
¶
Forward pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
Features
|
The features extracted from the model. The probe uses the patch
tokens of |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
The output with shape |
select_features(features)
¶
Select the part of the feature representation used by the probe.
Defaults to the patch tokens of self.layer with shape (B, N, C). Override to
read from a different part of the representation (e.g. the cls token or a
different token format).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
Features
|
The features extracted from the backbone. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
The selected feature tensor. |
serialize()
¶
Serialize the probe.
See Probe.serialize.
Probe
¶
Bases: Module, ABC
Abstract base class for probes.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Name of the probe type. |
layer |
str
|
Name of the backbone layer the probe reads from. |
metadata |
dict[str, Any]
|
Free-form dict of training/experiment information attached to the probe. Persisted in full when the probe is saved. |
name
abstractmethod
property
¶
Name of the probe type.
__init__(layer, input_dim, *, metadata=None)
¶
forward(features)
abstractmethod
¶
Apply the probe to the given features.
The probe selects the part of the representation it reads from via
select_features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
Features
|
The features extracted from the backbone. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
The prediction from the probe as torch.Tensor. Shape and dtype very per probe. |
select_features(features)
¶
Select the part of the feature representation used by the probe.
Defaults to the patch tokens of self.layer with shape (B, N, C). Override to
read from a different part of the representation (e.g. the cls token or a
different token format).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
Features
|
The features extracted from the backbone. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
The selected feature tensor. |
serialize()
abstractmethod
¶
Return all information needed to reconstruct the probe.
This returns a self-describing dictionary that includes both the torch
state_dict and all config options. The probe can be rebuilt from this
representation via build_probe.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A dictionary describing the probe, with detached CPU tensors for |
build_probe(name, layer, input_dim, metadata=None, state_dict=None, probe_types=None, **kwargs)
¶
Build the probe with the given name and arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the probe type (e.g. |
required |
layer
|
str
|
Name of the model layer the probe reads from. |
required |
input_dim
|
int
|
Number of dimensions of the input features. |
required |
metadata
|
dict[str, Any] | None
|
Free-form metadata to attach to the probe. |
None
|
state_dict
|
dict[str, Any] | None
|
Optional state dict for restoring pretrained probes. |
None
|
probe_types
|
dict[str, type[Probe]] | None
|
Optional mapping of custom probe type names to |
None
|
**kwargs
|
Any
|
Additional keyword arguments forwarded to the probe constructor (e.g.
|
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |