Skip to content

Commit f3a6fb2

Browse files
committed
update doc/example
1 parent 7217d94 commit f3a6fb2

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,8 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
examples/data/
131+
examples/mnist/mlruns/
132+
examples/sparse_lr/mlruns/
133+
.vscode/
134+
.DS_Store

docs/index.md

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ or becomes powerful with `params()`:
1616

1717
```python
1818
params().a.b.c.getOrElse(3) # 3 (default value)
19-
params().a.b.c(3) # 3 (shortcut for default value)
19+
params().a.b.c(3) # 3 (shortcut for default value)
2020

21-
params().a.b.c = 4 # set value to param `a.b.c`
22-
params().a.b.c(3) # 4 (default value is ignored)
21+
params().a.b.c = 4 # set value to param `a.b.c`
22+
params().a.b.c(3) # 4 (default value is ignored)
2323
```
2424

2525
## Scoped Parameter
@@ -35,8 +35,8 @@ or becomes powerful with `nested scope`:
3535
``` python
3636
with param_scope(a=1) as hp:
3737
with param_scope(a=2) as hp:
38-
hp.a == 2 # True
39-
hp.a == 1 # True
38+
hp.a == 2 # True, a=2 for inner scope
39+
hp.a == 1 # True, a=1 for outer scope
4040
```
4141

4242
even more powerful when using `param_scope` in function:
@@ -59,3 +59,17 @@ foo() # 1
5959
with param_scope(param1=2):
6060
foo() # 2
6161
```
62+
63+
## Predefined Parameter
64+
```python
65+
@auto_param #convert keyword arguments into hyper parameters
66+
def model_train(X, y, learning_rate = 1.0, penalty = 'l1'):
67+
LR = LogisticRegression(C=1.0,
68+
lr=local_param('learning_rate'),
69+
penalty=local_param('penalty'))
70+
LR.fit(X, y)
71+
72+
# specify predefined parameter using `param_scope`
73+
with param_scope('model_train.learning_rate=0.01'):
74+
model_train(X, y)
75+
```
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from hyperparameter import param_scope, set_tracker, Tracker
2+
from model import sparse_lr_plot
3+
4+
from sklearn import datasets
5+
from sklearn.preprocessing import StandardScaler
6+
7+
import mlflow
8+
9+
10+
def mlflow_tracker(params):
11+
for k, v in params.items():
12+
mlflow.log_param(k, v)
13+
14+
15+
set_tracker(mlflow_tracker)
16+
17+
X, y = datasets.load_digits(return_X_y=True)
18+
X = StandardScaler().fit_transform(X)
19+
20+
# classify small against large digits
21+
y = (y > 4).astype(int)
22+
23+
24+
def run(args):
25+
# run the lr model with parameter from cmdline
26+
with param_scope(*args.define): # set parameters according to cmd line
27+
sparse_lr_plot(X, y)
28+
29+
30+
if __name__ == '__main__':
31+
import argparse
32+
parser = argparse.ArgumentParser('example')
33+
parser.add_argument('-D', '--define', nargs='*', default=[], action="extend",
34+
help='define a parameter `param_name=param_value`, supported parameter list: \n' + '\n '.join(Tracker.all()))
35+
args = parser.parse_args()
36+
run(args)
37+
38+
print(Tracker.report())

0 commit comments

Comments
 (0)