【问题标题】:Is there a way to use tensorboard SummaryWriter with HuggingFace TrainerAPI?有没有办法将 tensorboard SummaryWriter 与 HuggingFace TrainerAPI 一起使用?
【发布时间】:2022-10-07 08:51:31
【问题描述】:

我正在使用 HF Seq2SeqTrainingArguments 和 Seq2SeqTrainer 微调 HuggingFace 转换器模型(PyTorch 版本),并且我想在 Tensorboard 中显示训练和验证损失(在同一张图表中)。

据我了解,为了将两个损失一起绘制,我需要使用 SummaryWriter。 HF Callbacks 文档描述了一个可以接收 tb_writer 参数的 TensorBoardCallback 函数:

https://huggingface.co/docs/transformers/v4.21.1/en/main_classes/callback#transformers.integrations.TensorBoardCallback

但是,如果它甚至应该与 Trainer API 一起使用,我无法弄清楚使用它的正确方法是什么。

我的代码看起来像这样:

args = Seq2SeqTrainingArguments(
    output_dir=output_dir,
    evaluation_strategy='epoch',
    learning_rate= 1e-5,
    per_device_train_batch_size=batch_size,
    per_device_eval_batch_size=batch_size,
    weight_decay=0.01,
    save_total_limit=3,
    num_train_epochs=num_train_epochs,
    predict_with_generate=True,
    logging_steps=logging_steps,
    report_to='tensorboard',
    push_to_hub=False,  
)

trainer = Seq2SeqTrainer(
    model,
    args,
    train_dataset=tokenized_train_data,
    eval_dataset=tokenized_val_data,
    data_collator=data_collator,
    tokenizer=tokenizer,
    compute_metrics=compute_metrics,
)

我假设我应该在训练器中包含对 TensorBoard 的回调,例如,

callbacks = [TensorBoardCallback(tb_writer=tb_writer)]

但我找不到一个全面的例子来说明如何使用/导入什么来使用它。

我还在 GitHub 上找到了这个功能请求,

https://github.com/huggingface/transformers/pull/4020

但没有使用示例,所以我很困惑......

任何见解将不胜感激

【问题讨论】:

    标签: pytorch tensorboard huggingface-transformers


    【解决方案1】:

    这很简单。您在“Seq2SeqTrainingArguments”中提到了它。无需在“Seq2SeqTrainer”函数中明确定义。

    model_arguments = Seq2SeqTrainingArguments(output_dir= "./best_model/",
                                            num_train_epochs = EPOCHS, 
                                            overwrite_output_dir= True, 
                                            do_train= True, 
                                            do_eval= True, 
                                            do_predict= True, 
                                            auto_find_batch_size= True, 
                                            evaluation_strategy = 'epoch',
                                            warmup_steps = 10000, 
                                            logging_dir = "./log_files/", 
                                            disable_tqdm = False, 
                                            load_best_model_at_end = True, 
                                            save_strategy= 'epoch', 
                                            save_total_limit = 1, 
                                            per_device_eval_batch_size= BATCH_SIZE, 
                                            per_device_train_batch_size= BATCH_SIZE, 
                                            predict_with_generate=True, 
                                            report_to='wandb',
                                            run_name="rober_based_encoder_decoder_text_summarisation"
                                            
                                            )
    

    同时你可以有其他回调:

    early_stopping = EarlyStoppingCallback(early_stopping_patience= 5, 
                                        early_stopping_threshold= 0.001)
    

    然后通过训练器参数将参数和回调作为列表传递:

    trainer = Seq2SeqTrainer(model = model, 
                            compute_metrics= compute_metrics,
                            args= model_arguments, 
                            train_dataset= Train, 
                            eval_dataset= Val, 
                            tokenizer=tokenizer, 
                            callbacks= [early_stopping, ]
                            )
    

    训练模型。确保在训练前登录 wandb

    trainer.train()
    

    【讨论】:

      猜你喜欢
      • 2018-03-15
      • 2016-11-01
      • 1970-01-01
      • 2018-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多