botorch.cross_validation

Cross-validation utilities using batch evaluation mode.

class botorch.cross_validation.CVFolds(train_X, test_X, train_Y, test_Y, train_Yvar, test_Yvar)[source]

Bases: tuple

Create new instance of CVFolds(train_X, test_X, train_Y, test_Y, train_Yvar, test_Yvar)

Parameters
  • train_X (torch.Tensor) –

  • test_X (torch.Tensor) –

  • train_Y (torch.Tensor) –

  • test_Y (torch.Tensor) –

  • train_Yvar (Optional[torch.Tensor]) –

  • test_Yvar (Optional[torch.Tensor]) –

property train_X

Alias for field number 0

property test_X

Alias for field number 1

property train_Y

Alias for field number 2

property test_Y

Alias for field number 3

property train_Yvar

Alias for field number 4

property test_Yvar

Alias for field number 5

class botorch.cross_validation.CVResults(model, posterior, observed_Y, observed_Yvar)[source]

Bases: tuple

Create new instance of CVResults(model, posterior, observed_Y, observed_Yvar)

Parameters
property model

Alias for field number 0

property posterior

Alias for field number 1

property observed_Y

Alias for field number 2

property observed_Yvar

Alias for field number 3

botorch.cross_validation.gen_loo_cv_folds(train_X, train_Y, train_Yvar=None)[source]

Generate LOO CV folds w.r.t. to n.

Parameters
  • train_X (torch.Tensor) – A n x d or batch_shape x n x d (batch mode) tensor of training features.

  • train_Y (torch.Tensor) – A n x (m) or batch_shape x n x (m) (batch mode) tensor of training observations.

  • train_Yvar (Optional[torch.Tensor]) – A batch_shape x n x (m) or batch_shape x n x (m) (batch mode) tensor of observed measurement noise.

Returns

CVFolds tuple with the following fields

  • train_X: A n x (n-1) x d or batch_shape x n x (n-1) x d tensor of training features.

  • test_X: A n x 1 x d or batch_shape x n x 1 x d tensor of test features.

  • train_Y: A n x (n-1) x m or batch_shape x n x (n-1) x m tensor of training observations.

  • test_Y: A n x 1 x m or batch_shape x n x 1 x m tensor of test observations.

  • train_Yvar: A n x (n-1) x m or batch_shape x n x (n-1) x m tensor of observed measurement noise.

  • test_Yvar: A n x 1 x m or batch_shape x n x 1 x m tensor of observed measurement noise.

Return type

botorch.cross_validation.CVFolds

Example

>>> train_X = torch.rand(10, 1)
>>> train_Y = torch.sin(6 * train_X) + 0.2 * torch.rand_like(train_X)
>>> cv_folds = gen_loo_cv_folds(train_X, train_Y)
botorch.cross_validation.batch_cross_validation(model_cls, mll_cls, cv_folds, fit_args=None, observation_noise=False)[source]

Perform cross validation by using gpytorch batch mode.

Parameters
  • model_cls (Type[botorch.models.gpytorch.GPyTorchModel]) – A GPyTorchModel class. This class must initialize the likelihood internally. Note: Multi-task GPs are not currently supported.

  • mll_cls (Type[gpytorch.mlls.marginal_log_likelihood.MarginalLogLikelihood]) – A MarginalLogLikelihood class.

  • cv_folds (botorch.cross_validation.CVFolds) – A CVFolds tuple.

  • fit_args (Optional[Dict[str, Any]]) – Arguments passed along to fit_gpytorch_model

  • observation_noise (bool) –

Returns

A CVResults tuple with the following fields

  • model: GPyTorchModel for batched cross validation

  • posterior: GPyTorchPosterior where the mean has shape n x 1 x m or batch_shape x n x 1 x m

  • observed_Y: A n x 1 x m or batch_shape x n x 1 x m tensor of observations.

  • observed_Yvar: A n x 1 x m or batch_shape x n x 1 x m tensor of observed measurement noise.

Return type

botorch.cross_validation.CVResults

Example

>>> train_X = torch.rand(10, 1)
>>> train_Y = torch.sin(6 * train_X) + 0.2 * torch.rand_like(train_X)
>>> cv_folds = gen_loo_cv_folds(train_X, train_Y)
>>> cv_results = batch_cross_validation(
>>>     SingleTaskGP,
>>>     ExactMarginalLogLikelihood,
>>>     cv_folds,
>>> )
WARNING: This function is currently very memory inefficient, use it only

for problems of small size.