Skip to content

Commit 50f15a7

Browse files
README: A more engaging code snippet ecample (#635)
* Slightly more engaging example * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent d39bd1c commit 50f15a7

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

README.md

+26-14
Original file line numberDiff line numberDiff line change
@@ -52,36 +52,48 @@ $ git commit -m "DVC init"
5252

5353
## Example code
5454

55-
Copy the snippet below as a basic example of the API usage:
55+
Copy the snippet below into `train.py` for a basic API usage example:
5656

5757
```python
58-
# train.py
58+
import time
5959
import random
60-
import sys
60+
6161
from dvclive import Live
6262

63+
params = {"learning_rate": 0.002, "optimizer": "Adam", "epochs": 20}
64+
6365
with Live(save_dvc_exp=True) as live:
64-
epochs = int(sys.argv[1])
65-
live.log_param("epochs", epochs)
66-
for epoch in range(epochs):
67-
live.log_metric("train/accuracy", epoch + random.random())
68-
live.log_metric("train/loss", epochs - epoch - random.random())
69-
live.log_metric("val/accuracy",epoch + random.random() )
70-
live.log_metric("val/loss", epochs - epoch - random.random())
66+
67+
# log a parameters
68+
for param in params:
69+
live.log_param(param, params[param])
70+
71+
# simulate training
72+
offset = random.uniform(0.0.2, 0.1)
73+
for epoch in range(1, params["epochs"]):
74+
fuzz = random.uniform(0.01, 0.1)
75+
accuracy = 1 - (2 ** - epoch) - fuzz - offset
76+
loss = (2 ** - epoch) + fuzz + offset
77+
78+
# log metrics to studio
79+
live.log_metric("accuracy", accuracy)
80+
live.log_metric("loss", loss)
7181
live.next_step()
82+
time.sleep(0.2)
7283
```
7384

7485
See [Integrations](https://dvc.org/doc/dvclive/ml-frameworks) for examples using
7586
DVCLive alongside different ML Frameworks.
7687

7788
## Running
7889

79-
Run couple of times passing different values:
90+
Run this a couple of times to simulate multiple experiments:
8091

8192
```console
82-
$ python train.py 5
83-
$ python train.py 5
84-
$ python train.py 7
93+
$ python train.py
94+
$ python train.py
95+
$ python train.py
96+
...
8597
```
8698

8799
## Comparing

0 commit comments

Comments
 (0)