Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

README: A more engaging code snippet example #635

Merged
merged 2 commits into from
Jul 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}
Copy link
Contributor

@daavoo daavoo Jul 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love that params don't have an effect nor change across runs, but not a blocker

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is a for a fake feel of familiarity / realism. But I've seen this in other places and I think since this is very very explicitly "fake" it's not misleading anyone (personal feel)


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