Comparator Model#
model
#
| CLASS | DESCRIPTION |
|---|---|
ModelComparator |
Interface for comparing neural network models. |
Classes#
ModelComparator(comparator_fn: Comparator | str = 'feature', comparator_kwargs: dict | None = None, matcher_fn: Matcher | str = 'padding', matcher_kwargs: dict | None = None, reduce: Literal['mean', 'median', 'min', 'max'] | None = 'mean')
#
Interface for comparing neural network models.
This class computes pairwise similarities between all layers of two models, returning an \(N \times M\) comparison matrix where \(N\) and \(M\) are the number of layers in each model.
| PARAMETER | DESCRIPTION |
|---|---|
comparator_fn
|
Name of comparison metric to use. Must be registered in
TYPE:
|
comparator_kwargs
|
Additional keyword arguments passed to the comparator.
TYPE:
|
matcher_fn
|
Dimension matching strategy: "svd", "projection", "padding", "truncation", "skip" or Matcher.
TYPE:
|
matcher_kwargs
|
Additional keyword arguments passed to the matcher.
TYPE:
|
reduce
|
How to reduce the score matrix into a single score.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
check_fitted |
Check if model has been fitted, raises error if not. |
fit |
Compare two models by computing pairwise layer similarities. |
get_results |
Get all fitted parameters as a dictionary. |
| ATTRIBUTE | DESCRIPTION |
|---|---|
score |
Returns the scores computed during the fit method.
TYPE:
|
reduce_score |
Returns the reduced scores computed during the fit method.
TYPE:
|
Source code in spectre/comparator/model.py
Attributes#
score: torch.Tensor
property
#
Returns the scores computed during the fit method.
reduce_score: torch.Tensor
property
#
Returns the reduced scores computed during the fit method.
Functions#
check_fitted() -> None
#
fit(model_1: torch.nn.Module | Parametric, model_2: torch.nn.Module | Parametric, X: torch.Tensor, Y: torch.Tensor | None = None, layers_1: list[str] | None = None, layers_2: list[str] | None = None) -> ModelComparator
#
Compare two models by computing pairwise layer similarities.
This method computes an \(N imes M\) comparison matrix where \(N\) is the
number of layers in model_1 and \(M\) is the number of layers in model_2.
Each entry (i, j) contains the similarity score between layer i of model_1
and layer j of model_2.
Optionally, if Y is provided, the method computes the similarity between
the output of model_1 on input X and model_2 on the input Y.
| PARAMETER | DESCRIPTION |
|---|---|
model_1
|
First model (
TYPE:
|
model_2
|
Second model (
TYPE:
|
X
|
Input data to pass through both models if
TYPE:
|
Y
|
Input data to pass through
TYPE:
|
layers_1
|
Layer names to extract from
TYPE:
|
layers_2
|
Layer names to extract from
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ModelComparator
|
Self for chaining. |
Examples:
>>> comparator = ModelComparator("kernel", matcher="svd", reduce=None)
>>> comparator.fit(model1, model2, X)
>>> comparator.score.shape
torch.Size([10, 12]) # model1 has 10 layers, model2 has 12 layers
>>> result = comparator.get_results()
>>> result["features_1"].keys()
dict_keys(['conv1', 'relu1', 'conv2', ...])
>>> result["features_2"].keys()
dict_keys(['layer1', 'layer2', ...])
With reduction:
>>> comparator = ModelComparator("kernel", reduce="mean")
>>> comparator.fit(model1, model2, X)
>>> comparator.score # Reduced score
tensor(0.823)
Source code in spectre/comparator/model.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |
get_results() -> dict[str, torch.Tensor | dict[str, torch.Tensor] | str | None]
#
Get all fitted parameters as a dictionary.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Tensor | dict[str, Tensor] | str | None]
|
Result dictionary with keys:
|