SwanLab

0.7.15 · active · verified Thu Apr 16

SwanLab is a Python library for streamlined tracking and management of AI training processes. It offers experiment tracking, visualization, automatic logging, hyperparameter recording, experiment comparison, and multi-user collaboration. SwanLab supports both cloud and offline usage, integrating with over 30 mainstream AI training frameworks. The current version is 0.7.15, with frequent patch and minor releases.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart initializes a SwanLab experiment, logs hyperparameters using `config`, and then simulates a training loop to log scalar metrics (`loss` and `accuracy`) using `swanlab.log()`. It concludes by explicitly calling `swanlab.finish()`.

import swanlab
import random

# Initialize a SwanLab experiment
run = swanlab.init(
    project="my-ml-project",
    experiment_name="basic_training_run",
    config={
        "learning_rate": 0.01,
        "epochs": 5,
        "batch_size": 32
    }
)

# Simulate a training loop
for epoch in range(run.config.epochs):
    loss = 1.0 / (epoch + 1) + random.uniform(-0.1, 0.1)
    accuracy = 0.5 + (epoch / run.config.epochs) * 0.4 + random.uniform(-0.05, 0.05)
    
    # Log metrics
    swanlab.log({"loss": loss, "accuracy": accuracy})
    print(f"Epoch {epoch+1}, Loss: {loss:.4f}, Accuracy: {accuracy:.4f}")

# Finish the experiment explicitly (optional in most scripts, but good practice)
swanlab.finish()

view raw JSON →