Пример #1
0
    def test_get_model(self):
        x = torch.zeros(2, 2)
        y = torch.zeros(2, 1)
        var = torch.zeros(2, 1)
        partial_var = torch.tensor([0, float("nan")]).unsqueeze(-1)
        unknown_var = torch.tensor([float("nan"), float("nan")]).unsqueeze(-1)
        model = _get_model(x, y, unknown_var, None)
        self.assertIsInstance(model, SingleTaskGP)

        model = _get_model(X=x, Y=y, Yvar=var)
        self.assertIsInstance(model, FixedNoiseGP)
        model = _get_model(X=x, Y=y, Yvar=unknown_var, task_feature=1)
        self.assertTrue(type(model) == MultiTaskGP)  # Don't accept subclasses.
        model = _get_model(X=x, Y=y, Yvar=var, task_feature=1)
        self.assertIsInstance(model, FixedNoiseMultiTaskGP)
        model = _get_model(X=x, Y=y, Yvar=partial_var.clone(), task_feature=1)
        self.assertIsInstance(model, FixedNoiseMultiTaskGP)
        model = _get_model(X=x,
                           Y=y,
                           Yvar=partial_var.clone(),
                           task_feature=1,
                           rank=1)
        self.assertEqual(model._rank, 1)
        with self.assertRaises(ValueError):
            model = _get_model(X=x, Y=y, Yvar=partial_var, task_feature=None)
        model = _get_model(X=x, Y=y, Yvar=var, fidelity_features=[-1])
        self.assertTrue(isinstance(model, SingleTaskMultiFidelityGP))
        with self.assertRaises(NotImplementedError):
            _get_model(X=x,
                       Y=y,
                       Yvar=var,
                       task_feature=1,
                       fidelity_features=[-1])
Пример #2
0
 def test_get_model(self):
     x = torch.zeros(2, 2)
     y = torch.zeros(2)
     se = torch.zeros(2)
     model = _get_model(x, y, se, None)
     self.assertTrue(isinstance(model, FixedNoiseGP))
     model = _get_model(x, y, se, 1)
     self.assertTrue(isinstance(model, FixedNoiseMultiTaskGP))
Пример #3
0
 def test_get_acquisition_func(self):
     x = torch.zeros(2, 2)
     y = torch.zeros(2, 1)
     unknown_var = torch.tensor([float("nan"), float("nan")]).unsqueeze(-1)
     model = _get_model(x, y, unknown_var, None)
     objective_weights = torch.tensor([1.0])
     outcome_constraints = (
         torch.tensor([[0.0, 1.0]]),
         torch.tensor([[5.0]]),
     )
     X_observed = torch.zeros(2, 2)
     with self.assertRaises(ValueError) as cm:
         _get_acquisition_func(
             model=model,
             acquisition_function_name="qNEI",
             objective_weights=objective_weights,
             outcome_constraints=outcome_constraints,
             X_observed=X_observed,
             constrained_mc_objective=None,
         )
     self.assertEqual(
         "constrained_mc_objective cannot be set to None "
         "when applying outcome constraints.",
         str(cm.exception),
     )
     with self.assertRaises(RuntimeError):
         _get_acquisition_func(
             model=model,
             acquisition_function_name="qNEI",
             objective_weights=objective_weights,
             mc_objective=PenalizedMCObjective,
             outcome_constraints=outcome_constraints,
             X_observed=X_observed,
         )
Пример #4
0
def _get_single_task_gpytorch_model(
    Xs: List[Tensor],
    Ys: List[Tensor],
    Yvars: List[Tensor],
    task_features: List[int],
    fidelity_features: List[int],
    state_dict: Optional[Dict[str, Tensor]] = None,
    num_samples: int = 512,
    thinning: int = 16,
    use_input_warping: bool = False,
    gp_kernel: str = "matern",
    **kwargs: Any,
) -> ModelListGP:
    r"""Instantiates a batched GPyTorchModel(ModelListGP) based on the given data.
    The model fitting is based on MCMC and is run separately using pyro. The MCMC
    samples will be loaded into the model instantiated here afterwards.

    Returns:
        A ModelListGP.
    """
    if len(task_features) > 0:
        raise NotImplementedError(
            "Currently do not support MT-GP models with MCMC!")
    if len(fidelity_features) > 0:
        raise NotImplementedError(
            "Fidelity MF-GP models are not currently supported with MCMC!")

    num_mcmc_samples = num_samples // thinning
    covar_modules = [
        _get_rbf_kernel(num_samples=num_mcmc_samples, dim=Xs[0].shape[-1])
        if gp_kernel == "rbf" else None for _ in range(len(Xs))
    ]

    models = [
        _get_model(
            X=X.unsqueeze(0).expand(num_mcmc_samples, X.shape[0], -1),
            Y=Y.unsqueeze(0).expand(num_mcmc_samples, Y.shape[0], -1),
            Yvar=Yvar.unsqueeze(0).expand(num_mcmc_samples, Yvar.shape[0], -1),
            fidelity_features=fidelity_features,
            use_input_warping=use_input_warping,
            covar_module=covar_module,
            **kwargs,
        ) for X, Y, Yvar, covar_module in zip(Xs, Ys, Yvars, covar_modules)
    ]
    model = ModelListGP(*models)
    model.to(Xs[0])
    return model
    def test_get_model(self):
        x = torch.zeros(2, 2)
        y = torch.zeros(2, 1)
        se = torch.zeros(2, 1)
        partial_se = torch.tensor([0, float("nan")])
        unknown_se = torch.tensor([float("nan"), float("nan")])
        model = _get_model(x, y, unknown_se, None)
        self.assertIsInstance(model, SingleTaskGP)

        model = _get_model(x, y, se, None)
        self.assertIsInstance(model, FixedNoiseGP)
        model = _get_model(x, y, unknown_se, 1)
        self.assertTrue(type(model) == MultiTaskGP)  # Don't accept subclasses.
        model = _get_model(x, y, se, 1)
        self.assertIsInstance(model, FixedNoiseMultiTaskGP)
        with self.assertRaises(ValueError):
            model = _get_model(x, y, partial_se, None)
        model = _get_model(x, y, se, 1, fidelity_model_id=0, fidelity_features=[-1])
        self.assertTrue(isinstance(model, SingleTaskGPLTKernel))
Пример #6
0
    def test_get_model(self):
        x = torch.zeros(2, 2)
        y = torch.zeros(2, 1)
        var = torch.zeros(2, 1)
        partial_var = torch.tensor([0, float("nan")]).unsqueeze(-1)
        unknown_var = torch.tensor([float("nan"), float("nan")]).unsqueeze(-1)
        model = _get_model(x, y, unknown_var, None)
        self.assertIsInstance(model, SingleTaskGP)

        model = _get_model(X=x, Y=y, Yvar=var)
        self.assertIsInstance(model, FixedNoiseGP)
        model = _get_model(X=x, Y=y, Yvar=unknown_var, task_feature=1)
        self.assertTrue(type(model) == MultiTaskGP)  # Don't accept subclasses.
        model = _get_model(X=x, Y=y, Yvar=var, task_feature=1)
        self.assertIsInstance(model, FixedNoiseMultiTaskGP)
        model = _get_model(X=x, Y=y, Yvar=partial_var.clone(), task_feature=1)
        self.assertIsInstance(model, FixedNoiseMultiTaskGP)
        model = _get_model(X=x, Y=y, Yvar=partial_var.clone(), task_feature=1, rank=1)
        self.assertEqual(model._rank, 1)
        with self.assertRaises(ValueError):
            model = _get_model(X=x, Y=y, Yvar=partial_var, task_feature=None)
        model = _get_model(X=x, Y=y, Yvar=var, fidelity_features=[-1])
        self.assertTrue(isinstance(model, SingleTaskMultiFidelityGP))
        with self.assertRaises(NotImplementedError):
            _get_model(X=x, Y=y, Yvar=var, task_feature=1, fidelity_features=[-1])
        # test fixed prior
        kwargs = {
            "prior": {
                "type": LKJCovariancePrior,
                "sd_prior": GammaPrior(2.0, 0.44),
                "eta": 0.6,
            }
        }
        model = _get_model(X=x, Y=y, Yvar=partial_var.clone(), task_feature=1, **kwargs)
        self.assertIsInstance(
            model.task_covar_module.IndexKernelPrior, LKJCovariancePrior
        )
        self.assertEqual(
            model.task_covar_module.IndexKernelPrior.sd_prior.concentration, 2.0
        )
        self.assertEqual(model.task_covar_module.IndexKernelPrior.sd_prior.rate, 0.44)
        self.assertEqual(
            model.task_covar_module.IndexKernelPrior.correlation_prior.eta, 0.6
        )

        kwargs2 = {"prior": {"type": LKJCovariancePrior}}
        model = _get_model(
            X=x, Y=y, Yvar=partial_var.clone(), task_feature=1, **kwargs2
        )
        self.assertIsInstance(
            model.task_covar_module.IndexKernelPrior, LKJCovariancePrior
        )
        self.assertEqual(
            model.task_covar_module.IndexKernelPrior.sd_prior.concentration, 1.0
        )
        self.assertEqual(model.task_covar_module.IndexKernelPrior.sd_prior.rate, 0.15)
        self.assertEqual(
            model.task_covar_module.IndexKernelPrior.correlation_prior.eta, 0.5
        )
        kwargs3 = {
            "prior": {
                "type": LKJCovariancePrior,
                "sd_prior": GammaPrior(2.0, 0.44),
                "eta": "hi",
            }
        }
        with self.assertRaises(ValueError):
            _get_model(X=x, Y=y, Yvar=partial_var.clone(), task_feature=1, **kwargs3)

        kwargs5 = {
            "prior": {"type": Prior, "sd_prior": GammaPrior(2.0, 0.44), "eta": 0.5}
        }
        with self.assertRaises(NotImplementedError):
            _get_model(X=x, Y=y, Yvar=partial_var.clone(), task_feature=1, **kwargs5)
Пример #7
0
def get_and_fit_model_mcmc(
    Xs: List[Tensor],
    Ys: List[Tensor],
    Yvars: List[Tensor],
    task_features: List[int],
    fidelity_features: List[int],
    metric_names: List[str],
    state_dict: Optional[Dict[str, Tensor]] = None,
    refit_model: bool = True,
    use_input_warping: bool = False,
    use_loocv_pseudo_likelihood: bool = False,
    num_samples: int = 512,
    warmup_steps: int = 1024,
    thinning: int = 16,
    max_tree_depth: int = 6,
    use_saas: bool = False,
    disable_progbar: bool = False,
    **kwargs: Any,
) -> GPyTorchModel:
    if len(task_features) > 0:
        raise NotImplementedError(
            "Currently do not support MT-GP models with MCMC!")
    if len(fidelity_features) > 0:
        raise NotImplementedError(
            "Fidelity MF-GP models are not currently supported with MCMC!")
    model = None
    # TODO: Better logic for deciding when to use a ModelListGP. Currently the
    # logic is unclear. The two cases in which ModelListGP is used are
    # (i) the training inputs (Xs) are not the same for the different outcomes, and
    # (ii) a multi-task model is used

    num_mcmc_samples = num_samples // thinning
    if len(Xs) == 1:
        # Use single output, single task GP
        model = _get_model(
            X=Xs[0].unsqueeze(0).expand(num_mcmc_samples, Xs[0].shape[0], -1),
            Y=Ys[0].unsqueeze(0).expand(num_mcmc_samples, Xs[0].shape[0], -1),
            Yvar=Yvars[0].unsqueeze(0).expand(num_mcmc_samples, Xs[0].shape[0],
                                              -1),
            fidelity_features=fidelity_features,
            use_input_warping=use_input_warping,
            **kwargs,
        )
    else:
        models = [
            _get_model(
                X=X.unsqueeze(0).expand(num_mcmc_samples, X.shape[0],
                                        -1).clone(),
                Y=Y.unsqueeze(0).expand(num_mcmc_samples, Y.shape[0],
                                        -1).clone(),
                Yvar=Yvar.unsqueeze(0).expand(num_mcmc_samples, Yvar.shape[0],
                                              -1).clone(),
                use_input_warping=use_input_warping,
                **kwargs,
            ) for X, Y, Yvar in zip(Xs, Ys, Yvars)
        ]
        model = ModelListGP(*models)
    model.to(Xs[0])
    if isinstance(model, ModelListGP):
        models = model.models
    else:
        models = [model]
    if state_dict is not None:
        # pyre-fixme[6]: Expected `OrderedDict[typing.Any, typing.Any]` for 1st
        #  param but got `Dict[str, Tensor]`.
        model.load_state_dict(state_dict)
    if state_dict is None or refit_model:
        for X, Y, Yvar, m in zip(Xs, Ys, Yvars, models):
            samples = run_inference(
                pyro_model=pyro_model,  # pyre-ignore [6]
                X=X,
                Y=Y,
                Yvar=Yvar,
                num_samples=num_samples,
                warmup_steps=warmup_steps,
                thinning=thinning,
                use_input_warping=use_input_warping,
                use_saas=use_saas,
                max_tree_depth=max_tree_depth,
                disable_progbar=disable_progbar,
            )
            if "noise" in samples:
                m.likelihood.noise_covar.noise = (
                    samples["noise"].detach().clone().view(
                        m.likelihood.noise_covar.noise.shape).clamp_min(
                            MIN_INFERRED_NOISE_LEVEL))
            m.covar_module.base_kernel.lengthscale = (
                samples["lengthscale"].detach().clone().view(
                    m.covar_module.base_kernel.lengthscale.shape))
            m.covar_module.outputscale = (
                samples["outputscale"].detach().clone().view(
                    m.covar_module.outputscale.shape))
            m.mean_module.constant.data = (
                samples["mean"].detach().clone().view(
                    m.mean_module.constant.shape))
            if "c0" in samples:
                m.input_transform._set_concentration(
                    i=0,
                    value=samples["c0"].detach().clone().view(
                        m.input_transform.concentration0.shape),
                )
                m.input_transform._set_concentration(
                    i=1,
                    value=samples["c1"].detach().clone().view(
                        m.input_transform.concentration1.shape),
                )
    return model