Kernel Linear#

linear #

CLASS DESCRIPTION
LinearKernel

Linear kernel for computing Gram matrices from feature matrices.

FUNCTION DESCRIPTION
linear_kernel

Compute linear kernel matrix from feature matrix (functional interface).

Classes#

LinearKernel(dtype: torch.dtype = torch.float32) #

Bases: Kernel

Linear kernel for computing Gram matrices from feature matrices.

Computes the linear kernel (dot product kernel) as \(K = X X^\top\). Unlike distance-based kernels, this operates directly on feature matrices.

PARAMETER DESCRIPTION
dtype

Data type for internal tensor computations.

TYPE: torch.dtype, optional, by default torch.float32 DEFAULT: float32

METHOD DESCRIPTION
get_kernel_params

Return current kernel parameters.

forward_step

Compute linear kernel matrix (Gram matrix).

Source code in spectre/kernel/linear.py
def __init__(self, dtype: torch.dtype = torch.float32) -> None:
    super().__init__(
        learnable=False, learnable_log=False, normalization=None, dtype=dtype
    )
Functions#
get_kernel_params() -> dict[str, torch.Tensor] #

Return current kernel parameters.

The linear kernel has no parameters.

RETURNS DESCRIPTION
dict[str, Tensor]

Empty dictionary (no parameters).

Source code in spectre/kernel/linear.py
def get_kernel_params(self) -> dict[str, torch.Tensor]:
    """
    Return current kernel parameters.

    The linear kernel has no parameters.

    Returns
    -------
    dict[str, torch.Tensor]
        Empty dictionary (no parameters).
    """
    return {}
forward_step(X: torch.Tensor, weights: torch.Tensor | None = None, **kwargs) -> torch.Tensor #

Compute linear kernel matrix (Gram matrix).

PARAMETER DESCRIPTION
X

Feature matrix, shape (n_samples, n_features).

TYPE: Tensor

weights

Sample weights (not used in this implementation).

TYPE: torch.Tensor | None, optional, by default None DEFAULT: None

**kwargs

Additional keyword arguments passed to the kernel.

TYPE: dict DEFAULT: {}

RETURNS DESCRIPTION
Tensor

Linear kernel matrix, shape (n_samples, n_samples).

Source code in spectre/kernel/linear.py
def forward_step(
    self,
    X: torch.Tensor,
    weights: torch.Tensor | None = None,
    **kwargs,
) -> torch.Tensor:  # ty: ignore
    """
    Compute linear kernel matrix (Gram matrix).

    Parameters
    ----------
    X : torch.Tensor
        Feature matrix, shape (n_samples, n_features).

    weights : torch.Tensor | None, optional, by default None
        Sample weights (not used in this implementation).

    **kwargs : dict
        Additional keyword arguments passed to the kernel.

    Returns
    -------
    torch.Tensor
        Linear kernel matrix, shape (n_samples, n_samples).
    """
    return linear_kernel(X)

Functions#

linear_kernel(X: torch.Tensor) -> torch.Tensor #

Compute linear kernel matrix from feature matrix (functional interface).

The linear kernel is simply the Gram matrix.

PARAMETER DESCRIPTION
X

Feature matrix, shape (n_samples, n_features).

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

Linear kernel matrix, shape (n_samples, n_samples).

Source code in spectre/kernel/linear.py
def linear_kernel(X: torch.Tensor) -> torch.Tensor:
    """
    Compute linear kernel matrix from feature matrix (functional interface).

    The linear kernel is simply the Gram matrix.

    Parameters
    ----------
    X : torch.Tensor
        Feature matrix, shape (n_samples, n_features).

    Returns
    -------
    torch.Tensor
        Linear kernel matrix, shape (n_samples, n_samples).
    """
    check_2d(X)

    return X @ X.T