kernel
Selects and applies a specified kernel function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kernel_name
|
str
|
The name of the kernel to apply. Supported kernels include: - 'linear_kernel', 'polynomial_kernel', 'hyperbolic_tangent_kernel' - 'exponential_kernel', 'cosine_similarity_kernel', 'euclidean_distance' - 'minkowski_distance', 'manhattan_distance', 'chebyshev_distance' - 'canberra_distance', 'gaussian_rbf_kernel', 'laplacian_kernel', - 'anisotropic_rbf_kernel', 'custom_hybrid_kernel'. |
required |
x
|
Tensor
|
Input tensor. Required for most kernels. |
None
|
x2
|
Tensor
|
Secondary input tensor. Required for non-batch kernels. |
None
|
Returns:
Type | Description |
---|---|
Tensor
|
The result of the selected kernel function. |
Raises:
Type | Description |
---|---|
ValueError
|
If the input tensors are invalid. |
Examples:
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the linear kernel (inner product) between tensors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
The input tensor. |
required |
x2
|
Tensor
|
Secondary input tensor for pairwise kernel computation. |
None
|
centered
|
bool
|
Whether to center the data before computing the kernel. |
False
|
dim
|
int
|
The dimension along which the kernel is computed. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
The kernel matrix or scalar value. |
Raises:
Type | Description |
---|---|
ValueError
|
If input tensors are invalid. |
Examples:
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the dot product between two 1D tensors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x1
|
Tensor
|
The first input tensor. |
required |
x2
|
Tensor
|
The second input tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
The scalar dot product. |
Raises:
Type | Description |
---|---|
ValueError
|
If tensors are invalid or have mismatched dimensions. |
Examples:
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the batch linear kernel matrix for a 2D tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor of shape (b, m). |
required |
centered
|
bool
|
Whether to center the data. |
False
|
dim
|
int
|
The dimension along which the kernel is computed. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
The linear kernel matrix of shape (m, m). |
Raises:
Type | Description |
---|---|
ValueError
|
If the input tensor is invalid. |
Examples:
>>> batch_linear_kernel(torch.tensor([[1.0, 2.0], [3.0, 4.0]]), dim=1)
tensor([[ 5., 11.],
[11., 25.]])
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the polynomial kernel between tensors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
x2
|
Tensor
|
Secondary input tensor for pairwise computation. |
None
|
centered
|
bool
|
Whether to center the data. |
False
|
c
|
float
|
Constant term in the kernel. |
0.0
|
d
|
int
|
Degree of the polynomial. |
1
|
dim
|
int
|
The dimension along which the kernel is computed. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
The polynomial kernel matrix. |
Raises:
Type | Description |
---|---|
ValueError
|
If the inputs are invalid or incompatible. |
Examples:
>>> polynomial_kernel(torch.tensor([[1.0, 2.0], [3.0, 4.0]]), d=2, dim=1)
tensor([[ 25., 121.],
[121., 625.]])
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the polynomial kernel for two 1D tensors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x1
|
Tensor
|
The first input tensor. |
required |
x2
|
Tensor
|
The second input tensor. |
required |
c
|
float
|
Constant term in the kernel. |
0.0
|
d
|
int
|
Degree of the polynomial. |
1
|
Returns:
Type | Description |
---|---|
Tensor
|
The scalar polynomial kernel value. |
Raises:
Type | Description |
---|---|
ValueError
|
If the inputs are invalid or incompatible. |
Examples:
>>> instance_polynomial_kernel(torch.tensor([1.0, 2.0]), torch.tensor([3.0, 4.0]), d=2)
tensor(121.)
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the batch polynomial kernel matrix for a 2D tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor of shape (b, m). |
required |
centered
|
bool
|
Whether to center the data. |
False
|
c
|
float
|
Constant term in the kernel. |
0.0
|
d
|
int
|
Degree of the polynomial. |
1
|
dim
|
int
|
The dimension along which the kernel is computed. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
The polynomial kernel matrix of shape (m, m). |
Raises:
Type | Description |
---|---|
ValueError
|
If the input tensor is invalid. |
Examples:
>>> batch_polynomial_kernel(torch.tensor([[1.0, 2.0], [3.0, 4.0]]), d=2, dim=1)
tensor([[ 25., 121.],
[121., 625.]])
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the hyperbolic tangent kernel for tensors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
x2
|
Tensor
|
Secondary input tensor for pairwise computation. |
None
|
centered
|
bool
|
Whether to center the data. |
False
|
c
|
float
|
Bias term in the kernel. |
0.0
|
alpha
|
float
|
Slope of the activation function. |
1.0
|
dim
|
int
|
The dimension along which the kernel is computed. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
The hyperbolic tangent kernel matrix or scalar value. |
Examples:
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the hyperbolic tangent kernel for two 1D tensors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x1
|
Tensor
|
The first input tensor. |
required |
x2
|
Tensor
|
The second input tensor. |
required |
c
|
float
|
Bias term in the kernel. |
0.0
|
alpha
|
float
|
Slope of the activation function. |
1.0
|
Returns:
Type | Description |
---|---|
Tensor
|
The scalar kernel value. |
Examples:
>>> instance_hyperbolic_tangent_kernel(torch.tensor([1.0, 2.0]), torch.tensor([3.0, 4.0]))
tensor(1.)
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the batch hyperbolic tangent kernel matrix for a 2D tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor of shape (b, m). |
required |
centered
|
bool
|
Whether to center the data. |
False
|
c
|
float
|
Bias term in the kernel. |
0.0
|
alpha
|
float
|
Slope of the activation function. |
1.0
|
dim
|
int
|
The dimension along which the kernel is computed. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
The kernel matrix of shape (m, m). |
Examples:
>>> batch_hyperbolic_tangent_kernel(torch.tensor([[1.0, 2.0], [3.0, 4.0]]))
tensor([[1., 1.],
[1., 1.]])
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the cosine similarity kernel for tensors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
x2
|
Tensor
|
Secondary input tensor for pairwise computation. |
None
|
centered
|
bool
|
Whether to center the data. |
False
|
dim
|
int
|
The dimension along which the kernel is computed. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
The cosine similarity matrix or scalar value. |
Examples:
>>> cosine_similarity_kernel(torch.tensor([[1.0, 2.0], [3.0, 4.0]]), dim=1)
tensor([[1.0000, 0.9839],
[0.9839, 1.0000]])
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the cosine similarity between two 1D tensors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x1
|
Tensor
|
The first input tensor. |
required |
x2
|
Tensor
|
The second input tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
The scalar similarity value. |
Examples:
>>> instance_cosine_similarity_kernel(torch.tensor([1.0, 2.0]), torch.tensor([3.0, 4.0]))
tensor(0.9839)
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the batch cosine similarity kernel matrix for a 2D tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor of shape (b, m). |
required |
centered
|
bool
|
Whether to center the data. |
False
|
dim
|
int
|
The dimension along which the kernel is computed. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
The cosine similarity matrix of shape (m, m). |
Examples:
>>> batch_cosine_similarity_kernel(torch.tensor([[1.0, 2.0], [3.0, 4.0]]), dim=1)
tensor([[1.0000, 0.9839],
[0.9839, 1.0000]])
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the Minkowski distance kernel.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
x2
|
Tensor
|
Secondary input tensor for pairwise computation. |
None
|
p
|
Union[int, float, str, Any]
|
Order of the Minkowski distance. |
None
|
centered
|
bool
|
Whether to center the data. |
False
|
dim
|
int
|
The dimension along which the kernel is computed. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
The distance kernel matrix or scalar value. |
Examples:
>>> minkowski_distance_kernel(torch.tensor([[1.0, 2.0], [3.0, 4.0]]), p=2, dim=1)
tensor([[1.0000, 0.0591],
[0.0591, 1.0000]])
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the Minkowski distance between tensors, either as a batch or for a single pair.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
The input tensor. For pairwise computation, this is the first tensor (x1). |
required |
x2
|
Tensor
|
The second input tensor for pairwise computation. If not provided, distances are computed in batch mode. |
None
|
p
|
Union[int, float, str, Any]
|
The order of the Minkowski distance. Must be positive or a valid norm type (e.g., 'fro', 'nuc'). |
None
|
centered
|
bool
|
Whether to center the data when computing distances in batch mode. |
False
|
dim
|
int
|
The dimension along which the distances are computed when in batch mode. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
|
Examples:
Pairwise Minkowski distance:
Batch Minkowski distance:
>>> minkowski_distance(torch.tensor([[1.0, 2.0], [3.0, 4.0]]), p=2, dim=1)
tensor([[0.0000, 2.8284],
[2.8284, 0.0000]])
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the Minkowski distance between two 1D tensors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x1
|
Tensor
|
The first input tensor. |
required |
x2
|
Tensor
|
The second input tensor. |
required |
p
|
Union[int, float, str, Any]
|
The order of the Minkowski distance. Must be positive or a valid norm type. |
None
|
Returns:
Type | Description |
---|---|
Tensor
|
The Minkowski distance as a scalar value. |
Examples:
>>> instance_minkowski_distance(torch.tensor([1.0, 2.0]), torch.tensor([3.0, 4.0]), p=2)
tensor(2.8284)
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the Minkowski distance matrix for a batch of vectors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor of shape (b, m). |
required |
p
|
Union[int, float, str, Any]
|
The order of the Minkowski distance. Must be positive or a valid norm type. |
None
|
centered
|
bool
|
Whether to center the data. |
False
|
dim
|
int
|
The dimension along which the distances are computed. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
A distance matrix of shape (m, m). |
Examples:
>>> batch_minkowski_distance(torch.tensor([[1.0, 2.0], [3.0, 4.0]]), p=2)
tensor([[0.0000, 1.4142],
[1.4142, 0.0000]])
>>> batch_minkowski_distance(torch.tensor([[1.0, 2.0], [3.0, 4.0]]), p=2, dim=1)
tensor([[0.0000, 2.8284],
[2.8284, 0.0000]])
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the Manhattan distance kernel, which is the exponential of the negative Manhattan distance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
The input tensor. For pairwise computation, this is the first tensor (x1). |
required |
x2
|
Tensor
|
The second input tensor for pairwise computation. If not provided, distances are computed in batch mode. |
None
|
centered
|
bool
|
Whether to center the data when computing distances in batch mode. |
False
|
dim
|
int
|
The dimension along which the distances are computed when in batch mode. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
The Manhattan distance kernel. Shape depends on whether x2 is provided or not: - If x2 is provided: A scalar kernel value. - If x2 is None: A kernel matrix of shape (m, m). |
Examples:
>>> manhattan_distance_kernel(torch.tensor([[1.0, 2.0], [3.0, 4.0]]), dim=1)
tensor([[1.0000, 0.0183],
[0.0183, 1.0000]])
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the Manhattan distance between tensors, either as a batch or for a single pair.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
The input tensor. For pairwise computation, this is the first tensor (x1). |
required |
x2
|
Tensor
|
The second input tensor for pairwise computation. If not provided, distances are computed in batch mode. |
None
|
centered
|
bool
|
Whether to center the data when computing distances in batch mode. |
False
|
dim
|
int
|
The dimension along which the distances are computed when in batch mode. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
|
Examples:
Pairwise Manhattan distance:
Batch Manhattan distance:
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the Manhattan distance (L1 norm) between two tensors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x1
|
Tensor
|
The first input tensor. Must be 1-dimensional. |
required |
x2
|
Tensor
|
The second input tensor. Must be 1-dimensional and have the same shape as x1. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
The Manhattan distance between x1 and x2. |
Examples:
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the Manhattan distance (L1 norm) between all pairs of rows in a batch of tensors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
The input tensor. Must be 2-dimensional. |
required |
centered
|
bool
|
Whether to center the data when computing distances. |
False
|
dim
|
int
|
The dimension along which the distances are computed. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
A matrix of pairwise Manhattan distances with shape (m, m), where m is the number of rows in x. |
Examples:
>>> batch_manhattan_distance(torch.tensor([[1.0, 2.0], [3.0, 4.0]]), dim=1)
tensor([[0., 4.],
[4., 0.]])
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the Euclidean distance kernel, which is the exponential of the negative Euclidean distance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
The input tensor. For pairwise computation, this is the first tensor (x1). |
required |
x2
|
Tensor
|
The second input tensor for pairwise computation. If not provided, distances are computed in batch mode. |
None
|
centered
|
bool
|
Whether to center the data when computing distances in batch mode. |
False
|
dim
|
int
|
The dimension along which the distances are computed in batch mode. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
The Euclidean distance kernel. Shape depends on whether x2 is provided: - If x2 is provided: A scalar kernel value. - If x2 is None: A kernel matrix of shape (m, m). |
Examples:
>>> euclidean_distance_kernel(torch.tensor([[1.0, 2.0], [3.0, 4.0]]), dim=1)
tensor([[1.0000, 0.0591],
[0.0591, 1.0000]])
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the Euclidean distance between tensors, either as a batch or for a single pair.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
The input tensor. For pairwise computation, this is the first tensor (x1). |
required |
x2
|
Tensor
|
The second input tensor for pairwise computation. If not provided, distances are computed in batch mode. |
None
|
centered
|
bool
|
Whether to center the data when computing distances in batch mode. |
False
|
dim
|
int
|
The dimension along which the distances are computed in batch mode. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
|
Examples:
Pairwise Euclidean distance:
Batch Euclidean distance:
>>> euclidean_distance(torch.tensor([[1.0, 2.0], [3.0, 4.0]]), dim=1)
tensor([[0.0000, 2.8284],
[2.8284, 0.0000]])
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the Euclidean distance (L2 norm) between two tensors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x1
|
Tensor
|
The first input tensor. Must be 1-dimensional. |
required |
x2
|
Tensor
|
The second input tensor. Must be 1-dimensional and have the same shape as x1. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
The Euclidean distance between x1 and x2. |
Examples:
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the Euclidean distance (L2 norm) between all pairs of rows in a batch of tensors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
The input tensor. Must be 2-dimensional. |
required |
centered
|
bool
|
Whether to center the data when computing distances. |
False
|
dim
|
int
|
The dimension along which the distances are computed. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
A matrix of pairwise Euclidean distances with shape (m, m), where m is the number of rows in x. |
Examples:
>>> batch_euclidean_distance(torch.tensor([[1.0, 2.0], [3.0, 4.0]]), dim=1)
tensor([[0.0000, 2.8284],
[2.8284, 0.0000]])
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the Chebyshev distance kernel, which is the exponential of the negative Chebyshev distance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
The input tensor. For pairwise computation, this is the first tensor (x1). |
required |
x2
|
Tensor
|
The second input tensor for pairwise computation. If not provided, distances are computed in batch mode. |
None
|
centered
|
bool
|
Whether to center the data when computing distances in batch mode. |
False
|
dim
|
int
|
The dimension along which the distances are computed in batch mode. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
The Chebyshev distance kernel. Shape depends on whether x2 is provided: - If x2 is provided: A scalar kernel value. - If x2 is None: A kernel matrix of shape (m, m). |
Examples:
Pairwise Chebyshev distance kernel:
Batch Chebyshev distance kernel:
>>> chebyshev_distance_kernel(torch.tensor([[1.0, 2.0], [3.0, 4.0]]), dim=1)
tensor([[1.0000, 0.1353],
[0.1353, 1.0000]])
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the Chebyshev distance between tensors, either as a batch or for a single pair.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
The input tensor. For pairwise computation, this is the first tensor (x1). |
required |
x2
|
Tensor
|
The second input tensor for pairwise computation. If not provided, distances are computed in batch mode. |
None
|
centered
|
bool
|
Whether to center the data when computing distances in batch mode. |
False
|
dim
|
int
|
The dimension along which the distances are computed in batch mode. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
|
Examples:
Pairwise Chebyshev distance:
Batch Chebyshev distance:
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the Chebyshev distance between two 1D tensors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x1
|
Tensor
|
The first input tensor. |
required |
x2
|
Tensor
|
The second input tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
The Chebyshev distance as a scalar value. |
Examples:
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the Chebyshev distance matrix for a batch of vectors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor of shape (b, m). |
required |
centered
|
bool
|
Whether to center the data. |
False
|
dim
|
int
|
The dimension along which the distances are computed. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
A distance matrix of shape (m, m). |
Examples:
>>> batch_chebyshev_distance(torch.tensor([[1.0, 2.0], [3.0, 5.0]]), dim=1)
tensor([[0., 3.],
[3., 0.]])
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the Canberra distance kernel, which is the exponential of the negative Canberra distance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
The input tensor. For pairwise computation, this is the first tensor (x1). |
required |
x2
|
Tensor
|
The second input tensor for pairwise computation. If not provided, distances are computed in batch mode. |
None
|
dim
|
int
|
The dimension along which the distances are computed in batch mode. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
The Canberra distance kernel. Shape depends on whether x2 is provided: - If x2 is provided: A scalar kernel value. - If x2 is None: A kernel matrix of shape (m, m). |
Examples:
Pairwise Canberra distance kernel:
Batch Canberra distance kernel:
>>> canberra_distance_kernel(torch.tensor([[1.0, 2.0], [3.0, 4.0]]), dim=1)
tensor([[1.0000, 0.4346],
[0.4346, 1.0000]])
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the Canberra distance between tensors, either as a batch or for a single pair.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
The input tensor. For pairwise computation, this is the first tensor (x1). |
required |
x2
|
Tensor
|
The second input tensor for pairwise computation. If not provided, distances are computed in batch mode. |
None
|
dim
|
int
|
The dimension along which the distances are computed in batch mode. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
|
Examples:
Pairwise Canberra distance:
Batch Canberra distance:
>>> canberra_distance(torch.tensor([[1.0, 2.0], [3.0, 4.0]]), dim=1)
tensor([[0.0000, 0.8333],
[0.8333, 0.0000]])
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the Canberra distance between two 1D tensors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x1
|
Tensor
|
The first input tensor. |
required |
x2
|
Tensor
|
The second input tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
The Canberra distance as a scalar value. |
Examples:
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the Canberra distance matrix for a batch of vectors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor of shape (b, m). |
required |
dim
|
int
|
The dimension along which the distances are computed. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
A distance matrix of shape (m, m). |
Examples:
>>> batch_canberra_distance(torch.tensor([[1.0, 2.0], [3.0, 4.0]]), dim=1)
tensor([[0.0000, 0.8333],
[0.8333, 0.0000]])
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the exponential kernel, which is an RBF-like kernel based on the exponential of squared Euclidean distance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
The input tensor. For pairwise computation, this is the first tensor (x1). |
required |
x2
|
Tensor
|
The second input tensor for pairwise computation. If not provided, distances are computed in batch mode. |
None
|
gamma
|
float
|
The scale parameter for the kernel. |
1.0
|
dim
|
int
|
The dimension along which the distances are computed in batch mode. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
|
Examples:
Pairwise exponential kernel:
>>> exponential_kernel(torch.tensor([1.0, 2.0]), torch.tensor([3.0, 4.0]), gamma=0.5)
tensor(0.0183)
Batch exponential kernel:
>>> exponential_kernel(torch.tensor([[1.0, 2.0], [3.0, 4.0]]), gamma=0.5, dim=1)
tensor([[1.0000, 0.0183],
[0.0183, 1.0000]])
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the exponential kernel for a single pair of tensors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x1
|
Tensor
|
The first input tensor. |
required |
x2
|
Tensor
|
The second input tensor. |
required |
gamma
|
float
|
The scale parameter for the kernel. |
1.0
|
Returns:
Type | Description |
---|---|
Tensor
|
The kernel value as a scalar tensor. |
Examples:
>>> instance_exponential_kernel(torch.tensor([1.0, 2.0]), torch.tensor([3.0, 4.0]), gamma=0.5)
tensor(0.0183)
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the exponential kernel in batch mode for a set of tensors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
The input tensor. Must be a 2D tensor with shape (b, m). |
required |
gamma
|
float
|
The scale parameter for the kernel. |
1.0
|
dim
|
int
|
The dimension along which the distances are computed. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
A kernel matrix of shape (m, m). |
Examples:
>>> batch_exponential_kernel(torch.tensor([[1.0, 2.0], [3.0, 4.0]]), gamma=0.5, dim=1)
tensor([[1.0000, 0.0183],
[0.0183, 1.0000]])
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the Gaussian Radial Basis Function (RBF) kernel.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
The input tensor. For pairwise computation, this is the first tensor (x1). |
required |
x2
|
Tensor
|
The second input tensor for pairwise computation. If not provided, the kernel is computed in batch mode. |
None
|
sigma
|
float
|
The standard deviation parameter for the RBF kernel. |
1.0
|
dim
|
int
|
The dimension along which the distances are computed in batch mode. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
|
Raises:
Type | Description |
---|---|
ValueError
|
If |
Examples:
Pairwise Gaussian RBF kernel:
>>> gaussian_rbf_kernel(torch.tensor([1.0, 2.0]), torch.tensor([3.0, 4.0]), sigma=1.0)
tensor(0.0183)
Batch Gaussian RBF kernel:
>>> gaussian_rbf_kernel(torch.tensor([[1.0, 2.0], [3.0, 4.0]]), sigma=1.0, dim=1)
tensor([[1.0000, 0.0183],
[0.0183, 1.0000]])
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the Gaussian Radial Basis Function (RBF) kernel for two 1D tensors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x1
|
Tensor
|
The first input tensor. |
required |
x2
|
Tensor
|
The second input tensor. |
required |
sigma
|
float
|
The standard deviation of the Gaussian kernel. |
1.0
|
Returns:
Type | Description |
---|---|
Tensor
|
The Gaussian RBF kernel value. |
Examples:
>>> instance_gaussian_rbf_kernel(torch.tensor([1.0, 2.0]), torch.tensor([3.0, 4.0]), sigma=1.0)
tensor(0.0183)
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the Gaussian Radial Basis Function (RBF) kernel matrix for a batch of vectors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor of shape (b, m). |
required |
sigma
|
float
|
The standard deviation of the Gaussian kernel. |
1.0
|
dim
|
int
|
The dimension along which the kernel is computed. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
The Gaussian RBF kernel matrix of shape (m, m). |
Examples:
>>> batch_gaussian_rbf_kernel(torch.tensor([[1.0, 2.0], [3.0, 4.0]]), sigma=1.0, dim=1)
tensor([[1.0000, 0.0183],
[0.0183, 1.0000]])
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the Laplacian kernel.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
The input tensor. For pairwise computation, this is the first tensor (x1). |
required |
x2
|
Tensor
|
The second input tensor for pairwise computation. If not provided, the kernel is computed in batch mode. |
None
|
sigma
|
float
|
The scale parameter for the Laplacian kernel. |
1.0
|
dim
|
int
|
The dimension along which the distances are computed in batch mode. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
|
Raises:
Type | Description |
---|---|
ValueError
|
If |
Examples:
Pairwise Laplacian kernel:
Batch Laplacian kernel:
>>> laplacian_kernel(torch.tensor([[1.0, 2.0], [3.0, 4.0]]), sigma=1.0, dim=1)
tensor([[1.0000, 0.0183],
[0.0183, 1.0000]])
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the Laplacian kernel for two 1D tensors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x1
|
Tensor
|
The first input tensor. |
required |
x2
|
Tensor
|
The second input tensor. |
required |
sigma
|
float
|
The kernel bandwidth parameter. |
1.0
|
Returns:
Type | Description |
---|---|
Tensor
|
The Laplacian kernel value. |
Examples:
>>> instance_laplacian_kernel(torch.tensor([1.0, 2.0]), torch.tensor([3.0, 4.0]), sigma=1.0)
tensor(0.0183)
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the Laplacian kernel matrix for a batch of vectors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor of shape (b, m). |
required |
sigma
|
float
|
The kernel bandwidth parameter. |
1.0
|
dim
|
int
|
The dimension along which the kernel is computed. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
The Laplacian kernel matrix of shape (m, m). |
Examples:
>>> batch_laplacian_kernel(torch.tensor([[1.0, 2.0], [3.0, 4.0]]), sigma=1.0, dim=1)
tensor([[1.0000, 0.0183],
[0.0183, 1.0000]])
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the anisotropic radial basis function (RBF) kernel.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
The input tensor. For pairwise computation, this is the first tensor (x1). |
required |
x2
|
Tensor
|
The second input tensor for pairwise computation. If not provided, the kernel is computed in batch mode. |
None
|
a_vector
|
Tensor
|
A vector defining the anisotropy in each dimension. Must have the same shape as |
None
|
a_scalar
|
float
|
A scalar defining the uniform anisotropy. Ignored if |
1.0
|
dim
|
int
|
The dimension along which the distances are computed in batch mode. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
|
Raises:
Type | Description |
---|---|
ValueError
|
If input tensors are empty or |
Warnings
|
If |
Examples:
Pairwise anisotropic RBF kernel:
>>> anisotropic_rbf_kernel(torch.tensor([1.0, 2.0]), torch.tensor([3.0, 4.0]), a_vector=torch.tensor([0.5, 0.5]))
tensor(0.0183)
Batch anisotropic RBF kernel:
>>> anisotropic_rbf_kernel(torch.tensor([[1.0, 2.0], [3.0, 4.0]]), a_scalar=0.5, dim=1)
tensor([[1.0000, 0.0183],
[0.0183, 1.0000]])
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the pairwise anisotropic RBF kernel for two input tensors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x1
|
Tensor
|
The first input tensor (1D). |
required |
x2
|
Tensor
|
The second input tensor (1D). |
required |
a_vector
|
Tensor
|
A vector defining the anisotropy in each dimension. Must have the same shape as |
None
|
a_scalar
|
float
|
A scalar defining the uniform anisotropy. Ignored if |
1.0
|
Returns:
Type | Description |
---|---|
Tensor
|
A scalar value of the kernel. |
Raises:
Type | Description |
---|---|
ValueError
|
If input tensors are empty or dimensions do not match. |
Warnings
|
If |
Examples:
>>> instance_anisotropic_rbf_kernel(torch.tensor([1.0, 2.0]), torch.tensor([3.0, 4.0]), a_vector=torch.tensor([0.5, 0.5]))
tensor(0.0183)
Source code in tinybig/koala/linear_algebra/kernel.py
Computes the batch anisotropic RBF kernel.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
The input tensor (2D). |
required |
a_vector
|
Tensor
|
A vector defining the anisotropy in each dimension. Must match the batch size of |
None
|
a_scalar
|
float
|
A scalar defining the uniform anisotropy. Ignored if |
1.0
|
dim
|
int
|
The dimension along which the distances are computed. |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
A kernel matrix of shape (m, m). |
Raises:
Type | Description |
---|---|
ValueError
|
If input tensors are empty or incompatible. |
Warnings
|
If |
Examples:
>>> batch_anisotropic_rbf_kernel(torch.tensor([[1.0, 2.0], [3.0, 4.0]]), a_scalar=0.5, dim=1)
tensor([[1.0000, 0.0183],
[0.0183, 1.0000]])
Source code in tinybig/koala/linear_algebra/kernel.py
1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 |
|
Computes a custom hybrid kernel by combining multiple kernel functions with specified weights.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
The first input tensor. Can be a batch or single instance depending on |
required |
x2
|
Tensor
|
The second input tensor. If |
None
|
kernels
|
List[Callable[[Tensor, Tensor], Tensor]]
|
A list of kernel functions to be combined. |
None
|
weights
|
Union[List, Tuple, float, Parameter]
|
Weights for combining the kernel functions. If |
None
|
dim
|
int
|
Dimension to apply the kernel operation. Default is |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
The computed hybrid kernel output. |
Raises:
Type | Description |
---|---|
ValueError
|
If input tensors are invalid, no kernels are provided, or weights and kernels mismatch. |
Source code in tinybig/koala/linear_algebra/kernel.py
Computes a hybrid kernel for two single instances using a combination of kernel functions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x1
|
Tensor
|
The first input tensor (single instance). |
required |
x2
|
Tensor
|
The second input tensor (single instance). |
required |
kernels
|
List[Callable[[Tensor, Tensor], Tensor]]
|
A list of kernel functions to be combined. |
required |
weights
|
Union[List, Tuple, float]
|
Weights for combining the kernel functions. If |
None
|
Returns:
Type | Description |
---|---|
Tensor
|
The computed hybrid kernel value for the two instances. |
Raises:
Type | Description |
---|---|
ValueError
|
If input tensors are invalid, no kernels are provided, or weights and kernels mismatch. |
Source code in tinybig/koala/linear_algebra/kernel.py
Computes a hybrid kernel for a batch of instances using a combination of kernel functions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Input tensor of shape |
required |
kernels
|
List[Callable[[Tensor, Tensor], Tensor]]
|
A list of kernel functions to be combined. |
required |
weights
|
Union[List, Tuple, float]
|
Weights for combining the kernel functions. If |
None
|
dim
|
int
|
Dimension to apply the kernel operation. Default is |
0
|
Returns:
Type | Description |
---|---|
Tensor
|
The computed hybrid kernel matrix for the batch. |
Raises:
Type | Description |
---|---|
ValueError
|
If input tensors are invalid, no kernels are provided, or weights and kernels mismatch. |