模型架构与原理3441 字
Transformers微调预训练模型
·全库 318 / 611
《Transformers微调预训练模型》共 0.3 万字,通篇围绕 数据集、微调、预训练、评估 展开,归在「模型架构与原理」这一层。
覆盖数据集微调预训练评估GPUCUDA
适合要跟模型团队对话、得听懂他们在说什么的人
目录骨架(8 节)
- 准备数据集
- 使用 PyTorch Trainer 进行训练
- 使用keras训练TensorFlow模型
- 在原生 PyTorch 中训练
- 附加资源
- 训练超参数
- 加载用于 Keras 的数据
- 将数据加载为 tf.data.Dataset
示例代码
注:将ppt后缀名改为zip后解压
微调预训练模型
使用预训练模型有许多显著的好处。它降低了计算成本,减少了碳排放,同时允许您使用最先进的模型,而无需从头开始训练一个。🤗 Transformers 提供了涉及各种任务的成千上万的预训练模型。当您使用预训练模型时,您需要在与任务相关的数据集上训练该模型。这种操作被称为微调,是一种非常强大的训练技术。在本教程中,您将使用您选择的深度学习框架来微调一个预训练模型:
- 使用 🤗 Transformers 的 [Trainer] 来微调预训练模型。
- 在 TensorFlow 中使用 Keras 来微调预训练模型。
- 在原生 PyTorch 中微调预训练模型。
准备数据集
在您进行预训练模型微调之前,需要下载一个数据集并为训练做好准备。之前的教程向您展示了如何处理训练数据,现在您有机会将这些技能付诸实践!
首先,加载Yelp评论数据集:
>>> from datasets import load_dataset
>>> dataset = load_dataset("yelp_review_full")
>>> dataset["train"][100]
{'label': 0,
'text': 'My expectations for McDonalds are t rarely high. But for one to still fail so spectacularly...that takes something special!\\nThe cashier took my friends\'s order, then promptly ignored me. I had to force myself in front of a cashier who opened his register to wait on the person BEHIND me. I waited over five minutes for a gigantic order that included precisely one kid\'s meal. After watching two people who ordered after me be handed their food, I asked where mine was. The manager started yelling at the cashiers for \\"serving off their orders\\" when they didn\'t have their food. But neither cashier was anywhere near those controls, and the manager was the one serving food to customers and clearing the boards.\\nThe manager was rude when giving me my order. She didn\'t make sure that I had everything ON MY RECEIPT, and never even had the decency to apologize that I felt I was getting poor service.\\nI\'ve eaten at various McDonalds restaurants for over 30 years. I\'ve worked at more than one location. I expect bad days, bad moods, and the occasional mistake. But I have yet to have a decent experience at this store. It will remain a place I avoid unless someone in my party needs to avoid illness from low blood sugar. Perhaps I should go back to the racially biased service of Steak n Shake instead!'}
正如您现在所知,您需要一个tokenizer来处理文本,包括填充和截断操作以处理可变的序列长度。如果要一次性处理您的数据集,可以使用 🤗 Datasets 的 map 方法,将预处理函数应用于整个数据集:
>>> from transformers import AutoTokenizer
>>> tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-cased")
>>> def tokenize_function(examples):
... return tokenizer(examples["text"], padding="max_length", truncation=True)