Transformer实战——使用 run_glue.py 微调模型

在现代自然语言处理(NLP)领域,Transformer架构已经成为了主流模型。其卓越的性能在各种任务上都展现得淋漓尽致,从文本分类到问答系统,Transformer模型的应用无处不在。本文将详细介绍如何使用`run_glue.py`脚本微调Transformer模型,特别是在GLUE(General Language Understanding Evaluation)基准上进行实战。

## 1. 什么是GLUE?

GLUE是一个多任务基准,旨在评估和比较自然语言处理模型在多种语言理解任务上的表现。它包括多个子任务,如文本蕴含、情感分析、问答等。通过在GLUE上进行微调,模型的泛化能力和性能得到了有效检验。

## 2. 环境准备

在开始微调之前,需要确保已经搭建好相应的环境。一般来说,推荐使用Python 3.6及以上版本,并安装流行的深度学习框架如PyTorch或TensorFlow。

### 2.1 安装依赖

首先,安装基础依赖项,你可以使用以下命令:

```bash
pip install transformers
pip install datasets
pip install torch  # 如果你使用的是PyTorch
```

如果你打算使用TensorFlow,则需要安装TensorFlow:

```bash
pip install tensorflow
```

### 2.2 下载模型和数据集

Hugging Face的Transformers库提供了多种预训练模型,这为我们的微调过程提供了便利。你可以选择BERT、RoBERTa等模型。以下是获取模型和GLUE数据集的步骤:

```bash
git clone https://github.com/huggingface/transformers.git
cd transformers/examples/pytorch/text-classification
```

### 2.3 数据集准备

<
[07/16 09:53:29] ppdet.utils.checkpoint INFO: Skipping import of the encryption module. loading annotations into memory... Done (t=0.00s) creating index... index created! [07/16 09:53:29] ppdet.data.source.coco INFO: Load [217 samples valid, 0 samples invalid] in file C:\Users\Administrator\Desktop\PaddleX\dataset\min_pai\annotations/instance_train.json. use cuda ms_deformable_attn op! [07/16 09:53:33] ppdet.utils.checkpoint INFO: The shape [23] in pretrained weight transformer.dec_score_head.0.bias is unmatched with the shape [11] in model transformer.dec_score_head.0.bias. And the weight transformer.dec_score_head.0.bias will not be loaded [07/16 09:53:33] ppdet.utils.checkpoint INFO: The shape [256, 23] in pretrained weight transformer.dec_score_head.0.weight is unmatched with the shape [256, 11] in model transformer.dec_score_head.0.weight. And the weight transformer.dec_score_head.0.weight will not be loaded [07/16 09:53:33] ppdet.utils.checkpoint INFO: The shape [23] in pretrained weight transformer.dec_score_head.1.bias is unmatched with the shape [11] in model transformer.dec_score_head.1.bias. And the weight transformer.dec_score_head.1.bias will not be loaded [07/16 09:53:33] ppdet.utils.checkpoint INFO: The shape [256, 23] in pretrained weight transformer.dec_score_head.1.weight is unmatched with the shape [256, 11] in model transformer.dec_score_head.1.weight. And the weight transformer.dec_score_head.1.weight will not be loaded [07/16 09:53:33] ppdet.utils.checkpoint INFO: The shape [23] in pretrained weight transformer.dec_score_head.2.bias is unmatched with the shape [11] in model transformer.dec_score_head.2.bias. And the weight transformer.dec_score_head.2.bias will not be loaded [07/16 09:53:33] ppdet.utils.checkpoint INFO: The shape [256, 23] in pretrained weight transformer.dec_score_head.2.weight is unmatched with the shape [256, 11] in model transformer.dec_score_head.2.weight. And the weight transformer.dec_score_head.2.weight will not be loaded [07/16 09:53:33] ppdet.utils.checkpoint INFO: The shape [23] in pretrained weight transformer.dec_score_head.3.bias is unmatched with the shape [11] in model transformer.dec_score_head.3.bias. And the weight transformer.dec_score_head.3.bias will not be loaded [07/16 09:53:33] ppdet.utils.checkpoint INFO: The shape [256, 23] in pretrained weight transformer.dec_score_head.3.weight is unmatched with the shape [256, 11] in model transformer.dec_score_head.3.weight. And the weight transformer.dec_score_head.3.weight will not be loaded [07/16 09:53:33] ppdet.utils.checkpoint INFO: The shape [23] in pretrained weight transformer.dec_score_head.4.bias is unmatched with the shape [11] in model transformer.dec_score_head.4.bias. And the weight transformer.dec_score_head.4.bias will not be loaded [07/16 09:53:33] ppdet.utils.checkpoint INFO: The shape [256, 23] in pretrained weight transformer.dec_score_head.4.weight is unmatched with the shape [256, 11] in model transformer.dec_score_head.4.weight. And the weight transformer.dec_score_head.4.weight will not be loaded [07/16 09:53:33] ppdet.utils.checkpoint INFO: The shape [23] in pretrained weight transformer.dec_score_head.5.bias is unmatched with the shape [11] in model transformer.dec_score_head.5.bias. And the weight transformer.dec_score_head.5.bias will not be loaded [07/16 09:53:33] ppdet.utils.checkpoint INFO: The shape [256, 23] in pretrained weight transformer.dec_score_head.5.weight is unmatched with the shape [256, 11] in model transformer.dec_score_head.5.weight. And the weight transformer.dec_score_head.5.weight will not be loaded [07/16 09:53:33] ppdet.utils.checkpoint INFO: The shape [23, 256] in pretrained weight transformer.denoising_class_embed.weight is unmatched with the shape [11, 256] in model transformer.denoising_class_embed.weight. And the weight transformer.denoising_class_embed.weight will not be loaded [07/16 09:53:33] ppdet.utils.checkpoint INFO: The shape [23] in pretrained weight transformer.enc_score_head.bias is unmatched with the shape [11] in model transformer.enc_score_head.bias. And the weight transformer.enc_score_head.bias will not be loaded [07/16 09:53:33] ppdet.utils.checkpoint INFO: The shape [256, 23] in pretrained weight transformer.enc_score_head.weight is unmatched with the shape [256, 11] in model transformer.enc_score_head.weight. And the weight transformer.enc_score_head.weight will not be loaded [07/16 09:53:33] ppdet.utils.checkpoint INFO: Finish loading model weights: C:\Users\Administrator/.cache/paddle/weights\PP-DocLayout-L_pretrained.pdparams Exception in thread Thread-2 (_thread_loop): Traceback (most recent call last): File "C:\ProgramData\miniconda3\envs\Paddlex\lib\threading.py", line 1016, in _bootstrap_inner self.run() File "C:\ProgramData\miniconda3\envs\Paddlex\lib\threading.py", line 953, in run self._target(*self._args, **self._kwargs) File "C:\ProgramData\miniconda3\envs\Paddlex\lib\site-packages\paddle\io\dataloader\dataloader_iter.py", line 282, in _thread_loop raise e File "C:\ProgramData\miniconda3\envs\Paddlex\lib\site-packages\paddle\io\dataloader\dataloader_iter.py", line 267, in _thread_loop tmp.set(slot, core.CPUPlace()) ValueError: (InvalidArgument) Input object type error or incompatible array data type. tensor.set() supports array with bool, float16, float32, float64, int8, int16, int32, int64, uint8 or uint16, please check your input or input array data type. (at ..\paddle/fluid/pybind/tensor_py.h:537) Traceback (most recent call last): File "C:\Users\Administrator\Desktop\PaddleX\paddlex\repo_manager\repos\PaddleDetection\tools\train.py", line 212, in <module> main() File "C:\Users\Administrator\Desktop\PaddleX\paddlex\repo_manager\repos\PaddleDetection\tools\train.py", line 208, in main run(FLAGS, cfg) File "C:\Users\Administrator\Desktop\PaddleX\paddlex\repo_manager\repos\PaddleDetection\tools\train.py", line 161, in run trainer.train(FLAGS.eval) File "C:\Users\Administrator\Desktop\PaddleX\paddlex\repo_manager\repos\PaddleDetection\ppdet\engine\trainer.py", line 567, in train for step_id, data in enumerate(self.loader): File "C:\ProgramData\miniconda3\envs\Paddlex\lib\site-packages\paddle\io\dataloader\dataloader_iter.py", line 298, in __next__ self._reader.read_next_list()[0] SystemError: (Fatal) Blocking queue is killed because the data reader raises an exception. [Hint: Expected killed_ != true, but received killed_:1 == true:1.] (at ..\paddle/fluid/operators/reader/blocking_queue.h:174)
07-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值