🚀Quick Start
Install SwanLab and start tracking your AI experiments in minutes

1. Install SwanLab
Install the swanlab library on a computer with a Python 3 environment using pip.
Open the command line and enter:
pip install swanlabPress Enter and wait for the installation to complete.
2. Log in
If you don't have a SwanLab account yet, please register for free on the official website.
Open the command line and enter:
swanlab loginWhen you see the following prompt:
swanlab: Logging into swanlab cloud.
swanlab: You can find your API key at: https://swanlab.cn/settings
swanlab: Paste an API key from your profile and hit enter, or press 'CTRL-C' to quit:Copy your API Key from the user settings page, paste it and press Enter to complete the login. You don't need to log in again after that.
INFO
If your computer isn't well-suited for logging in via command line by pasting the API Key (such as some Windows CMD commands), you can use:
swanlab login -k api-keyIf your computer does not support the swanlab login method, you can also log in using a Python script:
import swanlab
swanlab.login(api_key="Your API Key")To use Swanlab in a Notebook environment such as Kaggle, see Tracking Experiments with a Notebook.
3. Start an experiment and track hyperparameters
In the Python script, we use swanlab.init to create a SwanLab experiment and pass a dictionary containing hyperparameter key-value pairs to the config parameter:
import swanlab
run = swanlab.init(
# Set project
project="my-project",
# Track hyperparameters and experiment metadata
config={
"learning_rate": 0.01,
"epochs": 10,
},
)run is the fundamental component of SwanLab, and you will often use it to record and track experiment metrics.
4. Record experiment metrics
In the Python script, use swanlab.log to record experiment metrics (such as accuracy and loss value).
The usage is to pass a dictionary containing the metrics to swanlab.log:
swanlab.log({"accuracy": acc, "loss": loss})5. Complete code, view the visualization dashboard online
We integrate the above steps into the complete code shown below:
import swanlab
import random
# Initialize SwanLab
run = swanlab.init(
# Set project
project="my-project",
# Track hyperparameters and experiment metadata
config={
"learning_rate": 0.01,
"epochs": 10,
},
)
print(f"Learning rate is {run.config.learning_rate}")
offset = random.random() / 5
# Simulate training process
for epoch in range(2, run.config.epochs):
acc = 1 - 2**-epoch - random.random() / epoch - offset
loss = 2**-epoch + random.random() / epoch + offset
print(f"epoch={epoch}, accuracy={acc}, loss={loss}")
# Record metrics
swanlab.log({"accuracy": acc, "loss": loss})Run the code, visit SwanLab, and see the improvement of the metrics (accuracy and loss value) you recorded using SwanLab in each training step.

What's next
- See how SwanLab records multimedia content (images, audio, text,...)
- See how SwanLab records the MNIST handwritten recognition case
- See integration with other frameworks
- See how to collaborate with your team through SwanLab
FAQ
1. Where can I find my API Key?
After logging in to the SwanLab website, the API Key will be displayed on the user settings page.
2. Can I use SwanLab offline?
Yes, please refer to the self-hosting section for the specific process.