baxus.util package

Submodules

baxus.util.acquisition_function_types module

class baxus.util.acquisition_function_types.AcquisitionFunctionType(value)

Bases: Enum

An enumeration.

EXPECTED_IMPROVEMENT = 1

Expected improvement acquisition function.

THOMPSON_SAMPLING = 2

Thompson sampling acquisition function.

baxus.util.acquisition_functions module

class baxus.util.acquisition_functions.ExpectedImprovement(gp: GP, best_f: Union[float, ndarray], lb: ndarray, ub: ndarray, evaluation_batch_size: int = 100)

Bases: object

optimize()

baxus.util.console_entry_point module

baxus.util.console_entry_point.bench()

baxus.util.data_utils module

baxus.util.data_utils.join_data(X: ndarray, dims_and_bins: Dict[int, int]) ndarray

After splitting, copy the data from the splitting dim(s) into the new dim(s)

Parameters
  • X (np.ndarray) – the x-values before the splitting

  • dims_and_bins (Dict[int, int]) – the splitting: dims and number of bins. Be warned that we assume an ordered dict which we are allowed to in newer Python versions.

Returns: the x-values after splitting

baxus.util.exceptions module

exception baxus.util.exceptions.ArgumentError

Bases: Exception

An exception for an illegal input argmument.

exception baxus.util.exceptions.BoundsMismatchException

Bases: Exception

When the search space bounds don’t have the same length.

exception baxus.util.exceptions.EffectiveDimTooLargeException

Bases: Exception

When the effective dimensionality is too large (for example when larger than the input dimensionality).

exception baxus.util.exceptions.OutOfBoundsException

Bases: Exception

When a point falls outside the search space.

exception baxus.util.exceptions.UnknownBehaviorError

Bases: Exception

baxus.util.gp_utils module

baxus.util.gp_utils.initializer_factory(hyperparameter_configuration: Dict[str, float]) Callable[[turbo.gp.GP], None]

Take a hyperparameter configuration and return a lambda initializing a model with this configuration :param hyperparameter_configuration: the hyperparameter configuration :return: callabe, defined in GPyTorch model

baxus.util.gp_utils.latin_hypercube_hp_grid(hyperparameter_grid: Dict[str, Tuple[float, float, float]], n_samples: int) Dict[str, ndarray]

Draw samples from latin hypercube from hyperparameter grid. Default configuration will always be the first configuration. :param hyperparameter_grid: dictionary, key: hyperparameter name, value: Tuple[lower_bound, upper_bound, default value] :param n_samples: number of samples to return, if 1 return default values :return: dictionary, key: hyperparameter name, value: np.ndarray of sample values (shape: (n_samples, 1))

baxus.util.gp_utils.mle_optimization(initializer: Callable[[baxus.gp.GP], None], model: baxus.gp.GP, num_steps: int, train_x: Tensor, train_y: Tensor) Tuple[OrderedDict[str, Tensor], float]

Optimize likelihood of a model with an initializer. :param initializer: the model initializer :param model: the GP model :param num_steps: number gradient descent steps :param kernel_type: the kernel type of the GP model :param train_x: the training data :param train_y: the training data :param mll: the model likelihood :return: state dict and the average loss

baxus.util.gp_utils.pick_best_from_configurations(initializers: List[Callable[[baxus.gp.GP], None]], model: baxus.gp.GP, train_x: Tensor, train_y: Tensor, n_best: Optional[int] = 1) List[Callable[[baxus.gp.GP], None]]

Pick the n_best best performing initializers from a list of initializers based on a GP and a MLL :param initializers: list of initializers, sets GP hyperparameters :param model: the GP model :param train_x: the data to evaluate the model likelihood on :param train_y: the data to evaluate the model likelihood on :param n_best: number of best performing initializers to choose

Returns: list of initializer functions

baxus.util.parsing module

baxus.util.parsing.benchmark_loader(bench: str, args: Namespace)

Import the required implementation of a benchmark. We use this class to avoid imports of benchmarks that require optional dependencies.

Parameters
  • bench – the benchmark name

  • args – the parsed command line arguments

Returns

None. Just import the benchmark implementation.

baxus.util.parsing.fun_mapper()

Map benchmark names to their implementation.

Returns

a mapping of benchmark names to their (partially initialized) classes

Return type

dict

baxus.util.parsing.parse(args)

Define a CLI parser and parse command line arguments

Parameters

args – command line arguments

Returns

parsed command line arguments

Return type

Namespace

baxus.util.projections module

class baxus.util.projections.AxUS(input_dim: int, target_dim: int, seed: Optional[int] = None, bin_sizing=EmbeddingType.BAXUS)

Bases: ProjectionModel

AxUS embedding. Also support HeSBO embedding by choosing RANDOM bin sizing

property S: ndarray
contributing_dimensions(target_dimension: int)

Returns the dimensions in the ambient space that contribute to a target dimension. :param target_dimension: the target dimension for which to return the contributing input dimensions :return: the input dimensions contributing to the target dimension

increase_target_dimensionality(dims_and_bins: Dict[int, int])

Split up one target dimension. The contributing input dimensions will be randomly assigned to two bins. One bin is the current target dimension, the other bin will be assigned to a new target dimension. Therefore, the target dimensionality will be increased by one. The projection matrix will change by this! The affected target dimension and the new dimension will only have half the number of contributing input dimensions than the target dimension prior to the splitting. :param splitting_target_dim: the target dimension to split :return: None

property input_to_target_dim: Dict[int, int]
merge_dims(d1: int, d2: int)
project_down(X: ndarray) ndarray

Project one or multiple points from the ambient into the target space. :param X: Points in the ambient space. Shape: [num_points, input_dim] :return: numpy array, shape: [num_points, target_dim]

project_up(Y: ndarray) ndarray

Project one or multiple points from the target into the ambient space. :param X: Points in the target space. Shape: [num_points, target_dim] :return: numpy array, shape: [num_points, input_dim]

class baxus.util.projections.IdentityProjector(lb, ub)

Bases: ProjectionModel

project_down(X: ndarray) ndarray
project_up(Y: ndarray) ndarray
class baxus.util.projections.ProjectionModel

Bases: ABC

project_down(X: ndarray) ndarray
project_up(Y: ndarray) ndarray

baxus.util.utils module

class baxus.util.utils.ColorIterator

Bases: Iterator

A color iterator

colors = [(0.9677975592919913, 0.44127456009157356, 0.5358103155058701), (0.9700531261509934, 0.45770926349740065, 0.3531524128185177), (0.8938549928919908, 0.5179451226045084, 0.19578982312283577), (0.7984176591188128, 0.5683288211959375, 0.19493989247411406), (0.7230662070346502, 0.5998698459184864, 0.19435383116611019), (0.6513805524154538, 0.6246215677131698, 0.1938638514569588), (0.5698207216512174, 0.6476400297683411, 0.19338388003304807), (0.45335905426990747, 0.6726643016097544, 0.19283492774002386), (0.19267215382011754, 0.7024980364438371, 0.2190277618362616), (0.20016316839845322, 0.6923245352355919, 0.4558490078700114), (0.2049306910121812, 0.6855205795299731, 0.5516522153733634), (0.2086955048051592, 0.6799580179243367, 0.6158686104727769), (0.21220522114444668, 0.6746160410152996, 0.669798007433198), (0.21602078052440615, 0.6686313640347535, 0.7236525090548798), (0.22092954343584914, 0.6606495185740604, 0.7874206828795038), (0.2288208998036418, 0.6471129256412876, 0.8804597560436164), (0.40787210907827137, 0.6112575459886632, 0.9585717755650441), (0.604372577978537, 0.5628003207719868, 0.9583266876556983), (0.7467364292103781, 0.505636427792889, 0.9580701898641198), (0.8837845033635382, 0.4216395606013157, 0.9577546964768361), (0.959913647625796, 0.3772126409792086, 0.884255749835771), (0.9631117449045455, 0.4046276637794385, 0.7681490719405799), (0.9655137520691374, 0.4238744203836587, 0.6624745545344729)]
baxus.util.utils.from_1_around_origin(x: ndarray, lower_bounds: ndarray, upper_bounds: ndarray) ndarray

Project points that were scaled to one-around-origin cube back to full space.

Parameters
  • x – the points

  • lower_bounds – the lower bounds of the full space

  • upper_bounds – the upper bounds of the full space

Returns: scaled points

baxus.util.utils.from_unit_cube(x: ndarray, lower_bounds: ndarray, upper_bounds: ndarray) ndarray

Project points that were scaled to unit cube back to full space.

Parameters
  • x – the points

  • lower_bounds – the lower bounds of the full space

  • upper_bounds – the upper bounds of the full space

Returns: scaled points

baxus.util.utils.in_range(x: ndarray, incumbent: ndarray, lb: ndarray, ub: ndarray)

Whether the point x is within the range of the next slower trust region around incumbent given the current bounds lb, ub

Parameters
  • x – the point to test

  • incumbent – the point to center the next smallest TR around

  • lb – lower bound of the current trust region

  • ub – upper bound of the current trust region

Returns: true if point would fall in the next smaller trust region, false otherwise

baxus.util.utils.latin_hypercube(n_pts: int, dim: int) ndarray

Basic Latin hypercube implementation with center perturbation.

Parameters
  • n_pts – number of points to sample

  • dim – dimensionality of the space

Returns: the LHS points

baxus.util.utils.one_around_origin_latin_hypercube(n_pts: int, dim: int) ndarray

Basic Latin hypercube implementation with center perturbation in a one-around-origin cube.

Parameters
  • n_pts – number of points to sample

  • dim – dimensionality of the space

Returns: the LHS points

baxus.util.utils.partialclass(cls, *args, **kwargs)

A partially initialized class

Parameters
  • cls – the base class

  • *args

  • **kwargs

Returns:

baxus.util.utils.star_string(wrap_string: str) str

Wrap string in stars.

Parameters

wrap_string – string to wrap

Returns: wrapped string

baxus.util.utils.str2bool(value: str) bool

Parse string to boolean or throw error if string has no boolean type.

Parameters

value – the string to parse

Returns: True, if string is truthy, false if string is falsy

baxus.util.utils.to_1_around_origin(x: ndarray, lower_bounds: ndarray, upper_bounds: ndarray) ndarray

Project to [-1, 1]^d from hypercube with bounds lb and ub

Parameters
  • x – the points to scale

  • lower_bounds – the lower bounds in the unscaled space

  • upper_bounds – the upper bounds un the unscaled space

Returns: the scaled points.

baxus.util.utils.to_unit_cube(x: ndarray, lower_bounds: ndarray, upper_bounds: ndarray) ndarray

Project to [0, 1]^d from hypercube with bounds lb and ub

Parameters
  • x – the points to scale

  • lower_bounds – the lower bounds in the unscaled space

  • upper_bounds – the upper bounds un the unscaled space

Returns: scaled points

Module contents