Skip to content

Move infinite interval bounds check into Interval constructor #2259

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 2 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions gpytorch/constraints/constraints.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env python3

from __future__ import annotations

import math

import torch
Expand All @@ -23,11 +25,21 @@ def __init__(self, lower_bound, upper_bound, transform=sigmoid, inv_transform=in
lower_bound (float or torch.Tensor): The lower bound on the parameter.
upper_bound (float or torch.Tensor): The upper bound on the parameter.
"""
lower_bound = torch.as_tensor(lower_bound).float()
upper_bound = torch.as_tensor(upper_bound).float()
dtype = torch.get_default_dtype()
lower_bound = torch.as_tensor(lower_bound).to(dtype)
upper_bound = torch.as_tensor(upper_bound).to(dtype)

if torch.any(torch.ge(lower_bound, upper_bound)):
raise RuntimeError("Got parameter bounds with empty intervals.")
raise ValueError("Got parameter bounds with empty intervals.")

if type(self) == Interval:
max_bound = torch.max(upper_bound)
min_bound = torch.min(lower_bound)
if max_bound == math.inf or min_bound == -math.inf:
raise ValueError(
"Cannot make an Interval directly with non-finite bounds. Use a derived class like "
"GreaterThan or LessThan instead."
)

super().__init__()

Expand Down Expand Up @@ -111,16 +123,6 @@ def transform(self, tensor):
if not self.enforced:
return tensor

if settings.debug.on():
max_bound = torch.max(self.upper_bound)
min_bound = torch.min(self.lower_bound)

if max_bound == math.inf or min_bound == -math.inf:
raise RuntimeError(
"Cannot make an Interval directly with non-finite bounds. Use a derived class like "
"GreaterThan or LessThan instead."
)

transformed_tensor = (self._transform(tensor) * (self.upper_bound - self.lower_bound)) + self.lower_bound

return transformed_tensor
Expand Down
8 changes: 8 additions & 0 deletions test/constraints/test_constraints.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3

import math
import unittest

import torch
Expand Down Expand Up @@ -69,6 +70,13 @@ def test_initial_value(self):
lkhd = gpytorch.likelihoods.GaussianLikelihood(noise_constraint=constraint)
self.assertEqual(lkhd.noise.item(), 3.0)

def test_error_on_infinite(self):
err_msg = "Cannot make an Interval directly with non-finite bounds"
with self.assertRaisesRegex(ValueError, err_msg):
gpytorch.constraints.Interval(0.0, math.inf)
with self.assertRaisesRegex(ValueError, err_msg):
gpytorch.constraints.Interval(-math.inf, 0.0)


class TestGreaterThan(unittest.TestCase, BaseTestCase):
def test_transform_float_greater_than(self):
Expand Down