예제 #1
0
 def setUp(self):
     self.mean = MultitaskMean(
         [ConstantMean(),
          ZeroMean(),
          ZeroMean(),
          ConstantMean()],
         n_tasks=4)
     self.mean.base_means[0].constant.data.fill_(5)
     self.mean.base_means[3].constant.data.fill_(7)
 def __init__(self):
     super(GPClassificationModel, self).__init__(grid_size=10, grid_bounds=[(0, n), (0, n)])
     # Near-zero mean
     self.mean_module = ConstantMean(constant_bounds=[-1e-5, 1e-5])
     # RBF as universal approximator
     self.covar_module = RBFKernel(log_lengthscale_bounds=(-5, 6))
     self.register_parameter('log_outputscale', nn.Parameter(torch.Tensor([0])), bounds=(-5,6))
 def __init__(self, train_x, train_y, likelihood):
     super(SpectralMixtureGPModel, self).__init__(train_x, train_y,
                                                  likelihood)
     self.mean_module = ConstantMean(prior=SmoothedBoxPrior(-1, 1))
     self.covar_module = SpectralMixtureKernel(num_mixtures=4,
                                               ard_num_dims=1)
     self.covar_module.initialize_from_data(train_x, train_y)
 def __init__(self, train_inputs, train_targets, likelihood, batch_size=1):
     super(ExactGPModel, self).__init__(train_inputs, train_targets, likelihood)
     self.mean_module = MultitaskMean(
         ConstantMean(batch_size=batch_size, prior=gpytorch.priors.SmoothedBoxPrior(-1, 1)), num_tasks=2
     )
     if batch_size > 1:
         self.covar_module = MultitaskKernel(
             RBFKernel(
                 batch_size=batch_size,
                 lengthscale_prior=gpytorch.priors.NormalPrior(
                     loc=torch.zeros(batch_size, 1, 1), scale=torch.ones(batch_size, 1, 1)
                 ),
             ),
             num_tasks=2,
             rank=1,
         )
     else:
         self.covar_module = MultitaskKernel(
             RBFKernel(
                 lengthscale_prior=gpytorch.priors.NormalPrior(
                     loc=torch.zeros(batch_size, 1, 1), scale=torch.ones(batch_size, 1, 1)
                 ),
             ),
             num_tasks=2,
             rank=1,
         )
예제 #5
0
 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(
             exp(-3), exp(3), sigma=0.1)))
 def __init__(self):
     likelihood = GaussianLikelihood(log_noise_bounds=(-3, 3))
     super(Model, self).__init__(likelihood)
     self.mean_module = ConstantMean(constant_bounds=(-1, 1))
     covar_module = RBFKernel()
     self.grid_covar_module = GridInterpolationKernel(covar_module)
     self.initialize_interpolation_grid(10, [(0, 1), (0, 1)])
예제 #7
0
 def __init__(self,
              train_X,
              train_Y,
              outcome_transform=None,
              input_transform=None):
     with torch.no_grad():
         transformed_X = self.transform_inputs(
             X=train_X, input_transform=input_transform)
     if outcome_transform is not None:
         train_Y, _ = outcome_transform(train_Y)
     self._validate_tensor_args(transformed_X, train_Y)
     self._set_dimensions(train_X=train_X, train_Y=train_Y)
     train_X, train_Y, _ = self._transform_tensor_args(X=train_X, Y=train_Y)
     likelihood = GaussianLikelihood(batch_shape=self._aug_batch_shape)
     super().__init__(train_X, train_Y, likelihood)
     self.mean_module = ConstantMean(batch_shape=self._aug_batch_shape)
     self.covar_module = ScaleKernel(
         RBFKernel(batch_shape=self._aug_batch_shape),
         batch_shape=self._aug_batch_shape,
     )
     if outcome_transform is not None:
         self.outcome_transform = outcome_transform
     if input_transform is not None:
         self.input_transform = input_transform
     self.to(train_X)
예제 #8
0
 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 = InducingPointKernel(
         self.base_covar_module, inducing_points=torch.linspace(0, 1, 32), likelihood=likelihood
     )
예제 #9
0
 def __init__(self, train_x, train_y, likelihood):
     super(GPRegressionModel, self).__init__(train_x, train_y, likelihood)
     self.mean_module = ConstantMean(constant_bounds=[-1e-5, 1e-5])
     self.base_covar_module = RBFKernel(log_lengthscale_bounds=(-5, 6))
     self.covar_module = InducingPointKernel(self.base_covar_module,
                                             inducing_points=torch.linspace(
                                                 0, 1, 32))
예제 #10
0
 def __init__(self, train_x, train_y, likelihood, ex_var_dim, kernel,
              **ker_conf):
     super(ExactGPModel, self).__init__(train_x, train_y, likelihood)
     self.mean_module = ConstantMean()
     _ker_conf = {'ard_num_dims': ex_var_dim}
     _ker_conf.update(ker_conf)
     self.covar_module = set_kernel(kernel, **_ker_conf)
 def __init__(self, train_x):
     super(LatentFunction, self).__init__(inducing_points=train_x)
     self.mean_module = ConstantMean(constant_bounds=[-1e-5, 1e-5])
     self.covar_module = RBFKernel(log_lengthscale_bounds=(-5, 6))
     self.register_parameter('log_outputscale',
                             nn.Parameter(torch.Tensor([0])),
                             bounds=(-5, 6))
예제 #12
0
 def create_mean(self):
     return MultitaskMean([
         ConstantMean(batch_shape=torch.Size([2, 3])),
         ZeroMean(),
         ZeroMean()
     ],
                          num_tasks=3)
예제 #13
0
    def test_mi_acqf(self):

        mean = ConstantMean().initialize(constant=1.2)
        covar = LinearKernel().initialize(variance=1.0)
        model = GPClassificationModel(
            inducing_min=torch.Tensor([0]),
            inducing_max=torch.Tensor([1]),
            inducing_size=10,
            mean_module=mean,
            covar_module=covar,
        )
        x = torch.rand(size=(10, 1))
        acqf = BernoulliMCMutualInformation(model=model, objective=ProbitObjective())
        acq_pytorch = acqf(x)

        samps_numpy = norm.cdf(
            multivariate_normal.rvs(mean=np.ones(10) * 1.2, cov=x @ x.T, size=10000)
        )
        samp_entropies = bernoulli(samps_numpy).entropy()
        mean_entropy = bernoulli(samps_numpy.mean(axis=0)).entropy()
        acq_numpy = mean_entropy - samp_entropies.mean(axis=0)

        # this assertion fails, not sure why, these should be equal to numerical
        # precision
        # self.assertTrue(np.allclose(acq_numpy, acq_pytorch.detach().numpy().flatten()))
        # this one succeeds
        self.assertTrue(
            pearsonr(acq_numpy, acq_pytorch.detach().numpy().flatten())[0] > (1 - 1e-5)
        )
    def __init__(self, train_x, train_y, likelihood, num_tasks=2):

        self.shape = torch.Size([num_tasks])
        super().__init__(train_x, train_y, likelihood)
        self.mean_module = ConstantMean(batch_shape=self.shape)
        self.covar_module = ScaleKernel(MaternKernel(batch_shape=self.shape),
                                        batch_shape=self.shape)
예제 #15
0
 def __init__(self):
     super(KissGPModel, self).__init__(grid_size=300,
                                       grid_bounds=[(0, 1)])
     self.mean_module = ConstantMean(constant_bounds=(-1, 1))
     covar_module = RBFKernel(log_lengthscale_bounds=(-100, 100))
     covar_module.log_lengthscale.data = torch.FloatTensor([-2])
     self.covar_module = covar_module
예제 #16
0
파일: hidden.py 프로젝트: kivo360/continuum
    def __init__(self,
                 input_dims,
                 output_dims,
                 num_inducing=128,
                 mean_type='constant'):
        if output_dims is None:
            inducing_points = torch.randn(num_inducing, input_dims)
            batch_shape = torch.Size([])
        else:
            inducing_points = torch.randn(output_dims, num_inducing,
                                          input_dims)
            batch_shape = torch.Size([output_dims])

        variational_distribution = CholeskyVariationalDistribution(
            num_inducing_points=num_inducing, batch_shape=batch_shape)

        variational_strategy = VariationalStrategy(
            self,
            inducing_points,
            variational_distribution,
            learn_inducing_locations=True)

        super(ApproximateDeepGPHiddenLayer,
              self).__init__(variational_strategy, input_dims, output_dims)

        if mean_type == 'constant':
            self.mean_module = ConstantMean(batch_shape=batch_shape)
        else:
            self.mean_module = LinearMean(input_dims)
        self.covar_module = ScaleKernel(RBFKernel(batch_shape=batch_shape,
                                                  ard_num_dims=input_dims),
                                        batch_shape=batch_shape,
                                        ard_num_dims=None)

        self.linear_layer = Linear(input_dims, 1)
예제 #17
0
 def __init__(self, train_x, train_y, likelihood):
     super(GPRegressionModel, self).__init__(train_x, train_y, likelihood)
     self.mean_module = ConstantMean(constant_bounds=[-5,5])
     self.base_covar_module = RBFKernel(log_lengthscale_bounds=(-5, 6))
     self.covar_module = GridInterpolationKernel(self.base_covar_module, grid_size=500,
                                                 grid_bounds=[(-10, 10), (-10, 10)])
     self.register_parameter('log_outputscale', nn.Parameter(torch.Tensor([0])), bounds=(-5,6))
예제 #18
0
 def __init__(self, train_x, train_y, likelihood):
     super(GPRegressionModel, self).__init__(train_x, train_y, likelihood)
     self.mean_module = ConstantMean(prior=SmoothedBoxPrior(-1, 1))
     self.base_covar_module = ScaleKernel(RBFKernel())
     self.covar_module = ProductStructureKernel(GridInterpolationKernel(
         self.base_covar_module, grid_size=100, num_dims=1),
                                                num_dims=2)
예제 #19
0
    def __init__(self, n_inducing):

        # number of inducing points and optimisation samples
        assert isinstance(n_inducing, int)
        self.m = n_inducing

        # variational distribution and strategy
        # NOTE: we put random normal dumby inducing points
        # here, which we'll change in self.fit
        vardist = CholeskyVariationalDistribution(self.m)
        varstra = VariationalStrategy(self,
                                      torch.randn((self.m, 2)),
                                      vardist,
                                      learn_inducing_locations=True)
        VariationalGP.__init__(self, varstra)

        # kernel — implemented in self.forward
        self.mean = ConstantMean()
        self.cov = MaternKernel(ard_num_dims=2)
        # self.cov = GaussianSymmetrizedKLKernel()
        self.cov = ScaleKernel(self.cov, ard_num_dims=2)

        # likelihood
        self.likelihood = GaussianLikelihood()

        # hardware allocation
        self.device = torch.device(
            'cuda' if torch.cuda.is_available() else 'cpu')
        self.likelihood.to(self.device).float()
        self.to(self.device).float()
 def __init__(self, train_x, train_y, likelihood):
     super(GPRegressionModel, self).__init__(train_x, train_y, likelihood)
     self.mean_module = ConstantMean(prior=SmoothedBoxPrior(-1, 1))
     self.base_covar_module = ScaleKernel(
         RBFKernel(log_lengthscale_prior=SmoothedBoxPrior(exp(-3), exp(3), sigma=0.1, log_transform=True))
     )
     self.covar_module = GridInterpolationKernel(self.base_covar_module, grid_size=64, grid_bounds=[(0, 1), (0, 1)])
예제 #21
0
    def __init__(self, input_dims, output_dims, num_inducing=128):
        if output_dims is None:
            inducing_points = torch.randn(num_inducing, input_dims)
            if torch.cuda.is_available():
                inducing_points = inducing_points.cuda()
            batch_shape = torch.Size([])
        else:
            inducing_points = torch.randn(output_dims, num_inducing,
                                          input_dims)
            if torch.cuda.is_available():
                inducing_points = inducing_points.cuda()
            batch_shape = torch.Size([output_dims])

        variational_distribution = CholeskyVariationalDistribution(
            num_inducing_points=num_inducing, batch_shape=batch_shape)

        variational_strategy = VariationalStrategy(
            self,
            inducing_points,
            variational_distribution,
            learn_inducing_locations=True)

        super().__init__(variational_strategy, input_dims, output_dims)

        self.mean_module = ConstantMean(batch_shape=batch_shape)
        self.covar_module = ScaleKernel(RBFKernel(ard_num_dims=None),
                                        ard_num_dims=None)
예제 #22
0
 def __init__(self, train_X, train_Y):
     # squeeze output dim before passing train_Y to ExactGP
     super().__init__(train_X, train_Y.squeeze(-1), GaussianLikelihood())
     self.mean_module = ConstantMean()
     self.covar_module = ScaleKernel(
         base_kernel=RBFKernel(ard_num_dims=train_X.shape[-1]), )
     self.to(train_X)  # make sure we're on the right device/dtype
 def __init__(self, train_x, train_y, likelihood):
     super(GPRegressionModel, self).__init__(train_x, train_y, likelihood)
     self.mean_module = ConstantMean(constant_bounds=(-1, 1))
     self.base_covar_module = RBFKernel(log_lengthscale_bounds=(-3, 3))
     self.covar_module = AdditiveGridInterpolationKernel(
         self.base_covar_module, grid_size=100, grid_bounds=[(0, 1)], n_components=2
     )
예제 #24
0
def _parse_mean(input_size: int,
                dim_outputs: int = 1,
                kind: str = 'zero') -> Mean:
    """Parse Mean string.

    Parameters
    ----------
    input_size: int.
        Size of input to GP (needed for linear mean functions).
    kind: str.
        String that identifies mean function.

    Returns
    -------
    mean: Mean.
        Mean function.
    """
    if kind.lower() == 'constant':
        mean = ConstantMean()
    elif kind.lower() == 'zero':
        mean = ZeroMean()
    elif kind.lower() == 'linear':
        mean = LinearMean(input_size=input_size,
                          batch_shape=torch.Size([dim_outputs]))
    else:
        raise NotImplementedError(
            'Mean function {} not implemented.'.format(kind))
    return mean
 def __init__(self, train_x, train_y, likelihood):
     super(MultitaskGPModel, self).__init__(train_x, train_y, likelihood)
     self.mean_module = MultitaskMean(ConstantMean(), n_tasks=2)
     self.data_covar_module = RBFKernel()
     self.covar_module = MultitaskKernel(self.data_covar_module,
                                         n_tasks=2,
                                         rank=1)
예제 #26
0
 def __init__(self):
     super(GPClassificationModel, self).__init__(BernoulliLikelihood())
     self.mean_module = ConstantMean(constant_bounds=[-1e-5, 1e-5])
     self.covar_module = RBFKernel(log_lengthscale_bounds=(-5, 6))
     self.register_parameter('log_outputscale',
                             nn.Parameter(torch.Tensor([0])),
                             bounds=(-5, 6))
예제 #27
0
 def __init__(self, train_x, train_y, likelihood):
     super(MultitaskGPModel, self).__init__(train_x, train_y, likelihood)
     self.mean_module = MultitaskMean(ConstantMean(), num_tasks=2)
     self.base_kernel_list = [RBFKernel()]
     self.covar_module = LCMKernel(self.base_kernel_list,
                                   num_tasks=2,
                                   rank=1)
 def __init__(self, train_x, train_y, likelihood):
     super(GPRegressionModel, self).__init__(train_x, train_y, likelihood)
     self.mean_module = ConstantMean(prior=SmoothedBoxPrior(-1, 1))
     self.base_covar_module = RBFKernel(ard_num_dims=2)
     self.covar_module = GridInterpolationKernel(self.base_covar_module,
                                                 grid_size=16,
                                                 num_dims=2)
 def __init__(self, train_x):
     super(GPClassificationModel, self).__init__(train_x)
     self.mean_module = ConstantMean(prior=SmoothedBoxPrior(-1e-5, 1e-5))
     self.covar_module = ScaleKernel(
         RBFKernel(log_lengthscale_prior=SmoothedBoxPrior(exp(-5), exp(6), sigma=0.1, log_transform=True)),
         log_outputscale_prior=SmoothedBoxPrior(exp(-5), exp(6), sigma=0.1, log_transform=True),
     )
 def __init__(self):
     super(GPRegressionModel, self).__init__(grid_size=20,
                                             grid_bounds=[(-0.05, 1.05)])
     self.mean_module = ConstantMean(prior=SmoothedBoxPrior(-10, 10))
     self.covar_module = ScaleKernel(
         RBFKernel(log_lengthscale_prior=SmoothedBoxPrior(
             exp(-3), exp(6), sigma=0.1, log_transform=True)))