Neural Kernel¶
sgptools.kernels.neural_kernel.NeuralSpectralKernel
¶
Bases: Kernel
Neural Spectral Kernel function (non-stationary kernel function). This kernel models non-stationarity by using multiple Multi-Layer Perceptrons (MLPs) to map input locations to frequency, lengthscale, and variance parameters for a mixture of spectral components.
Based on the implementation from this repo.
Refer to the following papers for more details
- Neural Non-Stationary Spectral Kernel [Remes et al., 2018]
Attributes:
Name | Type | Description |
---|---|---|
input_dim |
int
|
Dimensionality of the input data points. |
Q |
int
|
Number of MLP mixture components used in the kernel function. |
num_hidden |
int
|
Number of hidden layers in each MLP. |
freq |
List[NN]
|
List of MLPs, one for each component, predicting frequencies. |
length |
List[NN]
|
List of MLPs, one for each component, predicting lengthscales. |
var |
List[NN]
|
List of MLPs, one for each component, predicting variances. |
Source code in sgptools/kernels/neural_kernel.py
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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
|
K(X, X2=None)
¶
Computes the covariance matrix between/amongst the input variables X
and X2
.
If X2
is None, the function computes K(X, X)
(a symmetric covariance matrix).
Otherwise, it computes K(X, X2)
(a cross-covariance matrix).
The kernel is a sum over Q
mixture components, where each component's
parameters (frequency, lengthscale, variance) are determined by MLPs
based on the input locations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
Tensor
|
(N1, D); First set of input variables to compute covariance from.
|
required |
X2
|
Optional[Tensor]
|
(N2, D); Optional second set of input variables.
If provided, computes cross-covariance |
None
|
Returns:
Type | Description |
---|---|
Tensor
|
tf.Tensor: (N1, N2); The computed covariance matrix. If |
Source code in sgptools/kernels/neural_kernel.py
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 |
|
K_diag(X)
¶
Computes the diagonal of the covariance matrix K(X, X)
.
For the Neural Spectral Kernel, this is sum_q(var_q(X)^2) + jitter
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
Tensor
|
(N, D); Input data points. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
tf.Tensor: (N,); A 1D tensor representing the diagonal elements of the covariance matrix. |
Source code in sgptools/kernels/neural_kernel.py
__init__(input_dim, active_dims=None, Q=1, hidden_sizes=None)
¶
Initializes the Neural Spectral Kernel.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_dim
|
int
|
Number of dimensions of the input data points (e.g., 2 for 2D data). |
required |
active_dims
|
Optional[List[int]]
|
A list of indices specifying which input dimensions the kernel operates on. If None, all dimensions are active. Defaults to None. |
None
|
Q
|
int
|
The number of MLP mixture components used in the kernel function. Each component has its own set of MLPs for frequency, lengthscale, and variance. Defaults to 1. |
1
|
hidden_sizes
|
List[int]
|
A list where each element specifies the number of hidden units in a layer of the MLPs. The length of this list determines the number of hidden layers. Defaults to [32, 32]. |
None
|
Usage
import gpflow
import numpy as np
from sgptools.kernels.neural_kernel import NeuralSpectralKernel
# Initialize a Neural Spectral Kernel for 2D data with 3 mixture components
# and MLPs with 2 hidden layers of 64 units each.
kernel = NeuralSpectralKernel(input_dim=2, Q=3, hidden_sizes=[64, 64])
# You can then use this kernel in a GPflow model:
# model = gpflow.models.SGPR(data=(X_train, Y_train), kernel=kernel, ...)
Source code in sgptools/kernels/neural_kernel.py
sgptools.kernels.neural_kernel.init_neural_kernel(X_train, Y_train, inducing_variable, Q, n_inits=1, hidden_sizes=None)
¶
Helper function to initialize a Sparse Gaussian Process Regression (SGPR) model with a Neural Spectral Kernel. This function can perform multiple random initializations and return the model with the best initial Evidence Lower Bound (ELBO).
Refer to the original paper for more details
- Neural Non-Stationary Spectral Kernel [Remes et al., 2018]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X_train
|
ndarray
|
(n, d); Input training set points. |
required |
Y_train
|
ndarray
|
(n, 1); Training set labels. |
required |
inducing_variable
|
ndarray
|
(m, d); Initial inducing points. These are passed directly to the SGPR model. |
required |
Q
|
int
|
The number of MLP mixture components for the Neural Spectral Kernel. |
required |
n_inits
|
int
|
Number of times to randomly initialize the kernel's MLPs and compute the initial ELBO. The model with the highest ELBO among these initializations is returned. Defaults to 1. |
1
|
hidden_sizes
|
Optional[List[int]]
|
List of integers specifying the number of hidden units in each MLP layer. If None, [32, 32] is used. |
None
|
Returns:
Name | Type | Description |
---|---|---|
SGPR |
SGPR
|
The SGPR model instance initialized with the Neural Spectral Kernel that yielded the best initial ELBO. |
Usage
import numpy as np
import gpflow
from sgptools.kernels.neural_kernel import init_neural_kernel
from sgptools.utils.misc import get_inducing_pts # For initial inducing points
# Dummy data
X_train_data = np.random.rand(100, 2).astype(np.float32)
Y_train_data = (np.sin(X_train_data[:, 0]) + np.cos(X_train_data[:, 1]))[:, None].astype(np.float32)
# Initial inducing points (e.g., subset of training data or k-means centers)
initial_inducing_points = get_inducing_pts(X_train_data, num_inducing=20)
# Initialize the SGPR model with Neural Spectral Kernel
# Try 3 random initializations for the MLPs.
model_ns_kernel = init_neural_kernel(
X_train=X_train_data,
Y_train=Y_train_data,
inducing_variable=initial_inducing_points,
Q=5, # 5 mixture components
n_inits=3, # 3 initializations
hidden_sizes=[16, 16] # Custom hidden layer sizes
)
# You would typically optimize this model further using optimize_model:
# from sgptools.utils.gpflow import optimize_model
# optimize_model(model_ns_kernel)
Source code in sgptools/kernels/neural_kernel.py
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 |
|