import numpy as np
from math import inf
from spotPython.fun.objectivefunctions import analytical
from spotPython.spot import spot
4 Using sklearn
Surrogates in spotPython
This notebook explains how different surrogate models from scikit-learn
can be used as surrogates in spotPython
optimization runs.
4.1 Example: Branin Function with spotPython
’s Internal Kriging Surrogate
4.1.1 The Objective Function Branin
The
spotPython
package provides several classes of objective functions.We will use an analytical objective function, i.e., a function that can be described by a (closed) formula.
Here we will use the Branin function:
y = a * (x2 - b * x1**2 + c * x1 - r) ** 2 + s * (1 - t) * np.cos(x1) + s, where values of a, b, c, r, s and t are: a = 1, b = 5.1 / (4*pi**2), c = 5 / pi, r = 6, s = 10 and t = 1 / (8*pi).
It has three global minima:
f(x) = 0.397887 at (-pi, 12.275), (pi, 2.275), and (9.42478, 2.475).
from spotPython.fun.objectivefunctions import analytical
= np.array([-5,-0])
lower = np.array([10,15]) upper
= analytical().fun_branin fun
Similar to the one-dimensional case, which was introduced in Section Section 1.7, we can use TensorBoard to monitor the progress of the optimization. We will use the same code, only the prefix is different:
from spotPython.utils.file import get_experiment_name
from spotPython.utils.init import fun_control_init
from spotPython.utils.file import get_spot_tensorboard_path
= "04"
PREFIX = get_experiment_name(prefix=PREFIX)
experiment_name print(experiment_name)
= fun_control_init(
fun_control =get_spot_tensorboard_path(experiment_name)) spot_tensorboard_path
04_bartz08-2_2023-07-10_00-20-12
4.1.2 Running the surrogate model based optimizer Spot
:
= spot.Spot(fun=fun,
spot_2 = lower,
lower = upper,
upper = 20,
fun_evals = inf,
max_time =123,
seed={"init_size": 10},
design_control=fun_control) fun_control
spot_2.run()
<spotPython.spot.spot.Spot at 0x168e621d0>
4.1.3 TensorBoard
Now we can start TensorBoard in the background with the following command:
tensorboard --logdir="./runs"
We can access the TensorBoard web server with the following URL:
http://localhost:6006/
The TensorBoard plot illustrates how spotPython
can be used as a microscope for the internal mechanisms of the surrogate-based optimization process. Here, one important parameter, the learning rate \(\theta\) of the Kriging surrogate is plotted against the number of optimization steps.
4.1.4 Print the Results
spot_2.print_results()
min y: 0.3983178342401956
x0: 3.135416996435963
x1: 2.2955490975636685
[['x0', 3.135416996435963], ['x1', 2.2955490975636685]]
4.1.5 Show the Progress and the Surrogate
=True) spot_2.plot_progress(log_y
spot_2.surrogate.plot()
4.2 Example: Using Surrogates From scikit-learn
- Default is the
spotPython
(i.e., the internal)kriging
surrogate. - It can be called explicitely and passed to
Spot
.
from spotPython.build.kriging import Kriging
= Kriging(name='kriging', seed=123) S_0
- Alternatively, models from
scikit-learn
can be selected, e.g., Gaussian Process, RBFs, Regression Trees, etc.
# Needed for the sklearn surrogates:
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn import linear_model
from sklearn import tree
import pandas as pd
- Here are some additional models that might be useful later:
= DecisionTreeRegressor(random_state=0)
S_Tree = linear_model.LinearRegression()
S_LM = linear_model.Ridge()
S_Ridge = RandomForestRegressor(max_depth=2, random_state=0) S_RF
4.2.1 GaussianProcessRegressor as a Surrogate
- To use a Gaussian Process model from
sklearn
, that is similar tospotPython
’sKriging
, we can proceed as follows:
= 1 * RBF(length_scale=1.0, length_scale_bounds=(1e-2, 1e2))
kernel = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=9) S_GP
The scikit-learn GP model
S_GP
is selected forSpot
as follows:surrogate = S_GP
We can check the kind of surogate model with the command
isinstance
:
isinstance(S_GP, GaussianProcessRegressor)
True
isinstance(S_0, Kriging)
True
- Similar to the
Spot
run with the internalKriging
model, we can call the run with thescikit-learn
surrogate:
= analytical(seed=123).fun_branin
fun = spot.Spot(fun=fun,
spot_2_GP = lower,
lower = upper,
upper = 20,
fun_evals =123,
seed={"init_size": 10},
design_control= S_GP)
surrogate spot_2_GP.run()
<spotPython.spot.spot.Spot at 0x2ac5a84c0>
spot_2_GP.plot_progress()
spot_2_GP.print_results()
min y: 0.3981631812933486
x0: 3.1491652274330053
x1: 2.2698186003445153
[['x0', 3.1491652274330053], ['x1', 2.2698186003445153]]
4.3 Example: One-dimensional Sphere Function With spotPython
’s Kriging
- In this example, we will use an one-dimensional function, which allows us to visualize the optimization process.
show_models= True
is added to the argument list.
from spotPython.fun.objectivefunctions import analytical
= np.array([-1])
lower = np.array([1])
upper = analytical(seed=123).fun_sphere fun
= spot.Spot(fun=fun,
spot_1 = lower,
lower = upper,
upper = 10,
fun_evals = inf,
max_time =123,
seed= True,
show_models= np.sqrt(np.spacing(1)),
tolerance_x ={"init_size": 3},)
design_control spot_1.run()
<spotPython.spot.spot.Spot at 0x2ac754a60>
4.3.1 Results
spot_1.print_results()
min y: 2.1786524623022742e-08
x0: -0.00014760259016366462
[['x0', -0.00014760259016366462]]
=True) spot_1.plot_progress(log_y
- The method
plot_model
plots the final surrogate:
spot_1.plot_model()
4.4 Example: Sklearn
Model GaussianProcess
- This example visualizes the search process on the
GaussianProcessRegression
surrogate fromsklearn
. - Therefore
surrogate = S_GP
is added to the argument list.
= analytical(seed=123).fun_sphere
fun = spot.Spot(fun=fun,
spot_1_GP = lower,
lower = upper,
upper = 10,
fun_evals = inf,
max_time =123,
seed= True,
show_models={"init_size": 3},
design_control= S_GP)
surrogate spot_1_GP.run()
<spotPython.spot.spot.Spot at 0x2acc3d240>
spot_1_GP.print_results()
min y: 1.6119389142446866e-09
x0: 4.0148959068009304e-05
[['x0', 4.0148959068009304e-05]]
=True) spot_1_GP.plot_progress(log_y
spot_1_GP.plot_model()
4.5 Exercises
4.5.1 DecisionTreeRegressor
- Describe the surrogate model.
- Use the surrogate as the model for optimization.
4.5.2 RandomForestRegressor
- Describe the surrogate model.
- Use the surrogate as the model for optimization.
4.5.3 linear_model.LinearRegression
- Describe the surrogate model.
- Use the surrogate as the model for optimization.
4.5.4 linear_model.Ridge
- Describe the surrogate model.
- Use the surrogate as the model for optimization.
4.6 Exercise 2
Compare the performance of the five different surrogates on both objective functions:
spotPython
’s internal KrigingDecisionTreeRegressor
RandomForestRegressor
linear_model.LinearRegression
linear_model.Ridge