Skip to content

Commit 0afd603

Browse files
facebook-github-botBalandat
authored andcommitted
Initial commit
fbshipit-source-id: 436d6dd4434140b6b24f9476b71ec3b4e1b095c2
0 parents commit 0afd603

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+2831
-0
lines changed

.flake8

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[flake8]
2+
3+
# E704: the linter doesn't parse types properly
4+
# T499, T484: We don't need to run pyre and mypy
5+
# W503: black and flake8 disagree on how to place operators)
6+
ignore = T484, T499, W503, E704
7+
# Black really wants lines to be 88 chars....
8+
max-line-length = 88

.gitignore

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# watchman
2+
.watchmanconfig
3+
4+
# Byte-compiled / optimized / DLL files
5+
__pycache__/
6+
*.py[cod]
7+
*$py.class
8+
9+
# Distribution / packaging
10+
.Python
11+
env/
12+
build/
13+
develop-eggs/
14+
dist/
15+
downloads/
16+
eggs/
17+
.eggs/
18+
lib/
19+
lib64/
20+
parts/
21+
sdist/
22+
var/
23+
wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
28+
# Atom plugin files and ctags
29+
.ftpconfig
30+
.ftpconfig.cson
31+
.ftpignore
32+
*.tags
33+
*.tags1
34+
35+
# PyInstaller
36+
# Usually these files are written by a python script from a template
37+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
38+
*.manifest
39+
*.spec
40+
41+
# Installer logs
42+
pip-log.txt
43+
pip-delete-this-directory.txt
44+
45+
# Unit test / coverage reports
46+
htmlcov/
47+
.tox/
48+
.coverage
49+
.coverage.*
50+
.cache
51+
nosetests.xml
52+
coverage.xml
53+
*.cover
54+
.hypothesis/
55+
56+
# Sphinx documentation
57+
docs/_build/
58+
59+
# PyBuilder
60+
target/
61+
62+
# Jupyter Notebook
63+
.ipynb_checkpoints
64+
65+
# pyenv
66+
.python-version
67+
68+
# dotenv
69+
.env
70+
71+
# virtualenv
72+
.venv
73+
venv/
74+
ENV/
75+
76+
# mypy
77+
.mypy_cache/
78+
79+
# vim
80+
*.swp

DESIGN_GUIDE.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# botorch design guide [WIP]
2+
3+
This document provides guidance on botorch's internal design.
4+
5+
## design philosophy
6+
7+
botorch follows a modular, low-overhead “unframework” design philosophy, which
8+
provides building blocks and abstractions, but does not force the user to do
9+
things in any one particular way. This empowers the user to easily prototype and
10+
test new approaches.
11+
12+
13+
## acquisition functions
14+
15+
botorch supports batch acquisition functions (e.g. q-EI, q-UCB, etc.) that
16+
assign a joint utility to a set of q design points in the parameter space.
17+
18+
Unfortunately, this batch nomenclature gets easily conflated with the pytorch
19+
notion of batch-evaluation. To avoid confusion in that respect, we adopt the
20+
convention of referring to batches in the batch-acquisition sense as "q-batches",
21+
and to batches in the torch batch-evaluation sense as "t-batches".
22+
23+
Internally, q-batch acquisition functions operate on input tensors of shape
24+
`b x q x d`, where `b` is the number of t-batches, `q` is the number of design
25+
points, and `d` is the dimension of the parameter space. Their output is a
26+
one-dimensional tensor with `b` elements, with the `i`-th element corresponding
27+
to the `i`-th t-batch.
28+
29+
To simplify the user-facing API, if provided with an input tensor of shape `q x d`,
30+
a t-batch size of 1 is inferred, and the result is returned as a torch scalar.

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
MIT License
2+
3+
Copyright (c) 2018-present, Facebook, Inc.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9+
of the Software, and to permit persons to whom the Software is furnished to do
10+
so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# botorch (pre-alpha) [WIP]
2+
3+
botorch is a library for Bayesian Optimization in pytorch.

botorch/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env python3
2+
3+
from . import acquisition, optim, test_functions
4+
from .fit import fit_model
5+
from .gen import gen_candidates
6+
from .utils import manual_seed
7+
8+
9+
__all__ = [
10+
acquisition,
11+
fit_model,
12+
gen_candidates,
13+
optim,
14+
manual_seed,
15+
test_functions,
16+
]

botorch/acquisition/__init__.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python3
2+
3+
from .batch_modules import (
4+
qExpectedImprovement,
5+
qProbabilityOfImprovement,
6+
qUpperConfidenceBound,
7+
)
8+
from .modules import (
9+
AcquisitionFunction,
10+
ExpectedImprovement,
11+
PosteriorMean,
12+
ProbabilityOfImprovement,
13+
UpperConfidenceBound,
14+
)
15+
16+
17+
__all__ = [
18+
AcquisitionFunction,
19+
ExpectedImprovement,
20+
PosteriorMean,
21+
ProbabilityOfImprovement,
22+
UpperConfidenceBound,
23+
qExpectedImprovement,
24+
qProbabilityOfImprovement,
25+
qUpperConfidenceBound,
26+
]
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#!/usr/bin/env python3
2+
3+
from typing import Callable, List, Optional
4+
5+
import torch
6+
from gpytorch import Module
7+
8+
from .functional import (
9+
batch_expected_improvement,
10+
batch_probability_of_improvement,
11+
batch_simple_regret,
12+
batch_upper_confidence_bound,
13+
)
14+
from .modules import AcquisitionFunction
15+
16+
17+
"""
18+
Wraps the batch acquisition functions defined in botorch.acquisition.functional
19+
into BatchAcquisitionFunction gpytorch modules.
20+
"""
21+
22+
23+
class BatchAcquisitionFunction(AcquisitionFunction):
24+
def forward(self, candidate_set: torch.Tensor) -> torch.Tensor:
25+
"""Takes in a `b x q x d` candidate_set Tensor of `b` t-batches with `q`
26+
`d`-dimensional design points each, and returns a one-dimensional Tensor
27+
with `b` elements."""
28+
raise NotImplementedError("BatchAcquisitionFunction cannot be used directly")
29+
30+
31+
class qExpectedImprovement(BatchAcquisitionFunction):
32+
"""TODO"""
33+
34+
def __init__(
35+
self,
36+
model: Module,
37+
best_f: float,
38+
objective: Callable[[torch.Tensor], torch.Tensor] = lambda Y: Y,
39+
constraints: Optional[List[Callable[[torch.Tensor], torch.Tensor]]] = None,
40+
mc_samples: int = 5000,
41+
) -> None:
42+
super(qExpectedImprovement, self).__init__(model)
43+
self.best_f = best_f
44+
self.objective = objective
45+
self.constraints = constraints
46+
self.mc_samples = mc_samples
47+
48+
def forward(self, candidate_set: torch.Tensor) -> torch.Tensor:
49+
return batch_expected_improvement(
50+
X=candidate_set,
51+
model=self.model,
52+
best_f=self.best_f,
53+
objective=self.objective,
54+
constraints=self.constraints,
55+
mc_samples=self.mc_samples,
56+
)
57+
58+
59+
class qProbabilityOfImprovement(BatchAcquisitionFunction):
60+
"""TODO"""
61+
62+
def __init__(self, model: Module, best_f: float, mc_samples: int = 5000) -> None:
63+
super(qProbabilityOfImprovement, self).__init__(model)
64+
self.best_f = best_f
65+
self.mc_samples = mc_samples
66+
67+
def forward(self, candidate_set: torch.Tensor) -> torch.Tensor:
68+
return batch_probability_of_improvement(
69+
X=candidate_set,
70+
model=self.model,
71+
best_f=self.best_f,
72+
mc_samples=self.mc_samples,
73+
)
74+
75+
76+
class qUpperConfidenceBound(BatchAcquisitionFunction):
77+
"""TODO"""
78+
79+
def __init__(self, model: Module, beta: float, mc_samples: int = 5000) -> None:
80+
super(qUpperConfidenceBound, self).__init__(model)
81+
self.beta = beta
82+
self.mc_samples = mc_samples
83+
84+
def forward(self, candidate_set: torch.Tensor) -> torch.Tensor:
85+
return batch_upper_confidence_bound(
86+
X=candidate_set,
87+
model=self.model,
88+
beta=self.beta,
89+
mc_samples=self.mc_samples,
90+
)
91+
92+
93+
class qSimpleRegret(BatchAcquisitionFunction):
94+
"""TODO"""
95+
96+
def __init__(self, model: Module, mc_samples: int = 5000) -> None:
97+
super(qSimpleRegret, self).__init__(model)
98+
self.mc_samples = mc_samples
99+
100+
def forward(self, candidate_set: torch.Tensor) -> torch.Tensor:
101+
return batch_simple_regret(
102+
X=candidate_set, model=self.model, mc_samples=self.mc_samples
103+
)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python3
2+
3+
from .acquisition import (
4+
expected_improvement,
5+
max_value_entropy_search,
6+
posterior_mean,
7+
probability_of_improvement,
8+
upper_confidence_bound,
9+
)
10+
11+
from .batch_acquisition import (
12+
batch_expected_improvement,
13+
batch_knowledge_gradient,
14+
batch_noisy_expected_improvement,
15+
batch_probability_of_improvement,
16+
batch_upper_confidence_bound,
17+
batch_simple_regret,
18+
)
19+
20+
__all__ = [
21+
expected_improvement,
22+
max_value_entropy_search,
23+
posterior_mean,
24+
probability_of_improvement,
25+
upper_confidence_bound,
26+
batch_expected_improvement,
27+
batch_knowledge_gradient,
28+
batch_noisy_expected_improvement,
29+
batch_probability_of_improvement,
30+
batch_upper_confidence_bound,
31+
batch_simple_regret,
32+
]

0 commit comments

Comments
 (0)