Attentive Kernel¶
sgptools.kernels.attentive.Attentive
¶
Bases: Kernel
Attentive kernel (non-stationary kernel).
This kernel uses a Multi-Layer Perceptron (MLP) to compute attention representations that weight a mixture of RBF components, producing a locally adaptive, non-stationary covariance function.
Implementation based on Weizhe-Chen/attentive_kernels.
Refer to the following paper for more details
- AK: Attentive Kernel for Information Gathering [Chen et al., 2022]
Attributes:
| Name | Type | Description |
|---|---|---|
_free_amplitude |
Variable
|
Trainable scalar amplitude parameter applied to the final covariance. |
lengthscales |
Variable
|
1D tensor of fixed lengthscale values for the RBF mixture components. |
num_lengthscales |
int
|
Number of RBF mixture components. |
nn |
NN
|
Neural network that maps input points to latent attention representations. |
Source code in sgptools/kernels/attentive.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 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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | |
K(X, X2=None)
¶
Compute full covariance matrix between X and X2.
The covariance is a weighted sum of RBF mixture components modulated by attention representations in the learned latent space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Tensor
|
Tensor of shape (N1, D). First set of input points. |
required |
X2
|
Tensor | None
|
Tensor of shape (N2, D). Optional second set of input points.
If None, |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
tf.Tensor: Tensor of shape (N1, N2) containing the covariance matrix K(X, X2). |
Source code in sgptools/kernels/attentive.py
K_diag(X)
¶
Compute the diagonal of K(X, X).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Tensor
|
Tensor of shape (N, D). Input points. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
tf.Tensor: Tensor of shape (N,) containing the diagonal of the covariance matrix (constant when representations are unit norm). |
Source code in sgptools/kernels/attentive.py
__init__(lengthscales=None, hidden_sizes=None, amplitude=1.0, num_dim=2)
¶
Initialize an Attentive kernel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lengthscales
|
List[float] | ndarray | None
|
Positive lengthscale values used for the fixed RBF mixture
components. These are treated as non-trainable parameters.
If None, a default grid |
None
|
hidden_sizes
|
List[int] | None
|
Hidden-layer widths of the MLP. The length of this list
determines the number of hidden layers. If None, defaults to
|
None
|
amplitude
|
float
|
Initial value for the trainable scalar amplitude parameter used to rescale the final covariance. |
1.0
|
num_dim
|
int
|
Dimensionality of each input data point (e.g. 2 for 2D inputs). |
2
|
Returns:
| Type | Description |
|---|---|
|
None |
Usage
Basic usage with fixed lengthscales for 2D data::
```python
import gpflow
import numpy as np
from sgptools.kernels.attentive import Attentive
# Example: 10 fixed lengthscales ranging from 0.01 to 2.0
l_scales = np.linspace(0.01, 2.0, 10).astype(np.float32)
# Initialize Attentive kernel for 2D data
kernel = Attentive(
lengthscales=l_scales,
hidden_sizes=[10, 10],
amplitude=1.0,
num_dim=2,
)
# Use this kernel in a GPflow model:
# model = gpflow.models.GPR(
# data=(X_train, Y_train),
# kernel=kernel,
# noise_variance=0.1,
# )
# optimize_model(model)
```
Source code in sgptools/kernels/attentive.py
get_lengthscales(X)
¶
Compute non-stationary effective lengthscales.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Array of shape (N, D). Input points at which to estimate effective lengthscales. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Array of shape (N,) containing effective spatially varying lengthscale values at the given input locations. |
Source code in sgptools/kernels/attentive.py
get_representations(X)
¶
Compute normalized latent attention representations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Tensor
|
Tensor of shape (N, D). Input data points. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
tf.Tensor: Tensor of shape (N, num_lengthscales) containing unit-norm latent representation vectors used for generating attention weights. |