.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/02_gp/plot_07_parameter_optimization.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via Binder. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_02_gp_plot_07_parameter_optimization.py: Parameter optimization inside the search ======================================== Constants inside nonlinear functions -- the 0.8 in :math:`e^{-0.8x_0}` -- are hard to evolve by mutation. Modern symbolic regression systems tune them by gradient methods during the search (Kommenda et al., 2020; Burlacu et al., 2020). eqdisco applies Levenberg--Marquardt with exact forward-mode Jacobians to the best share of the offspring and writes the tuned values back into the genome (Lamarckian learning). The target is :math:`y=2.5\,e^{-0.8x_0}\sin x_1`. .. GENERATED FROM PYTHON SOURCE LINES 12-23 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import eqdisco as eq rng = np.random.default_rng(1) X = rng.uniform(-2, 2, (200, 2)) y = 2.5 * np.exp(-0.8 * X[:, 0]) * np.sin(X[:, 1]) funcs = ("add", "sub", "mul", "div", "exp", "sin") .. GENERATED FROM PYTHON SOURCE LINES 24-26 With and without Levenberg--Marquardt ------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 26-49 .. code-block:: Python runs = {} for opt in ("none", "lm"): runs[opt] = [ eq.SymbolicRegressor(population_size=200, generations=20, optimize=opt, optimize_fraction=0.1, optimize_iterations=10, functions=funcs, random_state=s).fit(X, y) for s in range(5) ] for opt, rr in runs.items(): print(opt, [str(r.equation_) for r in rr[:2]]) for opt in ("none", "lm"): print(f"{opt:4s} median final NMSE {np.median([r.history_['best_loss'].iloc[-1] for r in runs[opt]]) / np.var(y):.2e} " f"median evaluations {np.median([r.history_['n_evaluations'].iloc[-1] for r in runs[opt]]):.0f}") fig, axs = plt.subplots(1, 2, figsize=(10, 3.4)) for opt, color in (("none", "#D55E00"), ("lm", "#285780")): for r in runs[opt]: h = r.history_ axs[0].semilogy(h["n_evaluations"], h["best_loss"] / np.var(y), color=color, lw=0.8, alpha=0.6) axs[0].plot([], [], color=color, label="no tuning" if opt == "none" else "Levenberg-Marquardt") eq.viz.plot_pareto(runs[opt][0], ax=axs[1], annotate=False, color=color, label=opt) axs[0].set_xlabel("fitness evaluations") axs[0].set_ylabel("normalized MSE of the best") axs[0].legend(frameon=False, fontsize=8) fig.tight_layout() .. image-sg:: /auto_examples/02_gp/images/sphx_glr_plot_07_parameter_optimization_001.png :alt: plot 07 parameter optimization :srcset: /auto_examples/02_gp/images/sphx_glr_plot_07_parameter_optimization_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none none ['0.03115 + 0.07203 * sin(x1) / exp(-3.266 + x0)', '0.005319 + 0.586 * (sin(x1) / exp(x0 - 0.981) + x1 / exp(exp(x0 - 0.237)) + sin(sin(x1)))'] lm ['-0.05235 + 0.03944 * (x1 + sin(x1) / (exp(-4.312 + exp(-7.193e+13 + x0)) + exp(-3.734 + x0)) + sin(x1) / (exp(-12.53 + x0) + exp(-3.536 + x0)) + exp(-4.817 + x0) + x0 - x0 + exp(sin(x1)))', '-68.73 + 0.611 * (sin(x1) / exp(exp(x0)) + sin(sin(x1)) / exp(x0 - 1.105) + sin(x1) + sin(x1 * 0.01234) + 112.5)'] none median final NMSE 6.03e-03 median evaluations 3262 lm median final NMSE 2.49e-05 median evaluations 3319 .. GENERATED FROM PYTHON SOURCE LINES 50-52 Parameter study: the share of optimized offspring ------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 52-64 .. code-block:: Python fracs = [0.0, 0.05, 0.2, 0.5] med = [] for f in fracs: vals = [eq.SymbolicRegressor(population_size=150, generations=15, optimize="lm", optimize_fraction=f, functions=funcs, random_state=s).fit(X, y).score(X, y) for s in range(3)] med.append(np.median(vals)) print("share optimized", fracs, "median 1-R2", [f"{1 - v:.1e}" for v in med]) fig, ax = plt.subplots(figsize=(4.5, 3)) ax.plot(fracs, 1 - np.array(med), "o-", color="#285780") ax.set_yscale("log") ax.set_xlabel("share of offspring tuned by LM") ax.set_ylabel("median $1-R^2$") plt.show() .. image-sg:: /auto_examples/02_gp/images/sphx_glr_plot_07_parameter_optimization_002.png :alt: plot 07 parameter optimization :srcset: /auto_examples/02_gp/images/sphx_glr_plot_07_parameter_optimization_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none share optimized [0.0, 0.05, 0.2, 0.5] median 1-R2 ['2.2e-03', '1.2e-03', '2.1e-04', '3.5e-05'] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 40.161 seconds) .. _sphx_glr_download_auto_examples_02_gp_plot_07_parameter_optimization.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/ComplexSystemsIntelligence/eqdisco/main?urlpath=lab/tree/notebooks/auto_examples/02_gp/plot_07_parameter_optimization.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_07_parameter_optimization.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_07_parameter_optimization.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_07_parameter_optimization.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_