Hyperparameters

Consider the Booth test function (below). This function varies differently in different directions, and these directions are somewhat skewed with respect to the inputs. Our agent will automatically fit the right hyperparameters to account for this.

[1]:
import numpy as np
import matplotlib as mpl
from matplotlib import pyplot as plt
from blop.utils import functions

x1 = x2 = np.linspace(-10, 10, 256)
X1, X2 = np.meshgrid(x1, x2)

F = functions.booth(X1, X2)

plt.pcolormesh(x1, x2, F, norm=mpl.colors.LogNorm(), shading="auto")
plt.colorbar()
plt.xlabel("x1")
plt.ylabel("x2")
[1]:
Text(0, 0.5, 'x2')
../_images/tutorials_hyperparameters_1_1.png

The optimization goes faster if our model understands how the function changes as we change the inputs in different ways. The way it picks up on this is by starting from a general model that could describe a lot of functions, and making it specific to this one by choosing the right hyperparameters. Our Bayesian agent is very good at this, and only needs a few samples to figure out what the function looks like:

[2]:
def digestion(df):
    for index, entry in df.iterrows():
        df.loc[index, "booth"] = functions.booth(entry.x1, entry.x2)

    return df
[3]:
from blop.utils import prepare_re_env

%run -i $prepare_re_env.__file__ --db-type=temp

from blop import DOF, Objective, Agent

dofs = [
    DOF(name="x1", search_domain=(-6, 6)),
    DOF(name="x2", search_domain=(-6, 6)),
]

objectives = [
    Objective(name="booth", target="min"),
]


agent = Agent(
    dofs=dofs,
    objectives=objectives,
    digestion=digestion,
    db=db,
)

RE(agent.learn(acqf="qr", n=16))

agent.plot_objectives()


Transient Scan ID: 1     Time: 2024-05-13 18:01:14
Persistent Unique Scan ID: 'c62d8e84-e339-4091-a1e6-1999c26972df'
New stream: 'primary'
+-----------+------------+------------+------------+
|   seq_num |       time |         x1 |         x2 |
+-----------+------------+------------+------------+
|         1 | 18:01:14.9 |     -1.759 |      1.363 |
|         2 | 18:01:14.9 |     -2.374 |     -0.787 |
|         3 | 18:01:14.9 |     -0.820 |     -3.002 |
|         4 | 18:01:14.9 |      0.123 |     -2.472 |
|         5 | 18:01:14.9 |      1.581 |     -4.709 |
|         6 | 18:01:14.9 |      4.664 |     -4.479 |
|         7 | 18:01:14.9 |      3.016 |     -0.715 |
|         8 | 18:01:14.9 |      4.100 |      0.120 |
|         9 | 18:01:14.9 |      5.452 |      3.861 |
|        10 | 18:01:14.9 |      2.559 |      4.952 |
|        11 | 18:01:14.9 |      1.017 |      2.691 |
|        12 | 18:01:14.9 |     -0.313 |      3.649 |
|        13 | 18:01:14.9 |     -3.207 |      5.538 |
|        14 | 18:01:14.9 |     -4.842 |      1.825 |
|        15 | 18:01:14.9 |     -5.268 |     -2.027 |
|        16 | 18:01:14.9 |     -3.903 |     -5.810 |
+-----------+------------+------------+------------+
generator list_scan ['c62d8e84'] (scan num: 1)



../_images/tutorials_hyperparameters_4_1.png

In addition to modeling the fitness of the task, the agent models the probability that an input will be feasible:

[4]:
agent.plot_acquisition(acqf="qei")
../_images/tutorials_hyperparameters_6_0.png
[5]:
RE(agent.learn("qei", n=4, iterations=4))
agent.plot_objectives()


Transient Scan ID: 2     Time: 2024-05-13 18:01:19
Persistent Unique Scan ID: '47b09035-cb99-4119-b91c-6f1093082963'
New stream: 'primary'
+-----------+------------+------------+------------+
|   seq_num |       time |         x1 |         x2 |
+-----------+------------+------------+------------+
|         1 | 18:01:19.4 |     -0.950 |      6.000 |
|         2 | 18:01:19.4 |      0.712 |      3.311 |
|         3 | 18:01:19.4 |      1.549 |      2.567 |
|         4 | 18:01:19.4 |      2.055 |      1.904 |
+-----------+------------+------------+------------+
generator list_scan ['47b09035'] (scan num: 2)



/usr/share/miniconda3/envs/blop-py3.10/lib/python3.10/site-packages/botorch/optim/optimize.py:251: RuntimeWarning: Optimization failed in `gen_candidates_scipy` with the following warning(s):
[OptimizationWarning('Optimization failed within `scipy.optimize.minimize` with status 2 and message ABNORMAL_TERMINATION_IN_LNSRCH.')]
Trying again with a new set of initial conditions.
  candidate, acq_value = _optimize_acqf_batch(new_inputs)


Transient Scan ID: 3     Time: 2024-05-13 18:01:21
Persistent Unique Scan ID: 'cf67e2d3-64d7-4888-a66b-555ec6331e98'
New stream: 'primary'
+-----------+------------+------------+------------+
|   seq_num |       time |         x1 |         x2 |
+-----------+------------+------------+------------+
|         1 | 18:01:21.5 |      1.148 |      2.895 |
|         2 | 18:01:21.5 |      1.001 |      2.999 |
|         3 | 18:01:21.5 |      0.788 |      3.124 |
|         4 | 18:01:21.5 |      0.517 |      3.616 |
+-----------+------------+------------+------------+
generator list_scan ['cf67e2d3'] (scan num: 3)



/usr/share/miniconda3/envs/blop-py3.10/lib/python3.10/site-packages/botorch/optim/optimize.py:251: RuntimeWarning: Optimization failed in `gen_candidates_scipy` with the following warning(s):
[OptimizationWarning('Optimization failed within `scipy.optimize.minimize` with status 2 and message ABNORMAL_TERMINATION_IN_LNSRCH.')]
Trying again with a new set of initial conditions.
  candidate, acq_value = _optimize_acqf_batch(new_inputs)


Transient Scan ID: 4     Time: 2024-05-13 18:01:22
Persistent Unique Scan ID: '1f1018eb-7b6d-4a99-b85e-5d7b79939ebd'
New stream: 'primary'
+-----------+------------+------------+------------+
|   seq_num |       time |         x1 |         x2 |
+-----------+------------+------------+------------+
|         1 | 18:01:22.8 |      0.851 |      3.077 |
|         2 | 18:01:22.8 |      1.008 |      3.004 |
|         3 | 18:01:22.8 |      1.053 |      3.039 |
|         4 | 18:01:22.8 |      1.121 |      2.854 |
+-----------+------------+------------+------------+
generator list_scan ['1f1018eb'] (scan num: 4)





Transient Scan ID: 5     Time: 2024-05-13 18:01:25
Persistent Unique Scan ID: '2cf98ded-8806-4e87-be9f-89a0902c32cc'
New stream: 'primary'
+-----------+------------+------------+------------+
|   seq_num |       time |         x1 |         x2 |
+-----------+------------+------------+------------+
|         1 | 18:01:25.0 |      1.133 |      2.856 |
|         2 | 18:01:25.0 |      1.000 |      3.003 |
|         3 | 18:01:25.0 |      0.864 |      3.078 |
|         4 | 18:01:25.0 |      0.593 |      3.397 |
+-----------+------------+------------+------------+
generator list_scan ['2cf98ded'] (scan num: 5)



../_images/tutorials_hyperparameters_7_5.png
[6]:
agent.best
[6]:
x1                                1.00113
x2                               2.999373
booth                            0.000003
time        2024-05-13 18:01:21.567051172
acqf     monte_carlo_expected_improvement
Name: 21, dtype: object