### Example Model Setup Source: https://github.com/meta-pytorch/botorch/blob/main/tutorials/optimization_issue_diagnostics/optimization_issue_diagnostics.ipynb Sets up an example BoTorch model with dummy training data. Replace this with your actual data and model for real-world use. ```python from botorch.models import SingleTaskGP train_X = torch.rand(10, 2) train_Y = torch.sin(train_X.sum(dim=-1, keepdim=True)) model = SingleTaskGP(train_X, train_Y) ``` -------------------------------- ### Multi-objective Batch Example Setup Source: https://github.com/meta-pytorch/botorch/blob/main/tutorials/information_theoretic_acquisition_functions/information_theoretic_acquisition_functions.ipynb Sets up a multi-objective Bayesian optimization problem using ZDT1, defines bounds, generates training data, fits a model, and samples optimal points for Pareto fronts. ```python from botorch.acquisition.multi_objective.utils import ( compute_sample_box_decomposition, random_search_optimizer, sample_optimal_points, ) from botorch.test_functions.multi_objective import ZDT1 d = 4 M = 2 n = 8 if SMOKE_TEST: q = 2 else: q = 4 problem = ZDT1(dim=d, num_objectives=M, noise_std=0, negate=True) bounds = problem.bounds.to(**tkwargs) train_X = draw_sobol_samples(bounds=bounds, n=n, q=1, seed=123).squeeze(-2) train_Y = problem(train_X) model = fit_model(train_X=train_X, train_Y=train_Y, num_outputs=M) num_pareto_samples = 8 num_pareto_points = 8 optimizer_kwargs = { "pop_size": 500, "max_tries": 10, } ps, pf = sample_optimal_points( model=model, bounds=bounds, num_samples=num_pareto_samples, num_points=num_pareto_points, optimizer=random_search_optimizer, optimizer_kwargs=optimizer_kwargs, ) ``` -------------------------------- ### Install Docusaurus Dependencies and Start Server Source: https://github.com/meta-pytorch/botorch/blob/main/website/README.md Installs Node.js and Yarn dependencies, then starts the Docusaurus development server. Navigate to the 'website' directory first. ```bash cd website yarn install yarn start ``` -------------------------------- ### Install Dependencies and Setup Source: https://github.com/meta-pytorch/botorch/blob/main/tutorials/orthogonal_additive_gp/orthogonal_additive_gp.ipynb Installs botorch if in Google Colab and sets up plotting, torch defaults, and warnings. This code is essential for running the tutorial. ```python # Install dependencies if we are running in colab import os import sys if 'google.colab' in sys.modules: %pip install botorch import math import warnings import matplotlib.pyplot as plt import torch from botorch.fit import fit_gpytorch_mll from botorch.models import SingleTaskGP from botorch.models.additive_gp import OrthogonalAdditiveGP from gpytorch.mlls import ExactMarginalLogLikelihood plt.rcParams["mathtext.fontset"] = "dejavusans" warnings.filterwarnings("ignore", message=".*not standardized.*") torch.manual_seed(0) torch.set_default_dtype(torch.float64) SMOKE_TEST = os.environ.get("SMOKE_TEST") %matplotlib inline ``` -------------------------------- ### Install BoTorch for Development Source: https://github.com/meta-pytorch/botorch/blob/main/CONTRIBUTING.md Clone the repository and install BoTorch with development dependencies for linting, testing, and documentation. ```bash git clone https://github.com/meta-pytorch/botorch.git cd botorch pip install -e ".[dev]" ``` -------------------------------- ### Install BoTorch with Dev and Tutorials Source: https://github.com/meta-pytorch/botorch/blob/main/README.md Installs BoTorch in editable mode, including dependencies for development (testing, linting, docs) and running tutorial notebooks. Can be modified to install only dev or tutorials dependencies. ```bash git clone https://github.com/meta-pytorch/botorch.git cd botorch pip install -e ".[dev, tutorials]" ``` -------------------------------- ### Build and Serve Documentation Locally Source: https://github.com/meta-pytorch/botorch/blob/main/CONTRIBUTING.md Build the BoTorch documentation and serve the website locally. Requires Node.js and Yarn to be installed. ```bash ./scripts/build_docs.sh ``` -------------------------------- ### Install BoTorch with pip Source: https://github.com/meta-pytorch/botorch/blob/main/docs/getting_started.mdx Use pip to install BoTorch. This is the recommended installation method. ```bash pip install botorch ``` -------------------------------- ### Install Dependencies Source: https://github.com/meta-pytorch/botorch/blob/main/website/README.md Installs the necessary dependencies for Botorch, including tutorial support. Ensure this is run within your virtual environment. ```bash pip install -e ".[tutorials]" ``` -------------------------------- ### Initialize and Fit FullyBayesianSingleTaskGP Source: https://github.com/meta-pytorch/botorch/blob/main/sphinx/source/models.md Example of initializing a FullyBayesianSingleTaskGP model and fitting it using `fit_fully_bayesian_model_nuts`. This is the recommended way to fit this model type. ```python >>> fully_bayesian_gp = FullyBayesianSingleTaskGP(train_X, train_Y) >>> fit_fully_bayesian_model_nuts(fully_bayesian_gp) >>> posterior = fully_bayesian_gp.posterior(test_X) ``` -------------------------------- ### Run Tutorials Locally Source: https://github.com/meta-pytorch/botorch/blob/main/website/README.md Executes tutorial notebooks locally. This is optional for website building and can be slow. Run from the project root. ```bash python3 scripts/run_tutorials.py -w . ``` -------------------------------- ### BoTorch Warnings and Setup Source: https://github.com/meta-pytorch/botorch/blob/main/tutorials/vae_mnist/vae_mnist.ipynb Suppresses warnings and prints a message indicating the start of the Bayesian Optimization process. This is boilerplate for running the optimization loop. ```python import warnings from matplotlib import pyplot as plt warnings.filterwarnings("ignore") print(f"\nRunning BO ", end="") state_dict = None ``` -------------------------------- ### Define Objective Function (Python) Source: https://github.com/meta-pytorch/botorch/blob/main/tutorials/information_theoretic_acquisition_functions/information_theoretic_acquisition_functions.ipynb This snippet defines a simple one-dimensional objective function for use in Bayesian optimization examples. Ensure BoTorch and its dependencies are installed. ```python import torch def branin(x: torch.Tensor) -> torch.Tensor: # Branin function # https://www.sfu.ca/~ssurjano/branin.html # Input x is expected to be of shape (..., 2) if x.shape[-1] != 2: raise ValueError("Branin function expects 2D input.") # Normalize x to the range [-5, 10] x [0, 15] x_ = (x[..., 0] - 5.0) / 15.0 * 10.0 y_ = x[..., 1] / 15.0 * 15.0 # Original Branin function a = 1.0 b = 5.0 / (4.0 * torch.pi**2) c = 5.0 / torch.pi r = 6.0 s = 10.0 t = 1.0 / (8.0 * torch.pi) return ( a * (x_ - r) ** 2 - b * torch.cos(x_ - r) + c * (y_ - s) * (x_ - r) + t * y_ ) def quadratic(x: torch.Tensor) -> torch.Tensor: # Simple quadratic function # Input x is expected to be of shape (..., 1) if x.shape[-1] != 1: raise ValueError("Quadratic function expects 1D input.") return (x.squeeze(-1) - 3.0) ** 2 ``` -------------------------------- ### Build HTML Documentation Source: https://github.com/meta-pytorch/botorch/blob/main/sphinx/README.md Navigate to the botorch/sphinx directory and run the make html command to generate the API reference documentation. The output will be located in the botorch/sphinx/build directory. ```bash make html ``` -------------------------------- ### Convert Notebooks to MDX Source: https://github.com/meta-pytorch/botorch/blob/main/website/README.md Converts tutorial notebooks to MDX format for embedding in documentation. This must be done before serving the website. Run from the project root. ```bash python3 scripts/convert_ipynb_to_mdx.py --clean ``` -------------------------------- ### Initialize SparseOutlierGaussianLikelihood and Model Source: https://github.com/meta-pytorch/botorch/blob/main/sphinx/source/models.md Example demonstrating how to initialize a SparseOutlierGaussianLikelihood with a base noise model and then use it to construct a SingleTaskGP model. This setup is useful for models that need to handle potential outliers robustly. ```python base_noise = HomoskedasticNoise( noise_constraint=NonTransformedInterval( 1e-5, 1e-1, initial_value=1e-3 ) ) likelihood = SparseOutlierGaussianLikelihood( base_noise=base_noise, dim=X.shape[0], ) model = SingleTaskGP(train_X=X, train_Y=Y, likelihood=likelihood) mll = ExactMarginalLogLikelihood(model.likelihood, model) # NOTE: ``likelihood.noise_covar`` is the ``SparseOutlierNoise`` sparse_module = likelihood.noise_covar backward_relevance_pursuit(sparse_module, mll) ``` -------------------------------- ### Example Output: Final Recommendation and Total Cost Source: https://github.com/meta-pytorch/botorch/blob/main/tutorials/discrete_multi_fidelity_bo/discrete_multi_fidelity_bo.ipynb Shows the recommended point for optimization and its corresponding objective value, along with the total cost accumulated. ```text recommended point: tensor([[0.435, 0.932, 0.100, 0.730, 0.431, 0.000, 1.000]], dtype=torch.float64) objective value: tensor([2.283], dtype=torch.float64) total cost: 9.5 ``` -------------------------------- ### Import Libraries and Set Up Parameters Source: https://github.com/meta-pytorch/botorch/blob/main/tutorials/risk_averse_bo_with_input_perturbations/risk_averse_bo_with_input_perturbations.ipynb Imports necessary libraries from BoTorch, PyTorch, and other utilities. It also sets up configuration parameters like batch size, number of restarts, sampling counts, and risk parameters, with conditional settings for smoke tests. ```python import os import warnings from time import time import matplotlib.pyplot as plt import numpy as np import torch from botorch import fit_gpytorch_mll from botorch.acquisition import qNoisyExpectedImprovement, qSimpleRegret from botorch.acquisition.risk_measures import VaR from botorch.models import SingleTaskGP from botorch.models.transforms import Standardize from botorch.models.transforms.input import InputPerturbation from botorch.sampling import SobolQMCNormalSampler from botorch.optim import optimize_acqf from botorch.utils.sampling import draw_sobol_samples, draw_sobol_normal_samples from botorch.utils.transforms import unnormalize from botorch.test_functions import SixHumpCamel from gpytorch import ExactMarginalLogLikelihood from torch import Tensor %matplotlib inline warnings.filterwarnings("ignore") SMOKE_TEST = os.environ.get("SMOKE_TEST") BATCH_SIZE = 2 if not SMOKE_TEST else 1 NUM_RESTARTS = 10 if not SMOKE_TEST else 2 RAW_SAMPLES = 128 if not SMOKE_TEST else 4 N_W = 16 if not SMOKE_TEST else 2 NUM_ITERATIONS = 25 if not SMOKE_TEST else 2 STD_DEV = 0.05 ALPHA = 0.8 tkwargs = {"device": "cpu", "dtype": torch.double} ``` -------------------------------- ### Instantiate and Use MOMF Acquisition Function Source: https://github.com/meta-pytorch/botorch/blob/main/sphinx/source/acquisition.md Example of initializing the MOMF acquisition function with a model, reference point, and cost function, then calling it to get acquisition values. Ensure the model is trained and the reference point dimensions match the outcome space plus fidelity objective. ```python >>> model = SingleTaskGP(train_X, train_Y) >>> ref_point = [0.0, 0.0, 0.0] >>> cost_func = lambda X: 5 + X[..., -1] >>> momf = MOMF(model, ref_point, partitioning, cost_func) >>> momf_val = momf(test_X) ``` -------------------------------- ### Initialize and Use qLogNoisyExpectedImprovement Source: https://github.com/meta-pytorch/botorch/blob/main/sphinx/source/acquisition.md Instantiate a qLogNoisyExpectedImprovement model and compute acquisition values. Requires a fitted model, training data, a sampler, and test data. ```python >>> model = SingleTaskGP(train_X, train_Y) >>> sampler = SobolQMCNormalSampler(1024) >>> qLogNEI = qLogNoisyExpectedImprovement(model, train_X, sampler) >>> acqval = qLogNEI(test_X) ``` -------------------------------- ### Install Latest Development Version from GitHub Source: https://github.com/meta-pytorch/botorch/blob/main/README.md Installs the bleeding-edge version of BoTorch directly from its GitHub repository. It's recommended to also install the latest development versions of gpytorch and linear_operator. ```bash pip install --upgrade git+https://github.com/cornellius-gp/linear_operator.git pip install --upgrade git+https://github.com/cornellius-gp/gpytorch.git pip install --upgrade git+https://github.com/meta-pytorch/botorch.git ``` -------------------------------- ### Example Tensor Output Source: https://github.com/meta-pytorch/botorch/blob/main/tutorials/optimize_with_cmaes/optimize_with_cmaes.ipynb Shows an example of the final tensor output after conversion. ```python tensor([1.0000, 1.0000]) ``` -------------------------------- ### Initialize and Use qLogExpectedImprovement Source: https://github.com/meta-pytorch/botorch/blob/main/sphinx/source/acquisition.md Demonstrates initializing a SingleTaskGP model and then using it with qLogExpectedImprovement to compute qei values. Ensure the model is fitted before use. ```python >>> model = SingleTaskGP(train_X, train_Y) >>> best_f = train_Y.max()[0] >>> sampler = SobolQMCNormalSampler(1024) >>> qLogEI = qLogExpectedImprovement(model, best_f, sampler) >>> qei = qLogEI(test_X) ``` -------------------------------- ### BoTorch Bayesian Optimization Loop Setup Source: https://github.com/meta-pytorch/botorch/blob/main/tutorials/multi_objective_bo/multi_objective_bo.ipynb Initializes necessary imports and settings for a Bayesian Optimization loop using various acquisition functions like qNEHVI, qEHVI, and qNParEGO. Includes warnings suppression and sampler configuration. ```python import time import warnings from botorch import fit_gpytorch_mll from botorch.exceptions import BadInitialCandidatesWarning from botorch.sampling.normal import SobolQMCNormalSampler from botorch.utils.multi_objective.box_decompositions.dominated import ( DominatedPartitioning, ) from botorch.utils.multi_objective.pareto import is_non_dominated warnings.filterwarnings("ignore", category=BadInitialCandidatesWarning) warnings.filterwarnings("ignore", category=RuntimeWarning) N_BATCH = 20 if not SMOKE_TEST else 5 MC_SAMPLES = 128 if not SMOKE_TEST else 16 verbose = True hvs_qparego, hvs_qehvi, hvs_qnehvi, hvs_random = [], [], [], [] ``` -------------------------------- ### Editable Install for Development Source: https://github.com/meta-pytorch/botorch/blob/main/README.md Installs BoTorch in editable mode, allowing changes to local files to be reflected immediately. This is suitable for contributing to the project. Development versions of gpytorch and linear_operator can be installed beforehand. ```bash git clone https://github.com/meta-pytorch/botorch.git cd botorch pip install -e . ``` -------------------------------- ### Install Matplotlib Source: https://github.com/meta-pytorch/botorch/blob/main/notebooks_community/multi_source_bo/multi_source_bo.ipynb Installs the matplotlib library, which is often used for plotting and visualization in Python projects. ```python !pip install matplotlib ``` -------------------------------- ### Initialize Prior-Guided Acquisition Function Source: https://github.com/meta-pytorch/botorch/blob/main/sphinx/source/acquisition.md Set up a PriorGuidedAcquisitionFunction to weight a base acquisition function by a prior distribution. Specify the base acquisition function, the prior module, and optionally the prior exponent and pending points. ```python >>> from botorch.acquisition import ExpectedImprovement >>> from torch.nn import Module >>> class MyPrior(Module): def forward(self, X): return torch.ones_like(X.sum(dim=-1)) # Dummy prior >>> acq_function = ExpectedImprovement(model, best_f=best_f) >>> prior_module = MyPrior() >>> prior_guided_acqf = PriorGuidedAcquisitionFunction(acq_function, prior_module) ``` -------------------------------- ### Install Sphinx Source: https://github.com/meta-pytorch/botorch/blob/main/sphinx/README.md Install Sphinx version 3.0 or higher using pip. This is a prerequisite for building the API documentation. ```bash pip install sphinx ``` -------------------------------- ### Configure I-BNN Kernel and Initialize Data Source: https://github.com/meta-pytorch/botorch/blob/main/tutorials/ibnn_bo/ibnn_bo.ipynb Sets up the InfiniteWidthBNNKernel with specified weight and bias variances, scales it, and generates initial data points using Sobol sampling within the defined bounds. ```python ibnn_kernel = InfiniteWidthBNNKernel(2, device=device) ibnn_kernel.weight_var = 10.0 ibnn_kernel.bias_var = 5.0 ibnn_kernel = ScaleKernel(ibnn_kernel, device=device) # generate initial x data init_x = draw_sobol_samples(bounds=bounds, n=N_INIT, q=1).to(**tkwargs) init_x = init_x.squeeze(-2) # remove batch dimension ``` -------------------------------- ### Install BoTorch with Conda Source: https://github.com/meta-pytorch/botorch/blob/main/docs/getting_started.mdx Install BoTorch using Conda from the conda-forge channel. Ensure you have the gpytorch channel available. ```bash conda install botorch -c gpytorch -c conda-forge ``` -------------------------------- ### Initialize and Use PosteriorStandardDeviation Source: https://github.com/meta-pytorch/botorch/blob/main/sphinx/source/acquisition.md Demonstrates setting up a SingleTaskGP model and then initializing and using the PosteriorStandardDeviation acquisition function. Requires torch and botorch imports. ```python import torch from botorch.models.gp_regression import SingleTaskGP from botorch.models.transforms.input import Normalize from botorch.models.transforms.outcome import Standardize # Set up a model train_X = torch.rand(20, 2, dtype=torch.float64) train_Y = torch.sin(train_X).sum(dim=1, keepdim=True) model = SingleTaskGP( train_X, train_Y, outcome_transform=Standardize(m=1), input_transform=Normalize(d=2), ) # Now set up the acquisition function PSTD = PosteriorStandardDeviation(model) test_X = torch.zeros((1, 2), dtype=torch.float64) std = PSTD(test_X) std.item() ``` -------------------------------- ### Bayesian Optimization Setup Source: https://github.com/meta-pytorch/botorch/blob/main/notebooks_community/vbll_thompson_sampling/vbll_thompson_sampling.ipynb Initializes components for Bayesian optimization using VBLLs, including acquisition functions like Thompson Sampling and LogExpectedImprovement, and optimization parameters. ```python from botorch_community.acquisition.bll_thompson_sampling import BLLMaxPosteriorSampling from botorch.acquisition.analytic import LogExpectedImprovement from botorch.optim import optimize_acqf batch_size = 1 max_iter = 10 plot_bo_step = True acq_functions = ["TS", "logEI"] # "logEI" or "TS" ``` -------------------------------- ### Instantiate and Use qExpectedImprovement Source: https://github.com/meta-pytorch/botorch/blob/main/sphinx/source/acquisition.md Example of how to instantiate and use the qExpectedImprovement acquisition function with a SingleTaskGP model. It demonstrates setting up the model, best objective value, sampler, and then calling the acquisition function. ```python >>> model = SingleTaskGP(train_X, train_Y) >>> best_f = train_Y.max()[0] >>> sampler = SobolQMCNormalSampler(1024) >>> qEI = qExpectedImprovement(model, best_f, sampler) >>> qei = qEI(test_X) ``` -------------------------------- ### Install BoTorch Dependency Source: https://github.com/meta-pytorch/botorch/blob/main/tutorials/GIBBON_for_efficient_batch_entropy_search/GIBBON_for_efficient_batch_entropy_search.ipynb Installs the BoTorch library if running in a Google Colab environment. This is a prerequisite for using BoTorch functionalities. ```python # Install dependencies if we are running in colab import sys if 'google.colab' in sys.modules: %pip install botorch ``` -------------------------------- ### Example Output: Final Recommendation and Total Cost Source: https://github.com/meta-pytorch/botorch/blob/main/tutorials/discrete_multi_fidelity_bo/discrete_multi_fidelity_bo.ipynb This output presents the final recommended point from the optimization process and the total cumulative cost incurred throughout all iterations. ```text recommended point: tensor([[0.355, 0.976, 0.713, 0.564, 0.496, 0.000, 1.000]], dtype=torch.float64) objective value: tensor([2.760], dtype=torch.float64) total cost: 15.0 ``` -------------------------------- ### LogConstrainedExpectedImprovement Example Source: https://github.com/meta-pytorch/botorch/blob/main/sphinx/source/acquisition.md Example demonstrating the usage of LogConstrainedExpectedImprovement for a model with a non-negativity constraint on the 0th output and the 1st output as the objective. ```python >>> model = SingleTaskGP(train_X, train_Y) >>> constraints = {0: (0.0, None)} >>> LogCEI = LogConstrainedExpectedImprovement(model, 0.2, 1, constraints) >>> cei = LogCEI(test_X) ``` -------------------------------- ### Fit and Predict with ExampleModel in BoTorch Source: https://github.com/meta-pytorch/botorch/blob/main/notebooks_community/example/example.ipynb This snippet shows how to initialize an ExampleModel, fit it using GPyTorch's MLL, and generate predictions with uncertainty. Ensure BoTorch and GPyTorch are installed. ```python import matplotlib.pyplot as plt import torch from botorch_community.models.example import ExampleModel from gpytorch import ExactMarginalLogLikelihood from botorch.fit import fit_gpytorch_mll torch.set_default_dtype(torch.float64) X = torch.rand(10, 1) Y = torch.sin(X*5) model = ExampleModel(train_X=X, train_Y=Y) mll = ExactMarginalLogLikelihood(model.likelihood, model) fit_gpytorch_mll(mll) test_X = torch.linspace(0, 1, 100) with torch.no_grad(): posterior = model.posterior(test_X.view(-1, 1)) mean, std = posterior.mean.squeeze(), posterior.variance.sqrt().squeeze() fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8, 6)) ax.set_title("Example Model Predictions") ax.scatter(X, Y, c="b", label="observations") ax.plot(test_X, mean, label="predictions") ax.fill_between(test_X, mean-2*std, mean+2*std, alpha=0.2) ax.legend() plt.show() ``` -------------------------------- ### Example Usage of evaluate_first_order_on_grid Source: https://github.com/meta-pytorch/botorch/blob/main/sphinx/source/models.md An example demonstrating how to use the `evaluate_first_order_on_grid` method to obtain posterior means and variances for the bias and first-order components. ```python grid = torch.linspace(0, 1, 50) (bias_mean, bias_var), (fo_mean, fo_var) = \ model.evaluate_first_order_on_grid(grid) # fo_mean[i] is component i's posterior mean on the 1D grid ``` -------------------------------- ### Sobol QMC Normal Sampler Example Source: https://github.com/meta-pytorch/botorch/blob/main/sphinx/source/sampling.md Example of using SobolQMCNormalSampler for quasi-Monte Carlo sampling. Requires a defined model and test_X. ```python >>> sampler = SobolQMCNormalSampler(torch.Size([1024]), seed=1234) >>> posterior = model.posterior(test_X) >>> samples = sampler(posterior) ``` -------------------------------- ### Initialize qExpectedHypervolumeImprovement Source: https://github.com/meta-pytorch/botorch/blob/main/sphinx/source/acquisition.md Instantiate the qExpectedHypervolumeImprovement acquisition function with a fitted model, reference point, and partitioning strategy. Ensure the model is trained and the partitioning is correctly set up for your objectives. ```python >>> model = SingleTaskGP(train_X, train_Y) >>> ref_point = [0.0, 0.0] >>> qEHVI = qExpectedHypervolumeImprovement(model, ref_point, partitioning) >>> qehvi = qEHVI(test_X) ``` -------------------------------- ### Install Dependencies for Colab Source: https://github.com/meta-pytorch/botorch/blob/main/tutorials/custom_model/custom_model.ipynb Installs necessary libraries like botorch, numpyro, jax, and jaxlib if the code is run in a Google Colab environment. ```python import sys if 'google.colab' in sys.modules: %pip install botorch numpyro jax jaxlib ``` -------------------------------- ### Install BoTorch in Google Colab Source: https://github.com/meta-pytorch/botorch/blob/main/tutorials/batch_mode_cross_validation/batch_mode_cross_validation.ipynb Installs the botorch library if the code is being run in a Google Colab environment. This is a prerequisite for using botorch functionalities. ```python import sys if 'google.colab' in sys.modules: %pip install botorch ``` -------------------------------- ### Initialize qLogEI with initial points Source: https://github.com/meta-pytorch/botorch/blob/main/tutorials/turbo_1/turbo_1.ipynb Initializes the qLogEI acquisition function with specified dimensions and number of initial points. Ensure torch and eval_objective are imported and configured. ```python torch.manual_seed(0) X_logei = get_initial_points(dim, n_init) Y_logei = torch.tensor( [eval_objective(x) for x in X_logei], dtype=dtype, device=device ).unsqueeze(-1) ``` -------------------------------- ### Install Pre-commit Hooks Source: https://github.com/meta-pytorch/botorch/blob/main/CONTRIBUTING.md Set up pre-commit hooks to automatically run ufmt and flake8 during the commit process. Install pre-commit via pip first. ```bash pip install pre-commit pre-commit install ``` -------------------------------- ### Example Usage of Chebyshev Scalarization Source: https://github.com/meta-pytorch/botorch/blob/main/sphinx/source/utils.md Example demonstrating how to obtain an augmented Chebyshev scalarization transform function using specified weights and observed outcomes. ```python weights = torch.tensor([0.75, -0.25]) transform = get_aug_chebyshev_scalarization(weights, Y) ``` -------------------------------- ### Initialize Training Data and Sampler Source: https://github.com/meta-pytorch/botorch/blob/main/tutorials/Multi_objective_multi_fidelity_BO/Multi_objective_multi_fidelity_BO.ipynb Initializes training data to zero, sets a manual seed for reproducibility, generates initial data points, calculates the objective function, and creates a Sobol Quasi-Monte Carlo sampler for Monte Carlo sampling. ```python verbose = False torch.manual_seed(0) train_x_momf, _ = gen_init_data(n_INIT) train_obj_momf = get_objective_momf(train_x_momf) # Generate Sampler momf_sampler = SobolQMCNormalSampler(sample_shape=torch.Size([MC_SAMPLES])) ``` -------------------------------- ### Instantiate and Use qNoisyExpectedImprovement Source: https://github.com/meta-pytorch/botorch/blob/main/sphinx/source/acquisition.md Instantiate the qNoisyExpectedImprovement acquisition function with a model and sampler, then call it with test data. It's recommended to use qLogNoisyExpectedImprovement for improved BO performance. ```python model = SingleTaskGP(train_X, train_Y) sampler = SobolQMCNormalSampler(1024) qNEI = qNoisyExpectedImprovement(model, train_X, sampler) qnei = qNEI(test_X) ``` -------------------------------- ### IID Normal Sampler Example Source: https://github.com/meta-pytorch/botorch/blob/main/sphinx/source/sampling.md Example usage of the IIDNormalSampler to generate samples from a posterior distribution. Ensure the model and test_X are defined prior to execution. ```python >>> sampler = IIDNormalSampler(1000, seed=1234) >>> posterior = model.posterior(test_X) >>> samples = sampler(posterior) ``` -------------------------------- ### FullyBayesianLinearSingleTaskGP Initialization and Fitting Source: https://github.com/meta-pytorch/botorch/blob/main/sphinx/source/models.md Demonstrates how to initialize and fit a FullyBayesianLinearSingleTaskGP model using provided training data and MCMC sampling. ```APIDOC ## FullyBayesianLinearSingleTaskGP ### Description Initialize the fully Bayesian single-task GP model. ### Method `FullyBayesianLinearSingleTaskGP(train_X, train_Y, train_Yvar=None, outcome_transform=None, input_transform=None, use_input_warping=False, indices_to_warp=None)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **train_X** (Tensor) - Required - Training inputs (n x d) - **train_Y** (Tensor) - Required - Training targets (n x 1) - **train_Yvar** (Tensor | None) - Optional - Observed noise variance (n x 1). Inferred if None. - **outcome_transform** (OutcomeTransform | None) - Optional - An outcome transform that is applied to the training data during instantiation and to the posterior during inference. - **input_transform** (InputTransform | None) - Optional - An input transform that is applied in the model’s forward pass. - **use_input_warping** (bool) - Optional - A boolean indicating whether to use input warping. - **indices_to_warp** (list[int] | None) - Optional - An optional list of indices to warp. The default is to warp all inputs. ### Request Example ```python >>> gp = FullyBayesianLinearSingleTaskGP(train_X, train_Y) >>> fit_fully_bayesian_model_nuts(gp) >>> posterior = gp.posterior(test_X) ``` ### Response #### Success Response (200) None #### Response Example None ## load_state_dict ### Description Custom logic for loading the state dict for `FullyBayesianLinearSingleTaskGP`. ### Method `load_state_dict(state_dict, strict=True, assign=False)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **state_dict** (Mapping[str, Any]) - Required - **strict** (bool) - Optional - **assign** (bool) - Optional ### Request Example None ### Response #### Success Response (200) None #### Response Example None ``` -------------------------------- ### Multi-task GP Model Definition Example Source: https://github.com/meta-pytorch/botorch/blob/main/sphinx/source/models.md Example of how to define a multi-task GP model using LatentFeatureMultiTaskPyroMixin and a base PyroModel. Place the mixin before the PyroModel in the MRO. ```python class MultitaskSaasPyroModel(LatentFeatureMultiTaskPyroMixin, SaasPyroModel): ... ``` -------------------------------- ### Log Probability of Improvement Example Source: https://github.com/meta-pytorch/botorch/blob/main/sphinx/source/acquisition.md Example of initializing and using the LogProbabilityOfImprovement acquisition function. Requires a fitted single-outcome model and the best observed function value. ```python >>> model = SingleTaskGP(train_X, train_Y) >>> LogPI = LogProbabilityOfImprovement(model, best_f=0.2) >>> log_pi = LogPI(test_X) ```