GPflow
sgptools.utils.gpflow
¶
TraceInducingPts
¶
Bases: MonitorTask
A GPflow monitoring task designed to trace the state of inducing points at every step during optimization of a Sparse Gaussian Process (SGP) model. This is particularly useful for visualizing the movement of inducing points during training.
Attributes:
Name | Type | Description |
---|---|---|
trace |
List[ndarray]
|
A list to store the numpy arrays of inducing points at each optimization step. |
model |
Union[GPR, SGPR]
|
The GPflow model being monitored. |
Source code in sgptools/utils/gpflow.py
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 |
|
__init__(model)
¶
Initializes the TraceInducingPts monitor task.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
Union[GPR, SGPR]
|
The GPflow GP or SGP model instance
to monitor. It is expected to have an
|
required |
Source code in sgptools/utils/gpflow.py
get_trace()
¶
Returns the collected inducing points at each optimization step.
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: (num_steps, num_inducing_points, num_dimensions);
An array where:
- |
Usage
Source code in sgptools/utils/gpflow.py
run(**kwargs)
¶
Method used to extract the inducing points and apply IPP fixed points transform if available
Source code in sgptools/utils/gpflow.py
get_model_params(X_train, y_train, max_steps=1500, verbose=True, lengthscales=1.0, variance=1.0, noise_variance=0.1, kernel=None, return_model=False, train_inducing_pts=False, num_inducing_pts=500, **kwargs)
¶
Trains a Gaussian Process (GP) or Sparse Gaussian Process (SGP) model on the given training set. A Sparse GP is used if the training set size exceeds 1500 samples.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X_train
|
ndarray
|
(n, d); Training set input features. |
required |
y_train
|
ndarray
|
(n, 1); Training set labels. |
required |
max_steps
|
int
|
Maximum number of optimization steps. Defaults to 1500. |
1500
|
verbose
|
bool
|
If True, prints a summary of the optimized GP parameters. Defaults to True. |
True
|
lengthscales
|
Union[float, List[float]]
|
Initial kernel lengthscale(s). If a float, it's applied uniformly to all dimensions. If a list, each element corresponds to a data dimension. Defaults to 1.0. |
1.0
|
variance
|
float
|
Initial kernel variance. Defaults to 1.0. |
1.0
|
noise_variance
|
float
|
Initial data noise variance. Defaults to 0.1. |
0.1
|
kernel
|
Optional[Kernel]
|
A pre-defined GPflow kernel function. If None,
a |
None
|
return_model
|
bool
|
If True, the trained GP/SGP model object is returned along with loss, variance, and kernel. Defaults to False. |
False
|
train_inducing_pts
|
bool
|
If True and using a Sparse GP model, the inducing points are optimized along with other model parameters. If False, inducing points remain fixed (default for SGP). Defaults to False. |
False
|
num_inducing_pts
|
int
|
Number of inducing points to use when training a Sparse GP model.
Ignored if |
500
|
**kwargs
|
Any
|
Additional keyword arguments passed to the |
{}
|
Returns:
Type | Description |
---|---|
Union[Tuple[ndarray, float, Kernel], Tuple[ndarray, float, Kernel, Union[GPR, SGPR]]]
|
Union[Tuple[np.ndarray, float, gpflow.kernels.Kernel], Tuple[np.ndarray, float, gpflow.kernels.Kernel, Union[gpflow.models.GPR, gpflow.models.SGPR]]]: |
Union[Tuple[ndarray, float, Kernel], Tuple[ndarray, float, Kernel, Union[GPR, SGPR]]]
|
|
Union[Tuple[ndarray, float, Kernel], Tuple[ndarray, float, Kernel, Union[GPR, SGPR]]]
|
|
Usage
import numpy as np
# Generate some dummy data
X = np.random.rand(1000, 2) * 10
y = np.sin(X[:, 0:1]) + np.cos(X[:, 1:2]) + np.random.randn(1000, 1) * 0.1
# Train a GPR model (since 1000 samples <= 1500)
losses, noise_var, trained_kernel = get_model_params(X, y, max_steps=500, verbose=True)
# Train an SGPR model (more than 1500 samples)
X_large = np.random.rand(2000, 2) * 10
y_large = np.sin(X_large[:, 0:1]) + np.cos(X_large[:, 1:2]) + np.random.randn(2000, 1) * 0.1
losses_sgpr, noise_var_sgpr, trained_kernel_sgpr, sgpr_model = get_model_params(X_large, y_large, max_steps=500, num_inducing_pts=100, return_model=True)
Source code in sgptools/utils/gpflow.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 |
|
optimize_model(model, max_steps=2000, optimize_hparams=True, optimizer='scipy.L-BFGS-B', verbose=False, trace_fn=None, convergence_criterion=True, trainable_variables=None, **kwargs)
¶
Trains a GPflow GP or SGP model using either SciPy's optimizers or TensorFlow's optimizers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
Union[GPR, SGPR]
|
The GPflow model (GPR or SGPR) to be trained. |
required |
max_steps
|
int
|
Maximum number of training steps (iterations). Defaults to 2000. |
2000
|
optimize_hparams
|
bool
|
If |
True
|
optimizer
|
str
|
Specifies the optimizer to use in " |
'scipy.L-BFGS-B'
|
verbose
|
bool
|
If |
False
|
trace_fn
|
Optional[Union[str, Callable[[Any], Any]]]
|
Specifies what to trace during training:
- |
None
|
convergence_criterion
|
bool
|
If |
True
|
trainable_variables
|
Optional[List[Variable]]
|
A list of specific model variables to train.
If None, variables are determined based on |
None
|
**kwargs
|
Any
|
Additional keyword arguments passed to the backend optimizers. |
{}
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: An array of loss values (or traced quantities if |
Raises:
Type | Description |
---|---|
ValueError
|
If an invalid optimizer format or an unsupported backend is specified. |
Usage
import gpflow
import numpy as np
# Create a dummy model (e.g., GPR for simplicity)
X = np.random.rand(100, 1)
y = X + np.random.randn(100, 1) * 0.1
kernel = gpflow.kernels.SquaredExponential()
model = gpflow.models.GPR((X, y), kernel=kernel, noise_variance=0.1)
# 1. Optimize using SciPy's L-BFGS-B (default)
losses_scipy = optimize_model(model, max_steps=500, verbose=True)
# 2. Optimize using TensorFlow's Adam optimizer
# Re-initialize model to reset parameters for new optimization
model_tf = gpflow.models.GPR((X, y), kernel=gpflow.kernels.SquaredExponential(), noise_variance=0.1)
losses_tf = optimize_model(model_tf, max_steps=1000, learning_rate=0.01, optimizer='tf.Adam', verbose=False)
# 3. Optimize SGPR and trace inducing points
X_sgpr = np.random.rand(2000, 2)
y_sgpr = np.sin(X_sgpr[:, 0:1]) + np.random.randn(2000, 1) * 0.1
inducing_points_init = get_inducing_pts(X_sgpr, 100)
sgpr_model = gpflow.models.SGPR((X_sgpr, y_sgpr), kernel=gpflow.kernels.SquaredExponential(),
inducing_variable=inducing_points_init, noise_variance=0.1)
traced_ips = optimize_model(sgpr_model, max_steps=100, optimizer='tf.Adam', trace_fn='traceXu', verbose=False)
Source code in sgptools/utils/gpflow.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 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 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
|