wandb使用教程(持续更新ing...)

简介: wandb使用教程(持续更新ing...)

1. 系统整体设置


https://wandb.ai/settings中可以设置什么时候以邮件或slack(我没下,所以我全用邮件了)提醒:

image.png

其中run finished在使用jupyter notebook环境时不会起效,以防每个cell运行结束后都提醒一次。如果用jupyter notebook的话,需要用wandb.alert()来触发提醒。


此外还可以关联GitHub以迅速提交benchmark runs(这个我还没试过,所以还没有关联):

image.png


储存空间可以进行管理,免费的有100个G,点击“manage storage”可以直接删不需要的文件:

image.png


2. 学术


2.1 免费学术小组

这个我还没试过,意思应该是学校或科研机构之类的可以免费建小组。如果有需要可以使用:https://wandb.ai/site/academic


2.2 在论文中引用wandb的方式

来自https://wandb.ai/site/academic


@misc{wandb,
title = {Experiment Tracking with Weights and Biases},
year = {2020},
note = {Software available from wandb.com},
url={https://www.wandb.com/},
author = {Biewald, Lukas},
}


2.3 wandb白皮书

https://www.dropbox.com/s/0ipub9ewwkml8jf/Experiment%20Tracking%20with%20Weights%20%26%20Biases.pdf?dl=1


3. wandb文档


Weights & Biases - Documentation


wandb支持多平台、多框架。

image.png


3.1 wandb quickstart

Quickstart - Documentation

Jupyter Notebook代码实践:Intro_to_Weights_&_Biases.ipynb - Colaboratory


3.1.1 set up

命令行:


pip install wandb
wandb login


(如果代码中有用到wandb,其实会自动要求登录,但是如果用nohup挂起脚本的话就无法实现,所以还是建议提前登录好)

API key的位置在:https://wandb.ai/authorize

(值得注意的是,这个配置应该是全局的,虽然我在一个虚拟环境下登录了我的账号,但是在别的虚拟环境下重新安装wandb还是可以直接通用。我估计是因为缓存在了本地)

用wandb login --relogin强制重新登录。


用jupyter notebook则为:


!pip install wandb
wandb.login()



3.1.2 开启新项目,跟踪指标、超参,添加报警信息

感觉就是init新项目,然后指标用log(在下面例子里可以看到,可以分组(如分为train/val)),超参用config,需要报警的内容就添加alert,结果添加到summary,最后finish就行,比较好用。


init文档:Launch Experiments with wandb.init - Documentation

对dashboard整体布局(run page)的介绍:Run Page - Documentation

数据可视化/跟踪指标:Data Visualization - Documentation

跟踪超参:Configure Experiments with wandb.config - Documentation

警告部分文档:Send Alerts with wandb.alert - Documentation


在测试代码时如临时不想与wandb同步,需设置环境变量,使wandb模式变成离线: WANDB_MODE=offline(具体做法是在运行Python代码的命令行中,在python前面加上这句命令)


文档中给出的核心代码:


import wandb
wandb.init(project="my-awesome-project")  #其他入参:name(见下例),config(见下)
wandb.log({'accuracy': train_acc, 'loss': train_loss})
wandb.config.dropout = 0.2
wandb.alert(
    title="Low accuracy", 
    text=f"Accuracy {acc} is below the acceptable threshold {thresh}"
)


在jupyter notebook上,用一个假示例来模拟:


import random
# Launch 5 simulated experiments
total_runs = 5
for run in range(total_runs):
  # 🐝 1️⃣ Start a new run to track this script
  wandb.init(
      # Set the project where this run will be logged
      project="wandbexample1", 
      # We pass a run name (otherwise it’ll be randomly assigned, like sunshine-lollypop-10)
      name=f"experiment_{run}", 
      # Track hyperparameters and run metadata
      config={
      "learning_rate": 0.02,
      "architecture": "CNN",
      "dataset": "CIFAR-100",
      "epochs": 10,
      })
  # This simple block simulates a training loop logging metrics
  epochs = 10
  offset = random.random() / 5
  for epoch in range(2, epochs):
      acc = 1 - 2 ** -epoch - random.random() / epoch - offset
      loss = 2 ** -epoch + random.random() / epoch + offset
      # 🐝 2️⃣ Log metrics from your script to W&B
      wandb.log({"acc": acc, "loss": loss})
  # Mark the run as finished
  wandb.finish()


在输出中会给出wandb项目链接,在浏览器中打开即可。

记录的内容:

image.png

输出的内容:

image.png

可以看出运行数据保存在了本地的wandb文件夹,额外的信息都没有上传到wandb上。


3.1.3 上一节的PyTorch例子

jupyter notebook上:MNIST分类器


#@title
import wandb
import math
import random
import torch, torchvision
import torch.nn as nn
import torchvision.transforms as T
from tqdm.notebook import tqdm
device = "cuda:0" if torch.cuda.is_available() else "cpu"
def get_dataloader(is_train, batch_size, slice=5):
    "Get a training dataloader"
    full_dataset = torchvision.datasets.MNIST(root=".", train=is_train, transform=T.ToTensor(), download=True)
    sub_dataset = torch.utils.data.Subset(full_dataset, indices=range(0, len(full_dataset), slice))
    loader = torch.utils.data.DataLoader(dataset=sub_dataset, 
                                         batch_size=batch_size, 
                                         shuffle=True if is_train else False, 
                                         pin_memory=True, num_workers=2)
    return loader
def get_model(dropout):
    "A simple model"
    model = nn.Sequential(nn.Flatten(),
                         nn.Linear(28*28, 256),
                         nn.BatchNorm1d(256),
                         nn.ReLU(),
                         nn.Dropout(dropout),
                         nn.Linear(256,10)).to(device)
    return model
def validate_model(model, valid_dl, loss_func, log_images=False, batch_idx=0):
    "Compute performance of the model on the validation dataset and log a wandb.Table"
    model.eval()
    val_loss = 0.
    with torch.inference_mode():
        correct = 0
        for i, (images, labels) in tqdm(enumerate(valid_dl), leave=False):
            images, labels = images.to(device), labels.to(device)
            # Forward pass ➡
            outputs = model(images)
            val_loss += loss_func(outputs, labels)*labels.size(0)
            # Compute accuracy and accumulate
            _, predicted = torch.max(outputs.data, 1)
            correct += (predicted == labels).sum().item()
            # Log one batch of images to the dashboard, always same batch_idx.
            if i==batch_idx and log_images:
                log_image_table(images, predicted, labels, outputs.softmax(dim=1))
    return val_loss / len(valid_dl.dataset), correct / len(valid_dl.dataset)
def log_image_table(images, predicted, labels, probs):
    "Log a wandb.Table with (img, pred, target, scores)"
    # 🐝 Create a wandb Table to log images, labels and predictions to
    table = wandb.Table(columns=["image", "pred", "target"]+[f"score_{i}" for i in range(10)])
    for img, pred, targ, prob in zip(images.to("cpu"), predicted.to("cpu"), labels.to("cpu"), probs.to("cpu")):
        table.add_data(wandb.Image(img[0].numpy()*255), pred, targ, *prob.numpy())
    wandb.log({"predictions_table":table}, commit=False)


训练:


# Launch 5 experiments, trying different dropout rates
for i in range(5):
    # 🐝 initialise a wandb run
    wandb.init(
        project="wandbexample1",
        name="pytorch_example"+str(i),
        config={
            "epochs": 10,
            "batch_size": 128,
            "lr": 1e-3,
            "dropout": random.uniform(0.01, 0.80),
            })
    # Copy your config 
    config = wandb.config
    # Get the data
    train_dl = get_dataloader(is_train=True, batch_size=config.batch_size)
    valid_dl = get_dataloader(is_train=False, batch_size=2*config.batch_size)
    n_steps_per_epoch = math.ceil(len(train_dl.dataset) / config.batch_size)
    # A simple MLP model
    model = get_model(config.dropout)
    # Make the loss and optimizer
    loss_func = nn.CrossEntropyLoss()
    optimizer = torch.optim.Adam(model.parameters(), lr=config.lr)
   # Training
    example_ct = 0
    step_ct = 0
    for epoch in tqdm(range(config.epochs)):
        model.train()
        for step, (images, labels) in enumerate(tqdm(train_dl, leave=False)):
            images, labels = images.to(device), labels.to(device)
            outputs = model(images)
            train_loss = loss_func(outputs, labels)
            optimizer.zero_grad()
            train_loss.backward()
            optimizer.step()
            example_ct += len(images)
            metrics = {"train/train_loss": train_loss, 
                       "train/epoch": (step + 1 + (n_steps_per_epoch * epoch)) / n_steps_per_epoch, 
                       "train/example_ct": example_ct}
            if step + 1 < n_steps_per_epoch:
                # 🐝 Log train metrics to wandb 
                wandb.log(metrics)
            step_ct += 1
        val_loss, accuracy = validate_model(model, valid_dl, loss_func, log_images=(epoch==(config.epochs-1)))
        # 🐝 Log train and validation metrics to wandb
        val_metrics = {"val/val_loss": val_loss, 
                       "val/val_accuracy": accuracy}
        wandb.log({**metrics, **val_metrics})
        print(f"Train Loss: {train_loss:.3f}, Valid Loss: {val_loss:3f}, Accuracy: {accuracy:.2f}")
    # If you had a test set, this is how you could log it as a Summary metric
    wandb.summary['test_accuracy'] = 0.8
    # 🐝 Close your wandb run 
    wandb.finish()


wandb网页首页:

image.png

点进一个run内:

image.png


输出:

image.png

image.png


3.1.4 jupyter notebook wandb警告示例

# Start a wandb run
wandb.init(project="wandbexample1")
# Simulating a model training loop
acc_threshold = 0.3
for training_step in range(1000):
    # Generate a random number for accuracy
    accuracy = round(random.random() + random.random(), 3)
    print(f'Accuracy is: {accuracy}, {acc_threshold}')
    # 🐝 Log accuracy to wandb
    wandb.log({"Accuracy": accuracy})
    # 🔔 If the accuracy is below the threshold, fire a W&B Alert and stop the run
    if accuracy <= acc_threshold:
        # 🐝 Send the wandb Alert
        wandb.alert(
            title='Low Accuracy',
            text=f'Accuracy {accuracy} at step {training_step} is below the acceptable theshold, {acc_threshold}',
        )
        print('Alert triggered')
        break
# Mark the run as finished (useful in Jupyter notebooks)
wandb.finish()


image.png

acc变成0.155,小于阈值0.3,所以报了警告。因为我没有slack所以是给发到邮件上:

image.png

项目的dashboard:

image.png


3.2 调参

Hyperparameter Tuning - Documentation

Organizing_Hyperparameter_Sweeps_in_PyTorch_with_W&B.ipynb


3.3 合作报告

Collaborative Reports - Documentation


3.4 跟踪pipeline的数据和模型版本

Data + Model Versioning - Documentation


3.5 数据可视化/跟踪指标日志

Data Visualization - Documentation


3.6 自动化深度学习平台的配置文件

Environment Variables - Documentation


3.7 本地化的解决方案

这个我暂时用不到,但是也列在这里以便备用:

Private Hosting - Documentation


3.8 示例

Examples - Documentation

示例的dashboard:wandb_example Workspace – Weights & Biases


3.9 Interagations

Integrations - Documentation


3.10 输入输出储存到wandb上的数据

Import & Export Data - Documentation


4. 常见问题


4.1 wandb.errors.UsageError: Error communicating with wandb process

参考https://docs.wandb.ai/library/init#init-start-error:在wandb.init()中添加入参settings=wandb.Settings(start_method="fork")即可解决问题。


4.2 本地储存空间不够→删除本地缓存

在 wandb.log() 处报错。


报错信息:


Thread SenderThread:
OSError: [Errno 28] No space left on device
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "env_path/lib/python3.8/site-packages/wandb/sdk/internal/internal_util.py", line 51, in run
    self._run()
  File "env_path/lib/python3.8/site-packages/wandb/sdk/internal/internal_util.py", line 102, in _run
    self._process(record)
  File "env_path/lib/python3.8/site-packages/wandb/sdk/internal/internal.py", line 310, in _process
    self._sm.send(record)
  File "env_path/lib/python3.8/site-packages/wandb/sdk/internal/sender.py", line 304, in send
    send_handler(record)
  File "env_path/lib/python3.8/site-packages/wandb/sdk/internal/sender.py", line 931, in send_summary
    self._update_summary()
  File "env_path/lib/python3.8/site-packages/wandb/sdk/internal/sender.py", line 944, in _update_summary
    f.write(json_summary)
OSError: [Errno 28] No space left on device
wandb: ERROR Internal wandb error: file data was not synced
Traceback (most recent call last):
  File "c49.py", line 293, in <module>
    wandb.log({'valid_law_acc':law_accuracy,'valid_charge_acc':charge_accuracy,
  File "env_path/lib/python3.8/site-packages/wandb/sdk/wandb_run.py", line 289, in wrapper
    return func(self, *args, **kwargs)
  File "env_path/lib/python3.8/site-packages/wandb/sdk/wandb_run.py", line 255, in wrapper
    return func(self, *args, **kwargs)
  File "env_path/lib/python3.8/site-packages/wandb/sdk/wandb_run.py", line 1591, in log
    self._log(data=data, step=step, commit=commit)
  File "env_path/lib/python3.8/site-packages/wandb/sdk/wandb_run.py", line 1375, in _log
    self._partial_history_callback(data, step, commit)
  File "env_path/lib/python3.8/site-packages/wandb/sdk/wandb_run.py", line 1259, in _partial_history_callback
    self._backend.interface.publish_partial_history(
  File "env_path/lib/python3.8/site-packages/wandb/sdk/interface/interface.py", line 553, in publish_partial_history
    self._publish_partial_history(partial_history)
  File "env_path/lib/python3.8/site-packages/wandb/sdk/interface/interface_shared.py", line 67, in _publish_partial_history
    self._publish(rec)
  File "env_path/lib/python3.8/site-packages/wandb/sdk/interface/interface_sock.py", line 51, in _publish
    self._sock_client.send_record_publish(record)
  File "env_path/lib/python3.8/site-packages/wandb/sdk/lib/sock_client.py", line 150, in send_record_publish
    self.send_server_request(server_req)
  File "env_path/lib/python3.8/site-packages/wandb/sdk/lib/sock_client.py", line 84, in send_server_request
    self._send_message(msg)
  File "env_path/lib/python3.8/site-packages/wandb/sdk/lib/sock_client.py", line 81, in _send_message
    self._sendall_with_error_handle(header + data)
  File "env_path/lib/python3.8/site-packages/wandb/sdk/lib/sock_client.py", line 61, in _sendall_with_error_handle
    sent = self._sock.send(data[total_sent:])
BrokenPipeError: [Errno 32] Broken pipe
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "env_path/lib/python3.8/site-packages/wandb/sdk/lib/sock_client.py", line 81, in _send_message
    self._sendall_with_error_handle(header + data)
  File "env_path/lib/python3.8/site-packages/wandb/sdk/lib/sock_client.py", line 61, in _sendall_with_error_handle
    sent = self._sock.send(data[total_sent:])
BrokenPipeError: [Errno 32] Broken pipe
相关文章
|
机器学习/深度学习 自然语言处理
文生图模型-Stable Diffusion | AIGC
所谓的生成式模型就是通过文本或者随机采样的方式来得到一张图或者一段话的模型,比如文生图,顾名思义通过文本描述来生成图像的过程。当前流行的文生图模型,如DALE-2, midjourney以及今天要介绍的Stable Diffusion,这3种都是基于Diffusion扩散模型【1月更文挑战第6天】
2820 0
|
机器学习/深度学习 数据可视化 数据挖掘
初学者该如何选择最适合自己的图像分类模型
初学者该如何选择最适合自己的图像分类模型
2739 0
初学者该如何选择最适合自己的图像分类模型
|
9月前
|
人工智能 运维 物联网
搞定多模态微调只需一杯咖啡的时间?FC DevPod + Llama-Factory 极速实战
告别显存不足、环境配置难与高昂成本!本文带你用阿里云函数计算FC + Llama-Factory,5分钟搭建Qwen2-VL多模态模型的Serverless微调流水线。无需运维,按需付费,通过WebUI点击完成数据准备、LoRA微调、效果验证与模型导出,让AI训练像P图一样简单。低成本、高效率,人人皆可玩转大模型。
|
机器学习/深度学习 存储 算法
强化学习:蒙特卡罗求解最优状态价值函数——手把手教你入门强化学习(五)
本文介绍了强化学习中的蒙特卡罗算法,包括其基本概念、两种估值方法(首次访问蒙特卡罗与每次访问蒙特卡罗)及增量平均优化方式。蒙特卡罗法是一种基于完整回合采样的无模型学习方法,通过统计经验回报的平均值估计状态或动作价值函数。文章详细讲解了算法流程,并指出其初期方差较大、估值不稳定等缺点。最后对比动态规划,说明了蒙特卡罗法在强化学习中的应用价值。适合初学者理解蒙特卡罗算法的核心思想与实现步骤。
1060 4
|
11月前
|
数据采集 人工智能 自然语言处理
121_训练评估:困惑度分析 - 分析指标与下游任务关系
在大规模语言模型(LLM)的训练过程中,评估模型性能是一个至关重要但常被简化处理的环节。2025年的研究表明,仅依赖单一指标(如困惑度)来判断模型质量已经无法满足复杂应用场景的需求。困惑度作为语言模型训练中最核心的评估指标,其与下游任务表现之间的关系远比直觉更复杂。本文将深入剖析困惑度的数学原理、计算方法、优化策略,以及其与各类下游任务表现的相关性分析,为大规模语言模型的训练优化提供全面的技术指导。
982 1
|
Shell
wandb.errors.UsageError: api_key not configured (no-tty). call wandb.login(key=[your_api_key])
wandb.errors.UsageError: api_key not configured (no-tty). call wandb.login(key=[your_api_key])
4849 0
wandb.errors.UsageError: api_key not configured (no-tty). call wandb.login(key=[your_api_key])
|
存储 NoSQL 区块链
开源:LMDB 操作工具:lmcmd
本文介绍了 LMDB(一种高效的键值存储数据库)和基于 Python 开发的命令行工具 `lmcmd`。由于 LMDB 使用二进制文件存储,管理和调试不便,因此开发了 `lmcmd`,提供了类似 Redis 的命令行操作界面,支持数据库操作、数据导入导出和查找等功能。文章涵盖了 `lmcmd` 的安装、连接数据库和常用命令(如 `set`、`get`、`export` 等)示例。最后强调了开源工具的价值,鼓励用户反馈和改进。
708 1
|
机器学习/深度学习 自然语言处理 算法
人类偏好对齐训练技术解析
大型语言模型(LLMs)通过在大量文本数据集上进行无监督预训练,获得丰富的语言模式和知识,这一阶段训练后的模型被称为base model。
|
编解码 自然语言处理 数据可视化
精通 Transformers(四)(4)
精通 Transformers(四)
1062 0