You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"\n==================================\nBayesian optimization with `skopt`\n==================================\n\nGilles Louppe, Manoj Kumar July 2016.\nReformatted by Holger Nahrstaedt 2020\n\n.. currentmodule:: skopt\n\nProblem statement\n-----------------\n\nWe are interested in solving\n\n\\begin{align}x^* = arg \\min_x f(x)\\end{align}\n\nunder the constraints that\n\n- $f$ is a black box for which no closed form is known\n (nor its gradients);\n- $f$ is expensive to evaluate;\n- and evaluations of $y = f(x)$ may be noisy.\n\n**Disclaimer.** If you do not have these constraints, then there\nis certainly a better optimization algorithm than Bayesian optimization.\n\n\nBayesian optimization loop\n--------------------------\n\nFor $t=1:T$:\n\n1. Given observations $(x_i, y_i=f(x_i))$ for $i=1:t$, build a\n probabilistic model for the objective $f$. Integrate out all\n possible true functions, using Gaussian process regression.\n\n2. optimize a cheap acquisition/utility function $u$ based on the posterior\n distribution for sampling the next point.\n\n .. math::\n x_{t+1} = arg \\min_x u(x)\n\n Exploit uncertainty to balance exploration against exploitation.\n\n3. Sample the next observation $y_{t+1}$ at $x_{t+1}$.\n\n\nAcquisition functions\n---------------------\n\nAcquisition functions $u(x)$ specify which sample $x$: should be\ntried next:\n\n- Expected improvement (default):\n $-EI(x) = -\\mathbb{E} [f(x) - f(x_t^+)]$\n\n- Lower confidence bound: $LCB(x) = \\mu_{GP}(x) + \\kappa \\sigma_{GP}(x)$\n\n- Probability of improvement: $-PI(x) = -P(f(x) \\geq f(x_t^+) + \\kappa)$\n\nwhere $x_t^+$ is the best point observed so far.\n\nIn most cases, acquisition functions provide knobs (e.g., $\\kappa$) for\ncontrolling the exploration-exploitation trade-off.\n- Search in regions where $\\mu_{GP}(x)$ is high (exploitation)\n- Probe regions where uncertainty $\\sigma_{GP}(x)$ is high (exploration)\n"
19
+
]
20
+
},
21
+
{
22
+
"cell_type": "code",
23
+
"execution_count": null,
24
+
"metadata": {
25
+
"collapsed": false
26
+
},
27
+
"outputs": [],
28
+
"source": [
29
+
"print(__doc__)\n\nimport numpy as np\nnp.random.seed(237)\nimport matplotlib.pyplot as plt"
30
+
]
31
+
},
32
+
{
33
+
"cell_type": "markdown",
34
+
"metadata": {},
35
+
"source": [
36
+
"Toy example\n-----------\n\nLet assume the following noisy function $f$:\n\n"
"**Note.** In `skopt`, functions $f$ are assumed to take as input a 1D\nvector $x$: represented as an array-like and to return a scalar\n$f(x)$:.\n\n"
55
+
]
56
+
},
57
+
{
58
+
"cell_type": "code",
59
+
"execution_count": null,
60
+
"metadata": {
61
+
"collapsed": false
62
+
},
63
+
"outputs": [],
64
+
"source": [
65
+
"# Plot f(x) + contours\nx = np.linspace(-2, 2, 400).reshape(-1, 1)\nfx = [f(x_i, noise_level=0.0) for x_i in x]\nplt.plot(x, fx, \"r--\", label=\"True (unknown)\")\nplt.fill(np.concatenate([x, x[::-1]]),\n np.concatenate(([fx_i - 1.9600 * noise_level for fx_i in fx],\n [fx_i + 1.9600 * noise_level for fx_i in fx[::-1]])),\n alpha=.2, fc=\"r\", ec=\"None\")\nplt.legend()\nplt.grid()\nplt.show()"
66
+
]
67
+
},
68
+
{
69
+
"cell_type": "markdown",
70
+
"metadata": {},
71
+
"source": [
72
+
"Bayesian optimization based on gaussian process regression is implemented in\n:class:`gp_minimize` and can be carried out as follows:\n\n"
73
+
]
74
+
},
75
+
{
76
+
"cell_type": "code",
77
+
"execution_count": null,
78
+
"metadata": {
79
+
"collapsed": false
80
+
},
81
+
"outputs": [],
82
+
"source": [
83
+
"from skopt import gp_minimize\n\nres = gp_minimize(f, # the function to minimize\n [(-2.0, 2.0)], # the bounds on each dimension of x\n acq_func=\"EI\", # the acquisition function\n n_calls=15, # the number of evaluations of f\n n_random_starts=5, # the number of random initialization points\n noise=0.1**2, # the noise level (optional)\n random_state=1234) # the random seed"
84
+
]
85
+
},
86
+
{
87
+
"cell_type": "markdown",
88
+
"metadata": {},
89
+
"source": [
90
+
"Accordingly, the approximated minimum is found to be:\n\n"
91
+
]
92
+
},
93
+
{
94
+
"cell_type": "code",
95
+
"execution_count": null,
96
+
"metadata": {
97
+
"collapsed": false
98
+
},
99
+
"outputs": [],
100
+
"source": [
101
+
"\"x^*=%.4f, f(x^*)=%.4f\" % (res.x[0], res.fun)"
102
+
]
103
+
},
104
+
{
105
+
"cell_type": "markdown",
106
+
"metadata": {},
107
+
"source": [
108
+
"For further inspection of the results, attributes of the `res` named tuple\nprovide the following information:\n\n- `x` [float]: location of the minimum.\n- `fun` [float]: function value at the minimum.\n- `models`: surrogate models used for each iteration.\n- `x_iters` [array]:\n location of function evaluation for each iteration.\n- `func_vals` [array]: function value for each iteration.\n- `space` [Space]: the optimization space.\n- `specs` [dict]: parameters passed to the function.\n\n"
109
+
]
110
+
},
111
+
{
112
+
"cell_type": "code",
113
+
"execution_count": null,
114
+
"metadata": {
115
+
"collapsed": false
116
+
},
117
+
"outputs": [],
118
+
"source": [
119
+
"print(res)"
120
+
]
121
+
},
122
+
{
123
+
"cell_type": "markdown",
124
+
"metadata": {},
125
+
"source": [
126
+
"Together these attributes can be used to visually inspect the results of the\nminimization, such as the convergence trace or the acquisition function at\nthe last iteration:\n\n"
"Let us now visually examine\n\n1. The approximation of the fit gp model to the original function.\n2. The acquisition values that determine the next point to be queried.\n\n"
"The first column shows the following:\n\n1. The true function.\n2. The approximation to the original function by the gaussian process model\n3. How sure the GP is about the function.\n\nThe second column shows the acquisition function values after every\nsurrogate model is fit. It is possible that we do not choose the global\nminimum but a local minimum depending on the minimizer used to minimize\nthe acquisition function.\n\nAt the points closer to the points previously evaluated at, the variance\ndips to zero.\n\nFinally, as we increase the number of points, the GP model approaches\nthe actual function. The final few points are clustered around the minimum\nbecause the GP does not gain anything more by further exploration:\n\n"
0 commit comments