-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reuse hyperparameters #41
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ed3f1d8
specify which dims are latent in the kernel
thomaswmorris 310a8b0
cleaner output
thomaswmorris 8264b90
load saved hyperparameters on initialization
thomaswmorris 1a49c55
added test for reading hypers
thomaswmorris 02c6631
fixed notebooks
thomaswmorris edfac57
Merge branch 'main' into reuse-hypers
thomaswmorris 1091233
oops
thomaswmorris ee213aa
fixed utils.py
thomaswmorris 0f98533
pandas context
thomaswmorris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,41 @@ | ||
import botorch | ||
import gpytorch | ||
import torch | ||
from botorch.models.gpytorch import GPyTorchModel | ||
from gpytorch.models import ExactGP | ||
|
||
from . import kernels | ||
|
||
|
||
class LatentDirichletClassifier(botorch.models.gp_regression.SingleTaskGP): | ||
def __init__(self, train_inputs, train_targets, *args, **kwargs): | ||
class LatentGP(botorch.models.gp_regression.SingleTaskGP): | ||
def __init__(self, train_inputs, train_targets, skew_dims=True, *args, **kwargs): | ||
super().__init__(train_inputs, train_targets, *args, **kwargs) | ||
|
||
self.mean_module = gpytorch.means.ConstantMean() | ||
self.mean_module = gpytorch.means.ConstantMean(constant_prior=gpytorch.priors.NormalPrior(loc=0, scale=1)) | ||
|
||
self.covar_module = kernels.LatentKernel( | ||
num_inputs=train_inputs.shape[-1], | ||
num_outputs=train_targets.shape[-1], | ||
off_diag=True, | ||
skew_dims=skew_dims, | ||
diag_prior=True, | ||
scale_output=True, | ||
scale=True, | ||
**kwargs | ||
) | ||
|
||
def log_prob(self, x, n_samples=256): | ||
*input_shape, n_dim = x.shape | ||
samples = self.posterior(x.reshape(-1, n_dim)).sample(torch.Size((n_samples,))).exp() | ||
return torch.log((samples / samples.sum(-1, keepdim=True)).mean(0)[:, 1]).reshape(*input_shape, 1) | ||
|
||
|
||
class LatentGP(botorch.models.gp_regression.SingleTaskGP): | ||
def __init__(self, train_inputs, train_targets, *args, **kwargs): | ||
class LatentDirichletClassifier(botorch.models.gp_regression.SingleTaskGP): | ||
def __init__(self, train_inputs, train_targets, skew_dims=True, *args, **kwargs): | ||
super().__init__(train_inputs, train_targets, *args, **kwargs) | ||
|
||
self.mean_module = gpytorch.means.ConstantMean(constant_prior=gpytorch.priors.NormalPrior(loc=0, scale=1)) | ||
|
||
self.mean_module = gpytorch.means.ConstantMean() | ||
self.covar_module = kernels.LatentKernel( | ||
num_inputs=train_inputs.shape[-1], | ||
num_outputs=train_targets.shape[-1], | ||
off_diag=True, | ||
skew_dims=skew_dims, | ||
diag_prior=True, | ||
scale_output=True, | ||
scale=True, | ||
**kwargs | ||
) | ||
|
||
|
||
class OldBoTorchSingleTaskGP(ExactGP, GPyTorchModel): | ||
def __init__(self, train_inputs, train_targets, likelihood): | ||
super(OldBoTorchSingleTaskGP, self).__init__(train_inputs, train_targets, likelihood) | ||
self.mean_module = gpytorch.means.ConstantMean() | ||
self.covar_module = gpytorch.kernels.ScaleKernel( | ||
kernels.LatentMaternKernel(n_dim=train_inputs.shape[-1], off_diag=True, diagonal_prior=True) | ||
) | ||
|
||
def forward(self, x): | ||
mean_x = self.mean_module(x) | ||
covar_x = self.covar_module(x) | ||
|
||
return gpytorch.distributions.MultivariateNormal(mean_x, covar_x) | ||
|
||
|
||
class BoTorchMultiTaskGP(ExactGP, GPyTorchModel): | ||
_num_outputs = 1 # to inform GPyTorchModel API | ||
|
||
def __init__(self, train_inputs, train_targets, likelihood): | ||
self._num_outputs = train_targets.shape[-1] | ||
|
||
super(BoTorchMultiTaskGP, self).__init__(train_inputs, train_targets, likelihood) | ||
self.mean_module = gpytorch.means.MultitaskMean(gpytorch.means.ConstantMean(), num_tasks=self._num_outputs) | ||
self.covar_module = gpytorch.kernels.MultitaskKernel( | ||
kernels.LatentMaternKernel(n_dim=train_inputs.shape[-1], off_diag=True, diagonal_prior=True), | ||
num_tasks=self._num_outputs, | ||
rank=1, | ||
) | ||
|
||
def forward(self, x): | ||
mean_x = self.mean_module(x) | ||
covar_x = self.covar_module(x) | ||
return gpytorch.distributions.MultitaskMultivariateNormal(mean_x, covar_x) | ||
|
||
|
||
class OldBoTorchDirichletClassifier(gpytorch.models.ExactGP, botorch.models.gpytorch.GPyTorchModel): | ||
_num_outputs = 1 # to inform GPyTorchModel API | ||
|
||
def __init__(self, train_inputs, train_targets, likelihood): | ||
super(OldBoTorchDirichletClassifier, self).__init__(train_inputs, train_targets, likelihood) | ||
self.mean_module = gpytorch.means.ConstantMean(batch_shape=len(train_targets.unique())) | ||
self.covar_module = gpytorch.kernels.ScaleKernel( | ||
kernels.LatentMaternKernel(n_dim=train_inputs.shape[-1], off_diag=False, diagonal_prior=False) | ||
) | ||
|
||
def forward(self, x): | ||
mean_x = self.mean_module(x) | ||
covar_x = self.covar_module(x) | ||
return gpytorch.distributions.MultivariateNormal(mean_x, covar_x) | ||
|
||
def log_prob(self, x, n_samples=256): | ||
*input_shape, n_dim = x.shape | ||
samples = self.posterior(x.reshape(-1, n_dim)).sample(torch.Size((n_samples,))).exp() | ||
return torch.log((samples / samples.sum(-3, keepdim=True)).mean(0)[1]).reshape(*input_shape) | ||
return torch.log((samples / samples.sum(-1, keepdim=True)).mean(0)[:, 1]).reshape(*input_shape, 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docstrings should be added.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is overhauled in the next PR