Skip to content

Commit 2620a65

Browse files
committed
add quick start
Signed-off-by: reiase <[email protected]>
1 parent 3fd4a83 commit 2620a65

File tree

4 files changed

+137
-2
lines changed

4 files changed

+137
-2
lines changed

docs/quick_start.md

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
Quick Start
2+
===========
3+
4+
HyperParameter is a configuration and parameter management library for Python. HyperParameter provides the following features:
5+
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.
11+
12+
`HyperParameter`
13+
----------------
14+
15+
### create hyperparameter
16+
17+
```python
18+
>>> from hyperparameter import HyperParameter
19+
>>> hp = HyperParameter(param1=1, param2="A")
20+
>>> hp
21+
{'param1': 1, 'param2': 'A'}
22+
23+
```
24+
25+
### access parameters in hyperparameter
26+
27+
we can access the parameter stored in HyperParameter with object-style API:
28+
29+
```python
30+
>>> hp.param1
31+
1
32+
33+
>>> hp.param2
34+
'A'
35+
36+
```
37+
38+
nested parameters are also supported:
39+
40+
```python
41+
>>> hp = HyperParameter(param1=1, obj2={"prop3": 3})
42+
>>> hp.obj2.prop3
43+
3
44+
45+
```
46+
47+
### access undefined parameter
48+
49+
Undefined parameter is treated as false in `if` statement:
50+
51+
```python
52+
>>> if not hp.undefined_param: print("param not defined")
53+
param not defined
54+
55+
```
56+
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.
58+
59+
```python
60+
>>> hp().undefined_param("default_value")
61+
'default_value'
62+
63+
```
64+
65+
`param_scope`
66+
-------------
67+
68+
### manage hyper-parameters with `param_scope`
69+
70+
`param_scope` create a `HyperParameter` object for the context it manages:
71+
72+
```python
73+
>>> from hyperparameter import param_scope
74+
>>> with param_scope(param1=1, param2="A") as ps:
75+
... ps.param1
76+
1
77+
78+
```
79+
80+
### access hyper-parameter in functions
81+
82+
`param_scope` is often used to configure the behavior of a function.
83+
84+
```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:
94+
... foo()
95+
1
96+
97+
>>> with param_scope(param1=1, param2="A") as ps:
98+
... foo_with_default()
99+
1
100+
101+
>>> with param_scope(param2="A") as ps:
102+
... foo_with_default()
103+
2
104+
105+
```
106+
107+
`auto_param`
108+
------------
109+
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.
113+
114+
```python
115+
>>> from hyperparameter import auto_param
116+
>>> @auto_param
117+
... def foo(a, b=1, c=2):
118+
... return (a, b, c)
119+
120+
>>> foo(0)
121+
(0, 1, 2)
122+
123+
```
124+
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.
128+
129+
```python
130+
>>> with param_scope("foo.b=0"):
131+
... foo(0)
132+
(0, 0, 2)
133+
134+
```

hyperparameter/hyperparameter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def get_or_else(self, default: Any = None):
103103
"""Get value for the parameter, or get default value if the parameter is not defined."""
104104
_read_tracker.add(self._path)
105105
value = self._root.get(self._path)
106-
return default if not value else value
106+
return default if isinstance(value, _Accessor) else value
107107

108108
def __getattr__(self, name: str) -> Any:
109109
# _path and _root are not allowed as keys for user.

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ plugins:
2323

2424
nav:
2525
- Home: index.md
26+
- Quick Start: quick_start.md
2627
- Reference: reference.md
2728

2829
watch:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[build-system]
2-
requires = ["setuptools", "setuptools-scm", "setuptools-rust"]
2+
requires = ["setuptools", "setuptools-scm"]
33
build-backend = "setuptools.build_meta"
44

55
[project]

0 commit comments

Comments
 (0)