HowToLaunchFromPython.rst 4.69 KB
Newer Older
1
**How to Launch an Experiment from Python**
2
3
===========================================

4
5
6
7
8
9
..  toctree::
    :hidden:

    Start Usage <python_api_start>
    Connect Usage <python_api_connect>

10
11
12
13
Overview
--------
Since ``nni v2.0``, we provide a new way to launch experiments. Before that, you need to configure the experiment in the yaml configuration file and then use the experiment ``nnictl`` command to launch the experiment. Now, you can also configure and run experiments directly in python file. If you are familiar with python programming, this will undoubtedly bring you more convenience.

14
15
Run a New Experiment
--------------------
16
After successfully installing ``nni``, you can start the experiment with a python script in the following 2 steps.
17
18
19

..

20
    Step 1 - Initialize an experiment instance and configure it
21
22
23

.. code-block:: python

24
25
    from nni.experiment import Experiment
    experiment = Experiment('local')
26

27
Now, you have a ``Experiment`` instance, and this experiment will launch trials on your local machine due to ``training_service='local'``.
28
29
30
31
32

See all `training services <../training_services.rst>`__ supported in NNI.

.. code-block:: python

33
    experiment.config.experiment_name = 'MNIST example'
34
    experiment.config.trial_concurrency = 2
35
    experiment.config.max_trial_number = 10
36
37
38
    experiment.config.search_space = search_space
    experiment.config.trial_command = 'python3 mnist.py'
    experiment.config.trial_code_directory = Path(__file__).parent
39
40
    experiment.config.tuner.name = 'TPE'
    experiment.config.tuner.class_args['optimize_mode'] = 'maximize'
41
42
43
44
    experiment.config.training_service.use_active_gpu = True

Use the form like ``experiment.config.foo = 'bar'`` to configure your experiment.

45
46
See all real `builtin tuners <../builtin_tuner.rst>`__ supported in NNI.

47
48
49
50
See `parameter configuration <../reference/experiment_config.rst>`__ required by different training services.

..

51
    Step 2 - Just run
52
53
54

.. code-block:: python

55
    experiment.run(port=8080)
56

57
Now, you have successfully launched an NNI experiment. And you can type ``localhost:8080`` in your browser to observe your experiment in real time.
58

59
.. Note:: In this way, experiment will run in the foreground and will automatically exit when the experiment finished. If you want to run an experiment in an interactive way, use ``start()`` in Step 2. 
60

61
Example
62
63
^^^^^^^
Below is an example for this new launching approach. You can also find this code in :githublink:`mnist-tfv2/launch.py <examples/trials/mnist-tfv2/launch.py>`.
64
65
66
67
68

.. code-block:: python

    from pathlib import Path

69
    from nni.experiment import Experiment
70
71
72
73
74
75
76
77
78

    search_space = {
        "dropout_rate": { "_type": "uniform", "_value": [0.5, 0.9] },
        "conv_size": { "_type": "choice", "_value": [2, 3, 5, 7] },
        "hidden_size": { "_type": "choice", "_value": [124, 512, 1024] },
        "batch_size": { "_type": "choice", "_value": [16, 32] },
        "learning_rate": { "_type": "choice", "_value": [0.0001, 0.001, 0.01, 0.1] }
    }

79
80
    experiment = Experiment('local')
    experiment.config.experiment_name = 'MNIST example'
81
    experiment.config.trial_concurrency = 2
82
    experiment.config.max_trial_number = 10
83
84
85
    experiment.config.search_space = search_space
    experiment.config.trial_command = 'python3 mnist.py'
    experiment.config.trial_code_directory = Path(__file__).parent
86
87
    experiment.config.tuner.name = 'TPE'
    experiment.config.tuner.class_args['optimize_mode'] = 'maximize'
88
89
    experiment.config.training_service.use_active_gpu = True

90
    experiment.run(8080)
91

92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
Start and Manage a New Experiment
---------------------------------
We migrate the API in ``NNI Client`` to this new launching approach.
Launch the experiment by ``start()`` instead of ``run()``, then you can use these APIs in interactive mode.

Please refer to `example usage <./python_api_start.rst>`__ and code file :githublink:`python_api_start.ipynb <examples/trials/sklearn/classification/python_api_start.ipynb>`.

.. Note:: ``run()`` polls the experiment status and will automatically call ``stop()`` when the experiment finished. ``start()`` just launched a new experiment, so you need to manually stop the experiment by calling ``stop()``.

Connect and Manage an Exist Experiment
--------------------------------------
If you launch the experiment by ``nnictl`` and also want to use these APIs, you can use ``Experiment.connect()`` to connect to an existing experiment.

Please refer to `example usage <./python_api_connect.rst>`__ and code file :githublink:`python_api_connect.ipynb <examples/trials/sklearn/classification/python_api_connect.ipynb>`.

.. Note:: You can use ``stop()`` to stop the experiment when connecting to an existing experiment.

109
110
111
112
113
API
---

..  autoclass:: nni.experiment.Experiment
    :members: