Comparator Matcher#
matcher
#
| CLASS | DESCRIPTION |
|---|---|
Matcher |
Base class for dimension matching strategies. |
SVDMatcher |
Match dimensions using Singular Value Decomposition (SVD). |
ProjectionMatcher |
Match dimensions using random projection. |
PaddingMatcher |
Match dimensions by zero-padding the smaller tensor. |
TruncationMatcher |
Match dimensions by truncating the larger tensor. |
SkipMatcher |
Strict matcher that requires exact dimension match. |
| FUNCTION | DESCRIPTION |
|---|---|
match_dimensions |
Match feature dimensions between two activation matrices. |
Classes#
Matcher
#
Bases: Module
Base class for dimension matching strategies.
Matchers are used to align feature dimensions between two activation matrices when comparing neural network representations.
| METHOD | DESCRIPTION |
|---|---|
forward |
Match dimensions between two tensors. |
| ATTRIBUTE | DESCRIPTION |
|---|---|
name |
Get the registered name of this matcher.
TYPE:
|
Attributes#
name: str | None
property
#
Get the registered name of this matcher.
| RETURNS | DESCRIPTION |
|---|---|
str | None
|
The registration name if registered, None otherwise. |
Functions#
forward(X: torch.Tensor, Y: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]
abstractmethod
#
Match dimensions between two tensors.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
First activation matrix, shape (n_samples, n_features).
TYPE:
|
Y
|
Second activation matrix, shape (n_samples, m_features).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Tensor, Tensor]
|
Matched activation matrices with same feature dimension. |
Source code in spectre/comparator/matcher.py
SVDMatcher(variance_threshold: float | None = None)
#
Bases: Matcher
Match dimensions using Singular Value Decomposition (SVD).
Projects activations to a common dimensionality using truncated SVD, which preserves maximum variance. Optionally supports variance-based component selection (SVCCA-style).
| PARAMETER | DESCRIPTION |
|---|---|
variance_threshold
|
If provided, keeps components that explain this fraction of variance (0 < threshold <= 1). If None, projects to min(n_features, m_features).
TYPE:
|
Source code in spectre/comparator/matcher.py
ProjectionMatcher()
#
PaddingMatcher()
#
TruncationMatcher()
#
SkipMatcher()
#
Functions#
initialize_matcher_fn(matcher_fn: Matcher | str | None, matcher_kwargs: dict[str, Any] | None = None) -> Matcher
#
Initialize a matcher from an instance or registry name.
Arguments
matcher_fn : Matcher | str | None Matcher instance or name of registered matcher. matcher_kwargs : dict[str, Any] | None Keyword arguments passed to matcher constructor (only for string names).
| RETURNS | DESCRIPTION |
|---|---|
Matcher
|
Initialized matcher instance. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
TypeError
|
If |
Source code in spectre/comparator/matcher.py
match_dimensions(X: torch.Tensor, Y: torch.Tensor, method: Literal['svd', 'projection', 'padding', 'truncation', 'skip'] = 'padding', variance_threshold: float | None = None, eps: float = torch.finfo(torch.float32).eps) -> tuple[torch.Tensor, torch.Tensor]
#
Match feature dimensions between two activation matrices.
This function provides multiple strategies for handling layers with different feature dimensions.
SVD (for static comparison):
- Preserves maximum variance in the data
- Deterministic and stable
- Projects to min(n_features, m_features) dimensions
Projection:
- Random projection (non-deterministic)
- Fast but may not preserve variance optimally
Padding:
- Simple but adds zeros that may affect kernel computation
- Zero-padded features have different statistical properties
Truncation:
- Simple but discards information from larger representation
- May lose important features
Skip:
- Strict mode requiring exact dimension match
- Use when you want to ensure comparable representations
| PARAMETER | DESCRIPTION |
|---|---|
X
|
First activation matrix, shape (n_samples, n_features).
TYPE:
|
Y
|
Second activation matrix, shape (n_samples, m_features).
TYPE:
|
method
|
Method for dimension matching.
TYPE:
|
variance_threshold
|
For method="svd" only: if provided, determines number of components to keep based on explained variance (0 < threshold <= 1). If None, projects to min(n_features, m_features). This implements variance-based dimensionality reduction as used in SVCCA.
TYPE:
|
eps
|
For method="svd" only: numerical stability parameter for SVD.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Tensor, Tensor]
|
Matched activation matrices with same feature dimension. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If method="skip" and dimensions don't match, or if unknown method. |
Examples:
Static comparison with SVD:
>>> X1 = torch.randn(100, 64)
>>> X2 = torch.randn(100, 32)
>>> X1_matched, X2_matched = match_dimensions(X1, X2, method="svd")
>>> X1_matched.shape, X2_matched.shape
(torch.Size([100, 32]), torch.Size([100, 32]))
Learnable projection for training:
Padding for simple matching:
>>> X1_padded, X2_padded = match_dimensions(X1, X2, method="padding")
>>> X1_padded.shape, X2_padded.shape
(torch.Size([100, 64]), torch.Size([100, 64]))
Source code in spectre/comparator/matcher.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | |