在 GitHub 上编辑

Hugging Face Accelerate

DVCLive 使您能够为您的 Hugging Face Accelerate 项目添加实验跟踪功能。

用法

如果您已安装 dvclive,则 DVCLive Tracker 将用于跟踪实验并记录指标、参数和图表,适用于 accelerate>=0.25.0。与 Hugging Face Transformers 回调 不同,您必须明确指定要记录的内容。以下代码片段展示了一个完整示例:

from accelerate import Accelerator

# optional, `log_with` defaults to "all"
accelerator = Accelerator(log_with="dvclive")

# log hyperparameters
hps = {"num_iterations": 5, "learning_rate": 1e-2}
accelerator.init_trackers("my_project", config=hps)

# log metrics
accelerator.log({"train_loss": 1.12, "valid_loss": 0.8})

# log model
accelerator.save_state("checkpoint_dir")
if accelerator.is_main_process:
    live = accelerator.get_tracker("dvclive", unwrap=True)
    live.log_artifact("checkpoint_dir")

# end logging
accelerator.end_training()

初始化

from accelerate import Accelerator

# optional, `log_with` defaults to "all"
accelerator = Accelerator(log_with="dvclive")
accelerator.init_trackers(project_name="my_project")

要自定义跟踪行为,请使用 init_kwargsLive 实例传递参数,例如:

accelerator.init_trackers(
    project_name="my_project",
    init_kwargs={"dvclive": {"dir": "my_directory"}}
)

记录参数

要记录超参数,请使用 config 添加,例如:

hps = {"num_iterations": 5, "learning_rate": 1e-2}
accelerator.init_trackers("my_project", config=hps)

记录指标

要记录指标:

accelerator.log({"train_loss": 1.12, "valid_loss": 0.8})

可选地传递步数(若未传递,将自动递增):

accelerator.log({"train_loss": 1.12, "valid_loss": 0.8}, step=1)

额外记录

要记录模型、其他工件或图像,请从跟踪器中获取 Live 实例:

accelerator.save_state("checkpoint_dir")
if accelerator.is_main_process:
    live = accelerator.get_tracker("dvclive", unwrap=True)
    live.log_artifact("checkpoint_dir")

accelerator.is_main_process 确保仅在主进程中调用。上述其他方法中,Accelerate 的 Tracker 会自动处理此逻辑,但在此示例中我们直接调用了 Live

unwrap=True 将返回 Live 实例而非 Tracker,以便您可以调用任何 Live 方法。

结束

最后,结束实验以触发 Live.end()

accelerator.end_training()
内容

🐛 发现问题?告诉我们!或者修复它:

在 GitHub 上编辑

有疑问?加入我们的聊天,我们会为您提供帮助:

Discord 聊天