Skip to content

Commit 7a9da8b

Browse files
committed
update readme
1 parent 00e1fa5 commit 7a9da8b

File tree

2 files changed

+65
-81
lines changed

2 files changed

+65
-81
lines changed

docs/quick_start.md

Lines changed: 64 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,134 +1,118 @@
1-
Quick Start
2-
===========
1+
# 快速开始
32

4-
HyperParameter is a configuration and parameter management library for Python. HyperParameter provides the following features:
3+
`HyperParameter` 是一个配置参数管理框架,为 Python 应用提供超参配置与参数调优等功能。可通过如下命令快速安装:
54

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+
```
118

12-
`HyperParameter`
13-
----------------
9+
主要特性:
1410

15-
### create hyperparameter
11+
1. `param_scope` 上下文,向 Python 应用提供线程安全的、可嵌套的参数管理上下文;提供对象化的树状参数管理,并支持默认值;
1612

1713
```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
2218

2319
```
2420

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`的参数控制;
2822

2923
```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}")
3728

38-
nested parameters are also supported:
29+
>>> foo(0)
30+
a=0, b=default
3931

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
4435

4536
```
4637

47-
### access undefined parameter
38+
## 超参配置
4839

49-
Undefined parameter is treated as false in `if` statement:
40+
1. 通过`param_scope`可以直接读取超参配置,而无需任何配置:
5041

5142
```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}")
5449

5550
```
5651

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`
5853

59-
```python
60-
>>> hp().undefined_param("default_value")
61-
'default_value'
54+
2. 通过`param_scope`传递超参
6255

63-
```
56+
```python
57+
# call `foo` with default parameter
58+
>>> foo()
59+
p=default, p2=default2
6460

65-
`param_scope`
66-
-------------
61+
# call `foo` with modified parameter
62+
>>> with param_scope("namespace.param2=modified"):
63+
... foo()
64+
p=default, p2=modified
6765

68-
### manage hyper-parameters with `param_scope`
66+
```
6967

70-
`param_scope` create a `HyperParameter` object for the context it manages:
68+
通过`with param_scope(...)`传递参数的时候支持两种语法,字符串语法与字典语法。字典语法如下所示:
7169

7270
```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
7778

7879
```
7980

80-
### access hyper-parameter in functions
81+
字典语法适合配合配置文件使用。
8182

82-
`param_scope` is often used to configure the behavior of a function.
83+
3. `param_scope`可以穿透多层函数调用传递参数:
8384

8485
```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():
9487
... foo()
95-
1
96-
97-
>>> with param_scope(param1=1, param2="A") as ps:
98-
... foo_with_default()
99-
1
10088

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
10493

10594
```
10695

107-
`auto_param`
108-
------------
96+
## 自动超参
10997

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)添加超参配置功能
11399

114100
```python
115101
>>> from hyperparameter import auto_param
116102
>>> @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}")
119105

120106
>>> foo(0)
121-
(0, 1, 2)
107+
param=0, param1=1
122108

123109
```
124110

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`传递参数:
128112

129113
```python
130-
>>> with param_scope(**{"foo.b": 0}):
114+
>>> with param_scope(**{"foo.param1": 0}):
131115
... foo(0)
132-
(0, 0, 2)
116+
param=0, param1=0
133117

134-
```
118+
```

docs/quick_start.zh.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pip install hyperparameter
1313
```python
1414
>>> from hyperparameter import param_scope
1515
>>> with param_scope(param1=1) as ps:
16-
... print(f"param1={ps.param1}, param2={ps.param2('undefined')}")
16+
... print(f"param1={ps.param1()}, param2={ps.param2('undefined')}")
1717
param1=1, param2=undefined
1818

1919
```

0 commit comments

Comments
 (0)