|
39 | 39 | "def maximum(x, y):\n",
|
40 | 40 | " return znp.maximum(x, y)\n",
|
41 | 41 | "\n",
|
| 42 | + "\n", |
42 | 43 | "print(f\"sqrt with np:{np.sqrt(maximum(1., 2.))}, with z:{znp.sqrt(maximum(1., 2.))}\")\n",
|
43 | 44 | "print(f\"vectorized: {np.sqrt(maximum(znp.array([1., 2.]), znp.array([3., 4.])))}\")"
|
44 | 45 | ]
|
|
306 | 307 | "id": "24",
|
307 | 308 | "metadata": {},
|
308 | 309 | "source": [
|
309 |
| - "## Result and serialization\n", |
| 310 | + "## Result\n", |
| 311 | + "\n", |
| 312 | + "The result has more usage for setting parameters, the `update_params` method is now available and can be used to set the parameters to the minimum as seen above.\n", |
310 | 313 | "\n",
|
311 |
| - "The result stays similar but can now be pickled like any object in zfit!\n", |
| 314 | + "It can also be used in a context manager to temporarily set the parameters to the minimum and restore them afterwards.\n" |
| 315 | + ] |
| 316 | + }, |
| 317 | + { |
| 318 | + "cell_type": "code", |
| 319 | + "execution_count": null, |
| 320 | + "id": "25", |
| 321 | + "metadata": {}, |
| 322 | + "outputs": [], |
| 323 | + "source": [ |
| 324 | + "param1again.set_value(1.5)\n", |
| 325 | + "with result:\n", |
| 326 | + " print(f\"param1 set temporarily to {param1again}\")\n", |
| 327 | + "print(f\"param1 is now {param1again} again\")" |
| 328 | + ] |
| 329 | + }, |
| 330 | + { |
| 331 | + "cell_type": "code", |
| 332 | + "execution_count": null, |
| 333 | + "id": "26", |
| 334 | + "metadata": {}, |
| 335 | + "outputs": [], |
| 336 | + "source": [ |
| 337 | + "# or to set the parameters to the minimum\n", |
| 338 | + "zfit.param.set_values(result) # supports also a dict of {param: value}!\n", |
| 339 | + "print(param1again)" |
| 340 | + ] |
| 341 | + }, |
| 342 | + { |
| 343 | + "cell_type": "markdown", |
| 344 | + "id": "27", |
| 345 | + "metadata": {}, |
| 346 | + "source": [ |
| 347 | + "## Serialization\n", |
| 348 | + "\n", |
| 349 | + "The result can now be pickled like any object in zfit!\n", |
312 | 350 | "(this was not possible before, only after calling `freeze` on the result)\n",
|
313 | 351 | " \n",
|
314 | 352 | "This works directly using `dill` (a library that extends `pickle`), but can fail if the garbage collector is not run. Therefore, zfit provides a slightly modified `dill` that can work as a drop-in replacement.\n",
|
|
319 | 357 | {
|
320 | 358 | "cell_type": "code",
|
321 | 359 | "execution_count": null,
|
322 |
| - "id": "25", |
| 360 | + "id": "28", |
323 | 361 | "metadata": {},
|
324 | 362 | "outputs": [],
|
325 | 363 | "source": [
|
326 | 364 | "result_serialized = zfit.dill.dumps(result)\n",
|
327 |
| - "result_deserialized = zfit.dill.loads(result_serialized)\n", |
328 |
| - "result_deserialized.errors()" |
| 365 | + "result_deserialized = zfit.dill.loads(result_serialized)" |
| 366 | + ] |
| 367 | + }, |
| 368 | + { |
| 369 | + "cell_type": "code", |
| 370 | + "execution_count": null, |
| 371 | + "id": "29", |
| 372 | + "metadata": {}, |
| 373 | + "outputs": [], |
| 374 | + "source": [ |
| 375 | + "# the result can be used as before\n", |
| 376 | + "result_deserialized.hesse() # the default name is now \"hesse\" and not \"minuit_hesse\"\n", |
| 377 | + "result_deserialized.errors() # the default name is now \"errors\" and not \"minuit_minos\"\n", |
| 378 | + "print(result_deserialized)" |
329 | 379 | ]
|
330 | 380 | },
|
331 | 381 | {
|
332 | 382 | "cell_type": "markdown",
|
333 |
| - "id": "26", |
| 383 | + "id": "30", |
334 | 384 | "metadata": {},
|
335 | 385 | "source": [
|
336 | 386 | "## Parameters as arguments\n",
|
|
343 | 393 | {
|
344 | 394 | "cell_type": "code",
|
345 | 395 | "execution_count": null,
|
346 |
| - "id": "27", |
| 396 | + "id": "31", |
347 | 397 | "metadata": {},
|
348 | 398 | "outputs": [],
|
349 | 399 | "source": [
|
|
358 | 408 | {
|
359 | 409 | "cell_type": "code",
|
360 | 410 | "execution_count": null,
|
361 |
| - "id": "28", |
| 411 | + "id": "32", |
362 | 412 | "metadata": {},
|
363 | 413 | "outputs": [],
|
364 | 414 | "source": [
|
|
381 | 431 | {
|
382 | 432 | "cell_type": "code",
|
383 | 433 | "execution_count": null,
|
384 |
| - "id": "29", |
| 434 | + "id": "33", |
| 435 | + "metadata": {}, |
| 436 | + "outputs": [], |
| 437 | + "source": [ |
| 438 | + "# a result can also be used as argument for PDFs or, here, for losses\n", |
| 439 | + "loss_before = loss.value()\n", |
| 440 | + "loss_min = loss.value(params=result_deserialized) # evaluate at minimum\n", |
| 441 | + "print(f\"loss before (random from before): {loss_before:.7} minimum value: {loss_min:.7} vs {result_deserialized.fmin:.7}\")" |
| 442 | + ] |
| 443 | + }, |
| 444 | + { |
| 445 | + "cell_type": "code", |
| 446 | + "execution_count": null, |
| 447 | + "id": "34", |
385 | 448 | "metadata": {},
|
386 | 449 | "outputs": [],
|
387 | 450 | "source": [
|
388 | 451 | "# creating a PDF looks also different, but here we use the name of the parametrization and the axis (integers)\n",
|
389 |
| - "\n", |
390 |
| - "\n", |
391 | 452 | "class MyGauss2D(zfit.pdf.ZPDF):\n",
|
392 | 453 | " _PARAMS = (\"mu\", \"sigma\")\n",
|
393 | 454 | " _N_OBS = 2\n",
|
|
404 | 465 | {
|
405 | 466 | "cell_type": "code",
|
406 | 467 | "execution_count": null,
|
407 |
| - "id": "30", |
| 468 | + "id": "35", |
408 | 469 | "metadata": {},
|
409 | 470 | "outputs": [],
|
410 | 471 | "source": [
|
|
415 | 476 | {
|
416 | 477 | "cell_type": "code",
|
417 | 478 | "execution_count": null,
|
418 |
| - "id": "31", |
| 479 | + "id": "36", |
419 | 480 | "metadata": {},
|
420 | 481 | "outputs": [],
|
421 | 482 | "source": []
|
|
0 commit comments