Kernel Gaussian#
gaussian
#
| CLASS | DESCRIPTION |
|---|---|
GaussianKernel |
Gaussian kernel for computing pairwise similarity matrices. |
| FUNCTION | DESCRIPTION |
|---|---|
gaussian_kernel |
Compute Gaussian kernel matrix from distance matrix (functional interface). |
Classes#
GaussianKernelResult
#
Bases: NamedTuple
Result container for Gaussian kernels.
| ATTRIBUTE | DESCRIPTION |
|---|---|
kernel |
Computed Gaussian kernel matrix.
TYPE:
|
bandwidth |
Squared bandwidth matrix used in computation, broadcasted to match kernel shape.
TYPE:
|
GaussianKernel(bw_method: str | torch.Tensor = 'median', learnable: bool = False, learnable_log: bool = True, normalization: KernelNormalizer | None = None, bw_kwargs: dict | None = None, dtype: torch.dtype = torch.float32)
#
Bases: Kernel
Gaussian kernel for computing pairwise similarity matrices.
Implements the Gaussian kernel with flexible bandwidth estimation. The kernel transforms distances into similarities using the exponential function, with the bandwidth parameter controlling the kernel width.
Supports both square (n_samples, n_samples) and non-square (n_samples, m_samples) distance matrices. Non-square matrices are useful for out-of-sample extensions like Nyström approximation.
Kernel form: \(K(d_{kl}) = \exp(-d_{kl}^2 / \sigma_{kl}^2)\), where \(d_{kl}\) is pairwise distance between samples \(k\) and \(l\), and \(\sigma_{kl}^2\) is the squared bandwidth (can be constant or adaptive per sample pair).
| PARAMETER | DESCRIPTION |
|---|---|
bw_method
|
Bandwidth estimation method or constant value. Available methods:
TYPE:
|
learnable
|
Whether constant bandwidth should be learnable during training.
TYPE:
|
learnable_log
|
Whether to parameterize learnable bandwidth in log space for numerical stability.
TYPE:
|
normalization
|
Kernel normalization instance. If None, no normalization is applied.
TYPE:
|
bw_kwargs
|
Parameters passed to bandwidth computation. See
TYPE:
|
dtype
|
Data type for tensors.
TYPE:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
bw |
Squared bandwidth matrix \(\sigma^2\) with shape matching the input distance matrix. Always stores the squared, broadcasted bandwidth for consistent computation across all bandwidth methods. Set after each forward pass.
TYPE:
|
bw_method |
The bandwidth estimation method being used.
TYPE:
|
bw_constant |
Constant bandwidth value if
TYPE:
|
bw_learnable |
Learnable bandwidth parameter if
TYPE:
|
Examples:
Basic usage with median bandwidth:
>>> import torch
>>> from spectre.kernel import GaussianKernel
>>> distances = torch.rand(100, 100)
>>> kernel = GaussianKernel(bw_method="median")
>>> K = kernel(distances)
>>> K.shape
torch.Size([100, 100])
Constant bandwidth:
>>> kernel = GaussianKernel(bw_method=torch.tensor(0.5))
>>> K = kernel(distances)
>>> kernel.bw[0, 0] # Squared bandwidth: 0.5² = 0.25
tensor(0.25)
Learnable bandwidth:
>>> kernel = GaussianKernel(
... bw_method=torch.tensor(1.0),
... learnable=True,
... learnable_log=True,
... )
>>> K = kernel(distances)
>>> kernel.bw_learnable.grad # Can optimize bandwidth via backprop
Learnable per-sample bandwidth (vector):
>>> # Learn different bandwidth for each sample (row-specific)
>>> bw_init = torch.ones(100, 1) * 0.5 # Shape (n_samples, 1)
>>> kernel = GaussianKernel(bw_method=bw_init, learnable=True)
>>> K = kernel(distances)
>>> kernel.bw_learnable.shape # (100, 1) - one bandwidth per row
Adaptive k-NN bandwidth:
>>> kernel = GaussianKernel(bw_method="knn", bw_kwargs={"k_neighbor": 10})
>>> K = kernel(distances)
Custom quantile:
Notes
- Bandwidth choice affects kernel behavior:
- Small: localized, sharp transitions
- Large: global, smooth transitions
- Adaptive methods ("knn", "median_1d") handle varying data density better
- The
bwattribute always stores σ² (squared bandwidth), broadcasted to distance matrix shape, for consistent computation across all methods - Per-dimension methods ("median_1d", "quantile_1d") create adaptive bandwidth matrices via outer products
- Constant tensor bandwidths can be scalar or vector (broadcast to matrix shape)
- Learnable bandwidths support flexible shapes:
- Scalar (0D or 1D with 1 element): Single bandwidth for entire kernel
- Row vector (n, 1): Per-sample bandwidth (different for each row)
- Column vector (1, m): Per-feature bandwidth or per-sample if the matrix represents pairwise distances (different for each column)
- Matrix (n, m): Element-wise adaptive bandwidth (most flexible)
| METHOD | DESCRIPTION |
|---|---|
forward_step |
Compute Gaussian kernel matrix. |
get_kernel_params |
Return current bandwidth parameter from last forward pass. |
freeze_params |
Freeze bandwidth to prevent recomputation on new data. |
from_pdist |
Create kernel with bandwidth estimated from a pairwise distance matrix. |
Source code in spectre/kernel/gaussian.py
Functions#
forward_step(D: torch.Tensor, weights: torch.Tensor | None = None, **kwargs) -> torch.Tensor
#
Compute Gaussian kernel matrix.
Delegates to gaussian_kernel() function and caches bandwidth.
| PARAMETER | DESCRIPTION |
|---|---|
D
|
Distance matrix, shape (n_samples, n_samples) or (n_samples, m_samples).
TYPE:
|
weights
|
Per-sample importance weights for reweighted bandwidth estimation. When provided, bandwidth is estimated under the reweighted distribution, correcting for sampling bias in biased simulations.
TYPE:
|
**kwargs
|
Additional keyword arguments passed to the kernel.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Gaussian kernel matrix with same shape as D. |
Notes
- Delegates all computation to
gaussian_kernel()function - Sets
GaussianKernel.bwto the squared, broadcasted bandwidth matrix - Normalization (if required) is handled by parent
Kernel.forward() - When
weightsare provided andbw_methodis a string (e.g., "median"), the bandwidth estimation accounts for importance weights to prevent inflated bandwidths from biased sampling
Source code in spectre/kernel/gaussian.py
get_kernel_params() -> dict[str, torch.Tensor]
#
Return current bandwidth parameter from last forward pass.
Returns the actual bandwidth matrix used in kernel computation. This is \(\sigma^2\) (squared bandwidth) broadcast to match the distance matrix shape from the most recent forward pass.
For compact scalar statistics, use get_param_summary() instead.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Tensor]
|
Dictionary with "bandwidth" key containing \(\sigma^2\) matrix from
last forward pass. Returns empty dict if |
Examples:
Basic usage after forward pass:
>>> kernel = GaussianKernel(bw_method=torch.tensor(1.0), learnable=True)
>>> D = torch.rand(3, 3)
>>> _ = kernel(D)
>>> params = kernel.get_kernel_params()
>>> params["bandwidth"].shape
torch.Size([3, 3])
>>> # Returns squared, broadcast bandwidth used in computation
Before forward pass:
>>> kernel = GaussianKernel(bw_method="median")
>>> params = kernel.get_kernel_params()
>>> params
{}
Logging kernel parameters during training with PyTorch Lightning:
>>> import torch
>>> from spectre.parametric import SpectralMap
>>> from spectre.kernel import GaussianKernel
>>> from spectre.utils.callbacks import MetricsCallback, kernel_params_extractor
>>> from pytorch_lightning import Trainer
>>>
>>> # Create spectral map with learnable bandwidth
>>> kernel = GaussianKernel(bw_method=torch.tensor(1.0), learnable=True)
>>> spectral_map = SpectralMap(model=my_encoder, kernel_fn=kernel, n_states=3)
>>>
>>> # Create callback to log kernel parameters
>>> callback = MetricsCallback(
... extractors=[kernel_params_extractor],
... log_every_n_epochs=1,
... )
>>>
>>> # Train with callback
>>> trainer = Trainer(callbacks=[callback], max_epochs=100)
>>> trainer.fit(spectral_map, train_dataloader)
>>>
>>> # Logged metrics will include:
>>> # - bandwidth_mean: mean of squared bandwidth matrix
>>> # - bandwidth_std: std of squared bandwidth matrix
>>> # - bandwidth_min: min of squared bandwidth matrix
>>> # - bandwidth_max: max of squared bandwidth matrix
>>> # - kernel_type: "GaussianKernel"
>>> # - kernel_learnable: True
Notes
The returned bandwidth is \(\sigma^2\), not \(\sigma\), and is broadcast to the shape of the distance matrix from the last forward pass. This represents the actual values used in the kernel computation: \(K = \exp(-D^2/\sigma^2)\).
Source code in spectre/kernel/gaussian.py
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 | |
freeze_params() -> GaussianKernel
#
Freeze bandwidth to prevent recomputation on new data.
Converts dynamic bandwidth methods (e.g., "median", "scott") to constant bandwidth using the current cached value from the last forward pass.
| RETURNS | DESCRIPTION |
|---|---|
GaussianKernel
|
Returns self for method chaining. |
| RAISES | DESCRIPTION |
|---|---|
RuntimeError
|
If called before any forward pass (no bandwidth cached). |
Source code in spectre/kernel/gaussian.py
from_pdist(D: torch.Tensor, bw_method: str = 'median', learnable: bool = False, learnable_log: bool = True, normalization: KernelNormalizer | None = None, bw_kwargs: dict | None = None, dtype: torch.dtype = torch.float32) -> GaussianKernel
classmethod
#
Create kernel with bandwidth estimated from a pairwise distance matrix.
Computes bandwidth from the distance distribution using the specified
method and returns a GaussianKernel with constant bandwidth. Can be
used to get a data-driven estimate of bandwidth as an initial value
optimized during learning.
| PARAMETER | DESCRIPTION |
|---|---|
D
|
Pairwise distance matrix, shape
TYPE:
|
bw_method
|
Bandwidth estimation method to apply to
TYPE:
|
learnable
|
Whether the estimated bandwidth should be learnable.
TYPE:
|
learnable_log
|
Whether to parameterize in log space when learnable.
TYPE:
|
normalization
|
Kernel normalization specification.
TYPE:
|
bw_kwargs
|
Additional parameters for bandwidth estimation.
TYPE:
|
dtype
|
Data type.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
GaussianKernel
|
Kernel with constant bandwidth derived from |
Examples:
>>> import torch
>>> from spectre.kernel import GaussianKernel
>>> X = torch.randn(100, 10)
>>> D = torch.cdist(X, X)
>>> kernel = GaussianKernel.from_pdist(D, bw_method="median")
>>> K = kernel(D)
Source code in spectre/kernel/gaussian.py
Functions#
gaussian_kernel(D: torch.Tensor, bw_method: str | torch.Tensor = 'median', bw_kwargs: dict | None = None, dtype: torch.dtype = torch.float32, weights: torch.Tensor | None = None) -> GaussianKernelResult
#
Compute Gaussian kernel matrix from distance matrix (functional interface).
Core Gaussian kernel computation. Returns both the kernel matrix and the bandwidth values used in computation.
This is a pure function that computes the unnormalized Gaussian kernel. For
normalized kernels, use the GaussianKernel class with a normalization.
| PARAMETER | DESCRIPTION |
|---|---|
D
|
Distance matrix, shape (n_samples, n_samples) or (n_samples, m_samples).
TYPE:
|
bw_method
|
Bandwidth estimation method or constant value. Available methods:
TYPE:
|
bw_kwargs
|
Additional parameters for bandwidth methods via dict. Supported keys:
TYPE:
|
dtype
|
Data type for tensors.
TYPE:
|
weights
|
Per-sample importance weights, shape (n_samples,). When provided, bandwidth is estimated under the reweighted distribution to correct for sampling bias. Only used for string-based bandwidth methods; ignored for constant/learnable tensor bandwidths.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
GaussianKernelResult
|
NamedTuple containing:
|
Examples:
Basic usage with median bandwidth:
>>> import torch
>>> from spectre.kernel import gaussian_kernel
>>> D = torch.rand(50, 50)
>>> result = gaussian_kernel(D, bw_method="median")
>>> result.kernel.shape
torch.Size([50, 50])
>>> result.bandwidth.shape
torch.Size([50, 50])
Constant bandwidth:
>>> result = gaussian_kernel(D, bw_method=torch.tensor(0.5))
>>> result.kernel[0, 0] # Diagonal should be 1.0
tensor(1.)
Custom quantile:
Adaptive k-NN bandwidth:
Source code in spectre/kernel/gaussian.py
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 | |