|
1 | | -Quick Start |
2 | | -=========== |
| 1 | +# 快速开始 |
3 | 2 |
|
4 | | -HyperParameter is a configuration and parameter management library for Python. HyperParameter provides the following features: |
| 3 | +`HyperParameter` 是一个配置参数管理框架,为 Python 应用提供超参配置与参数调优等功能。可通过如下命令快速安装: |
5 | 4 |
|
6 | | -1. `HyperParameter` class, providing object-style api for accessing dict data and handling undefine parameters with default values; |
7 | | -2. `param_scope` context manager, maintains a thread-safe `HyperParameter` and manage parameter modifications with nested `param_scope`; |
8 | | -3. `auto_param` decorator, allowing default parameters of a function or class to be supplied from `param_scope`; |
9 | | - |
10 | | -HyperParameter is particularly well suited for machine learning experiments and related systems, which have many parameters and nested codes. |
| 5 | +```shell |
| 6 | +pip install hyperparameter |
| 7 | +``` |
11 | 8 |
|
12 | | -`HyperParameter` |
13 | | ----------------- |
| 9 | +主要特性: |
14 | 10 |
|
15 | | -### create hyperparameter |
| 11 | +1. `param_scope` 上下文,向 Python 应用提供线程安全的、可嵌套的参数管理上下文;提供对象化的树状参数管理,并支持默认值; |
16 | 12 |
|
17 | 13 | ```python |
18 | | ->>> from hyperparameter import HyperParameter |
19 | | ->>> hp = HyperParameter(param1=1, param2="A") |
20 | | ->>> hp |
21 | | -{'param1': 1, 'param2': 'A'} |
| 14 | +>>> from hyperparameter import param_scope |
| 15 | +>>> with param_scope(param1=1) as ps: |
| 16 | +... print(f"param1={ps.param1()}, param2={ps.param2('undefined')}") |
| 17 | +param1=1, param2=undefined |
22 | 18 |
|
23 | 19 | ``` |
24 | 20 |
|
25 | | -### access parameters in hyperparameter |
26 | | - |
27 | | -we can access the parameter stored in HyperParameter with object-style API: |
| 21 | +2. `auto_param` 装饰器,自动将函数(或者 class)的默认参数转化为超参,并接受`param_scope`的参数控制; |
28 | 22 |
|
29 | 23 | ```python |
30 | | ->>> hp.param1 |
31 | | -1 |
32 | | - |
33 | | ->>> hp.param2 |
34 | | -'A' |
35 | | - |
36 | | -``` |
| 24 | +>>> from hyperparameter import auto_param, param_scope |
| 25 | +>>> @auto_param |
| 26 | +... def foo(a, b="default"): |
| 27 | +... print(f"a={a}, b={b}") |
37 | 28 |
|
38 | | -nested parameters are also supported: |
| 29 | +>>> foo(0) |
| 30 | +a=0, b=default |
39 | 31 |
|
40 | | -```python |
41 | | ->>> hp = HyperParameter(param1=1, obj2={"prop3": 3}) |
42 | | ->>> hp.obj2.prop3 |
43 | | -3 |
| 32 | +>>> with param_scope(**{"foo.b": "modified"}): |
| 33 | +... foo(0) |
| 34 | +a=0, b=modified |
44 | 35 |
|
45 | 36 | ``` |
46 | 37 |
|
47 | | -### access undefined parameter |
| 38 | +## 超参配置 |
48 | 39 |
|
49 | | -Undefined parameter is treated as false in `if` statement: |
| 40 | +1. 通过`param_scope`可以直接读取超参配置,而无需任何配置: |
50 | 41 |
|
51 | 42 | ```python |
52 | | ->>> if not hp.undefined_param: print("param not defined") |
53 | | -param not defined |
| 43 | +>>> from hyperparameter import param_scope |
| 44 | +>>> def foo(): |
| 45 | +... # read parameter from param_scope |
| 46 | +... p = param_scope.param("default") |
| 47 | +... p2 = param_scope.namespace.param2("default2") |
| 48 | +... print(f"p={p}, p2={p2}") |
54 | 49 |
|
55 | 50 | ``` |
56 | 51 |
|
57 | | -The hyperparameter object is a `Callable`, and will be turned into `safe-mode` when called. In safe-mode the users must provide a default value when accessing parameters. |
| 52 | +在上述函数`foo`中,尝试访问名为`param`的超参,超参默认值为`default`。`param_scope`首先尝试从上下文中读取同名参数并返回给调用者,若超参未定义则返回默认值。为了更好的组织参数,也可以给参数名添加命名空间`namespace.param`。命名空间也支持嵌套多层,比如`namespace.subspace.param`。 |
58 | 53 |
|
59 | | -```python |
60 | | ->>> hp().undefined_param("default_value") |
61 | | -'default_value' |
| 54 | +2. 通过`param_scope`传递超参 |
62 | 55 |
|
63 | | -``` |
| 56 | +```python |
| 57 | +# call `foo` with default parameter |
| 58 | +>>> foo() |
| 59 | +p=default, p2=default2 |
64 | 60 |
|
65 | | -`param_scope` |
66 | | -------------- |
| 61 | +# call `foo` with modified parameter |
| 62 | +>>> with param_scope("namespace.param2=modified"): |
| 63 | +... foo() |
| 64 | +p=default, p2=modified |
67 | 65 |
|
68 | | -### manage hyper-parameters with `param_scope` |
| 66 | +``` |
69 | 67 |
|
70 | | -`param_scope` create a `HyperParameter` object for the context it manages: |
| 68 | +通过`with param_scope(...)`传递参数的时候支持两种语法,字符串语法与字典语法。字典语法如下所示: |
71 | 69 |
|
72 | 70 | ```python |
73 | | ->>> from hyperparameter import param_scope |
74 | | ->>> with param_scope(param1=1, param2="A") as ps: |
75 | | -... ps.param1 |
76 | | -1 |
| 71 | +# call `foo` with modified parameter |
| 72 | +>>> with param_scope(**{ |
| 73 | +... "param": "modified", |
| 74 | +... "namespace": {"param2": "modified2"} |
| 75 | +... }): |
| 76 | +... foo() |
| 77 | +p=modified, p2=modified2 |
77 | 78 |
|
78 | 79 | ``` |
79 | 80 |
|
80 | | -### access hyper-parameter in functions |
| 81 | +字典语法适合配合配置文件使用。 |
81 | 82 |
|
82 | | -`param_scope` is often used to configure the behavior of a function. |
| 83 | +3. `param_scope`可以穿透多层函数调用传递参数: |
83 | 84 |
|
84 | 85 | ```python |
85 | | ->>> def foo(): # access parameter |
86 | | -... ps = param_scope.current() |
87 | | -... return ps.param1 |
88 | | - |
89 | | ->>> def foo_with_default(): # access parameter with default value |
90 | | -... ps = param_scope.current() |
91 | | -... return ps().param1(2) # default value is `2` |
92 | | - |
93 | | ->>> with param_scope(param1=1, param2="A") as ps: |
| 86 | +>>> def bar(): |
94 | 87 | ... foo() |
95 | | -1 |
96 | | - |
97 | | ->>> with param_scope(param1=1, param2="A") as ps: |
98 | | -... foo_with_default() |
99 | | -1 |
100 | 88 |
|
101 | | ->>> with param_scope(param2="A") as ps: |
102 | | -... foo_with_default() |
103 | | -2 |
| 89 | +# call `foo` within nested function call |
| 90 | +>>> with param_scope("namespace.param2=modified"): |
| 91 | +... bar() |
| 92 | +p=default, p2=modified |
104 | 93 |
|
105 | 94 | ``` |
106 | 95 |
|
107 | | -`auto_param` |
108 | | ------------- |
| 96 | +## 自动超参 |
109 | 97 |
|
110 | | -### Define a function with `auto_param` decorator |
111 | | - |
112 | | -`auto_param` provides an easier way to define a configurable function. The configurable parameters and their default values are defined with kwargs if `auto_param` is used. |
| 98 | +1. `auto_param` 可以自动为函数(或者 class)添加超参配置功能 |
113 | 99 |
|
114 | 100 | ```python |
115 | 101 | >>> from hyperparameter import auto_param |
116 | 102 | >>> @auto_param |
117 | | -... def foo(a, b=1, c=2): |
118 | | -... return (a, b, c) |
| 103 | +... def foo(param, param1=1): |
| 104 | +... print(f"param={param}, param1={param1}") |
119 | 105 |
|
120 | 106 | >>> foo(0) |
121 | | -(0, 1, 2) |
| 107 | +param=0, param1=1 |
122 | 108 |
|
123 | 109 | ``` |
124 | 110 |
|
125 | | -### config function default parameter with `param_scope` |
126 | | - |
127 | | -We can config the default behavior of a function with `param_scope`. Since the `param_scope` API is scoped and thread-safe, we can use it without too much warry. |
| 111 | +2. 通过`param_scope`向`auto_param`传递参数: |
128 | 112 |
|
129 | 113 | ```python |
130 | | ->>> with param_scope(**{"foo.b": 0}): |
| 114 | +>>> with param_scope(**{"foo.param1": 0}): |
131 | 115 | ... foo(0) |
132 | | -(0, 0, 2) |
| 116 | +param=0, param1=0 |
133 | 117 |
|
134 | | -``` |
| 118 | +``` |
0 commit comments