"src/vscode:/vscode.git/clone" did not exist on "ac6aee81002701454194fcc088bb5839e22dbea3"
AnnotationSpec.md 4.82 KB
Newer Older
1
# NNI Annotation
Scarlett Li's avatar
Scarlett Li committed
2

Yan Ni's avatar
Yan Ni committed
3
## Overview
Scarlett Li's avatar
Scarlett Li committed
4

5
To improve user experience and reduce user effort, we design an annotation grammar. Using NNI annotation, users can adapt their code to NNI just by adding some standalone annotating strings, which does not affect the execution of the original code.
chicm-ms's avatar
chicm-ms committed
6

Yan Ni's avatar
Yan Ni committed
7
Below is an example:
Scarlett Li's avatar
Scarlett Li committed
8

Yan Ni's avatar
Yan Ni committed
9
10
11
```python
'''@nni.variable(nni.choice(0.1, 0.01, 0.001), name=learning_rate)'''
learning_rate = 0.1
12

Yan Ni's avatar
Yan Ni committed
13
14
```
The meaning of this example is that NNI will choose one of several values (0.1, 0.01, 0.001) to assign to the learning_rate variable. Specifically, this first line is an NNI annotation, which is a single string. Following is an assignment statement. What nni does here is to replace the right value of this assignment statement according to the information provided by the annotation line.
Scarlett Li's avatar
Scarlett Li committed
15
16


Yan Ni's avatar
Yan Ni committed
17
In this way, users could either run the python code directly or launch NNI to tune hyper-parameter in this code, without changing any codes.
Scarlett Li's avatar
Scarlett Li committed
18

Yan Ni's avatar
Yan Ni committed
19
## Types of Annotation:
Scarlett Li's avatar
Scarlett Li committed
20

Yan Ni's avatar
Yan Ni committed
21
In NNI, there are mainly four types of annotation:
Scarlett Li's avatar
Scarlett Li committed
22
23


Yan Ni's avatar
Yan Ni committed
24
### 1. Annotate variables
Scarlett Li's avatar
Scarlett Li committed
25

Yan Ni's avatar
Yan Ni committed
26
   `'''@nni.variable(sampling_algo, name)'''`
Scarlett Li's avatar
Scarlett Li committed
27

Yan Ni's avatar
Yan Ni committed
28
`@nni.variable` is used in NNI to annotate a variable.
Scarlett Li's avatar
Scarlett Li committed
29

Yan Ni's avatar
Yan Ni committed
30
**Arguments**
Scarlett Li's avatar
Scarlett Li committed
31

32
- **sampling_algo**: Sampling algorithm that specifies a search space. User should replace it with a built-in NNI sampling function whose name consists of an `nni.` identification and a search space type specified in [SearchSpaceSpec](SearchSpaceSpec.md) such as `choice` or `uniform`.
Yan Ni's avatar
Yan Ni committed
33
- **name**: The name of the variable that the selected value will be assigned to. Note that this argument should be the same as the left value of the following assignment statement.
Scarlett Li's avatar
Scarlett Li committed
34

35
36
37
38
There are 10 types to express your search space as follows:

* `@nni.variable(nni.choice(option1,option2,...,optionN),name=variable)`
  Which means the variable value is one of the options, which should be a list The elements of options can themselves be stochastic expressions
39
40
* `@nni.variable(nni.randint(lower, upper),name=variable)`
  Which means the variable value is a value like round(uniform(low, high)). For now, the type of chosen value is float. If you want to use integer value, please convert it explicitly.
41
42
43
* `@nni.variable(nni.uniform(low, high),name=variable)`
  Which means the variable value is a value uniformly between low and high.
* `@nni.variable(nni.quniform(low, high, q),name=variable)`
44
  Which means the variable value is a value like clip(round(uniform(low, high) / q) * q, low, high), where the clip operation is used to constraint the generated value in the bound.
45
46
47
* `@nni.variable(nni.loguniform(low, high),name=variable)`
  Which means the variable value is a value drawn according to exp(uniform(low, high)) so that the logarithm of the return value is uniformly distributed.
* `@nni.variable(nni.qloguniform(low, high, q),name=variable)`
48
  Which means the variable value is a value like clip(round(loguniform(low, high) / q) * q, low, high), where the clip operation is used to constraint the generated value in the bound.
49
50
51
52
53
54
55
56
57
58
* `@nni.variable(nni.normal(mu, sigma),name=variable)`
  Which means the variable value is a real value that's normally-distributed with mean mu and standard deviation sigma.
* `@nni.variable(nni.qnormal(mu, sigma, q),name=variable)`
  Which means the variable value is a value like round(normal(mu, sigma) / q) * q
* `@nni.variable(nni.lognormal(mu, sigma),name=variable)`
  Which means the variable value is a value drawn according to exp(normal(mu, sigma))
* `@nni.variable(nni.qlognormal(mu, sigma, q),name=variable)`
  Which means the variable value is a value like round(exp(normal(mu, sigma)) / q) * q

Below is an example:
Scarlett Li's avatar
Scarlett Li committed
59

Yan Ni's avatar
Yan Ni committed
60
61
62
63
```python
'''@nni.variable(nni.choice(0.1, 0.01, 0.001), name=learning_rate)'''
learning_rate = 0.1
```
Scarlett Li's avatar
Scarlett Li committed
64

Yan Ni's avatar
Yan Ni committed
65
### 2. Annotate functions
Scarlett Li's avatar
Scarlett Li committed
66

Yan Ni's avatar
Yan Ni committed
67
   `'''@nni.function_choice(*functions, name)'''`
Scarlett Li's avatar
Scarlett Li committed
68

Yan Ni's avatar
Yan Ni committed
69
`@nni.function_choice` is used to choose one from several functions.
Scarlett Li's avatar
Scarlett Li committed
70

Yan Ni's avatar
Yan Ni committed
71
**Arguments**
Scarlett Li's avatar
Scarlett Li committed
72

73
- **functions**: Several functions that are waiting to be selected from. Note that it should be a complete function call with arguments. Such as `max_pool(hidden_layer, pool_size)`.
Yan Ni's avatar
Yan Ni committed
74
- **name**: The name of the function that will be replaced in the following assignment statement.
Scarlett Li's avatar
Scarlett Li committed
75

Yan Ni's avatar
Yan Ni committed
76
77
78
79
80
81
82
83
84
85
86
An example here is:

```python
"""@nni.function_choice(max_pool(hidden_layer, pool_size), avg_pool(hidden_layer, pool_size), name=max_pool)"""
h_pooling = max_pool(hidden_layer, pool_size)
```

### 3. Annotate intermediate result

   `'''@nni.report_intermediate_result(metrics)'''`

zhangyuge's avatar
zhangyuge committed
87
`@nni.report_intermediate_result` is used to report intermediate result, whose usage is the same as `nni.report_intermediate_result` in the doc of [Write a trial run on NNI](../TrialExample/Trials.md)
Yan Ni's avatar
Yan Ni committed
88
89
90
91
92

### 4. Annotate final result

   `'''@nni.report_final_result(metrics)'''`

zhangyuge's avatar
zhangyuge committed
93
`@nni.report_final_result` is used to report the final result of the current trial, whose usage is the same as `nni.report_final_result` in the doc of [Write a trial run on NNI](../TrialExample/Trials.md)