vopy.models
- class vopy.models.Model
Defines the abstract model class.
- class vopy.models.ModelList
Defines the abstract model list class.
- class vopy.models.UncertaintyPredictiveModel
Defines the abstract uncertainty predictive model class, which gives mean and variance estimates as predictions.
- class vopy.models.GPModel
Defines the abstract Gaussian process model (GP) class.
- get_kernel_type()
Get the kernel type of the GP model.
- get_lengthscale_and_var()
Get the lengthscale and variance of the GP model.
Empirical Models
EmpiricalMeanVarModel
- class vopy.models.EmpiricalMeanVarModel(input_dim: int, output_dim: int, noise_var: float, design_count: int, track_means: bool = True, track_variances: bool = True)
Implements a model that tracks empirical means and variances for each design.
This class tracks the empirical means and variances for each design in the input space, controlled by flags track_means and track_variances.
- Parameters:
input_dim (int) – The dimension of the input space.
output_dim (int) – The dimension of the output space.
noise_var (float) – The variance of the noise in the output space.
design_count (int) – The number of designs to track.
track_means (bool) – A flag to enable/disable tracking of means, defaults to True.
track_variances (bool) – A flag to enable/disable tracking of variances, defaults to True.
- add_sample(indices: Iterable[int], Y_t: numpy.ndarray)
Add new samples for specified design indices.
- Parameters:
indices (Iterable[int]) – Represents the indices of designs to which the samples belong.
Y_t (np.ndarray) – A N-by-output_dim array containing the new samples to be added.
- clear_data()
This method generates/clears the sample containers.
- predict(test_X: numpy.ndarray) tuple[numpy.ndarray, numpy.ndarray]
This method takes test inputs and returns the predicted means and variances based on the tracked data. If track_means is enabled, it returns the corresponding means for the test inputs. If track_variances is enabled, it returns the corresponding variances for the test inputs.
- Parameters:
test_X (np.ndarray) – The test inputs for which predictions are to be made. The last column of test_X should contain indices.
- Returns:
A tuple containing two numpy arrays: the predicted means and variances.
- Return type:
tuple[np.ndarray, np.ndarray]
- train()
This method is a no-op for this model.
- update()
This method calculates and updates the means and variances of the design samples based on the current data. If track_means is enabled, it updates the means attribute with the mean of each design sample. If track_variances is enabled, it updates the variances attribute with the variance of each design sample.
Gaussian Process Based Models
- class vopy.models.GPyTorchModel
Base class for Gaussian Process models using GPyTorch. Provides utility methods for data handling and kernel type identification.
- get_kernel_type() Literal['RBF', 'Other']
Identify the type of kernel used in the model iteratively.
- Returns:
Kernel type as a string. Currently only “RBF”, or “Other” are returned.
- Return type:
Literal[“RBF”, “Other”]
- to_tensor(data: numpy.typing.ArrayLike) torch.Tensor
Convert input data to a PyTorch tensor.
- Parameters:
data (ArrayLike) – Input data to convert to tensor format.
- Returns:
Converted PyTorch tensor.
- Return type:
torch.Tensor
- class vopy.models.SingleTaskGP(*args: Any, **kwargs: Any)
Exact GP model for single-task problems.
- Parameters:
train_inputs (torch.Tensor) – Training input data.
train_targets (torch.Tensor) – Training target data.
likelihood (gpytorch.likelihoods.GaussianLikelihood) – Gaussian likelihood module.
mean_module (Optional[gpytorch.means.Mean]) – Mean module for the GP model. If None, a default module will be created. Defaults to None.
covar_module (Optional[gpytorch.kernels.Kernel]) – Covariance module for the GP model. If None, a default module will be created. Defaults to None.
- forward(x: torch.Tensor) gpytorch.distributions.MultivariateNormal
Computes the marginalized joint distribution over the given input points.
- Parameters:
x (torch.Tensor) – Input data.
- Returns:
An instance of MultivariateNormal distribution.
- Return type:
gpytorch.distributions.MultivariateNormal
- class vopy.models.MultitaskExactGPModel(*args: Any, **kwargs: Any)
Exact GP model for multitask problems with dependent objectives, i.e., a Linear Model of Coregionalization (LMC).
- Parameters:
train_inputs (torch.Tensor) – Input training data.
train_targets (torch.Tensor) – Target training data.
likelihood (gpytorch.likelihoods.MultitaskGaussianLikelihood) – Gaussian likelihood module.
covar_module (Optional[gpytorch.kernels.Kernel]) – Covariance module for the GP model. If None, a default module will be created. Defaults to None.
mean_module (Optional[gpytorch.means.Mean]) – Mean module for the GP model. If None, a default module will be created. Defaults to None.
- forward(x: torch.Tensor) gpytorch.distributions.MultitaskMultivariateNormal
Computes the marginalized joint distribution over the given input points.
- Parameters:
x (torch.Tensor) – Input data.
- Returns:
An instance of MultitaskMultivariateNormal distribution.
- Return type:
gpytorch.distributions.MultitaskMultivariateNormal
- class vopy.models.BatchIndependentExactGPModel(*args: Any, **kwargs: Any)
Exact GP model for multitask problems with independent objectives.
- Parameters:
train_inputs (torch.Tensor) – Input training data.
train_targets (torch.Tensor) – Target training data.
likelihood (gpytorch.likelihoods.MultitaskGaussianLikelihood) – Gaussian likelihood module.
mean_module (Optional[gpytorch.means.Mean]) – Mean module for the GP model. If None, a default module will be created. Defaults to None.
covar_module (Optional[gpytorch.kernels.Kernel]) – Covariance module for the GP model. If None, a default module will be created. Defaults to None.
- forward(x: torch.Tensor) gpytorch.distributions.MultitaskMultivariateNormal
Computes the marginalized joint distribution over the given input points.
- Parameters:
x (torch.Tensor) – Input data.
- Returns:
An instance of MultitaskMultivariateNormal distribution.
- Return type:
gpytorch.distributions.MultitaskMultivariateNormal
- class vopy.models.GPyTorchMultioutputExactModel(input_dim: int, output_dim: int, model_kind: type[BatchIndependentExactGPModel | MultitaskExactGPModel], noise_var: float | numpy.typing.ArrayLike | None = None, noise_rank: int | None = None, input_transform: InputTransform | None = None, output_transform: OutputTransform | None = None, mean_module: gpytorch.means.Mean | None = None, covar_module: gpytorch.kernels.Kernel | None = None)
Multioutput GP model with exact inference, assuming known noise variance/covariance.
- Parameters:
input_dim (int) – Dimensionality of the input data.
output_dim (int) – Dimensionality of the output data.
model_kind (type[Union[BatchIndependentExactGPModel, MultitaskExactGPModel]]) – Type of Exact GP model to be used.
noise_var (Optional[Union[float, ArrayLike]]) – Noise variance for Gaussian likelihood, if known noise variance is assumed. Defaults to None. This should only be provided if
noise_rankis None.noise_rank (Optional[int]) – Rank of the noise covariance matrix. Defaults to None. This should only be provided if
noise_varis None. For details, see gpytorch.likelihoods.MultitaskGaussianLikelihood. No global noise is assumed.input_transform (Optional[InputTransform]) – An input transform to apply on training and prediction data. Defaults to None. Generally, normalization should be applied.
output_transform (Optional[OutputTransform]) – An output transform to apply on training and prediction data. Defaults to None. Generally, standardization should be applied.
mean_module (Optional[gpytorch.means.Mean]) – Mean module for the GP model. If None, a default module will be created when the model is instantiated. Defaults to None.
covar_module (Optional[gpytorch.kernels.Kernel]) – Covariance module for the GP model. If None, a default module will be created when the model is instantiated. Defaults to None.
- add_sample(X_t: numpy.typing.ArrayLike, Y_t: numpy.typing.ArrayLike)
Add new samples to the training data.
- Parameters:
X_t (ArrayLike) – Input data sample.
Y_t (ArrayLike) – Target data sample.
- clear_data()
Clear stored training data.
- evaluate_kernel(X: numpy.typing.ArrayLike | None = None)
Evaluate the kernel matrix for given data points.
- Parameters:
X (ArrayLike) – Input data to evaluate the kernel for. Defaults to training data if None.
- Returns:
Evaluated kernel matrix.
- Return type:
np.ndarray
- get_lengthscale_and_var() tuple[numpy.ndarray, numpy.ndarray]
Return the kernel lengthscales and variances.
- Returns:
Lengthscales and variances as arrays.
- Return type:
tuple[np.ndarray, np.ndarray]
- train(lr=0.01, training_iterations: int = 1000)
Train the hyperparameters of GP model.
- update()
Create GP model or update it with the current training data.
IndependentExactGPyTorchModel
- class vopy.models.IndependentExactGPyTorchModel(input_dim: int, output_dim: int, noise_var: float | numpy.typing.ArrayLike | None = None, noise_rank: int | None = None, input_transform: InputTransform | None = None, output_transform: OutputTransform | None = None, mean_module: gpytorch.means.Mean | None = None, covar_module: gpytorch.kernels.Kernel | None = None)
Independent multitask GP model using the multioutput exact GP framework.
- Parameters:
input_dim (int) – Dimensionality of the input data.
output_dim (int) – Dimensionality of the output data.
noise_var (Optional[Union[float, ArrayLike]]) – Noise variance for Gaussian likelihood, if known noise variance is assumed. Defaults to None. This should only be provided if
noise_rankis None.noise_rank (Optional[int]) – Rank of the noise covariance matrix. Defaults to None. This should only be provided if
noise_varis None.input_transform (Optional[InputTransform]) – An input transform to apply on training and prediction data. Defaults to None. Generally, normalization should be applied.
output_transform (Optional[OutputTransform]) – An output transform to apply on training and prediction data. Defaults to None. Generally, standardization should be applied.
mean_module (Optional[gpytorch.means.Mean]) – Mean module for the GP model. If None, a default batch of zero means will be created. Defaults to None.
covar_module (Optional[gpytorch.kernels.Kernel]) – Covariance module for the GP model. If None, a default batch of RBF kernels will be created. Defaults to None.
- predict(test_X: numpy.typing.ArrayLike) tuple[numpy.ndarray, numpy.ndarray]
Make predictions on test data.
- Parameters:
test_X (ArrayLike) – Test data.
- Returns:
Predicted means and variances corresponding to each test point.
- Return type:
tuple[np.ndarray, np.ndarray]
GPyTorchModelListExactModel
- class vopy.models.GPyTorchModelListExactModel(input_dim, output_dim, noise_var: float | numpy.typing.ArrayLike | None = None, input_transform: InputTransform | None = None, output_transform: OutputTransform | None = None, mean_modules: gpytorch.means.Mean | List[gpytorch.means.Mean] | None = None, covar_modules: gpytorch.kernels.Kernel | List[gpytorch.kernels.Kernel] | None = None)
Multi-output GP model implemented as a list of independent single-task GP models, allowing decoupled updates.
- Parameters:
input_dim (int) – Dimensionality of the input data.
output_dim (int) – Dimensionality of the output data.
noise_var (Optional[Union[float, ArrayLike]]) – Noise variance for Gaussian likelihood, if known noise variance is assumed. Defaults to None.
input_transform (Optional[InputTransform]) – An input transform to apply on training and prediction data. Defaults to None. Generally, normalization should be applied.
output_transform (Optional[OutputTransform]) – An output transform to apply on training and prediction data. Defaults to None. Generally, standardization should be applied.
mean_modules (Optional[Union[gpytorch.means.Mean, List[gpytorch.means.Mean]]]) – Mean modules for the GP models. If None, a default module will be created when each model is instantiated. Defaults to None. Can be a single module or a list of modules, one for each output dimension.
covar_modules (Optional[Union[gpytorch.kernels.Kernel, List[gpytorch.kernels.Kernel]]]) – Covariance modules for the GP models. If None, a default module will be created when each model is instantiated. Defaults to None. Can be a single module or a list of modules, one for each output dimension.
- add_sample(X_t: numpy.typing.ArrayLike, Y_t: numpy.typing.ArrayLike, dim_index: int | List[int])
Add new samples to the training data for specified output dimension(s).
- Parameters:
X_t (ArrayLike) – Input sample data.
Y_t (ArrayLike) – Target sample data.
dim_index (Union[int, List[int]]) – Index or indices of output dimensions to update. If an integer is provided, the sample is added to the corresponding model. If a list of integers is provided, the samples are added to the corresponding models.
- clear_data()
Clear stored training data.
- evaluate_kernel(X: numpy.typing.ArrayLike)
Evaluate the kernels for the input data across all output dimensions and combine.
- Parameters:
X (ArrayLike) – Input data to evaluate the kernel for.
- Returns:
Covariance matrix of shape (num_objective*num_data) x (num_objective*num_data).
- Return type:
np.ndarray
- get_lengthscale_and_var() tuple[numpy.ndarray, numpy.ndarray]
Return the kernel lengthscales and variances for each model (objective).
- Returns:
Lengthscales and variances for each objective.
- Return type:
tuple[np.ndarray, np.ndarray]
- predict(test_X: numpy.typing.ArrayLike) tuple[numpy.ndarray, numpy.ndarray]
Make predictions on test data.
- Parameters:
test_X (ArrayLike) – Test data.
- Returns:
Predicted means and variances corresponding to each test point.
- Return type:
tuple[np.ndarray, np.ndarray]
- sample_from_posterior(test_X: numpy.typing.ArrayLike, sample_count: int = 1)
Sample from the posterior distribution, considering all objectives.
- Parameters:
test_X (ArrayLike) – Test data.
sample_count (int) – Number of samples to draw.
- Returns:
Samples from the posterior distribution with shape sample_count x num_test_data x num_objective.
- Return type:
np.ndarray
- sample_from_single_posterior(test_X: numpy.typing.ArrayLike, dim_index: int, sample_count=1)
Sample from the posterior distribution, considering only one objective.
- Parameters:
test_X (ArrayLike) – Test data.
dim_index (int) – Index of the objective to take posterior samples from.
sample_count (int) – Number of samples to draw.
- Returns:
Samples from the posterior distribution of the specified objective, with shape sample_count x num_test_data.
- Return type:
np.ndarray
- train()
Train the hyperparameters of GP model.
- update()
Create GP model or update it with the current training data.
Kernels
- class vopy.models.MixedKernel(*args: Any, **kwargs: Any)
Kernel that combines a Matern kernel and a categorical (Hamming) kernel. It contains two components (one additive and one multiplicative) that are added together as follows:
- K(x1, x2) =
K_cont_1(x1[cont], x2[cont]) + K_cat_1(x1[cat], x2[cat]) + K_cont_2(x1[cont], x2[cont]) * K_cat_2(x1[cat], x2[cat])
where x1[cont], x1[cont] are continuous inputs and x1[cat], x2[cat] are categorical inputs.
- Parameters:
continuous_dims (Tuple[int]) – Indices of continuous dimensions (used for continuous kernel).
categorical_dims (Tuple[int]) – Indices of categorical dimensions (used for categorical kernel).
continuous_kwargs (Optional[dict]) – Additional keyword arguments for the continuous kernel.
categorical_kwargs (Optional[dict]) – Additional keyword arguments for the categorical kernel.
- forward(x1: torch.Tensor, x2: torch.Tensor, **params)
Compute the kernel function as given in the class docstring.
- Parameters:
x1 (torch.Tensor) – First input tensor (continuous and categorical).
x2 (torch.Tensor) – Second input tensor (continuous and categorical).
- Returns:
The computed kernel matrix. Shape is (batch_shape, n1, n2), where n1 and n2 are the number of elements in x1 and x2.
- Return type:
torch.Tensor