当前位置:   article > 正文

Huggingface-NLP笔记7:使用Trainer API来微调模型_you should probably train this model on a down-str

you should probably train this model on a down-stream task to be able to use

「Huggingface NLP笔记系列-第7集」
最近跟着Huggingface上的NLP tutorial走了一遍,惊叹居然有如此好的讲解Transformers系列的NLP教程,于是决定记录一下学习的过程,分享我的笔记,可以算是官方教程的精简+注解版。但最推荐的,还是直接跟着官方教程来一遍,真是一种享受。


使用Trainer API来微调模型

1. 数据集准备和预处理:

这部分就是回顾上一集的内容:

  • 通过dataset包加载数据集
  • 加载预训练模型和tokenizer
  • 定义Dataset.map要使用的预处理函数
  • 定义DataCollator来用于构造训练batch
  1. import numpy as np
  2. from transformers import AutoTokenizer, DataCollatorWithPadding
  3. import datasets
  4. checkpoint = 'bert-base-cased'
  5. tokenizer = AutoTokenizer.from_pretrained(checkpoint)
  6. raw_datasets = datasets.load_dataset('glue', 'mrpc')
  7. def tokenize_function(sample):
  8. return tokenizer(sample['sentence1'], sample['sentence2'], truncation=True)
  9. tokenized_datasets = raw_datasets.map(tokenize_function, batched=True)
  10. data_collator = DataCollatorWithPadding(tokenizer=tokenizer)

2. 加载我们要fine-tune的模型:

  1. from transformers import AutoModelForSequenceClassification
  2. model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2)
  3. >>> (warnings)
  4. Some weights of the model checkpoint at bert-base-cased were not used when initializing BertForSequenceClassification: ['cls.predictions.bias', 'cls.predictions.transform.dense.weight', 'cls.predictions.transform.dense.bias', 'cls.predictions.decoder.weight', 'cls.seq_relationship.weight', 'cls.seq_relationship.bias', 'cls.predictions.transform.LayerNorm.weight', 'cls.predictions.transform.LayerNorm.bias']
  5. - This IS expected if you are initializing BertForSequenceClassification from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).
  6. - This IS NOT expected if you are initializing BertForSequenceClassification from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).
  7. Some weights of BertForSequenceClassification were not initialized from the model checkpoint at bert-base-cased and are newly initialized: ['classifier.weight', 'classifier.bias']
  8. You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.

不得不说,这个Huggingface很贴心,这里的warning写的很清楚。这里我们使用的是带ForSequenceClassification这个Head的模型,但是我们的bert-baed-cased虽然它本身也有自身的Head,但跟我们这里的二分类任务不匹配,所以可以看到,它的Head被移除了,使用了一个随机初始化的ForSequenceClassificationHead。

所以这里提示还说:"You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference."

3. 使用Trainer来训练

Trainer是Huggingface transformers库的一个高级API,可以帮助我们快速搭建训练框架:

  1. from transformers import Trainer, TrainingArguments
  2. training_args = TrainingArguments(output_dir='test_trainer') # 指定输出文件夹,没有会自动创建
  3. trainer = Trainer(
  4. model,
  5. training_args,
  6. train_dataset=tokenized_datasets["train"],
  7. eval_dataset=tokenized_datasets["validation"],
  8. data_collator=data_collator, # 在定义了tokenizer之后,其实这里的data_collator就不用再写了,会自动根据tokenizer创建
  9. tokenizer=tokenizer,
  10. )

我们看看TrainingArgumentsTrainer的参数都有些啥:

  1. TrainingArguments(
  2. output_dir: Union[str, NoneType] = None,
  3. overwrite_output_dir: bool = False,
  4. do_train: bool = False,
  5. do_eval: bool = None,
  6. do_predict: bool = False,
  7. evaluation_strategy: transformers.trainer_utils.EvaluationStrategy = 'no',
  8. prediction_loss_only: bool = False,
  9. per_device_train_batch_size: int = 8, # 默认的batch_size=8
  10. per_device_eval_batch_size: int = 8,
  11. per_gpu_train_batch_size: Union[int, NoneType] = None,
  12. per_gpu_eval_batch_size: Union[int, NoneType] = None,
  13. gradient_accumulation_steps: int = 1,
  14. eval_accumulation_steps: Union[int, NoneType] = None,
  15. learning_rate: float = 5e-05,
  16. weight_decay: float = 0.0,
  17. adam_beta1: float = 0.9,
  18. adam_beta2: float = 0.999,
  19. adam_epsilon: float = 1e-08,
  20. max_grad_norm: float = 1.0,
  21. num_train_epochs: float = 3.0, # 默认跑3
  22. ...
  23. Trainer(
  24. model: Union[transformers.modeling_utils.PreTrainedModel, torch.nn.modules.module.Module] = None,
  25. args: transformers.training_args.TrainingArguments = None,
  26. data_collator: Union[DataCollator, NoneType] = None,
  27. train_dataset: Union[torch.utils.data.dataset.Dataset, NoneType] = None,
  28. eval_dataset: Union[torch.utils.data.dataset.Dataset, NoneType] = None,
  29. tokenizer: Union[ForwardRef('PreTrainedTokenizerBase'), NoneType] = None,
  30. model_init: Callable[[], transformers.modeling_utils.PreTrainedModel] = None,
  31. compute_metrics: Union[Callable[[transformers.trainer_utils.EvalPrediction], Dict], NoneType] = None,
  32. callbacks: Union[List[transformers.trainer_callback.TrainerCallback], NoneType] = None,
  33. optimizers: Tuple[torch.optim.optimizer.Optimizer, torch.optim.lr_scheduler.LambdaLR] = (None, None), # 默认会使用AdamW
  34. )
  35. Docstring:
  36. Trainer is a simple but feature-complete training and eval loop for PyTorch, optimized for Transformers.

可见,这个Trainer把所有训练中需要考虑的参数、设计都包括在内了,我们可以在这里指定训练验证集、data_collator、metrics、optimizer,并通过TrainingArguments来提供各种超参数。

默认情况下,TrainerTrainingArguments会使用:

  • batch size=8
  • epochs = 3
  • AdamW优化器

定义好之后,直接使用.train()来启动训练:

trainer.train()

输出:

TrainOutput(global_step=1377, training_loss=0.35569445984728887, metrics={'train_runtime': 383.0158, 'train_samples_per_second': 3.595, 'total_flos': 530185443455520, 'epoch': 3.0})

然后我们用Trainer来预测:

trainer.predict()函数处理的结果是一个named_tuple(一种可以直接通过key来取值的tuple),类似一个字典,包含三个属性:predictions, label_ids, metrics

注意,这里的三个属性:

  • predictions实际上就是logits
  • label_ids不是预测出来的id,而是数据集中自带的ground truth的标签,因此如果输入的数据集中没给标签,这里也不会输出
  • metrics,也是只有输入的数据集中提供了label_ids才会输出metrics,包括loss之类的指标

其中metrics中还可以包含我们自定义的字段,我们需要在定义Trainer的时候给定compute_metrics参数。

文档参考: https://huggingface.co/transformers/master/main_classes/trainer.html#transformers.Trainer.predict

  1. predictions = trainer.predict(tokenized_datasets['validation'])
  2. print(predictions.predictions.shape) # logits
  3. # array([[-2.7887206, 3.1986978],
  4. # [ 2.5258656, -1.832253 ], ...], dtype=float32)
  5. print(predictions.label_ids.shape) # array([1, 0, 0, 1, 0, 1, 0, 1, 1, 1, ...], dtype=int64)
  6. print(predictions.metrics)

输出:

  1. [51/51 00:03]
  2. (408, 2)
  3. (408,)
  4. {'eval_loss': 0.7387174963951111, 'eval_runtime': 3.2872, 'eval_samples_per_second': 124.117}

然后就可以用preds和labels来计算一些相关的metrics了。

Huggingface datasets里面可以直接导入跟数据集相关的metrics:

  1. from datasets import load_metric
  2. preds = np.argmax(predictions.predictions, axis=-1)
  3. metric = load_metric('glue', 'mrpc')
  4. metric.compute(predictions=preds, references=predictions.label_ids)
  5. >>>
  6. {'accuracy': 0.8455882352941176, 'f1': 0.8911917098445595}

看看这里的metric(glue type)的文档:

  1. Args:
  2. predictions: list of predictions to score.
  3. Each translation should be tokenized into a list of tokens.
  4. references: list of lists of references for each translation.
  5. Each reference should be tokenized into a list of tokens.
  6. Returns: depending on the GLUE subset, one or several of:
  7. "accuracy": Accuracy
  8. "f1": F1 score
  9. "pearson": Pearson Correlation
  10. "spearmanr": Spearman Correlation
  11. "matthews_correlation": Matthew Correlation

4.构建Trainer中的compute_metrics函数

前面我们注意到Trainer的参数中,可以提供一个compute_metrics函数,用于输出我们希望有的一些指标。

这个compute_metrics有一些输入输出的要求:

  • 输入:是一个EvalPrediction对象,是一个named tuple,需要有至少predictionslabel_ids两个字段;经过查看源码,这里的predictions,就是logits
  • 输出:一个字典,包含各个metrics和对应的数值。

源码地址: https://huggingface.co/transformers/master/_modules/transformers/trainer.html#Trainer

  1. from datasets import load_metric
  2. def compute_metrics(eval_preds):
  3. metric = load_metric("glue", "mrpc")
  4. logits, labels = eval_preds.predictions, eval_preds.label_ids
  5. # 上一行可以直接简写成:
  6. # logits, labels = eval_preds 因为它相当于一个tuple
  7. predictions = np.argmax(logits, axis=-1)
  8. return metric.compute(predictions=predictions, references=labels)

总结一下这个过程:

  • 首先我们定义了一个compute_metrics函数,交给Trainer
  • Trainer训练模型,模型会对样本计算,产生 predictions (logits);
  • Trainer再把 predictions 和数据集中给定的 label_ids 打包成一个对象,发送给compute_metrics函数;
  • compute_metrics函数计算好相应的 metrics 然后返回。

看看带上了 compute_metrics 之后的训练:

  1. training_args = TrainingArguments(output_dir='test_trainer', evaluation_strategy='epoch')
  2. model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2) # new model
  3. trainer = Trainer(
  4. model,
  5. training_args,
  6. train_dataset=tokenized_datasets["train"],
  7. eval_dataset=tokenized_datasets["validation"],
  8. data_collator=data_collator, # 在定义了tokenizer之后,其实这里的data_collator就不用再写了,会自动根据tokenizer创建
  9. tokenizer=tokenizer,
  10. compute_metrics=compute_metrics
  11. )
  12. trainer.train()

输出:

TrainOutput(global_step=1377, training_loss=0.32063739751678666, metrics={'train_runtime': 414.1719, 'train_samples_per_second': 3.325, 'total_flos': 530351810395680, 'epoch': 3.0})

可见,带上了compute_metircs函数之后,在Trainer训练过程中,会把增加的metric也打印出来,方便我们时刻了解训练的进展。


前情回顾:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/364830
推荐阅读
相关标签
  

闽ICP备14008679号