-
Notifications
You must be signed in to change notification settings - Fork 221
add Tracker protocol and training tracking guide. #1741
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
Open
janfb
wants to merge
4
commits into
main
Choose a base branch
from
refactor-tracker-interface
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7546d35
add Tracker protocol and training tracking guide.
janfb 9f1e789
update gitgnore, remove unrelated docs change
janfb 9ee671d
Merge remote-tracking branch 'origin/main' into refactor-tracker-inte…
janfb ad06a85
Merge branch 'main' into refactor-tracker-interface
janfb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -102,7 +102,3 @@ target/ | |
| # uv | ||
| uv.lock | ||
| .python-version | ||
|
|
||
| # Serena cache | ||
| .serena/ | ||
| .claude/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,306 @@ | ||
| { | ||
| "cells": [ | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "7fb27b941602401d91542211134fc71a", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "# How to track experiments" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "9dcf97f2", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "Experiment tracking helps compare model variants and keep a record of hyperparameters and training metrics. By default, `sbi` logs to TensorBoard. You can also bring your own tracker by implementing the lightweight `Tracker` protocol and passing it as `tracker=...`.\n", | ||
| "\n", | ||
| "If using your own tracker (e.g., `wandb`, `mlflow` or `trackio`), note that the run lifecycle (e.g., `wandb.init`, `mlflow.start_run`) is handled on the user side." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "6492510e", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Define a minimal training setup" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "898c316a", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "import torch\n", | ||
| "\n", | ||
| "from sbi.inference import NPE\n", | ||
| "from sbi.neural_nets import posterior_nn\n", | ||
| "from sbi.neural_nets.embedding_nets import FCEmbedding\n", | ||
| "from sbi.utils import BoxUniform\n", | ||
| "\n", | ||
| "torch.manual_seed(0)\n", | ||
| "\n", | ||
| "def simulator(theta):\n", | ||
| " return theta + 0.1 * torch.randn_like(theta)\n", | ||
| "\n", | ||
| "prior = BoxUniform(low=-2 * torch.ones(2), high=2 * torch.ones(2))\n", | ||
| "\n", | ||
| "theta = prior.sample((5000,))\n", | ||
| "x = simulator(theta)\n", | ||
| "\n", | ||
| "embedding_net = FCEmbedding(input_dim=x.shape[1], output_dim=32)\n", | ||
| "density_estimator = posterior_nn(\n", | ||
| " model=\"nsf\",\n", | ||
| " embedding_net=embedding_net,\n", | ||
| " num_transforms=4,\n", | ||
| ")\n", | ||
| "\n", | ||
| "train_kwargs = dict(\n", | ||
| " max_num_epochs=50,\n", | ||
| " training_batch_size=128,\n", | ||
| " validation_fraction=0.1,\n", | ||
| " show_train_summary=False,\n", | ||
| ")" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "9142d4b6", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Train with a tracker\n", | ||
| "\n", | ||
| "By default, `sbi` uses a TensorBoard tracker to log training loss, validation loss,\n", | ||
| "number of epochs and more. \n", | ||
| "\n", | ||
| "When you want to track additional quantities, you instantiate the tracker yourself and\n", | ||
| "pass it to the inference class:" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "1a62bc10", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "from torch.utils.tensorboard.writer import SummaryWriter\n", | ||
| "\n", | ||
| "from sbi.utils.tracking import TensorBoardTracker\n", | ||
| "\n", | ||
| "tracker = TensorBoardTracker(SummaryWriter(\"sbi-logs\"))\n", | ||
| "tracker.log_params({\"embedding_dim\": 32, \"num_transforms\": 4})\n", | ||
| "\n", | ||
| "inference = NPE(prior=prior, tracker=tracker)\n", | ||
| "inference.append_simulations(theta, x)\n", | ||
| "estimator = inference.train(**train_kwargs)\n", | ||
| "posterior = inference.build_posterior(estimator)" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "c4da9894", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## View TensorBoard results\n", | ||
| "\n", | ||
| "You can then view your tracked run(s) on a TensorBoard shown on your localhost in the\n", | ||
| "browser. By default, `sbi` will create a log directory `sbi-logs` at the location the\n", | ||
| "training script was called.\n", | ||
| "\n", | ||
| "```bash\n", | ||
| "tensorboard --logdir=sbi-logs\n", | ||
| "```" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "6f2777dd", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Using other trackers\n", | ||
| "\n", | ||
| "To enable usage of other trackers, we provide a lightweight `Protocol` that trackers\n", | ||
| "need to follow. You can implement a small adapter that satisfies the `Tracker` protocol\n", | ||
| "and pass it to `tracker=`. Below are minimal examples for common tools." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "43644d68", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "```python\n", | ||
| "# W&B adapter (requires `wandb.init()` before training)\n", | ||
| "class WandBAdapter:\n", | ||
| " log_dir = None\n", | ||
| "\n", | ||
| " def __init__(self, run):\n", | ||
| " self._run = run\n", | ||
| "\n", | ||
| " def log_metric(self, name, value, step=None):\n", | ||
| " self._run.log({name: value}, step=step)\n", | ||
| "\n", | ||
| " def log_metrics(self, metrics, step=None):\n", | ||
| " self._run.log(metrics, step=step)\n", | ||
| "\n", | ||
| " def log_params(self, params):\n", | ||
| " self._run.config.update(params)\n", | ||
| "\n", | ||
| " def add_figure(self, name, figure, step=None):\n", | ||
| " import wandb\n", | ||
| " self._run.log({name: wandb.Image(figure)}, step=step)\n", | ||
| "\n", | ||
| " def flush(self):\n", | ||
| " pass\n", | ||
| "```" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "031651b3", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "```python\n", | ||
| "# MLflow adapter (configure tracking URI separately)\n", | ||
| "class MLflowAdapter:\n", | ||
| " log_dir = None\n", | ||
| "\n", | ||
| " def __init__(self, mlflow):\n", | ||
| " self._mlflow = mlflow\n", | ||
| "\n", | ||
| " def log_metric(self, name, value, step=None):\n", | ||
| " self._mlflow.log_metric(name, value, step=step)\n", | ||
| "\n", | ||
| " def log_metrics(self, metrics, step=None):\n", | ||
| " for name, value in metrics.items():\n", | ||
| " self.log_metric(name, value, step=step)\n", | ||
| "\n", | ||
| " def log_params(self, params):\n", | ||
| " self._mlflow.log_params(params)\n", | ||
| "\n", | ||
| " def add_figure(self, name, figure, step=None):\n", | ||
| " self._mlflow.log_figure(figure, f\"{name}.png\")\n", | ||
| "\n", | ||
| " def flush(self):\n", | ||
| " pass\n", | ||
| "```" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "6d891d85", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "```python\n", | ||
| "# Trackio adapter (requires `trackio.init()` before training)\n", | ||
| "class TrackioAdapter:\n", | ||
| " log_dir = None\n", | ||
| "\n", | ||
| " def __init__(self, trackio):\n", | ||
| " self._trackio = trackio\n", | ||
| "\n", | ||
| " def log_metric(self, name, value, step=None):\n", | ||
| " self._trackio.log({name: value}, step=step)\n", | ||
| "\n", | ||
| " def log_metrics(self, metrics, step=None):\n", | ||
| " self._trackio.log(metrics, step=step)\n", | ||
| "\n", | ||
| " def log_params(self, params):\n", | ||
| " self._trackio.log(params)\n", | ||
| "\n", | ||
| " def add_figure(self, name, figure, step=None):\n", | ||
| " self._trackio.log_image(figure, name=name, step=step)\n", | ||
| "\n", | ||
| " def flush(self):\n", | ||
| " pass\n", | ||
| "```" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "43b453f7", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "When using external trackers, create an adapter instance and pass it to `tracker=`:\n", | ||
| "\n", | ||
| "```python\n", | ||
| "# wandb.init(...)\n", | ||
| "tracker = WandBAdapter(wandb.run)\n", | ||
| "inference = NPE(prior=prior, density_estimator=density_estimator, tracker=tracker)\n", | ||
| "```" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "5c748c81", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Log figures\n", | ||
| "\n", | ||
| "Trackers can also store matplotlib figures. For example, after training you can log a pairplot:\n", | ||
| "\n", | ||
| "```python\n", | ||
| "from sbi.analysis import pairplot\n", | ||
| "\n", | ||
| "x_o = x[:1]\n", | ||
| "samples = posterior.sample((1000,), x=x_o)\n", | ||
| "fig, _ = pairplot(samples)\n", | ||
| "tracker.add_figure(\"posterior_pairplot\", fig, step=0)\n", | ||
| "```\n", | ||
| "\n", | ||
| "Figure logging depends on the tracker implementation (e.g., `wandb.Image`, `mlflow.log_figure`)." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "ab99ec92", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Custom training loop (optional)\n", | ||
| "\n", | ||
| "If you want to log custom diagnostics per epoch, use the training interface tutorial: https://sbi.readthedocs.io/en/latest/advanced_tutorials/18_training_interface.html." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "b206a6a7", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Notes\n", | ||
| "\n", | ||
| "- Each tool supports richer logging (artifacts, checkpoints, plots), but the patterns above are enough to track hyperparameters, epoch-wise losses, and validation metrics.\n", | ||
| "- If you already use Optuna or other sweep tools, you can call the logger inside the objective function to log each trial." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "46465002", | ||
| "metadata": {}, | ||
| "source": [] | ||
| } | ||
| ], | ||
| "metadata": { | ||
| "kernelspec": { | ||
| "display_name": "sbi (3.12.9)", | ||
| "language": "python", | ||
| "name": "python3" | ||
| }, | ||
| "language_info": { | ||
| "codemirror_mode": { | ||
| "name": "ipython", | ||
| "version": 3 | ||
| }, | ||
| "file_extension": ".py", | ||
| "mimetype": "text/x-python", | ||
| "name": "python", | ||
| "nbconvert_exporter": "python", | ||
| "pygments_lexer": "ipython3", | ||
| "version": "3.12.9" | ||
| } | ||
| }, | ||
| "nbformat": 4, | ||
| "nbformat_minor": 5 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.