howto_2_CustomizedTuner.md 4.41 KB
Newer Older
1
# **How To** - Customize Your Own Tuner
QuanluZhang's avatar
QuanluZhang committed
2
3
4
5
6

*Tuner receive result from Trial as a matric to evaluate the performance of a specific parameters/architecture configure. And tuner send next hyper-parameter or architecture configure to Trial.*

So, if user want to implement a customized Tuner, she/he only need to:

Chi Song's avatar
Chi Song committed
7
8
1. Inherit a tuner of a base Tuner class
1. Implement receive_trial_result and generate_parameter function
9
1. Configure your customized tuner in experiment YAML config file
QuanluZhang's avatar
QuanluZhang committed
10

Chi Song's avatar
Chi Song committed
11
Here is an example:
QuanluZhang's avatar
QuanluZhang committed
12
13

**1) Inherit a tuner of a base Tuner class**
Chi Song's avatar
Chi Song committed
14

QuanluZhang's avatar
QuanluZhang committed
15
16
17
18
19
20
21
22
23
```python
from nni.tuner import Tuner

class CustomizedTuner(Tuner):
    def __init__(self, ...):
        ...
```

**2) Implement receive_trial_result and generate_parameter function**
Chi Song's avatar
Chi Song committed
24

QuanluZhang's avatar
QuanluZhang committed
25
26
27
28
29
30
```python
from nni.tuner import Tuner

class CustomizedTuner(Tuner):
    def __init__(self, ...):
        ...
Chi Song's avatar
Chi Song committed
31

32
    def receive_trial_result(self, parameter_id, parameters, value):
QuanluZhang's avatar
QuanluZhang committed
33
34
35
36
    '''
    Record an observation of the objective function and Train
    parameter_id: int
    parameters: object created by 'generate_parameters()'
37
    value: final metrics of the trial, including reward
QuanluZhang's avatar
QuanluZhang committed
38
39
40
    '''
    # your code implements here.
    ...
Chi Song's avatar
Chi Song committed
41

QuanluZhang's avatar
QuanluZhang committed
42
43
44
45
46
47
48
49
50
51
    def generate_parameters(self, parameter_id):
    '''
    Returns a set of trial (hyper-)parameters, as a serializable object
    parameter_id: int
    '''
    # your code implements here.
    return your_parameters
    ...
```

Chi Song's avatar
Chi Song committed
52
53
54
`receive_trial_result` will receive the `parameter_id, parameters, value` as parameters input. Also, Tuner will receive the `value` object are exactly same value that Trial send.

The `your_parameters` return from `generate_parameters` function, will be package as json object by NNI SDK. NNI SDK will unpack json object so the Trial will receive the exact same `your_parameters` from Tuner.
QuanluZhang's avatar
QuanluZhang committed
55
56

For example:
Chi Song's avatar
Chi Song committed
57
58
If the you implement the `generate_parameters` like this:

QuanluZhang's avatar
QuanluZhang committed
59
60
61
62
63
64
65
66
67
```python
    def generate_parameters(self, parameter_id):
        '''
        Returns a set of trial (hyper-)parameters, as a serializable object
        parameter_id: int
        '''
        # your code implements here.
        return {"dropout": 0.3, "learning_rate": 0.4}
```
Chi Song's avatar
Chi Song committed
68
69
70
71

It means your Tuner will always generate parameters `{"dropout": 0.3, "learning_rate": 0.4}`. Then Trial will receive `{"dropout": 0.3, "learning_rate": 0.4}` by calling API `nni.get_next_parameter()`. Once the trial ends with a result (normally some kind of metrics), it can send the result to Tuner by calling API `nni.report_final_result()`, for example `nni.report_final_result(0.93)`. Then your Tuner's `receive_trial_result` function will receied the result like:

```python
QuanluZhang's avatar
QuanluZhang committed
72
73
parameter_id = 82347
parameters = {"dropout": 0.3, "learning_rate": 0.4}
74
value = 0.93
QuanluZhang's avatar
QuanluZhang committed
75
76
```

Chi Song's avatar
Chi Song committed
77
78
79
**Note that** if you want to access a file (e.g., `data.txt`) in the directory of your own tuner, you cannot use `open('data.txt', 'r')`. Instead, you should use the following:

```python
QuanluZhang's avatar
QuanluZhang committed
80
81
82
_pwd = os.path.dirname(__file__)
_fd = open(os.path.join(_pwd, 'data.txt'), 'r')
```
Chi Song's avatar
Chi Song committed
83
84

This is because your tuner is not executed in the directory of your tuner (i.e., `pwd` is not the directory of your own tuner).
QuanluZhang's avatar
QuanluZhang committed
85

86
**3) Configure your customized tuner in experiment YAML config file**
QuanluZhang's avatar
QuanluZhang committed
87
88

NNI needs to locate your customized tuner class and instantiate the class, so you need to specify the location of the customized tuner class and pass literal values as parameters to the \_\_init__ constructor.
Chi Song's avatar
Chi Song committed
89

90
```yml
QuanluZhang's avatar
QuanluZhang committed
91
92
93
94
95
96
97
98
99
100
101
tuner:
  codeDir: /home/abc/mytuner
  classFileName: my_customized_tuner.py
  className: CustomizedTuner
  # Any parameter need to pass to your tuner class __init__ constructor
  # can be specified in this optional classArgs field, for example 
  classArgs:
    arg1: value1
```

More detail example you could see:
Chi Song's avatar
Chi Song committed
102

QuanluZhang's avatar
QuanluZhang committed
103
104
105
> * [evolution-tuner](../src/sdk/pynni/nni/evolution_tuner)
> * [hyperopt-tuner](../src/sdk/pynni/nni/hyperopt_tuner)
> * [evolution-based-customized-tuner](../examples/tuners/ga_customer_tuner)
QuanluZhang's avatar
QuanluZhang committed
106
107

## Write a more advanced automl algorithm
Chi Song's avatar
Chi Song committed
108
109

The information above are usually enough to write a general tuner. However, users may also want more information, for example, intermediate results, trials' state (e.g., the information in assessor), in order to have a more powerful automl algorithm. Therefore, we have another concept called `advisor` which directly inherits from `MsgDispatcherBase` in [`src/sdk/pynni/nni/msg_dispatcher_base.py`](../src/sdk/pynni/nni/msg_dispatcher_base.py). Please refer to [here](./howto_3_CustomizedAdvisor.md) for how to write a customized advisor.