You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+73-66Lines changed: 73 additions & 66 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
**H**_yper_**P**_arameter_
1
+
Hyperparameter
2
2
===========================
3
3
4
4
<h3align="center">
@@ -7,88 +7,99 @@
7
7
</p>
8
8
</h3>
9
9
10
-
HyperParameter is a pythonic configuration framework designed to simplify the massive configuration in complex applications. The key feature is a dynamic hierarchical parameter tree composited with scopes. HyperParameter is particularly well suited for machine learning experiments and related systems, which have many parameters and nested codes.
10
+
<palign="center">
11
11
12
-
Key Conceptions
13
-
---------------
14
-
15
-
1.`parameter tree`, a nested python dict with support for default values and object-style API;
16
-
1.`param_scope`, a context manager for compositing the ` parameter tree` with nested `param_scope`;
17
-
2.`auto_param`, a decorator allowing default parameters of a function or class to be supplied from `param_scope`;
12
+
**Hyperparameter, Make configurable AI applications.Build for Python hackers.**
18
13
14
+
</p>
19
15
20
16
Quick Start
21
17
-----------
22
18
23
-
A quick example for defining a model with HyperParameter:
19
+
`Hyperparameter` uses `auto _ param` decorator to convert keywords arguments into configurable parameters:
24
20
25
21
```python
26
-
@auto_param
27
-
defdnn(input, layers=3, activation="relu"):
28
-
"""
29
-
build a DNN model with the following configurations:
30
-
- dnn.layers(default: 3)
31
-
- dnn.activation(default: "relu")
32
-
"""
33
-
for i inrange(layers):
34
-
input= Linear(input)
35
-
input= activation_fn(
36
-
activation,
37
-
input
38
-
)
39
-
returninput
40
-
41
-
# call dnn with default configuration
42
-
# and create a 3 layer dnn with relu activation
43
-
dnn(x)
44
-
45
-
# passing parameter using param_scope
46
-
with param_scope(**{
47
-
"dnn.layers": 4,
48
-
"dnn.activation": "sigmoid"}):
49
-
# create a 4 layer dnn with sigmoid activation
50
-
dnn()
22
+
from hyperparameter import auto_param
23
+
24
+
@auto_param("foo")
25
+
deffoo(x, y=1, z="a"):
26
+
returnf"x={x}, y={y}, z={z}"
51
27
```
52
28
53
-
Another example for building ML system:
29
+
The parameters can be controlled with `param_scope`
54
30
55
31
```python
56
-
@auto_param
57
-
definference(x, backend="tvm"):
58
-
...
32
+
from hyperparameter import param_scope
59
33
60
-
with param_scope(backend="onnx"):
61
-
inference(x)
34
+
foo(1) # x=1, y=1, z='a'
35
+
with param_scope(**{"foo.y":2}):
36
+
foo(1) # x=1, y=2, z='a'
62
37
```
63
38
64
39
Advanced Usage
65
40
--------------
66
-
### Nested Scope and Configuration Composition
67
41
68
-
HyperParameter uses nested `param_scope` for configuration composition :
42
+
### Read/Write Parameters
69
43
70
-
```python
44
+
```python
71
45
from hyperparameter import param_scope
72
-
# on initialization, the parameter tree is empty: {}
73
-
with param_scope(a=1) as ps:
74
-
# in the with context, the composited parameter tree is {"a": 1}
75
-
ps == {"a": 1}
76
-
with param_scope(a=2, b=3) as ps2:
77
-
# in the nested scope, the composited parameter tree is {"a": 2, "b": 3}
78
-
# param `b` is a new, and param `a` is overwrite by new value
79
-
ps2 == {"a": 2, "b": 3}
80
-
# when exit the inner scope, the modification of inner scope is cleaned up
81
-
# the composited parameter tree is {"a": 1}
82
-
ps == {"a": 1}
46
+
47
+
# create param_scope
48
+
with param_scope():
49
+
pass
50
+
51
+
with param_scope("foo.y=1", "foo.z=b"):
52
+
pass
53
+
54
+
with param_scope(**{"foo.y":1, "foo.z":2}):
55
+
pass
56
+
57
+
# read param with default value
58
+
with param_scope(**{"foo.y":2}) as ps:
59
+
y = ps.foo.y(1)
60
+
y = ps.foo.y |1
61
+
y = param_scope.foo.y(1)
62
+
y = param_scope.foo.y |1
63
+
foo(1) # x=1, y=2, z='a'
64
+
65
+
# wite values to param_scope
66
+
with param_scope(**{"foo.y":2}) as ps:
67
+
ps.foo.y =2
68
+
param_scope.foo.y =2
83
69
```
84
70
85
-
### Manage Parameters from CMD Line
71
+
### Nested Scope
86
72
87
-
It is recommended to use three-layer configuration for complex programmes:
73
+
`Hyperparameter` support nested `param_scope`:
88
74
89
-
1.`inline default values`;
90
-
2.`config file`, which will override `inline default values`;
91
-
3.`cmdline arguments` that override both `config file` and `inline default values`;
75
+
```python
76
+
from hyperparameter import param_scope
77
+
78
+
# no param_scope, use the default value defined in foo
79
+
foo(1) # x=1, y=1, z='a'
80
+
81
+
# start a new param_scope
82
+
# and set the default value of `foo.y` to `2`
83
+
with param_scope(**{"foo.y":2}) as ps:
84
+
# found one param_scope `ps`,
85
+
# and receive default value of `foo.y` from `ps`
86
+
foo(1) # x=1, y=2, z='a'
87
+
88
+
# start another param_scope
89
+
# and set the default value of `foo.y` to `3`
90
+
with param_scope(**{"foo.z": "b"}) as ps2:
91
+
# found nested param_scope `ps2`,
92
+
# and receive default values of `foo.z` from `ps2`
93
+
foo(1) # x=1, y=2, z='b'
94
+
# `ps2` ends here, and `foo.y` is restored to `2`
95
+
foo(1) # x=1, y=2, z='a'
96
+
# `ps` ends here, and `foo.y` is restored to `1`
97
+
foo(1) # x=1, y=1, z='a'
98
+
```
99
+
100
+
### CMD Line Arguments
101
+
102
+
An example CLI app:
92
103
93
104
```python
94
105
from hyperparameter import param_scope, auto_param
0 commit comments