
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/03_eddy-lifetime_fit_3term_loss.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_03_eddy-lifetime_fit_3term_loss.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_03_eddy-lifetime_fit_3term_loss.py:


=============================================================
Example 3: Adding Regularization and Penalty Terms to Fitting
=============================================================

This example is nearly identical to Example 2, however we
use a more sophisticated loss function, introducing an additional first-order
penalty term. The previous synthetic fit relied only on MSE loss and a second-order penalty.

.. GENERATED FROM PYTHON SOURCE LINES 12-17

Import packages
---------------

First, we import the packages we need for this example. Additionally, we choose to use
CUDA if it is available.

.. GENERATED FROM PYTHON SOURCE LINES 17-46

.. code-block:: Python


    import torch
    import torch.nn as nn

    from drdmannturb.parameters import (
        LossParameters,
        NNParameters,
        PhysicalParameters,
        ProblemParameters,
    )
    from drdmannturb.spectra_fitting import CalibrationProblem, OnePointSpectraDataGenerator

    device = "cuda" if torch.cuda.is_available() else "cpu"

    if torch.cuda.is_available():
        torch.set_default_tensor_type("torch.cuda.FloatTensor")

    zref = 40  # reference height
    ustar = 1.773  # friction velocity

    # Scales associated with Kaimal spectrum
    L = 0.59 * zref  # length scale
    Gamma = 3.9  # time scale
    sigma = 3.2 * ustar**2.0 / zref ** (2.0 / 3.0)  # magnitude (σ = αϵ^{2/3})

    print(f"Physical Parameters: {L,Gamma,sigma}")

    k1 = torch.logspace(-1, 2, 20) / zref





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Physical Parameters: (23.599999999999998, 3.9, 0.8600574364289042)




.. GENERATED FROM PYTHON SOURCE LINES 47-54

%%
Now, we construct our ``CalibrationProblem``.

Compared to Example 2, we are instead using GELU
activations and will train for fewer epochs. The more interesting difference
is that we will have activated a first order term in the loss function by passing
``alpha_pen1`` a value in the ``LossParameters`` constructor.

.. GENERATED FROM PYTHON SOURCE LINES 54-70

.. code-block:: Python


    pb = CalibrationProblem(
        nn_params=NNParameters(
            nlayers=2,
            hidden_layer_sizes=[10, 10],
            activations=[nn.GELU(), nn.GELU()],
        ),
        prob_params=ProblemParameters(nepochs=5),
        loss_params=LossParameters(alpha_pen2=1.0, alpha_pen1=1.0e-5, beta_reg=2e-4),
        phys_params=PhysicalParameters(
            L=L, Gamma=Gamma, sigma=sigma, ustar=ustar, domain=k1
        ),
        logging_directory="runs/synthetic_3term",
        device=device,
    )








.. GENERATED FROM PYTHON SOURCE LINES 71-73

In the following cell, we construct our :math:`k_1` data points grid and
generate the values. ``Data`` will be a tuple ``(<data points>, <data values>)``.

.. GENERATED FROM PYTHON SOURCE LINES 73-75

.. code-block:: Python

    Data = OnePointSpectraDataGenerator(data_points=k1, zref=zref, ustar=ustar).Data








.. GENERATED FROM PYTHON SOURCE LINES 76-80

Calibration
-----------
Now, we fit our model. ``CalibrationProblem.calibrate`` takes the tuple ``Data``
which we just constructed and performs a typical training loop.

.. GENERATED FROM PYTHON SOURCE LINES 80-84

.. code-block:: Python

    optimal_parameters = pb.calibrate(data=Data)

    pb.print_calibrated_params()





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    ========================================
    Initial loss: 0.046960198025841615
    ========================================

      0%|          | 0/5 [00:00<?, ?it/s]
     20%|██        | 1/5 [00:02<00:09,  2.30s/it]
     40%|████      | 2/5 [00:04<00:06,  2.33s/it]
     60%|██████    | 3/5 [00:06<00:04,  2.30s/it]
     80%|████████  | 4/5 [00:09<00:02,  2.29s/it]
    100%|██████████| 5/5 [00:11<00:00,  2.35s/it]
    100%|██████████| 5/5 [00:11<00:00,  2.33s/it]
    ========================================
    Spectra fitting concluded with final loss: 0.007155118811755071
    ========================================
    Optimal calibrated L        :  26.1055 
    Optimal calibrated Γ        :   4.7513 
    Optimal calibrated αϵ^{2/3} :   0.8069 
    ========================================




.. GENERATED FROM PYTHON SOURCE LINES 85-94

Plotting
--------
``DRDMannTurb`` offers built-in plotting utilities and Tensorboard integration
which make visualizing results and various aspects of training performance
very simple. The training logs can be accessed from the logging directory
with Tensorboard utilities, but we also provide a simple internal utility for a single
training log plot.

The following will plot our fit.

.. GENERATED FROM PYTHON SOURCE LINES 94-96

.. code-block:: Python

    pb.plot()




.. image-sg:: /auto_examples/images/sphx_glr_03_eddy-lifetime_fit_3term_loss_001.png
   :alt: One-point spectra, Eddy lifetime
   :srcset: /auto_examples/images/sphx_glr_03_eddy-lifetime_fit_3term_loss_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 97-99

This plots out the loss function terms as specified, each multiplied by the
respective coefficient hyperparameter.

.. GENERATED FROM PYTHON SOURCE LINES 99-100

.. code-block:: Python

    pb.plot_losses(run_number=0)



.. image-sg:: /auto_examples/images/sphx_glr_03_eddy-lifetime_fit_3term_loss_002.png
   :alt: Total Loss, Regularization, 2nd Order Penalty, 1st Order Penalty, MSE Loss
   :srcset: /auto_examples/images/sphx_glr_03_eddy-lifetime_fit_3term_loss_002.png
   :class: sphx-glr-single-img






.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 12.548 seconds)


.. _sphx_glr_download_auto_examples_03_eddy-lifetime_fit_3term_loss.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: 03_eddy-lifetime_fit_3term_loss.ipynb <03_eddy-lifetime_fit_3term_loss.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: 03_eddy-lifetime_fit_3term_loss.py <03_eddy-lifetime_fit_3term_loss.py>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
