python - 在 GLUE 任务上微调 BERT 时,如何监控训练和评估损失?

标签 python pytorch huggingface-transformers

我正在运行 https://github.com/huggingface/transformers/blob/master/examples/run_glue.py对二元分类任务 (CoLA) 执行微调。 我想同时监控训练和评估损失以防止过度拟合。

目前库是 2.8.0,我是从源代码安装的。

当我用

运行示例时
python run_glue.py --model_name_or_path bert-base-uncased 
                   --task_name CoLA 
                   --do_train 
                   --do_eval
                   --data_dir my_dir 
                   --max_seq_length 128
                   --per_gpu_train_batch_size 8
                   --per_gpu_eval_batch_size 8 
                   --learning_rate 2e-5
                   --num_train_epochs 3.0
                   --output_dir ./outputs
                   --logging_steps 5

在 stdout 日志中,我看到只有一个损失值的行,例如

{"learning_rate": 3.3333333333333333e-06, "loss": 0.47537623047828675, "step": 25}

通过查看 https://github.com/huggingface/transformers/blob/master/src/transformers/trainer.py我看到那里计算了训练和评估损失(在我看来,代码最近被重构了)。

我因此替换了https://github.com/huggingface/transformers/blob/abb1fa3f374811ea09d0bc3440d820c50735008d/src/transformers/trainer.py#L314

 cr_loss = self._training_step(model, inputs, optimizer)
 tr_loss += cr_loss

并在 https://github.com/huggingface/transformers/blob/abb1fa3f374811ea09d0bc3440d820c50735008d/src/transformers/trainer.py#L345 行之后添加

logs["training loss"] = cr_loss

有了这个我得到:

0502 14:12:18.644119 23632 summary.py:47] Summary name training loss is illegal; using training_loss instead.  
                          | 4/10  [00:02<00:04,  1.49it/s]  
{"learning_rate":  3.3333333333333333e-06, "loss": 0.47537623047828675, "training loss": 0.5451719760894775, "step": 25}

这样可以吗,还是我做错了什么?

在微调期间,在标准输出两者中监控给定记录间隔的平均训练和评估损失的最佳方法是什么?

最佳答案

如果安装较新的版本(我通过 pip 尝试了 2.9.0),代码中可能不需要更改:只需使用附加标志 --evaluate_during_training 启动微调,输出就可以了

0506 12:11:30.021593 34540 trainer.py:551] ***** Running Evaluation ***** 
I0506 12:11:30.022596 34540 trainer.py:552]   Num examples = 140  
I0506 12:11:30.023634 34540 trainer.py:553]   Batch size = 8 Evaluation:  
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 18/18 [00:19<00:00,  1.10s/it]  
{"eval_mcc": 0.0, "eval_loss": 0.6600487811697854, "learning_rate": 3.3333333333333333e-06, "loss": 0.50044886469841, "step": 25}

请注意,示例脚本经常更改,因此完成此操作的标志可能会更改名称...另请参见此处 https://discuss.huggingface.co/t/how-to-monitor-both-train-and-validation-metrics-at-the-same-step/1301

关于python - 在 GLUE 任务上微调 BERT 时,如何监控训练和评估损失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61551797/

相关文章:

pytorch - 如何定义拥抱面变压器管道的摘要比例?

python - 如何使用 Huggingface-Transformers 批量制作 Trainer pad 输入?

pytorch - 为什么在 iOS 上使用 Caffe2 或 Core-ML 而不是 LibTorch(.pt 文件)?

python - 获取 matplotlib 颜色循环状态

python - OpenCV+Python : Firewire Cams not supported on Windows XP?

python - 使用 Python 将所有文件从一个目录 move 到另一个目录

python - 在 Pytorch 中对张量 [batch, channel, sequence, H,W] 运行 conv2d

pytorch - 在管道中使用带有参数的拥抱面转换器

google-colaboratory - Colab 的 Huggingface AlBert 标记器 NoneType 错误

python - Suds 生成空元素;如何删除它们?