Optimizer API¶
Optimizers is much like optimizers in PyTorch, but for the purpose of optimizing queries and search. Each optimizer would perform a few steps that collectively would guide the search towards the optimal trajectory.
AxServiceOptimizer
¶
AxServiceOptimizer(
index_eval: IndexEvaluator,
index: Index,
*,
sobol_steps: int = 0,
device: Device = "cpu",
task: Task = Task.EXPLORE,
acqf: str | AcquisitionFunc = AcquisitionFunc.AUTO,
surrogate: str | SurrogateModel = SurrogateModel.AUTO,
surrogate_kwargs: SurrogateOptions | None = None
)
Bases: Optimizer
The Ax optimizer that uses the service API. See https://ax.dev/tutorials/gpei_hartmann_service.html
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index_eval
|
IndexEvaluator
|
The evaluator to use for the query. |
required |
index
|
Index
|
The index to for querying. |
required |
sobol_steps
|
int
|
The number of steps to use for the Sobol sequence. |
0
|
device
|
Device
|
The device to use for the optimization. |
'cpu'
|
task
|
Task
|
The task to use for the optimization. |
EXPLORE
|
acqf
|
str | AcquisitionFunc
|
The acquisition function to use for the optimization. |
AUTO
|
surrogate
|
str | SurrogateModel
|
The surrogate model to use for the optimization. |
AUTO
|
surrogate_kwargs
|
SurrogateOptions | None
|
The keyword arguments to pass to the surrogate model. |
None
|
Source code in src/bocoel/optim/ax/optim.py
step
¶
Optimize one step with the ax optimizer.
Note
Somehow it seems that with recent versions of Ax,
it would crash when workers > 1 in get_next_trials.
Therefore, it's removed.
Raises:
| Type | Description |
|---|---|
StopIteration
|
When there are no more steps. |
Returns:
| Type | Description |
|---|---|
Mapping[int, float]
|
The resulting trial-id to the evaluation result. |
Source code in src/bocoel/optim/ax/optim.py
AxServiceParameter
¶
CorpusEvaluator
dataclass
¶
Bases: IndexEvaluator
Evaluates the corpus with the given adaptor.
__call__
¶
Evaluates the given batched query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
idx
|
ArrayLike
|
The indices to evaluate. |
required |
Returns:
| Type | Description |
|---|---|
NDArray
|
The results of the query. Must be in the same order as the query. |
Source code in src/bocoel/optim/corpora.py
IndexEvaluator
¶
Bases: Protocol
A protocol for evaluating with the indices.
__call__
abstractmethod
¶
Evaluates the given batched query. The order of the results must be kept in the original order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
idx
|
ArrayLike
|
The indices to evaluate. Must be a 1D array. |
required |
Returns:
| Type | Description |
|---|---|
NDArray
|
The indices of the results. Must be in the same order as the query. |
Source code in src/bocoel/optim/interfaces/evals.py
CachedIndexEvaluator
¶
Bases: IndexEvaluator
Since there might be duplicate indices (and a lot of them during evaluation), this utility evaluator would cache the results and only compute the unseen indices. This would help evaluating the larger models of evaluation a lot faster.
Source code in src/bocoel/optim/interfaces/evals.py
Optimizer
¶
Bases: Protocol
The protocol for optimizers. Optimizers are used for optimizing the search space, Find the best exploration sequence for a given task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index_eval
|
IndexEvaluator
|
The id evaluator. Evalutes the items at the given storage indices. |
required |
index
|
Index
|
The index that contains information about the domain. |
required |
**kwargs
|
Any
|
The keyword arguments. |
{}
|
Source code in src/bocoel/optim/interfaces/optim.py
task
abstractmethod
property
¶
The task to use for the optimization.
Returns:
| Type | Description |
|---|---|
Task
|
One of |
step
abstractmethod
¶
Perform a single step of optimization. This is a shortcut into the optimization process. For methods that evaluate the entire search at once, this method would output the slices of the entire search.
Returns:
| Type | Description |
|---|---|
Mapping[int, float]
|
A mapping of storage indices to the corresponding scores. |
Raises:
| Type | Description |
|---|---|
StopIteration
|
If the optimization is complete. |
Source code in src/bocoel/optim/interfaces/optim.py
RandomOptimizer
¶
Bases: Optimizer
The random optimizer that uses random search.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index_eval
|
IndexEvaluator
|
The evaluator to use for the storage. |
required |
index
|
Index
|
The index to use for the query. |
required |
samples
|
int
|
The number of samples to use for the optimization. |
required |
batch_size
|
int
|
The number of samples to evaluate at once. |
required |
Source code in src/bocoel/optim/random.py
KMeansOptimizer
¶
KMeansOptimizer(
index_eval: IndexEvaluator,
index: Index,
*,
batch_size: int,
embeddings: NDArray,
model_kwargs: KMeansOptions
)
Bases: ScikitLearnOptimizer
The KMeans optimizer that uses clustering algorithms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index_eval
|
IndexEvaluator
|
The evaluator to use on the storage. |
required |
index
|
Index
|
The index to use for the query. |
required |
batch_size
|
int
|
The number of embeddings to evaluate at once. |
required |
embeddings
|
NDArray
|
The embeddings to cluster. |
required |
model_kwargs
|
KMeansOptions
|
The keyword arguments to pass to the KMeans model. |
required |
Source code in src/bocoel/optim/sklearn/kmeans.py
KMedoidsOptimizer
¶
KMedoidsOptimizer(
index_eval: IndexEvaluator,
index: Index,
*,
batch_size: int,
embeddings: NDArray,
model_kwargs: KMedoidsOptions
)
Bases: ScikitLearnOptimizer
The KMedoids optimizer that uses clustering algorithms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index_eval
|
IndexEvaluator
|
The evaluator to use for the index. |
required |
index
|
Index
|
The index to use for the query. |
required |
batch_size
|
int
|
The number of embeddings to evaluate at once. |
required |
embeddings
|
NDArray
|
The embeddings to cluster. |
required |
model_kwargs
|
KMedoidsOptions
|
The keyword arguments to pass to the KMedoids model. |
required |
Source code in src/bocoel/optim/sklearn/kmedoids.py
ScikitLearnOptimizer
¶
ScikitLearnOptimizer(
index_eval: IndexEvaluator,
index: Index,
embeddings: NDArray,
model: ScikitLearnCluster,
batch_size: int,
)
Bases: Optimizer
The sklearn optimizer that uses clustering algorithms. See the following webpage for options https://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index_eval
|
IndexEvaluator
|
The evaluator to use for the query. |
required |
index
|
Index
|
The index to use for the query. |
required |
model
|
ScikitLearnCluster
|
The model to use for the optimization. |
required |
batch_size
|
int
|
The number of embeddings to evaluate at once. |
required |
Source code in src/bocoel/optim/sklearn/optim.py
UniformOptimizer
¶
UniformOptimizer(
index_eval: IndexEvaluator,
index: Index,
*,
grids: Sequence[int],
batch_size: int
)
Bases: Optimizer
The uniform optimizer that uses grid-based search.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index_eval
|
IndexEvaluator
|
The evaluator to use for the storage. |
required |
index
|
Index
|
The index to use for the query. |
required |
grids
|
Sequence[int]
|
The number of grids to use for the optimization. |
required |
batch_size
|
int
|
The number of grids to evaluate at once. |
required |