-
Notifications
You must be signed in to change notification settings - Fork 575
Heteroskedastic likelihoods and log-noise models #337
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
77cc6d2
[WIP] Heteroskedastic Likelihood
Balandat 199db36
DeprecationWarning for GaussianLikelihood, add MT version
Balandat cda00da
Merge branch 'master' into heteroskedastic
Balandat 529456c
New base class, clean up, fix MTGLikelihood
Balandat ad257fd
Remove stray IPython debugger import
Balandat 46ae81c
Merge branch 'master' into heteroskedastic
Balandat fbc5724
Rename log_noise_covar to noise_covar, small fixes
Balandat 1454558
Modify HeteroskedasticNoise to subset output
Balandat 464779b
Merge branch 'master' into heteroskedastic
Balandat 7abe05a
Bugfixes
Balandat d44a657
Some test fixes
Balandat a50f22f
Merge branch 'arbibatch_diag_lazy' into heteroskedastic
Balandat b40e136
Address some (not all) issues with multi-dim batch shapes
Balandat d656ff4
Merge branch 'master' into heteroskedastic
Balandat 9af9ab5
Various fixes, streamline log_ deprecation
Balandat 864ce0c
Update docstrings, use Kronecker MTGLh in batch MT test case
Balandat d6ca194
Fix lint
Balandat 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,18 +1,19 @@ | ||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
from __future__ import unicode_literals | ||
from __future__ import absolute_import, division, print_function, unicode_literals | ||
|
||
from .bernoulli_likelihood import BernoulliLikelihood | ||
from .gaussian_likelihood import GaussianLikelihood, HomoskedasticGaussianLikelihood | ||
from .likelihood import Likelihood | ||
from .gaussian_likelihood import GaussianLikelihood | ||
from .multitask_gaussian_likelihood import MultitaskGaussianLikelihood | ||
from .bernoulli_likelihood import BernoulliLikelihood | ||
from .noise_models import HeteroskedasticNoise | ||
from .softmax_likelihood import SoftmaxLikelihood | ||
|
||
|
||
__all__ = [ | ||
"Likelihood", | ||
"BernoulliLikelihood", | ||
"GaussianLikelihood", | ||
"HeteroskedasticNoise", | ||
"HomoskedasticGaussianLikelihood", | ||
"Likelihood", | ||
"MultitaskGaussianLikelihood", | ||
"BernoulliLikelihood", | ||
"SoftmaxLikelihood", | ||
] |
This file contains hidden or 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,42 +1,56 @@ | ||
from __future__ import absolute_import, division, print_function, unicode_literals | ||
|
||
import math | ||
import torch | ||
from ..distributions import MultivariateNormal | ||
from ..functions import add_diag | ||
from ..likelihoods import Likelihood | ||
from .. import settings | ||
import warnings | ||
import logging | ||
|
||
from .. import settings | ||
from ..distributions import MultivariateNormal | ||
from ..lazy import AddedDiagLazyTensor, DiagLazyTensor | ||
from .likelihood import Likelihood | ||
from .noise_models import HomoskedasticNoise | ||
|
||
class GaussianLikelihood(Likelihood): | ||
r""" | ||
""" | ||
|
||
def __init__(self, log_noise_prior=None, batch_size=1): | ||
super(GaussianLikelihood, self).__init__() | ||
self.register_parameter( | ||
name="log_noise", parameter=torch.nn.Parameter(torch.zeros(batch_size, 1)), prior=log_noise_prior | ||
) | ||
DEPRECATION_WARNING = "'GaussianLikelihood' was renamed to 'HomoskedasticGaussianLikelihood'" | ||
|
||
@property | ||
def noise(self): | ||
return self.log_noise.exp() | ||
|
||
def forward(self, input): | ||
class GaussianLikelihood(Likelihood): | ||
Balandat marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
def __init__(self, *args, **kwargs): | ||
if len(args) + len(kwargs) == 0 or "log_noise_prior" in kwargs or "batch_size" in kwargs: | ||
warnings.warn(DEPRECATION_WARNING, DeprecationWarning) | ||
logging.warning(DEPRECATION_WARNING) | ||
self.__init__(log_noise_covar=HomoskedasticNoise(*args, **kwargs)) | ||
self._is_homoskedastic = True | ||
else: | ||
super(GaussianLikelihood, self).__init__() | ||
self.log_noise_covar = args[0] if len(kwargs) == 0 else kwargs["log_noise_covar"] | ||
|
||
def forward(self, input, *params): | ||
if not isinstance(input, MultivariateNormal): | ||
raise ValueError("GaussianLikelihood requires a MultivariateNormal input") | ||
mean, covar = input.mean, input.lazy_covariance_matrix | ||
noise = self.noise | ||
if covar.ndimension() == 2: | ||
if settings.debug.on() and noise.size(0) > 1: | ||
raise RuntimeError("With batch_size > 1, expected a batched MultivariateNormal distribution.") | ||
noise = noise.squeeze(0) | ||
log_noise_covar = self.log_noise_covar(*params) | ||
Balandat marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if isinstance(log_noise_covar, DiagLazyTensor): | ||
Balandat marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
full_covar = AddedDiagLazyTensor(covar, log_noise_covar.exp()) | ||
else: | ||
# TODO: Deal with non-diagonal noise covariance models | ||
full_covar = covar + log_noise_covar.exp() | ||
|
||
return input.__class__(mean, full_covar) | ||
|
||
def variational_log_probability(self, input, target): | ||
if hasattr(self, "_is_homoskedastic"): | ||
return HomoskedasticGaussianLikelihood.variational_log_probability(self, input, target) | ||
else: | ||
raise NotImplementedError | ||
|
||
|
||
return input.__class__(mean, add_diag(covar, noise)) | ||
class HomoskedasticGaussianLikelihood(GaussianLikelihood): | ||
def __init__(self, log_noise_prior=None, batch_size=1): | ||
log_noise_covar = HomoskedasticNoise(log_noise_prior=log_noise_prior, batch_size=1) | ||
super(HomoskedasticGaussianLikelihood, self).__init__(log_noise_covar=log_noise_covar) | ||
|
||
def variational_log_probability(self, input, target): | ||
mean, variance = input.mean, input.variance | ||
log_noise = self.log_noise | ||
log_noise = self.log_noise_covar.log_noise | ||
if variance.ndimension() == 1: | ||
if settings.debug.on() and log_noise.size(0) > 1: | ||
raise RuntimeError("With batch_size > 1, expected a batched MultivariateNormal distribution.") | ||
|
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from __future__ import absolute_import, division, print_function, unicode_literals | ||
Balandat marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
import torch | ||
from torch.nn import Parameter | ||
|
||
from ..lazy import DiagLazyTensor | ||
from ..module import Module | ||
|
||
|
||
class HomoskedasticNoise(Module): | ||
def __init__(self, log_noise_prior=None, batch_size=1): | ||
super(HomoskedasticNoise, self).__init__() | ||
self.register_parameter( | ||
name="log_noise", parameter=Parameter(torch.zeros(batch_size, 1)), prior=log_noise_prior | ||
) | ||
|
||
def forward(self, params): | ||
noise = self.log_noise.exp() | ||
if isinstance(params, list): | ||
variance_shape = params[0].shape[:-2] + params[0].shape[-1:] | ||
else: | ||
variance_shape = params.shape[:-2] + params.shape[-1:] | ||
if len(variance_shape) == 1: | ||
noise = noise.squeeze(0) | ||
variances = noise * torch.ones(*variance_shape, dtype=noise.dtype, device=noise.device) | ||
return DiagLazyTensor(variances) |
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.
Uh oh!
There was an error while loading. Please reload this page.