예제 #1
0
    def test_computes_cubic_kernel(self):
        a = torch.tensor([[4, 1], [2, 2], [8, 0]], dtype=torch.float)
        b = torch.tensor([[0, 0], [2, 1], [1, 0]], dtype=torch.float)
        kernel = PolynomialKernel(power=3)
        kernel.eval()

        actual = torch.zeros(3, 3)
        for i in range(3):
            for j in range(3):
                actual[i, j] = (a[i].matmul(b[j]) + kernel.offset).pow(
                    kernel.power)

        res = kernel(a, b).evaluate()
        self.assertLess(torch.norm(res - actual), 1e-5)

        # diag
        res = kernel(a, b).diag()
        actual = actual.diag()
        self.assertLess(torch.norm(res - actual), 1e-5)

        # batch_dims
        actual = torch.zeros(2, 3, 3)
        for l in range(2):
            actual[l] = kernel(a[:, l].unsqueeze(-1),
                               b[:, l].unsqueeze(-1)).evaluate()

        res = kernel(a, b, last_dim_is_batch=True).evaluate()
        self.assertLess(torch.norm(res - actual), 1e-5)

        # batch_dims + diag
        res = kernel(a, b, last_dim_is_batch=True).diag()
        actual = torch.cat(
            [actual[i].diag().unsqueeze(0) for i in range(actual.size(0))])
        self.assertLess(torch.norm(res - actual), 1e-5)
예제 #2
0
    def test_quadratic_kernel_batch(self):
        a = torch.tensor([[4, 2, 8], [1, 2, 3]],
                         dtype=torch.float).view(2, 3, 1)
        b = torch.tensor([[0, 2, 1], [-1, 2, 0]],
                         dtype=torch.float).view(2, 3, 1)
        kernel = PolynomialKernel(power=2, batch_shape=torch.Size(
            [2])).initialize(offset=torch.rand(2, 1))
        kernel.eval()

        actual = torch.zeros(2, 3, 3)
        for k in range(2):
            for i in range(3):
                for j in range(3):
                    actual[k, i, j] = (a[k, i].matmul(b[k, j]) +
                                       kernel.offset[k]).pow(kernel.power)

        res = kernel(a, b).evaluate()
        self.assertLess(torch.norm(res - actual), 1e-5)
    def __init__(self,
                 mean_name='constant',
                 kernel_name='RBF',
                 grid_bounds=[(-1, 1), (-1, 1)],
                 grid_size=100,
                 num_samples=1000):

        self.mean = mean_name
        self.kernel = kernel_name
        self.num_samples = num_samples
        self.grid_bounds = grid_bounds
        self.grid_size = grid_size
        self.x_dim = 2  # x and y dim are fixed for this dataset.
        self.y_dim = 1

        self.data = []

        # create grid
        grid = torch.zeros(grid_size, len(grid_bounds))
        for i in range(len(grid_bounds)):
            grid_diff = float(grid_bounds[i][1] -
                              grid_bounds[i][0]) / (grid_size - 2)
            grid[:,
                 i] = torch.linspace(grid_bounds[i][0] - grid_diff,
                                     grid_bounds[i][1] + grid_diff, grid_size)

        x = gpytorch.utils.grid.create_data_from_grid(grid)

        # initialize likelihood and model
        likelihood = gpytorch.likelihoods.GaussianLikelihood()

        mean_dict = {'constant': ConstantMean()}
        kernel_dict = {
            'RBF': RBFKernel(),
            'cosine': CosineKernel(),
            'linear': LinearKernel(),
            'periodic': PeriodicKernel(),
            'LCM': LCMKernel(base_kernels=[CosineKernel()], num_tasks=1),
            'polynomial': PolynomialKernel(power=3),
            'matern': MaternKernel()
        }

        # evaluate GP on prior distribution
        with gpytorch.settings.prior_mode(True):
            model = ExactGPModel(x,
                                 None,
                                 likelihood,
                                 mean_module=mean_dict[self.mean],
                                 kernel_module=gpytorch.kernels.GridKernel(
                                     kernel_dict[self.kernel], grid=grid))
            gp = model(x)
            for i in range(num_samples):
                y = gp.sample()
                self.data.append(
                    (x, y.unsqueeze(1)))  #+torch.randn(y.size())*0.2))
예제 #4
0
 def __init__(self, train_x, train_y, num_mixtures=10):
     smk = SpectralMixtureKernel(num_mixtures)
     smk.initialize_from_data(train_x, train_y)
     kernel = AdditiveKernel(
         smk,
         PolynomialKernel(2),
         RBFKernel(),
     )
     super(CompositeKernelGP, self).__init__(kernel, train_x, train_y)
     self.mean = gp.means.ConstantMean()
     self.smk = smk
    def __init__(self,
                 mean_list,
                 kernel_list,
                 num_points=100,
                 num_samples=1000,
                 amplitude_range=(-5., 5.)):
        self.mean_list = mean_list
        self.kernel_list = kernel_list
        self.num_config = len(mean_list) * len(kernel_list)
        self.num_samples = num_samples
        self.num_points = num_points
        self.x_dim = 1  # x and y dim are fixed for this dataset.
        self.y_dim = 1
        self.amplitude_range = amplitude_range
        self.data = []

        # initialize likelihood and model
        x = torch.linspace(self.amplitude_range[0], self.amplitude_range[1],
                           num_points).unsqueeze(1)

        likelihood = gpytorch.likelihoods.GaussianLikelihood()
        mean_dict = {'constant': ConstantMean(), 'linear': LinearMean(1)}
        kernel_dict = {
            'RBF': RBFKernel(),
            'cosine': CosineKernel(),
            'linear': LinearKernel(),
            'periodic': PeriodicKernel(period_length=0.5),
            'LCM': LCMKernel(base_kernels=[CosineKernel()], num_tasks=1),
            'polynomial': PolynomialKernel(power=2),
            'matern': MaternKernel()
        }

        # create a different GP from each possible configuration
        for mean in self.mean_list:
            for kernel in self.kernel_list:
                # evaluate GP on prior distribution
                with gpytorch.settings.prior_mode(True):
                    model = ExactGPModel(x,
                                         None,
                                         likelihood,
                                         mean_module=mean_dict[mean],
                                         kernel_module=kernel_dict[kernel])

                    gp = model(x)
                    # sample from current configuration
                    for i in range(num_samples // self.num_config + 1):
                        y = gp.sample()
                        self.data.append(
                            (x, y.unsqueeze(1)))  #+torch.randn(y.shape)*0))
예제 #6
0
 def create_kernel_no_ard(self, **kwargs):
     return PolynomialKernel(power=2, **kwargs)