Пример #1
0
 def __init__(
     self,
     train_X: Tensor,
     train_Y: Tensor,
     train_iteration_fidelity: bool = True,
     train_data_fidelity: bool = True,
     likelihood: Optional[Likelihood] = None,
 ) -> None:
     train_X, train_Y, _ = self._set_dimensions(train_X=train_X,
                                                train_Y=train_Y)
     num_fidelity = train_iteration_fidelity + train_data_fidelity
     ard_num_dims = train_X.shape[-1] - num_fidelity
     active_dimsX = list(range(train_X.shape[-1] - num_fidelity))
     rbf_kernel = RBFKernel(
         ard_num_dims=ard_num_dims,
         batch_shape=self._aug_batch_shape,
         lengthscale_prior=GammaPrior(3.0, 6.0),
         active_dims=active_dimsX,
     )
     exp_kernel = ExpDecayKernel(
         batch_shape=self._aug_batch_shape,
         lengthscale_prior=GammaPrior(3.0, 6.0),
         offset_prior=GammaPrior(3.0, 6.0),
         power_prior=GammaPrior(3.0, 6.0),
     )
     ds_kernel = DownsamplingKernel(
         batch_shape=self._aug_batch_shape,
         offset_prior=GammaPrior(3.0, 6.0),
         power_prior=GammaPrior(3.0, 6.0),
     )
     if train_iteration_fidelity and train_data_fidelity:
         active_dimsS1 = [train_X.shape[-1] - 1]
         active_dimsS2 = [train_X.shape[-1] - 2]
         exp_kernel.active_dims = torch.tensor(active_dimsS1)
         ds_kernel.active_dims = torch.tensor(active_dimsS2)
         kernel = rbf_kernel * exp_kernel * ds_kernel
     elif train_iteration_fidelity or train_data_fidelity:
         active_dimsS = [train_X.shape[-1] - 1]
         if train_iteration_fidelity:
             exp_kernel.active_dims = torch.tensor(active_dimsS)
             kernel = rbf_kernel * exp_kernel
         else:
             ds_kernel.active_dims = torch.tensor(active_dimsS)
             kernel = rbf_kernel * ds_kernel
     else:
         raise UnsupportedError(
             "You should have at least one fidelity parameter.")
     covar_module = ScaleKernel(
         kernel,
         batch_shape=self._aug_batch_shape,
         outputscale_prior=GammaPrior(2.0, 0.15),
     )
     super().__init__(train_X=train_X,
                      train_Y=train_Y,
                      covar_module=covar_module)
     self.to(train_X)
Пример #2
0
    def test_computes_exponential_decay_function(self):
        a = torch.tensor([1.0, 2.0]).view(2, 1)
        b = torch.tensor([2.0, 4.0]).view(2, 1)
        lengthscale = 1
        power = 1
        offset = 1

        kernel = ExpDecayKernel()
        kernel.initialize(lengthscale=lengthscale, power=power, offset=offset)
        kernel.eval()

        diff = torch.tensor([[4.0, 6.0], [5.0, 7.0]])
        actual = offset + torch.tensor([1.0]).div(diff.pow(power))
        res = kernel(a, b).evaluate()

        self.assertLess(torch.norm(res - actual), 1e-5)
Пример #3
0
    def test_subset_active_compute_exponential_decay_function(self):
        a = torch.tensor([1.0, 2.0]).view(2, 1)
        a_p = torch.tensor([3.0, 4.0]).view(2, 1)
        a = torch.cat((a, a_p), 1)
        b = torch.tensor([2.0, 4.0]).view(2, 1)
        lengthscale = 1
        power = 1
        offset = 1

        kernel = ExpDecayKernel(active_dims=[0])
        kernel.initialize(lengthscale=lengthscale, power=power, offset=offset)
        kernel.eval()

        diff = torch.tensor([[4.0, 6.0], [5.0, 7.0]])
        actual = offset + diff.pow(-power)
        res = kernel(a, b).evaluate()

        self.assertLess(torch.norm(res - actual), 1e-5)
Пример #4
0
    def test_computes_exponential_decay_function_batch(self):
        a = torch.tensor([[1.0, 2.0], [3.0, 4.0]]).view(2, 2, 1)
        b = torch.tensor([[5.0, 6.0], [7.0, 8.0]]).view(2, 2, 1)
        lengthscale = 1
        power = 1
        offset = 1

        kernel = ExpDecayKernel(batch_shape=torch.Size([2]))
        kernel.initialize(lengthscale=lengthscale, power=power, offset=offset)
        kernel.eval()

        actual = torch.zeros(2, 2, 2)

        diff = torch.tensor([[7.0, 8.0], [8.0, 9.0]])
        actual[0, :, :] = offset + diff.pow(-power)

        diff = torch.tensor([[11.0, 12.0], [12.0, 13.0]])
        actual[1, :, :] = offset + diff.pow(-power)

        res = kernel(a, b).evaluate()
        self.assertLess(torch.norm(res - actual), 1e-5)
Пример #5
0
 def test_initialize_lengthscale(self):
     kernel = ExpDecayKernel()
     kernel.initialize(lengthscale=1)
     actual_value = torch.tensor(1.0).view_as(kernel.lengthscale)
     self.assertLess(torch.norm(kernel.lengthscale - actual_value), 1e-5)
Пример #6
0
 def test_initialize_offset_prior(self):
     kernel = ExpDecayKernel()
     kernel.offset_prior = NormalPrior(1, 1)
     self.assertTrue(isinstance(kernel.offset_prior, NormalPrior))
     kernel2 = ExpDecayKernel(offset_prior=GammaPrior(1, 1))
     self.assertTrue(isinstance(kernel2.offset_prior, GammaPrior))
Пример #7
0
 def test_initialize_power_batch(self):
     kernel = ExpDecayKernel(batch_shape=torch.Size([2]))
     power_init = torch.tensor([1.0, 2.0])
     kernel.initialize(power=power_init)
     actual_value = power_init.view_as(kernel.power)
     self.assertLess(torch.norm(kernel.power - actual_value), 1e-5)
Пример #8
0
 def test_initialize_power(self):
     kernel = ExpDecayKernel()
     kernel.initialize(power=1)
     actual_value = torch.tensor(1.0).view_as(kernel.power)
     self.assertLess(torch.norm(kernel.power - actual_value), 1e-5)
Пример #9
0
 def create_kernel_no_ard(self, **kwargs):
     return ExpDecayKernel(**kwargs)
Пример #10
0
 def test_initialize_offset_batch(self):
     kernel = ExpDecayKernel(batch_shape=torch.Size([2]))
     off_init = torch.tensor([1.0, 2.0])
     kernel.initialize(offset=off_init)
     actual_value = off_init.view_as(kernel.offset)
     self.assertLess(torch.norm(kernel.offset - actual_value), 1e-5)
Пример #11
0
 def test_initialize_offset(self):
     kernel = ExpDecayKernel()
     kernel.initialize(offset=1)
     actual_value = torch.tensor(1.0).view_as(kernel.offset)
     self.assertLess(torch.norm(kernel.offset - actual_value), 1e-5)
Пример #12
0
 def test_initialize_lengthscale_batch(self):
     kernel = ExpDecayKernel(batch_shape=torch.Size([2]))
     ls_init = torch.tensor([1.0, 2.0])
     kernel.initialize(lengthscale=ls_init)
     actual_value = ls_init.view_as(kernel.lengthscale)
     self.assertLess(torch.norm(kernel.lengthscale - actual_value), 1e-5)