SafeOpt¶
-
class
safeopt.
SafeOpt
(gp, parameter_set, fmin, lipschitz=None, beta=2, num_contexts=0, threshold=0, scaling='auto')¶ A class for Safe Bayesian Optimization.
This class implements the SafeOpt algorithm. It uses a Gaussian process model in order to determine parameter combinations that are safe with high probability. Based on these, it aims to both expand the set of safe parameters and to find the optimal parameters within the safe set.
Parameters: - gp: GPy Gaussian process
A Gaussian process which is initialized with safe, initial data points. If a list of GPs then the first one is the value, while all the other ones are safety constraints.
- parameter_set: 2d-array
List of parameters
- fmin: list of floats
Safety threshold for the function value. If multiple safety constraints are used this can also be a list of floats (the first one is always the one for the values, can be set to None if not wanted)
- lipschitz: list of floats
The Lipschitz constant of the system, if None the GP confidence intervals are used directly.
- beta: float or callable
A constant or a function of the time step that scales the confidence interval of the acquisition function.
- threshold: float or list of floats
The algorithm will not try to expand any points that are below this threshold. This makes the algorithm stop expanding points eventually. If a list, this represents the stopping criterion for all the gps. This ignores the scaling factor.
- scaling: list of floats or “auto”
A list used to scale the GP uncertainties to compensate for different input sizes. This should be set to the maximal variance of each kernel. You should probably leave this to “auto” unless your kernel is non-stationary.
Examples
>>> from safeopt import SafeOpt >>> from safeopt import linearly_spaced_combinations >>> import GPy >>> import numpy as np
Define a Gaussian process prior over the performance
>>> x = np.array([[0.]]) >>> y = np.array([[1.]]) >>> gp = GPy.models.GPRegression(x, y, noise_var=0.01**2)
>>> bounds = [[-1., 1.]] >>> parameter_set = linearly_spaced_combinations([[-1., 1.]], ... num_samples=100)
Initialize the Bayesian optimization and get new parameters to evaluate
>>> opt = SafeOpt(gp, parameter_set, fmin=[0.]) >>> next_parameters = opt.optimize()
Add a new data point with the parameters and the performance to the GP. The performance has normally be determined through an external function call.
>>> performance = np.array([[1.]]) >>> opt.add_new_data_point(next_parameters, performance)
Attributes: context
Return the current context variables.
context_fixed_inputs
Return the fixed inputs for the current context.
data
Return the data within the GP models.
parameter_set
Discrete parameter samples for Bayesian optimization.
t
Return the time step (number of measurements).
use_lipschitz
Boolean that determines whether to use the Lipschitz constant.
- x
- y
Methods
add_new_data_point
(x, y[, context])Add a new function observation to the GPs. compute_safe_set
()Compute only the safe set based on the current confidence bounds. compute_sets
([full_sets])Compute the safe set of points, based on current confidence bounds. get_maximum
([context])Return the current estimate for the maximum. get_new_query_point
([ucb])Compute a new point at which to evaluate the function. optimize
([context, ucb])Run Safe Bayesian optimization and get the next parameters. plot
(n_samples[, axis, figure, plot_3d])Plot the current state of the optimization. remove_last_data_point
()Remove the data point that was last added to the GP. update_confidence_intervals
([context])Recompute the confidence intervals form the GP. -
add_new_data_point
(x, y, context=None)¶ Add a new function observation to the GPs.
Parameters: - x: 2d-array
- y: 2d-array
- context: array_like
The context(s) used for the data points.
-
compute_safe_set
()¶ Compute only the safe set based on the current confidence bounds.
-
compute_sets
(full_sets=False)¶ Compute the safe set of points, based on current confidence bounds.
Parameters: - context: ndarray
Array that contains the context used to compute the sets
- full_sets: boolean
Whether to compute the full set of expanders or whether to omit computations that are not relevant for running SafeOpt (This option is only useful for plotting purposes)
-
context
¶ Return the current context variables.
-
context_fixed_inputs
¶ Return the fixed inputs for the current context.
-
data
¶ Return the data within the GP models.
-
get_maximum
(context=None)¶ Return the current estimate for the maximum.
Parameters: - context: ndarray
A vector containing the current context
Returns: - x - ndarray
Location of the maximum
- y - 0darray
Maximum value
Notes
Uses the current context and confidence intervals! Run update_confidence_intervals first if you recently added a new data point.
-
get_new_query_point
(ucb=False)¶ Compute a new point at which to evaluate the function.
Parameters: - ucb: bool
If True the safe-ucb criteria is used instead.
Returns: - x: np.array
The next parameters that should be evaluated.
-
optimize
(context=None, ucb=False)¶ Run Safe Bayesian optimization and get the next parameters.
Parameters: - context: ndarray
A vector containing the current context
- ucb: bool
If True the safe-ucb criteria is used instead.
Returns: - x: np.array
The next parameters that should be evaluated.
-
parameter_set
¶ Discrete parameter samples for Bayesian optimization.
-
plot
(n_samples, axis=None, figure=None, plot_3d=False, **kwargs)¶ Plot the current state of the optimization.
Parameters: - n_samples: int
How many samples to use for plotting
- axis: matplotlib axis
The axis on which to draw (does not get cleared first)
- figure: matplotlib figure
Ignored if axis is already defined
- plot_3d: boolean
If set to true shows a 3D plot for 2 dimensional data
-
remove_last_data_point
()¶ Remove the data point that was last added to the GP.
-
t
¶ Return the time step (number of measurements).
-
update_confidence_intervals
(context=None)¶ Recompute the confidence intervals form the GP.
Parameters: - context: ndarray
Array that contains the context used to compute the sets
-
use_lipschitz
¶ Boolean that determines whether to use the Lipschitz constant.
By default this is set to False, which means the adapted SafeOpt algorithm is used, that uses the GP confidence intervals directly. If set to True, the self.lipschitz parameter is used to compute the safe and expanders sets.