Models
BaseModel
Bases: ABC
Interface of a probabilistic model.
Initializes a model with the specified device name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device_name |
str
|
The name of the PyTorch device to be used for computations. |
required |
Source code in pypolo/models/base_model.py
learn
abstractmethod
Optimizes the model parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_new |
ndarray
|
New training inputs of shape (num_inputs, dim_inputs). |
required |
y_new |
ndarray
|
New training outputs of shape (num_outputs, dim_outputs). |
required |
num_iter |
int
|
Number of optimization/training iterations. |
required |
verbose |
bool
|
Print the optimization information or not? |
True
|
writer |
Union[SummaryWriter, None]
|
Tensorboard writer. |
required |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
This is an abstract method and must be implemented by derived classes. |
Source code in pypolo/models/base_model.py
predict
abstractmethod
Makes predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_test |
ndarray
|
Test inputs of shape (num_inputs, dim_inputs). |
required |
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, ndarray]
|
Tuple[np.ndarray, np.ndarray]: A tuple containing predictive mean and predictive standard deviation of shape (num_inputs, 1). |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
This is an abstract method and must be implemented by derived classes. |
Source code in pypolo/models/base_model.py
GPRModel
Bases: BaseModel, Module
Gaussian Process Regression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device_name |
str
|
The name of the device to run the model. |
required |
kernel |
BaseKernel
|
The kernel function. |
required |
noise |
float
|
The noise variance of the Gaussian likelihood. |
required |
lr_hyper |
float
|
Learning rate of hyper-parameters. |
0.01
|
jitter |
float
|
The jitter to add to the diagonal of the covariance matrix. Defaults to 1e-6. |
1e-06
|
What is jitter and why is it necessary?
Jitter is a small positive number added to the diagonal of the covariance matrix to ensure that it is positive definite. This is necessary because the covariance matrix is not always positive definite due to numerical errors.
Source code in pypolo/models/gpr_model.py
noise
property
writable
The noise variance hyper-parameter.
Returns:
| Type | Description |
|---|---|
Tensor
|
torch.Tensor: The noise variance of the Gaussian likelihood. |
Positivity
The positivity of noise variance is ensured by a softplus function.
forward
Make prediction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_test |
Tensor
|
Test inputs of shape (num_inputs, dim_inputs). |
required |
noise_free |
bool
|
If True, predict the latent function values. Otherwise, predict the noisy targets. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
mean |
Tensor
|
Predictive mean of shape (num_inputs, 1) |
std |
Tensor
|
Predictive standard deviation of shape (num_inputs, 1). |
Difference between forward and predict
The forward method uses PyTorch tensors as inputs and outputs.
The predict method uses NumPy arrays as inputs and outputs.
Users should use predict method for model prediction.
Source code in pypolo/models/gpr_model.py
learn
Optimizes the model parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_new |
ndarray
|
New training inputs of shape (num_inputs, dim_inputs). |
required |
y_new |
ndarray
|
New training outputs of shape (num_outputs, dim_outputs). |
required |
num_iter |
int
|
Number of optimization/training iterations. |
required |
verbose |
bool
|
Print the optimization information or not? |
True
|
writer |
Union[SummaryWriter, None]
|
Tensorboard writer. |
required |
Source code in pypolo/models/gpr_model.py
predict
Makes predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_test |
ndarray
|
Test inputs of shape (num_inputs, dim_inputs). |
required |
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, ndarray]
|
Tuple[np.ndarray, np.ndarray]: A tuple containing predictive mean and predictive standard deviation of shape (num_inputs, 1). |