Skip to content

Commit

Permalink
Slightly more engaging example
Browse files Browse the repository at this point in the history
  • Loading branch information
omesser committed Jul 23, 2023
1 parent d39bd1c commit e11fb17
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,36 +52,48 @@ $ git commit -m "DVC init"

## Example code

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

```python
# train.py
import time
import random
import sys

from dvclive import Live

params = {"learning_rate": 0.002, "optimizer": "Adam", "epochs": 20}

with Live(save_dvc_exp=True) as live:
epochs = int(sys.argv[1])
live.log_param("epochs", epochs)
for epoch in range(epochs):
live.log_metric("train/accuracy", epoch + random.random())
live.log_metric("train/loss", epochs - epoch - random.random())
live.log_metric("val/accuracy",epoch + random.random() )
live.log_metric("val/loss", epochs - epoch - random.random())

# log a parameters
for param in params:
live.log_param(param, params[param])

# simulate training
offset = random.uniform(0.0.2, 0.1)
for epoch in range(1, params["epochs"]):
fuzz = random.uniform(0.01, 0.1)
accuracy = 1 - (2 ** - epoch) - fuzz - offset
loss = (2 ** - epoch) + fuzz + offset

# log metrics to studio
live.log_metric("accuracy", accuracy)
live.log_metric("loss", loss)
live.next_step()
time.sleep(0.2)
```

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

## Running

Run couple of times passing different values:
Run this a couple of times to simulate multiple experiments:

```console
$ python train.py 5
$ python train.py 5
$ python train.py 7
$ python train.py
$ python train.py
$ python train.py
...
```

## Comparing
Expand Down

0 comments on commit e11fb17

Please sign in to comment.