Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self, train_x, train_y, likelihood):
super(GPRegressionModel, self).__init__(train_x, train_y, likelihood)
self.mean_module = ConstantMean(prior=SmoothedBoxPrior(-1e-5, 1e-5))
self.base_covar_module = ScaleKernel(RBFKernel(lengthscale_prior=SmoothedBoxPrior(exp(-5), exp(6), sigma=0.1)))
self.covar_module = GridInterpolationKernel(self.base_covar_module, grid_size=50, num_dims=1)
self.feature_extractor = feature_extractor
def __init__(self, grid_size=20, grid_bounds=[(-0.1, 1.1)]):
variational_distribution = gpytorch.variational.CholeskyVariationalDistribution(
num_inducing_points=int(pow(grid_size, len(grid_bounds)))
)
variational_strategy = gpytorch.variational.GridInterpolationVariationalStrategy(
self, grid_size=grid_size, grid_bounds=grid_bounds, variational_distribution=variational_distribution
)
super(GPRegressionModel, self).__init__(variational_strategy)
self.mean_module = ConstantMean(prior=SmoothedBoxPrior(-10, 10))
self.covar_module = ScaleKernel(RBFKernel(lengthscale_prior=SmoothedBoxPrior(exp(-3), exp(6), sigma=0.1)))
def __init__(self, grid_size=32, grid_bounds=[(0, 1)]):
variational_distribution = gpytorch.variational.CholeskyVariationalDistribution(
num_inducing_points=int(pow(grid_size, len(grid_bounds)))
)
variational_strategy = gpytorch.variational.GridInterpolationVariationalStrategy(
self, grid_size=grid_size, grid_bounds=grid_bounds, variational_distribution=variational_distribution
)
super(GPClassificationModel, self).__init__(variational_strategy)
self.mean_module = ConstantMean(prior=SmoothedBoxPrior(-5, 5))
self.covar_module = ScaleKernel(
RBFKernel(lengthscale_prior=SmoothedBoxPrior(exp(-5), exp(6), sigma=0.1)),
outputscale_prior=SmoothedBoxPrior(exp(-5), exp(6), sigma=0.1),
)
def test_lkj_covariance_prior_validate_args(self):
sd_prior = SmoothedBoxPrior(exp(-1), exp(1), validate_args=True)
LKJCovariancePrior(2, 1.0, sd_prior)
with self.assertRaises(ValueError):
LKJCovariancePrior(1.5, 1.0, sd_prior, validate_args=True)
with self.assertRaises(ValueError):
LKJCovariancePrior(2, -1.0, sd_prior, validate_args=True)
def test_prior(self, cuda=False):
train_x, test_x, train_y, test_y = self._get_data(cuda=cuda)
# We're manually going to set the hyperparameters to be ridiculous
likelihood = GaussianLikelihood(noise_prior=SmoothedBoxPrior(exp(-3), exp(3), sigma=0.1))
gp_model = ExactGPModel(None, None, likelihood)
# Update lengthscale prior to accommodate extreme parameters
gp_model.covar_module.base_kernel.register_prior(
"lengthscale_prior", SmoothedBoxPrior(exp(-10), exp(10), sigma=0.5), "raw_lengthscale"
)
gp_model.mean_module.initialize(constant=1.5)
gp_model.covar_module.base_kernel.initialize(lengthscale=1)
likelihood.initialize(noise=0)
if cuda:
gp_model.cuda()
likelihood.cuda()
# Compute posterior distribution
gp_model.eval()
likelihood.eval()
# The model should predict in prior mode
function_predictions = likelihood(gp_model(train_x))
correct_variance = gp_model.covar_module.outputscale + likelihood.noise
def __init__(self, inducing_points):
variational_distribution = CholeskyVariationalDistribution(
inducing_points.size(-2), batch_shape=torch.Size([2]),
)
variational_strategy = WhitenedVariationalStrategy(
self, inducing_points, variational_distribution, learn_inducing_locations=True
)
super(SVGPRegressionModel, self).__init__(variational_strategy)
self.mean_module = gpytorch.means.ConstantMean()
self.covar_module = gpytorch.kernels.ScaleKernel(
gpytorch.kernels.RBFKernel(lengthscale_prior=gpytorch.priors.SmoothedBoxPrior(0.001, 1.0, sigma=0.1))
)
def test_smoothed_box_prior_to_gpu(self):
if torch.cuda.is_available():
prior = SmoothedBoxPrior(torch.zeros(2), torch.ones(2)).cuda()
self.assertEqual(prior.a.device.type, "cuda")
self.assertEqual(prior.b.device.type, "cuda")
self.assertEqual(prior.sigma.device.type, "cuda")
self.assertEqual(prior._c.device.type, "cuda")
self.assertEqual(prior._r.device.type, "cuda")
self.assertEqual(prior._M.device.type, "cuda")
self.assertEqual(prior.tails.loc.device.type, "cuda")
self.assertEqual(prior.tails.scale.device.type, "cuda")
def test_smoothed_box_prior_log_prob_log_transform(self, cuda=False):
device = torch.device("cuda") if cuda else torch.device("cpu")
a, b = torch.zeros(2, device=device), torch.ones(2, device=device)
sigma = 0.1
prior = SmoothedBoxPrior(a, b, sigma, transform=torch.exp)
t = torch.tensor([0.5, 1.1], device=device).log()
self.assertAlmostEqual(prior.log_prob(t).item(), -0.9473, places=4)
t = torch.tensor([[0.5, 1.1], [0.1, 0.25]], device=device).log()
log_prob_expected = torch.tensor([-0.947347, -0.447347], device=t.device)
self.assertTrue(torch.all(approx_equal(prior.log_prob(t), log_prob_expected)))
with self.assertRaises(RuntimeError):
prior.log_prob(torch.ones(3, device=device))
def __init__(self, train_inputs, train_targets, likelihood):
super(ExactGPModel, self).__init__(train_inputs, train_targets, likelihood)
self.mean_module = ConstantMean(prior=SmoothedBoxPrior(-1, 1))
self.covar_module = ScaleKernel(
RBFKernel(lengthscale_prior=SmoothedBoxPrior(math.exp(-3), math.exp(3), sigma=0.1))
)
def __init__(self):
super(GPClassificationModel, self).__init__(grid_size=32, grid_bounds=[(0, 1)])
self.mean_module = ConstantMean(prior=SmoothedBoxPrior(-1e-5, 1e-5))
self.covar_module = RBFKernel(
log_lengthscale_prior=SmoothedBoxPrior(exp(-5), exp(6), sigma=0.1, log_transform=True)
)
self.register_parameter(
name="log_outputscale",
parameter=torch.nn.Parameter(torch.Tensor([0])),
prior=SmoothedBoxPrior(exp(-5), exp(6), sigma=0.1, log_transform=True),
)