Skip to content

Commit b94bc52

Browse files
committed
add chinese readme
Signed-off-by: reiase <[email protected]>
1 parent c3f65f8 commit b94bc52

File tree

5 files changed

+176
-0
lines changed

5 files changed

+176
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
**H**_yper_**P**_arameter_
22
===========================
33

4+
<h3 align="center">
5+
<p style="text-align: center;">
6+
<a href="README.md" target="_blank">ENGLISH</a> | <a href="README.zh.md">中文文档</a>
7+
</p>
8+
</h3>
9+
410
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.
511

612
Key Conceptions

README.zh.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
**H**_yper_**P**_arameter_
2+
===========================
3+
4+
<h3 align="center">
5+
<p style="text-align: center;">
6+
<a href="README.md" target="_blank">ENGLISH</a> | <a href="README.zh.md">中文文档</a>
7+
</p>
8+
</h3>
9+
10+
HyperParameter 是轻量级python代码配置框架,用户仅需对代码添加标记即可实现配置化。特别适用于机器学习模型的参数管理,帮助开发者对接MLOps工具;也适用于大型Python应用的配置管理。
11+
12+
### 一个示例
13+
14+
假设我们开发了一个MLP结构,并使用在某个模型中:
15+
16+
```python
17+
class MLP(nn.Module):
18+
def __init__(self, units=[64, 32, 16], activation="ReLU", use_bn=False):
19+
...
20+
21+
class MyAwesomeModel(nn.Module):
22+
def __init__(self):
23+
self.mlp = MLP()
24+
...
25+
26+
model = MyAwesomeModel()
27+
```
28+
29+
如果我们想尝试修改MLP结构的参数来提升模型效果,通常要修改其代码,或者在MyAwesomeModel中添加额外参数。前者代码难以维护,而后者需要大量冗余代码。
30+
31+
HyperParameter 通过 `auto_param`装饰器自动抽取配置参数:
32+
33+
```python
34+
from hyperparameter import auto_param
35+
36+
@auto_param
37+
class MLP(nn.Module):
38+
def __init__(self, units=[64, 32, 16], activation="ReLU", use_bn=False):
39+
...
40+
```
41+
42+
上述代码将自动生成三个参数,分别对应`MLP.__init__`方法的keyword参数:
43+
44+
1. `MLP.units`,默认值`[64, 32, 16]`;
45+
2. `MLP.activation`,默认值`ReLU`;
46+
3. `MLP.use_bn`,默认值`False`;
47+
48+
三个参数与keyword参数同名,并被放在`MLP`这个namespace下。参数值通过`param_scope`来控制:
49+
50+
```python
51+
from hyperparameter import param_scope
52+
53+
with param_scope(**{"MLP.activation": "sigmoid"}):
54+
model = MyAwesomeModel()
55+
```
56+
57+
高级用法
58+
-------
59+
### 嵌套scope
60+
61+
我们可以通过嵌套`param_scope`来精细控制参数,当退出一个`param_scope`,该scope对参数的修改自动失效:
62+
63+
``` python
64+
from hyperparameter import auto_param, param_scope
65+
66+
@auto_param
67+
def foo(a=1, b=2):
68+
print(f"a={a}, b={b}")
69+
70+
with param_scope(a=3):
71+
foo() # a=3, b=2!
72+
with param_scope(a=2, b=3):
73+
foo() # a=2, b=3!
74+
foo() # a=3, b=2!
75+
```
76+
77+
### 从命令行管理参数
78+
79+
我们通常推荐使用如下三层结构管理参数:
80+
81+
1. 代码的inline默认值,即写在函数或者类定义中的默认值;
82+
2. 配置文件,会覆盖inline默认值;
83+
3. 命令行参数,会覆盖配置文件和inline默认值;
84+
85+
我们推荐的命令脚本示例如下:
86+
87+
```python
88+
from hyperparameter import param_scope, auto_param
89+
90+
@auto_param
91+
def main(a=0, b=1): # inline默认值
92+
print(a, b)
93+
94+
if __name__ == "__main__":
95+
import argparse
96+
import json
97+
parser = argparse.ArgumentParser()
98+
parser.add_argument("--config", type=str, default=None)
99+
parser.add_argument("-D", "--define", nargs="*", default=[], action="extend")
100+
args = parser.parse_args()
101+
102+
if args.config is not None: # 加载配置文件
103+
with open(args.config) as f:
104+
cfg = json.load(f)
105+
else:
106+
cfg = {}
107+
108+
with param_scope(**cfg): # 配置文件的scope
109+
with param_scope(*args.define): # 命令行参数的scope
110+
main()
111+
```
112+
113+
更多示例
114+
-------
115+
116+
### [超参调节](examples/sparse_lr/README.md)
117+
118+
该示例展示了如何使用`hyperparameter`来搭建机器学习项目,并保证复现性。
119+
120+
### [试验跟踪](examples/mnist/README.md)
121+
122+
该例子说明了如何通过`hyperparameter`进行试验管理,并跟踪试验结果。

examples/application/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
1. run the application with default parameter:
2+
3+
```shell
4+
python examples/application/app.py
5+
```
6+
7+
2. run the application with config file
8+
9+
```shell
10+
python examples/application/app.py --config examples/application/cfg.json
11+
```
12+
13+
3. run the application with command line arguments
14+
15+
```shell
16+
python examples/application/app.py -D main.a=1
17+
```

examples/application/app.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from hyperparameter import param_scope, auto_param
2+
3+
4+
@auto_param
5+
def main(a="default a", b="default b"): # inline默认值
6+
print(f"a={a}, b={b}")
7+
with param_scope() as ps:
8+
print(f"params in main = {ps}")
9+
10+
11+
if __name__ == "__main__":
12+
import argparse
13+
import json
14+
15+
parser = argparse.ArgumentParser()
16+
parser.add_argument("--config", type=str, default=None)
17+
parser.add_argument("-D", "--define", nargs="*", default=[], action="extend")
18+
args = parser.parse_args()
19+
20+
if args.config is not None: # 加载配置文件
21+
with open(args.config) as f:
22+
cfg = json.load(f)
23+
else:
24+
cfg = {}
25+
26+
with param_scope(**cfg): # 配置文件的scope
27+
with param_scope(*args.define): # 命令行参数的scope
28+
main()

examples/application/cfg.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"main": {"a": "param a from cfg.json"}
3+
}

0 commit comments

Comments
 (0)