wandb快速上手、使用心得(超好用的Tensorboard高替品)

1 wandb介绍

wandb地址:wandb
Wandb(Weights & Biases)是一个用于机器学习实验跟踪和可视化的工具和平台。它旨在帮助机器学习团队更好地组织、记录和共享实验过程和结果。相较于Tensorboard更加方便和智能。

1. 实验跟踪和版本控制: W&B可以记录和跟踪您的机器学习实验,包括超参数、指标、模型架构等。已用到
2. 可视化和分析: W&B提供丰富的可视化工具,可以直观地展示实验结果、训练曲线、指标趋势等。可以轻松地创建交互式图表、散点图、直方图等,以更好地理解和分析实验数据。已用到
3. 模型登记和部署: W&B可以帮助您登记和管理训练的模型,包括模型文件、权重和元数据。可以轻松地共享和部署模型,以便在其他环境中进行推理和评估。暂时没用到
4. 协作和共享: W&B提供团队协作和共享功能,可以邀请团队成员参与实验、查看结果,并进行讨论和反馈。还可以将实验和结果与其他人共享,使其可以在不同的环境中重现和使用您的工作。暂时没用到
5. 集成和兼容性: W&B与常用的机器学习框架(如PyTorch、TensorFlow、Scikit-learn等)以及其他工具(如Jupyter Notebook、Docker等)具有良好的集成和兼容性,可以无缝地与您的工作流程集成。已用到

上图:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2 快速上手

参考链接:wandb quickstart

  1. 激活conda环境,安装wandb
pip install wandb 

如果下载太慢,可以使用以下命令从清华源下载

pip install wandb -i https://pypi.tuna.tsinghua.edu.cn/simple
  1. 登陆账号,输入API KEY
wandb login

如果报以下错误,请关闭VPN

wandb: Network error (ProxyError), entering retry loop.
wandb: W&B API key is configured. Use `wandb login --relogin` to force relogin
  1. 运行教程案例
import wandb
import random

# start a new wandb run to track this script
wandb.init(
    # set the wandb project where this run will be logged
    project="my-awesome-project",
    
    # track hyperparameters and run metadata
    config={
    "learning_rate": 0.02,
    "architecture": "CNN",
    "dataset": "CIFAR-100",
    "epochs": 10,
    }
)

# simulate training
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
    
    # log metrics to wandb
    wandb.log({"acc": acc, "loss": loss})
    
# [optional] finish the wandb run, necessary in notebooks
wandb.finish()
  1. 查看结果
    在这里插入图片描述
    运行的结果,系统GPU使用性能啥的,应有尽有:
    在这里插入图片描述

3 使用心得

3.1 一张图展示两条线

需求: 在一张图上展示训练的损失和验证损失,或者是训练的准确度和验证的准确度等

初步解决: 将每一步的结果都放在列表中,然后使用wandb.plot.line_series()画图上传到wandb服务器上。 不推荐,数据显示不友好

最终解决: 直接wandb.log上传结果,然后再wandb服务器上挑选数据重新生成新图

大致架构:

import wandb
# 初始化wandb
wandb.init(
    # set the wandb project where this run will be logged
    project="项目名称",
    
    # track hyperparameters and run metadata
    config={
    "learning_rate": lr,
    ...
    }
)

# 整体嵌入架构
epochs = 100
for i in range(epochs):
    net.train():
    train_total_loss = 0
    for ... in train_loader:
        ...
        ...
        train_total_loss += loss.item()
        train_total_acc += acc(....)
    train_acc = train_total_acc/len(train_loader)
    
    net.eval()
    val_total_loss = 0
    for ... in val_loader:
        ...
        ...
        val_total_loss += loss.item()
        val_total_acc += acc(...)
    val_acc = val_total_acc/len(val_loader)
    wandb.log({"train_loss":train_total_loss, "val_loss":val_total_loss, "train_acc": train_acc, "val_acc":val_acc})
wandb.finish()

生成图:
在这里插入图片描述

提醒: 每一个epoch中最好只使用一个wandb.log。因为我们每次只是记录纵坐标,横坐标是wandb自动记录wandb.log个数的。
不过也可以自行记录纵坐标,有需要可以留言,我会解答的。

3.2 想要科学上网和wandb一起使用(离线使用)

需求: 一般情况下挂VPN会导致wandb初始化出错,不能连接到wandb服务器进行数据同步上传,因此可以采用离线上传数据

解决: 使用os.environ["WANDB_API_KEY"] 先离线训练,等到训练完成之后,关闭VPN,使用wandb sync命令进行上传数据

大致架构:

import os
import wandb
os.environ["WANDB_API_KEY"] = '自己的API KEY' 
os.environ["WANDB_MODE"] = "offline"

在导包之后添加这两行代码,然后训练结束之后根据提示上传:
在这里插入图片描述

使用命令wandb sync 数据地址 上传数据

查看结果:
在这里插入图片描述
这些运行时间几秒钟的就是我离线上传的数据。

3.3 未完待续

虽然只是初步探索一下,但是发现它的功能真的是超级强大,相较于Tensoboard好用很多,后期用到其他功能也会持续更新的。
wandb操作手册: wandb tutorials

  • 17
    点赞
  • 152
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 24
    评论
Wandb (Weights & Biases) is a popular tool used for tracking and visualizing machine learning experiments. It provides a Python library that can be integrated with PyTorch, among other deep learning frameworks. To use Wandb with PyTorch, you will need to install the wandb library first by running the following command: ``` pip install wandb ``` Once installed, you can import and initialize wandb in your PyTorch script or notebook. Here's an example: ```python import wandb import torch import torch.nn as nn import torch.optim as optim # Initialize wandb wandb.init(project="your-project-name", entity="your-username") # Define your PyTorch model class MyModel(nn.Module): def __init__(self): super(MyModel, self).__init__() # Add your model layers here def forward(self, x): # Define the forward pass of your model here pass # Instantiate your model model = MyModel() # Define your loss function and optimizer criterion = nn.CrossEntropyLoss() optimizer = optim.Adam(model.parameters(), lr=0.001) # Training loop for epoch in range(num_epochs): # Training code goes here # Log metrics to wandb wandb.log({"loss": loss.item(), "accuracy": accuracy}) # Validation code goes here # Save the trained model torch.save(model.state_dict(), "model.pth") # Finish wandb run wandb.finish() ``` In this example, wandb.init() initializes the wandb run and connects it to your project. You can specify the project name and your username or organization in the arguments. Throughout your training loop or experiment, you can use wandb.log() to log metrics such as loss and accuracy. Finally, wandb.finish() is called to finish the wandb run. You can then view and analyze your experiment results on the Wandb platform.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Philo`

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值