Kernels and Utilities API¶
This page documents public kernel and utility functions.
Kernel functions¶
linear_kernel(x1, x2, **kwargs)
¶
Compute the linear kernel between two input vectors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x1
|
ndarray
|
First input vector. |
required |
x2
|
ndarray
|
Second input vector. |
required |
**kwargs
|
Ignored keyword arguments accepted for compatibility with other kernel functions. |
{}
|
Returns:
| Type | Description |
|---|---|
float or ndarray
|
The dot product |
Source code in torchkm/kernels.py
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
polynomial_kernel(x1, x2, degree=3, coef0=1, gamma=1, **kwargs)
¶
Compute the polynomial kernel between two input vectors.
The kernel is defined as (gamma * np.dot(x1, x2) + coef0) ** degree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x1
|
ndarray
|
First input vector. |
required |
x2
|
ndarray
|
Second input vector. |
required |
degree
|
int
|
Degree of the polynomial kernel. |
3
|
coef0
|
float
|
Additive constant in the polynomial kernel. |
1
|
gamma
|
float
|
Multiplicative scale applied to the dot product. |
1
|
**kwargs
|
Ignored keyword arguments accepted for compatibility with other kernel functions. |
{}
|
Returns:
| Type | Description |
|---|---|
float or ndarray
|
Polynomial kernel value. |
Source code in torchkm/kernels.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | |
rbf_kernel(x1, x2, gamma=0.1, **kwargs)
¶
Compute the radial basis function kernel between two input vectors.
The kernel is defined as exp(-gamma * ||x1 - x2||^2).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x1
|
ndarray
|
First input vector. |
required |
x2
|
ndarray
|
Second input vector. |
required |
gamma
|
float
|
Positive scale parameter controlling the width of the RBF kernel. |
0.1
|
**kwargs
|
Ignored keyword arguments accepted for compatibility with other kernel functions. |
{}
|
Returns:
| Type | Description |
|---|---|
float
|
RBF kernel value. |
Source code in torchkm/kernels.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
Utility functions¶
sigest(x, frac=0.5)
¶
PyTorch equivalent of the R function sigest.
Parameters: - x (torch.Tensor): Input tensor of shape (m, n), where m is the number of samples and n is the number of features. - frac (float): Fraction of samples to use for computing the distance.
Returns: - sigma_estimate (float): Estimated sigma based on quantiles of squared distances.
Source code in torchkm/functions.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
rbf_kernel(x, sigma)
¶
Compute the RBF (Gaussian) kernel matrix in PyTorch.
Parameters: - x (torch.Tensor): Input tensor of shape (n_samples, n_features). - sigma (float): The standard deviation parameter for the RBF kernel (Gaussian width).
Returns: - K (torch.Tensor): RBF kernel matrix of shape (n_samples, n_samples).
Source code in torchkm/functions.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
kernelMult(X, X_new, sigma)
¶
Compute the RBF (Gaussian) kernel matrix between X and X_new in PyTorch.
Parameters: - X (torch.Tensor): Input tensor of shape (n_samples_X, n_features). - X_new (torch.Tensor): Input tensor of shape (n_samples_X_new, n_features). - sigma (float): The standard deviation parameter for the RBF kernel (Gaussian width).
Returns: - K (torch.Tensor): RBF kernel matrix of shape (n_samples_X, n_samples_X_new).
Source code in torchkm/functions.py
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 | |
Probability calibration¶
PlattScalerTorch
¶
Platt scaling: P(y=1|f) = 1 / (1 + exp(A*f + B)) Fits A,B with regularized logistic regression on decision values f and labels y∈{-1,1}. Uses Newton updates with damping and target smoothing per Platt (1999).
Source code in torchkm/platt.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 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 | |
fit(f, y)
¶
f: (n,) decision values (raw scores), torch tensor y: (n,) labels in {-1,1}, torch tensor
Source code in torchkm/platt.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 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 | |
reliability_curve(y_true, p_pred, n_bins=15)
¶
y_true: tensor/array in {-1,1} p_pred: predicted prob P(y=1|x) in [0,1] returns: bin_centers, mean_pred, frac_pos, counts
Source code in torchkm/platt.py
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 | |
Notes¶
The utility API is lower-level than the estimator API. Most users should begin with the high-level estimators and use these functions only when they need custom kernels, kernel matrices, or direct solver access.