Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
fbshipit-source-id: 436d6dd4434140b6b24f9476b71ec3b4e1b095c2
  • Loading branch information
facebook-github-bot authored and Balandat committed Apr 25, 2019
0 parents commit 0afd603
Show file tree
Hide file tree
Showing 50 changed files with 2,831 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .flake8
@@ -0,0 +1,8 @@
[flake8]

# E704: the linter doesn't parse types properly
# T499, T484: We don't need to run pyre and mypy
# W503: black and flake8 disagree on how to place operators)
ignore = T484, T499, W503, E704
# Black really wants lines to be 88 chars....
max-line-length = 88
80 changes: 80 additions & 0 deletions .gitignore
@@ -0,0 +1,80 @@
# watchman
.watchmanconfig

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg

# Atom plugin files and ctags
.ftpconfig
.ftpconfig.cson
.ftpignore
*.tags
*.tags1

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# dotenv
.env

# virtualenv
.venv
venv/
ENV/

# mypy
.mypy_cache/

# vim
*.swp
30 changes: 30 additions & 0 deletions DESIGN_GUIDE.md
@@ -0,0 +1,30 @@
# botorch design guide [WIP]

This document provides guidance on botorch's internal design.

## design philosophy

botorch follows a modular, low-overhead “unframework” design philosophy, which
provides building blocks and abstractions, but does not force the user to do
things in any one particular way. This empowers the user to easily prototype and
test new approaches.


## acquisition functions

botorch supports batch acquisition functions (e.g. q-EI, q-UCB, etc.) that
assign a joint utility to a set of q design points in the parameter space.

Unfortunately, this batch nomenclature gets easily conflated with the pytorch
notion of batch-evaluation. To avoid confusion in that respect, we adopt the
convention of referring to batches in the batch-acquisition sense as "q-batches",
and to batches in the torch batch-evaluation sense as "t-batches".

Internally, q-batch acquisition functions operate on input tensors of shape
`b x q x d`, where `b` is the number of t-batches, `q` is the number of design
points, and `d` is the dimension of the parameter space. Their output is a
one-dimensional tensor with `b` elements, with the `i`-th element corresponding
to the `i`-th t-batch.

To simplify the user-facing API, if provided with an input tensor of shape `q x d`,
a t-batch size of 1 is inferred, and the result is returned as a torch scalar.
20 changes: 20 additions & 0 deletions LICENSE
@@ -0,0 +1,20 @@
MIT License

Copyright (c) 2018-present, Facebook, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3 changes: 3 additions & 0 deletions README.md
@@ -0,0 +1,3 @@
# botorch (pre-alpha) [WIP]

botorch is a library for Bayesian Optimization in pytorch.
16 changes: 16 additions & 0 deletions botorch/__init__.py
@@ -0,0 +1,16 @@
#!/usr/bin/env python3

from . import acquisition, optim, test_functions
from .fit import fit_model
from .gen import gen_candidates
from .utils import manual_seed


__all__ = [
acquisition,
fit_model,
gen_candidates,
optim,
manual_seed,
test_functions,
]
26 changes: 26 additions & 0 deletions botorch/acquisition/__init__.py
@@ -0,0 +1,26 @@
#!/usr/bin/env python3

from .batch_modules import (
qExpectedImprovement,
qProbabilityOfImprovement,
qUpperConfidenceBound,
)
from .modules import (
AcquisitionFunction,
ExpectedImprovement,
PosteriorMean,
ProbabilityOfImprovement,
UpperConfidenceBound,
)


__all__ = [
AcquisitionFunction,
ExpectedImprovement,
PosteriorMean,
ProbabilityOfImprovement,
UpperConfidenceBound,
qExpectedImprovement,
qProbabilityOfImprovement,
qUpperConfidenceBound,
]
103 changes: 103 additions & 0 deletions botorch/acquisition/batch_modules.py
@@ -0,0 +1,103 @@
#!/usr/bin/env python3

from typing import Callable, List, Optional

import torch
from gpytorch import Module

from .functional import (
batch_expected_improvement,
batch_probability_of_improvement,
batch_simple_regret,
batch_upper_confidence_bound,
)
from .modules import AcquisitionFunction


"""
Wraps the batch acquisition functions defined in botorch.acquisition.functional
into BatchAcquisitionFunction gpytorch modules.
"""


class BatchAcquisitionFunction(AcquisitionFunction):
def forward(self, candidate_set: torch.Tensor) -> torch.Tensor:
"""Takes in a `b x q x d` candidate_set Tensor of `b` t-batches with `q`
`d`-dimensional design points each, and returns a one-dimensional Tensor
with `b` elements."""
raise NotImplementedError("BatchAcquisitionFunction cannot be used directly")


class qExpectedImprovement(BatchAcquisitionFunction):
"""TODO"""

def __init__(
self,
model: Module,
best_f: float,
objective: Callable[[torch.Tensor], torch.Tensor] = lambda Y: Y,
constraints: Optional[List[Callable[[torch.Tensor], torch.Tensor]]] = None,
mc_samples: int = 5000,
) -> None:
super(qExpectedImprovement, self).__init__(model)
self.best_f = best_f
self.objective = objective
self.constraints = constraints
self.mc_samples = mc_samples

def forward(self, candidate_set: torch.Tensor) -> torch.Tensor:
return batch_expected_improvement(
X=candidate_set,
model=self.model,
best_f=self.best_f,
objective=self.objective,
constraints=self.constraints,
mc_samples=self.mc_samples,
)


class qProbabilityOfImprovement(BatchAcquisitionFunction):
"""TODO"""

def __init__(self, model: Module, best_f: float, mc_samples: int = 5000) -> None:
super(qProbabilityOfImprovement, self).__init__(model)
self.best_f = best_f
self.mc_samples = mc_samples

def forward(self, candidate_set: torch.Tensor) -> torch.Tensor:
return batch_probability_of_improvement(
X=candidate_set,
model=self.model,
best_f=self.best_f,
mc_samples=self.mc_samples,
)


class qUpperConfidenceBound(BatchAcquisitionFunction):
"""TODO"""

def __init__(self, model: Module, beta: float, mc_samples: int = 5000) -> None:
super(qUpperConfidenceBound, self).__init__(model)
self.beta = beta
self.mc_samples = mc_samples

def forward(self, candidate_set: torch.Tensor) -> torch.Tensor:
return batch_upper_confidence_bound(
X=candidate_set,
model=self.model,
beta=self.beta,
mc_samples=self.mc_samples,
)


class qSimpleRegret(BatchAcquisitionFunction):
"""TODO"""

def __init__(self, model: Module, mc_samples: int = 5000) -> None:
super(qSimpleRegret, self).__init__(model)
self.mc_samples = mc_samples

def forward(self, candidate_set: torch.Tensor) -> torch.Tensor:
return batch_simple_regret(
X=candidate_set, model=self.model, mc_samples=self.mc_samples
)
32 changes: 32 additions & 0 deletions botorch/acquisition/functional/__init__.py
@@ -0,0 +1,32 @@
#!/usr/bin/env python3

from .acquisition import (
expected_improvement,
max_value_entropy_search,
posterior_mean,
probability_of_improvement,
upper_confidence_bound,
)

from .batch_acquisition import (
batch_expected_improvement,
batch_knowledge_gradient,
batch_noisy_expected_improvement,
batch_probability_of_improvement,
batch_upper_confidence_bound,
batch_simple_regret,
)

__all__ = [
expected_improvement,
max_value_entropy_search,
posterior_mean,
probability_of_improvement,
upper_confidence_bound,
batch_expected_improvement,
batch_knowledge_gradient,
batch_noisy_expected_improvement,
batch_probability_of_improvement,
batch_upper_confidence_bound,
batch_simple_regret,
]

0 comments on commit 0afd603

Please sign in to comment.