|
141 | 141 | "cell_type": "code", |
142 | 142 | "execution_count": 7, |
143 | 143 | "metadata": {}, |
144 | | - "outputs": [], |
| 144 | + "outputs": [ |
| 145 | + { |
| 146 | + "name": "stdout", |
| 147 | + "output_type": "stream", |
| 148 | + "text": [ |
| 149 | + "| \u001b[39m2 \u001b[39m | \u001b[39m0.7862 \u001b[39m | \u001b[39m-0.331911\u001b[39m | \u001b[39m1.3219469\u001b[39m |\n" |
| 150 | + ] |
| 151 | + } |
| 152 | + ], |
145 | 153 | "source": [ |
146 | 154 | "optimizer.register(\n", |
147 | 155 | " params=next_point_to_probe,\n", |
|
160 | 168 | }, |
161 | 169 | { |
162 | 170 | "cell_type": "code", |
163 | | - "execution_count": 8, |
| 171 | + "execution_count": null, |
164 | 172 | "metadata": {}, |
165 | 173 | "outputs": [ |
166 | 174 | { |
167 | 175 | "name": "stdout", |
168 | 176 | "output_type": "stream", |
169 | 177 | "text": [ |
170 | | - "-18.707136686093495 {'x': np.float64(1.9261486197444082), 'y': np.float64(-2.9996360060323246)}\n", |
171 | | - "0.750594563473972 {'x': np.float64(-0.3763326769822668), 'y': np.float64(1.328297354179696)}\n", |
172 | | - "-6.559031075654336 {'x': np.float64(1.979183535803597), 'y': np.float64(2.9083667381450318)}\n", |
173 | | - "-6.915481333972961 {'x': np.float64(-1.9686133847781613), 'y': np.float64(-1.009985740060171)}\n", |
174 | | - "-6.8600832617014085 {'x': np.float64(-1.9763198875239296), 'y': np.float64(2.9885278383464513)}\n", |
| 178 | + "| \u001b[39m3 \u001b[39m | \u001b[39m-18.41 \u001b[39m | \u001b[39m1.9506186\u001b[39m | \u001b[39m-2.950721\u001b[39m |\n", |
| 179 | + "-18.413111112960056 {'x': np.float64(1.9506186451101901), 'y': np.float64(-2.9507212017944955)}\n", |
| 180 | + "| \u001b[39m4 \u001b[39m | \u001b[39m0.7603 \u001b[39m | \u001b[39m-0.379805\u001b[39m | \u001b[39m1.3089202\u001b[39m |\n", |
| 181 | + "0.7603162209132889 {'x': np.float64(-0.37980530851809036), 'y': np.float64(1.3089202270946163)}\n", |
| 182 | + "| \u001b[39m5 \u001b[39m | \u001b[39m-6.841 \u001b[39m | \u001b[39m-1.990473\u001b[39m | \u001b[39m2.9694974\u001b[39m |\n", |
| 183 | + "-6.840906127161674 {'x': np.float64(-1.9904737772920469), 'y': np.float64(2.9694974661254085)}\n", |
| 184 | + "| \u001b[39m6 \u001b[39m | \u001b[39m-6.879 \u001b[39m | \u001b[39m1.9740210\u001b[39m | \u001b[39m2.9954409\u001b[39m |\n", |
| 185 | + "-6.8785435274794136 {'x': np.float64(1.9740210595375953), 'y': np.float64(2.995440899646362)}\n", |
| 186 | + "| \u001b[39m7 \u001b[39m | \u001b[39m-7.124 \u001b[39m | \u001b[39m-1.985509\u001b[39m | \u001b[39m-1.044851\u001b[39m |\n", |
| 187 | + "-7.123667302755344 {'x': np.float64(-1.9855094780813816), 'y': np.float64(-1.0448519298972099)}\n", |
175 | 188 | "{'target': np.float64(0.7861845912690544), 'params': {'x': np.float64(-0.331911981189704), 'y': np.float64(1.3219469606529486)}}\n" |
176 | 189 | ] |
177 | 190 | } |
|
181 | 194 | " next_point = optimizer.suggest()\n", |
182 | 195 | " target = black_box_function(**next_point)\n", |
183 | 196 | " optimizer.register(params=next_point, target=target)\n", |
184 | | - " \n", |
| 197 | + "\n", |
185 | 198 | " print(target, next_point)\n", |
186 | 199 | "print(optimizer.max)" |
187 | 200 | ] |
|
215 | 228 | "text": [ |
216 | 229 | "| iter | target | x | y |\n", |
217 | 230 | "-------------------------------------------------\n", |
218 | | - "| \u001b[39m1 \u001b[39m | \u001b[39m0.7862 \u001b[39m | \u001b[39m-0.331911\u001b[39m | \u001b[39m1.3219469\u001b[39m |\n", |
219 | | - "| \u001b[39m2 \u001b[39m | \u001b[39m-18.34 \u001b[39m | \u001b[39m1.9021640\u001b[39m | \u001b[39m-2.965222\u001b[39m |\n", |
220 | | - "| \u001b[35m3 \u001b[39m | \u001b[35m0.8731 \u001b[39m | \u001b[35m-0.298167\u001b[39m | \u001b[35m1.1948749\u001b[39m |\n", |
221 | | - "| \u001b[39m4 \u001b[39m | \u001b[39m-6.497 \u001b[39m | \u001b[39m1.9876938\u001b[39m | \u001b[39m2.8830942\u001b[39m |\n", |
222 | | - "| \u001b[39m5 \u001b[39m | \u001b[39m-4.286 \u001b[39m | \u001b[39m-1.995643\u001b[39m | \u001b[39m-0.141769\u001b[39m |\n", |
223 | | - "| \u001b[39m6 \u001b[39m | \u001b[39m-6.781 \u001b[39m | \u001b[39m-1.953302\u001b[39m | \u001b[39m2.9913127\u001b[39m |\n", |
| 231 | + "| \u001b[39m2 \u001b[39m | \u001b[39m0.7862 \u001b[39m | \u001b[39m-0.331911\u001b[39m | \u001b[39m1.3219469\u001b[39m |\n", |
| 232 | + "| \u001b[39m3 \u001b[39m | \u001b[39m-18.34 \u001b[39m | \u001b[39m1.9021640\u001b[39m | \u001b[39m-2.965222\u001b[39m |\n", |
| 233 | + "| \u001b[35m4 \u001b[39m | \u001b[35m0.8731 \u001b[39m | \u001b[35m-0.298167\u001b[39m | \u001b[35m1.1948749\u001b[39m |\n", |
| 234 | + "| \u001b[39m5 \u001b[39m | \u001b[39m-6.497 \u001b[39m | \u001b[39m1.9876938\u001b[39m | \u001b[39m2.8830942\u001b[39m |\n", |
| 235 | + "| \u001b[39m6 \u001b[39m | \u001b[39m-4.286 \u001b[39m | \u001b[39m-1.995643\u001b[39m | \u001b[39m-0.141769\u001b[39m |\n", |
| 236 | + "| \u001b[39m7 \u001b[39m | \u001b[39m-6.781 \u001b[39m | \u001b[39m-1.953302\u001b[39m | \u001b[39m2.9913127\u001b[39m |\n", |
224 | 237 | "=================================================\n" |
225 | 238 | ] |
226 | 239 | } |
|
257 | 270 | "\n", |
258 | 271 | "By default this package uses the Matern 2.5 kernel. Depending on your use case you may find that tuning the GP kernel could be beneficial. You're on your own here since these are very specific solutions to very specific problems. You should start with the [scikit learn docs](https://scikit-learn.org/stable/modules/gaussian_process.html#kernels-for-gaussian-processes)." |
259 | 272 | ] |
260 | | - }, |
261 | | - { |
262 | | - "cell_type": "markdown", |
263 | | - "metadata": {}, |
264 | | - "source": [ |
265 | | - "## Observers Continued\n", |
266 | | - "\n", |
267 | | - "Observers are objects that subscribe and listen to particular events fired by the `BayesianOptimization` object. \n", |
268 | | - "\n", |
269 | | - "When an event gets fired a callback function is called with the event and the `BayesianOptimization` instance passed as parameters. The callback can be specified at the time of subscription. If none is given it will look for an `update` method from the observer." |
270 | | - ] |
271 | | - }, |
272 | | - { |
273 | | - "cell_type": "code", |
274 | | - "execution_count": 10, |
275 | | - "metadata": {}, |
276 | | - "outputs": [], |
277 | | - "source": [ |
278 | | - "from bayes_opt.event import DEFAULT_EVENTS, Events" |
279 | | - ] |
280 | | - }, |
281 | | - { |
282 | | - "cell_type": "code", |
283 | | - "execution_count": 11, |
284 | | - "metadata": {}, |
285 | | - "outputs": [], |
286 | | - "source": [ |
287 | | - "optimizer = BayesianOptimization(\n", |
288 | | - " f=black_box_function,\n", |
289 | | - " pbounds={'x': (-2, 2), 'y': (-3, 3)},\n", |
290 | | - " verbose=2,\n", |
291 | | - " random_state=1,\n", |
292 | | - ")" |
293 | | - ] |
294 | | - }, |
295 | | - { |
296 | | - "cell_type": "code", |
297 | | - "execution_count": 12, |
298 | | - "metadata": {}, |
299 | | - "outputs": [], |
300 | | - "source": [ |
301 | | - "class BasicObserver:\n", |
302 | | - " def update(self, event, instance):\n", |
303 | | - " \"\"\"Does whatever you want with the event and `BayesianOptimization` instance.\"\"\"\n", |
304 | | - " print(\"Event `{}` was observed\".format(event))" |
305 | | - ] |
306 | | - }, |
307 | | - { |
308 | | - "cell_type": "code", |
309 | | - "execution_count": 13, |
310 | | - "metadata": {}, |
311 | | - "outputs": [], |
312 | | - "source": [ |
313 | | - "my_observer = BasicObserver()\n", |
314 | | - "\n", |
315 | | - "optimizer.subscribe(\n", |
316 | | - " event=Events.OPTIMIZATION_STEP,\n", |
317 | | - " subscriber=my_observer,\n", |
318 | | - " callback=None, # Will use the `update` method as callback\n", |
319 | | - ")" |
320 | | - ] |
321 | | - }, |
322 | | - { |
323 | | - "cell_type": "markdown", |
324 | | - "metadata": {}, |
325 | | - "source": [ |
326 | | - "Alternatively you have the option to pass a completely different callback." |
327 | | - ] |
328 | | - }, |
329 | | - { |
330 | | - "cell_type": "code", |
331 | | - "execution_count": 14, |
332 | | - "metadata": {}, |
333 | | - "outputs": [], |
334 | | - "source": [ |
335 | | - "def my_callback(event, instance):\n", |
336 | | - " print(\"Go nuts here!\")\n", |
337 | | - "\n", |
338 | | - "optimizer.subscribe(\n", |
339 | | - " event=Events.OPTIMIZATION_START,\n", |
340 | | - " subscriber=\"Any hashable object\",\n", |
341 | | - " callback=my_callback,\n", |
342 | | - ")" |
343 | | - ] |
344 | | - }, |
345 | | - { |
346 | | - "cell_type": "code", |
347 | | - "execution_count": 15, |
348 | | - "metadata": {}, |
349 | | - "outputs": [ |
350 | | - { |
351 | | - "name": "stdout", |
352 | | - "output_type": "stream", |
353 | | - "text": [ |
354 | | - "Go nuts here!\n", |
355 | | - "Event `optimization:step` was observed\n", |
356 | | - "Event `optimization:step` was observed\n", |
357 | | - "Event `optimization:step` was observed\n" |
358 | | - ] |
359 | | - } |
360 | | - ], |
361 | | - "source": [ |
362 | | - "optimizer.maximize(init_points=1, n_iter=2)" |
363 | | - ] |
364 | | - }, |
365 | | - { |
366 | | - "cell_type": "markdown", |
367 | | - "metadata": {}, |
368 | | - "source": [ |
369 | | - "For a list of all default events you can checkout `DEFAULT_EVENTS`" |
370 | | - ] |
371 | | - }, |
372 | | - { |
373 | | - "cell_type": "code", |
374 | | - "execution_count": 16, |
375 | | - "metadata": {}, |
376 | | - "outputs": [ |
377 | | - { |
378 | | - "data": { |
379 | | - "text/plain": [ |
380 | | - "['optimization:start', 'optimization:step', 'optimization:end']" |
381 | | - ] |
382 | | - }, |
383 | | - "execution_count": 16, |
384 | | - "metadata": {}, |
385 | | - "output_type": "execute_result" |
386 | | - } |
387 | | - ], |
388 | | - "source": [ |
389 | | - "DEFAULT_EVENTS" |
390 | | - ] |
391 | 273 | } |
392 | 274 | ], |
393 | 275 | "metadata": { |
|
406 | 288 | "name": "python", |
407 | 289 | "nbconvert_exporter": "python", |
408 | 290 | "pygments_lexer": "ipython3", |
409 | | - "version": "3.10.13" |
| 291 | + "version": "3.13.1" |
410 | 292 | }, |
411 | 293 | "nbdime-conflicts": { |
412 | 294 | "local_diff": [ |
|
0 commit comments