.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/optimization/gradient_descent/plot_01_fixed_vs_line_search.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. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_optimization_gradient_descent_plot_01_fixed_vs_line_search.py: Fixed step vs. backtracking line search on an ill-conditioned bowl ========================================================================= Gradient descent zig-zags across a narrow, elongated quadratic bowl. Backtracking line search adapts its step length automatically and converges in far fewer iterations than any single fixed step size. .. GENERATED FROM PYTHON SOURCE LINES 11-14 .. code-block:: Python from mathematicskit.optimization import GradientDescent, GradientDescentLineSearch, quadratic_bowl, quadratic_bowl_grad from mathematicskit.optimization.visualizers.plots import plot_contour_path .. GENERATED FROM PYTHON SOURCE LINES 15-17 Run both methods from the same starting point ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 17-25 .. code-block:: Python x0 = [1.8, 2.5] result_fixed = GradientDescent(alpha=0.09, tol=1e-8, max_iter=5000).minimize(quadratic_bowl, quadratic_bowl_grad, x0) result_ls = GradientDescentLineSearch(tol=1e-8, max_iter=5000).minimize(quadratic_bowl, quadratic_bowl_grad, x0) print(f"fixed step: {result_fixed.iterations} iterations, x = {result_fixed.x}") print(f"line search: {result_ls.iterations} iterations, x = {result_ls.x}") .. rst-class:: sphx-glr-script-out .. code-block:: none fixed step: 202 iterations, x = [9.58591898e-009 2.50000000e-202] line search: 76 iterations, x = [4.24022886e-09 5.38868804e-10] .. GENERATED FROM PYTHON SOURCE LINES 26-28 Plot both iterate paths over the bowl's contours ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 28-31 .. code-block:: Python plot_contour_path(quadratic_bowl, result_fixed, x_range=(-2.0, 2.0), y_range=(-1.0, 3.0)) plot_contour_path(quadratic_bowl, result_ls, x_range=(-2.0, 2.0), y_range=(-1.0, 3.0)) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /api/gallery/optimization/gradient_descent/images/sphx_glr_plot_01_fixed_vs_line_search_001.png :alt: Optimizer iterate path :srcset: /api/gallery/optimization/gradient_descent/images/sphx_glr_plot_01_fixed_vs_line_search_001.png :class: sphx-glr-multi-img * .. image-sg:: /api/gallery/optimization/gradient_descent/images/sphx_glr_plot_01_fixed_vs_line_search_002.png :alt: Optimizer iterate path :srcset: /api/gallery/optimization/gradient_descent/images/sphx_glr_plot_01_fixed_vs_line_search_002.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.310 seconds) .. _sphx_glr_download_api_gallery_optimization_gradient_descent_plot_01_fixed_vs_line_search.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_fixed_vs_line_search.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_fixed_vs_line_search.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_fixed_vs_line_search.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_