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 bloptools.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(db, uid):
    products = db[uid].table()

    for index, entry in products.iterrows():
        products.loc[index, "booth"] = functions.booth(entry.x1, entry.x2)

    return products
[3]:
from bloptools.utils import prepare_re_env

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

from bloptools.bayesian import DOF, Objective, Agent

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

objectives = [
    Objective(key="booth", minimize=True),
]


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

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

agent.plot_objectives()
running iteration 1 / 1


Transient Scan ID: 1     Time: 2023-11-08 23:56:11
Persistent Unique Scan ID: 'a766c09f-7b44-481f-b99a-a26acd0212b5'
New stream: 'primary'
+-----------+------------+------------+------------+
|   seq_num |       time |         x1 |         x2 |
+-----------+------------+------------+------------+
|         1 | 23:56:11.3 |     -0.503 |     -0.207 |
|         2 | 23:56:11.3 |     -1.076 |      1.212 |
|         3 | 23:56:11.3 |     -3.580 |      2.658 |
|         4 | 23:56:11.3 |     -2.821 |      3.645 |
|         5 | 23:56:11.3 |     -4.811 |      5.094 |
|         6 | 23:56:11.3 |      0.072 |      5.856 |
|         7 | 23:56:11.3 |      4.271 |      4.383 |
|         8 | 23:56:11.3 |      2.030 |      1.920 |
|         9 | 23:56:11.4 |      5.337 |      0.450 |
|        10 | 23:56:11.4 |      5.137 |     -0.945 |
|        11 | 23:56:11.4 |      3.343 |     -3.009 |
|        12 | 23:56:11.4 |      2.579 |     -2.497 |
|        13 | 23:56:11.4 |      1.396 |     -4.564 |
|        14 | 23:56:11.4 |     -1.523 |     -3.771 |
|        15 | 23:56:11.4 |     -3.758 |     -1.735 |
|        16 | 23:56:11.4 |     -5.762 |     -5.303 |
+-----------+------------+------------+------------+
generator list_scan ['a766c09f'] (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(acq_func="qei")
../_images/tutorials_hyperparameters_6_0.png
[5]:
RE(agent.learn("qei", n=4, iterations=4))
agent.plot_objectives()
running iteration 1 / 4


Transient Scan ID: 2     Time: 2023-11-08 23:56:15
Persistent Unique Scan ID: '55c7fc47-856c-42d6-b13c-c4dc1eaf300b'
New stream: 'primary'
+-----------+------------+------------+------------+
|   seq_num |       time |         x1 |         x2 |
+-----------+------------+------------+------------+
|         1 | 23:56:15.3 |      0.209 |      3.715 |
|         2 | 23:56:15.3 |      0.745 |      3.193 |
|         3 | 23:56:15.3 |      1.378 |      2.488 |
|         4 | 23:56:15.3 |      2.253 |      1.603 |
+-----------+------------+------------+------------+
generator list_scan ['55c7fc47'] (scan num: 2)



running iteration 2 / 4


Transient Scan ID: 3     Time: 2023-11-08 23:56:16
Persistent Unique Scan ID: 'cff600c6-ebd6-4fd9-8d18-07bbb35890d7'
New stream: 'primary'
+-----------+------------+------------+------------+
|   seq_num |       time |         x1 |         x2 |
+-----------+------------+------------+------------+
|         1 | 23:56:16.7 |      2.361 |      1.506 |
|         2 | 23:56:16.7 |      1.324 |      2.543 |
|         3 | 23:56:16.7 |      0.743 |      3.193 |
|         4 | 23:56:16.7 |      0.209 |      3.721 |
+-----------+------------+------------+------------+
generator list_scan ['cff600c6'] (scan num: 3)



running iteration 3 / 4


Transient Scan ID: 4     Time: 2023-11-08 23:56:18
Persistent Unique Scan ID: '1d7b8de2-3eb9-463b-a35f-74b47b95a5e8'
New stream: 'primary'
+-----------+------------+------------+------------+
|   seq_num |       time |         x1 |         x2 |
+-----------+------------+------------+------------+
|         1 | 23:56:18.3 |      0.209 |      3.709 |
|         2 | 23:56:18.3 |      0.743 |      3.193 |
|         3 | 23:56:18.3 |      1.433 |      2.447 |
|         4 | 23:56:18.3 |      2.344 |      1.515 |
+-----------+------------+------------+------------+
generator list_scan ['1d7b8de2'] (scan num: 4)



running iteration 4 / 4


Transient Scan ID: 5     Time: 2023-11-08 23:56:19
Persistent Unique Scan ID: 'd852edf1-a76b-4cc8-9b33-f559f57976ef'
New stream: 'primary'
+-----------+------------+------------+------------+
|   seq_num |       time |         x1 |         x2 |
+-----------+------------+------------+------------+
|         1 | 23:56:19.7 |      2.399 |      1.459 |
|         2 | 23:56:19.7 |      1.369 |      2.512 |
|         3 | 23:56:19.7 |      0.743 |      3.194 |
|         4 | 23:56:19.7 |      0.152 |      3.751 |
+-----------+------------+------------+------------+
generator list_scan ['d852edf1'] (scan num: 5)



../_images/tutorials_hyperparameters_7_1.png