:orphan:

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

.. only:: html

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

        Click :ref:`here <sphx_glr_download_tutorials_hpo_quickstart_tensorflow_model.py>`
        to download the full example code

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

.. _sphx_glr_tutorials_hpo_quickstart_tensorflow_model.py:


Port TensorFlow Quickstart to NNI
=================================
This is a modified version of `TensorFlow quickstart`_.

It can be run directly and will have the exact same result as original version.

Furthermore, it enables the ability of auto-tuning with an NNI *experiment*, which will be discussed later.

For now, we recommend to run this script directly to verify the environment.

There are only 3 key differences from the original version:

 1. In `Get optimized hyperparameters`_ part, it receives auto-generated hyperparameters.
 2. In `(Optional) Report intermediate results`_ part, it reports per-epoch accuracy for visualization.
 3. In `Report final result`_ part, it reports final accuracy for tuner to generate next hyperparameter set.

.. _TensorFlow quickstart: https://www.tensorflow.org/tutorials/quickstart/beginner

.. GENERATED FROM PYTHON SOURCE LINES 22-25

.. code-block:: default

    import nni
    import tensorflow as tf








.. GENERATED FROM PYTHON SOURCE LINES 26-28

Hyperparameters to be tuned
---------------------------

.. GENERATED FROM PYTHON SOURCE LINES 28-35

.. code-block:: default

    params = {
        'dense_units': 128,
        'activation_type': 'relu',
        'dropout_rate': 0.2,
        'learning_rate': 0.001,
    }








.. GENERATED FROM PYTHON SOURCE LINES 36-40

Get optimized hyperparameters
-----------------------------
If run directly, ``nni.get_next_parameters()`` is a no-op and returns an empty dict.
But with an NNI *experiment*, it will receive optimized hyperparameters from tuning algorithm.

.. GENERATED FROM PYTHON SOURCE LINES 40-43

.. code-block:: default

    optimized_params = nni.get_next_parameter()
    params.update(optimized_params)





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

 Out:

 .. code-block:: none

    /home/lz/code/nnisrc/nni/runtime/platform/standalone.py:32: RuntimeWarning: Running NNI code without runtime. Check the following tutorial if you are new to NNI: https://nni.readthedocs.io/en/stable/Tutorial/QuickStart.html#id1
      warnings.warn(warning_message, RuntimeWarning)




.. GENERATED FROM PYTHON SOURCE LINES 44-46

Load dataset
------------

.. GENERATED FROM PYTHON SOURCE LINES 46-51

.. code-block:: default

    mnist = tf.keras.datasets.mnist

    (x_train, y_train), (x_test, y_test) = mnist.load_data()
    x_train, x_test = x_train / 255.0, x_test / 255.0








.. GENERATED FROM PYTHON SOURCE LINES 52-54

Build model with hyperparameters
--------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 54-67

.. code-block:: default

    model = tf.keras.models.Sequential([
        tf.keras.layers.Flatten(input_shape=(28, 28)),
        tf.keras.layers.Dense(params['dense_units'], activation=params['activation_type']),
        tf.keras.layers.Dropout(params['dropout_rate']),
        tf.keras.layers.Dense(10)
    ])

    adam = tf.keras.optimizers.Adam(learning_rate=params['learning_rate'])

    loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)

    model.compile(optimizer=adam, loss=loss_fn, metrics=['accuracy'])








.. GENERATED FROM PYTHON SOURCE LINES 68-74

(Optional) Report intermediate results
--------------------------------------
The callback reports per-epoch accuracy to show learning curve in NNI web portal.
And in :doc:`/hpo/assessors`, you will see how to leverage the metrics for early stopping.

You can safely skip this and the experiment will work fine.

.. GENERATED FROM PYTHON SOURCE LINES 74-78

.. code-block:: default

    callback = tf.keras.callbacks.LambdaCallback(
        on_epoch_end = lambda epoch, logs: nni.report_intermediate_result(logs['accuracy'])
    )








.. GENERATED FROM PYTHON SOURCE LINES 79-81

Train and evluate the model
---------------------------

.. GENERATED FROM PYTHON SOURCE LINES 81-84

.. code-block:: default

    model.fit(x_train, y_train, epochs=5, verbose=2, callbacks=[callback])
    loss, accuracy = model.evaluate(x_test, y_test, verbose=2)





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

 Out:

 .. code-block:: none

    Epoch 1/5
    [2022-03-07 02:37:35] INFO (nni/MainThread) Intermediate result: 0.9145833253860474  (Index 0)
    1875/1875 - 12s - loss: 0.2940 - accuracy: 0.9146 - 12s/epoch - 6ms/step
    Epoch 2/5
    [2022-03-07 02:37:41] INFO (nni/MainThread) Intermediate result: 0.9573833346366882  (Index 1)
    1875/1875 - 5s - loss: 0.1422 - accuracy: 0.9574 - 5s/epoch - 3ms/step
    Epoch 3/5
    [2022-03-07 02:37:49] INFO (nni/MainThread) Intermediate result: 0.967283308506012  (Index 2)
    1875/1875 - 8s - loss: 0.1075 - accuracy: 0.9673 - 8s/epoch - 4ms/step
    Epoch 4/5
    [2022-03-07 02:37:57] INFO (nni/MainThread) Intermediate result: 0.9723333120346069  (Index 3)
    1875/1875 - 8s - loss: 0.0885 - accuracy: 0.9723 - 8s/epoch - 4ms/step
    Epoch 5/5
    [2022-03-07 02:38:06] INFO (nni/MainThread) Intermediate result: 0.9762333035469055  (Index 4)
    1875/1875 - 9s - loss: 0.0747 - accuracy: 0.9762 - 9s/epoch - 5ms/step
    313/313 - 1s - loss: 0.0766 - accuracy: 0.9772 - 647ms/epoch - 2ms/step




.. GENERATED FROM PYTHON SOURCE LINES 85-88

Report final result
-------------------
Report final accuracy to NNI so the tuning algorithm can predict best hyperparameters.

.. GENERATED FROM PYTHON SOURCE LINES 88-89

.. code-block:: default

    nni.report_final_result(accuracy)




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

 Out:

 .. code-block:: none

    [2022-03-07 02:38:06] INFO (nni/MainThread) Final result: 0.9771999716758728





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

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


.. _sphx_glr_download_tutorials_hpo_quickstart_tensorflow_model.py:


.. only :: html

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



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

     :download:`Download Python source code: model.py <model.py>`



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

     :download:`Download Jupyter notebook: model.ipynb <model.ipynb>`


.. only:: html

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

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