Exemple #1
0
    def from_config(cls, config: Config):
        classname = cls.__name__
        acqf = config.getobj("common", "acqf", fallback=None)
        extra_acqf_args = cls._get_acqf_options(acqf, config)

        options = {}
        options["num_restarts"] = config.getint(classname, "restarts", fallback=10)
        options["raw_samples"] = config.getint(classname, "samps", fallback=1000)
        options["verbosity_freq"] = config.getint(
            classname, "verbosity_freq", fallback=-1
        )
        options["lr"] = config.getfloat(classname, "lr", fallback=0.01)  # type: ignore
        options["momentum"] = config.getfloat(classname, "momentum", fallback=0.9)  # type: ignore
        options["nesterov"] = config.getboolean(classname, "nesterov", fallback=True)
        options["epochs"] = config.getint(classname, "epochs", fallback=50)
        options["milestones"] = config.getlist(
            classname, "milestones", fallback=[25, 40]  # type: ignore
        )
        options["gamma"] = config.getfloat(classname, "gamma", fallback=0.1)  # type: ignore
        options["loss_constraint_fun"] = config.getobj(
            classname, "loss_constraint_fun", fallback=default_loss_constraint_fun
        )

        explore_features = config.getlist(classname, "explore_idxs", fallback=None)  # type: ignore

        return cls(
            acqf=acqf,
            acqf_kwargs=extra_acqf_args,
            model_gen_options=options,
            explore_features=explore_features,
        )
Exemple #2
0
def monotonic_mean_covar_factory(
    config: Config,
) -> Tuple[ConstantMeanPartialObsGrad, gpytorch.kernels.ScaleKernel]:
    """Default factory for monotonic GP models based on derivative observations.

    Args:
        config (Config): Config containing (at least) bounds, and optionally LSE target.

    Returns:
        Tuple[ConstantMeanPartialObsGrad, gpytorch.kernels.ScaleKernel]: Instantiated mean and
            scaled RBF kernels with partial derivative observations.
    """
    lb = config.gettensor("monotonic_mean_covar_factory", "lb")
    ub = config.gettensor("monotonic_mean_covar_factory", "ub")
    assert lb.shape[0] == ub.shape[0], "bounds shape mismatch!"
    dim = lb.shape[0]
    fixed_mean = config.getboolean("monotonic_mean_covar_factory",
                                   "fixed_mean",
                                   fallback=False)

    mean = ConstantMeanPartialObsGrad()

    if fixed_mean:
        try:
            target = config.getfloat("monotonic_mean_covar_factory", "target")
            mean.constant.requires_grad_(False)
            mean.constant.copy_(torch.tensor([norm.ppf(target)]))
        except NoOptionError:
            raise RuntimeError(
                "Config got fixed_mean=True but no target included!")

    ls_prior = gpytorch.priors.GammaPrior(
        concentration=__default_invgamma_concentration,
        rate=__default_invgamma_rate,
        transform=lambda x: 1 / x,
    )
    ls_prior_mode = ls_prior.rate / (ls_prior.concentration + 1)
    ls_constraint = gpytorch.constraints.Positive(transform=None,
                                                  initial_value=ls_prior_mode)

    covar = gpytorch.kernels.ScaleKernel(
        RBFKernelPartialObsGrad(
            lengthscale_prior=ls_prior,
            lengthscale_constraint=ls_constraint,
            ard_num_dims=dim,
        ),
        outputscale_prior=gpytorch.priors.SmoothedBoxPrior(a=1, b=4),
    )

    return mean, covar
Exemple #3
0
def default_mean_covar_factory(
    config: Config,
) -> Tuple[gpytorch.means.ConstantMean, gpytorch.kernels.ScaleKernel]:
    """Default factory for generic GP models

    Args:
        config (Config): Object containing bounds (and potentially other
            config details).

    Returns:
        Tuple[gpytorch.means.Mean, gpytorch.kernels.Kernel]: Instantiated
            ConstantMean and ScaleKernel with priors based on bounds.
    """

    lb = config.gettensor("default_mean_covar_factory", "lb")
    ub = config.gettensor("default_mean_covar_factory", "ub")
    fixed_mean = config.getboolean("default_mean_covar_factory",
                                   "fixed_mean",
                                   fallback=False)
    lengthscale_prior = config.get("default_mean_covar_factory",
                                   "lengthscale_prior",
                                   fallback="gamma")
    outputscale_prior = config.get("default_mean_covar_factory",
                                   "outputscale_prior",
                                   fallback="box")
    kernel = config.getobj("default_mean_covar_factory",
                           "kernel",
                           fallback=gpytorch.kernels.RBFKernel)

    assert lb.shape[0] == ub.shape[0], "bounds shape mismatch!"
    dim = lb.shape[0]
    mean = gpytorch.means.ConstantMean()

    if fixed_mean:
        try:
            target = config.getfloat("default_mean_covar_factory", "target")
            mean.constant.requires_grad_(False)
            mean.constant.copy_(torch.tensor([norm.ppf(target)]))
        except NoOptionError:
            raise RuntimeError(
                "Config got fixed_mean=True but no target included!")

    if lengthscale_prior == "invgamma":

        ls_prior = gpytorch.priors.GammaPrior(
            concentration=__default_invgamma_concentration,
            rate=__default_invgamma_rate,
            transform=lambda x: 1 / x,
        )

        ls_prior_mode = ls_prior.rate / (ls_prior.concentration + 1)
    elif lengthscale_prior == "gamma":
        ls_prior = gpytorch.priors.GammaPrior(concentration=3.0, rate=6.0)
        ls_prior_mode = (ls_prior.concentration - 1) / ls_prior.rate
    else:
        raise RuntimeError(
            f"Lengthscale_prior should be invgamma or gamma, got {lengthscale_prior}"
        )

    if outputscale_prior == "gamma":
        os_prior = gpytorch.priors.GammaPrior(concentration=2.0, rate=0.15)
    elif outputscale_prior == "box":
        os_prior = gpytorch.priors.SmoothedBoxPrior(a=1, b=4)
    else:
        raise RuntimeError(
            f"Outputscale_prior should be gamma or box, got {outputscale_prior}"
        )

    ls_constraint = gpytorch.constraints.Positive(transform=None,
                                                  initial_value=ls_prior_mode)

    covar = gpytorch.kernels.ScaleKernel(
        kernel(
            lengthscale_prior=ls_prior,
            lengthscale_constraint=ls_constraint,
            ard_num_dims=dim,
        ),
        outputscale_prior=os_prior,
    )

    return mean, covar