{ "cells": [ { "cell_type": "markdown", "id": "3de606b8", "metadata": { "origin_pos": 0 }, "source": [ "# 自然语言推断:微调BERT\n", ":label:`sec_natural-language-inference-bert`\n", "\n", "在本章的前面几节中,我们已经为SNLI数据集( :numref:`sec_natural-language-inference-and-dataset`)上的自然语言推断任务设计了一个基于注意力的结构( :numref:`sec_natural-language-inference-attention`)。现在,我们通过微调BERT来重新审视这项任务。正如在 :numref:`sec_finetuning-bert`中讨论的那样,自然语言推断是一个序列级别的文本对分类问题,而微调BERT只需要一个额外的基于多层感知机的架构,如 :numref:`fig_nlp-map-nli-bert`中所示。\n", "\n", "![将预训练BERT提供给基于多层感知机的自然语言推断架构](../img/nlp-map-nli-bert.svg)\n", ":label:`fig_nlp-map-nli-bert`\n", "\n", "本节将下载一个预训练好的小版本的BERT,然后对其进行微调,以便在SNLI数据集上进行自然语言推断。\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "b8292939", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T07:03:01.182131Z", "iopub.status.busy": "2023-08-18T07:03:01.181284Z", "iopub.status.idle": "2023-08-18T07:03:04.072192Z", "shell.execute_reply": "2023-08-18T07:03:04.070948Z" }, "origin_pos": 2, "tab": [ "pytorch" ] }, "outputs": [], "source": [ "import json\n", "import multiprocessing\n", "import os\n", "import torch\n", "from torch import nn\n", "from d2l import torch as d2l" ] }, { "cell_type": "markdown", "id": "c36b8eaa", "metadata": { "origin_pos": 4 }, "source": [ "## [**加载预训练的BERT**]\n", "\n", "我们已经在 :numref:`sec_bert-dataset`和 :numref:`sec_bert-pretraining`WikiText-2数据集上预训练BERT(请注意,原始的BERT模型是在更大的语料库上预训练的)。正如在 :numref:`sec_bert-pretraining`中所讨论的,原始的BERT模型有数以亿计的参数。在下面,我们提供了两个版本的预训练的BERT:“bert.base”与原始的BERT基础模型一样大,需要大量的计算资源才能进行微调,而“bert.small”是一个小版本,以便于演示。\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "e9c4aae6", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T07:03:04.081229Z", "iopub.status.busy": "2023-08-18T07:03:04.079078Z", "iopub.status.idle": "2023-08-18T07:03:04.087710Z", "shell.execute_reply": "2023-08-18T07:03:04.086601Z" }, "origin_pos": 6, "tab": [ "pytorch" ] }, "outputs": [], "source": [ "d2l.DATA_HUB['bert.base'] = (d2l.DATA_URL + 'bert.base.torch.zip',\n", " '225d66f04cae318b841a13d32af3acc165f253ac')\n", "d2l.DATA_HUB['bert.small'] = (d2l.DATA_URL + 'bert.small.torch.zip',\n", " 'c72329e68a732bef0452e4b96a1c341c8910f81f')" ] }, { "cell_type": "markdown", "id": "b5203402", "metadata": { "origin_pos": 8 }, "source": [ "两个预训练好的BERT模型都包含一个定义词表的“vocab.json”文件和一个预训练参数的“pretrained.params”文件。我们实现了以下`load_pretrained_model`函数来[**加载预先训练好的BERT参数**]。\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "d9d49c25", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T07:03:04.095593Z", "iopub.status.busy": "2023-08-18T07:03:04.093665Z", "iopub.status.idle": "2023-08-18T07:03:04.106137Z", "shell.execute_reply": "2023-08-18T07:03:04.104967Z" }, "origin_pos": 10, "tab": [ "pytorch" ] }, "outputs": [], "source": [ "def load_pretrained_model(pretrained_model, num_hiddens, ffn_num_hiddens,\n", " num_heads, num_layers, dropout, max_len, devices):\n", " data_dir = d2l.download_extract(pretrained_model)\n", " # 定义空词表以加载预定义词表\n", " vocab = d2l.Vocab()\n", " vocab.idx_to_token = json.load(open(os.path.join(data_dir,\n", " 'vocab.json')))\n", " vocab.token_to_idx = {token: idx for idx, token in enumerate(\n", " vocab.idx_to_token)}\n", " bert = d2l.BERTModel(len(vocab), num_hiddens, norm_shape=[256],\n", " ffn_num_input=256, ffn_num_hiddens=ffn_num_hiddens,\n", " num_heads=4, num_layers=2, dropout=0.2,\n", " max_len=max_len, key_size=256, query_size=256,\n", " value_size=256, hid_in_features=256,\n", " mlm_in_features=256, nsp_in_features=256)\n", " # 加载预训练BERT参数\n", " bert.load_state_dict(torch.load(os.path.join(data_dir,\n", " 'pretrained.params')))\n", " return bert, vocab" ] }, { "cell_type": "markdown", "id": "3ed9ec34", "metadata": { "origin_pos": 12 }, "source": [ "为了便于在大多数机器上演示,我们将在本节中加载和微调经过预训练BERT的小版本(“bert.small”)。在练习中,我们将展示如何微调大得多的“bert.base”以显著提高测试精度。\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "83fa73f8", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T07:03:04.114631Z", "iopub.status.busy": "2023-08-18T07:03:04.112010Z", "iopub.status.idle": "2023-08-18T07:03:08.335657Z", "shell.execute_reply": "2023-08-18T07:03:08.334563Z" }, "origin_pos": 13, "tab": [ "pytorch" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Downloading ../data/bert.small.torch.zip from http://d2l-data.s3-accelerate.amazonaws.com/bert.small.torch.zip...\n" ] } ], "source": [ "devices = d2l.try_all_gpus()\n", "bert, vocab = load_pretrained_model(\n", " 'bert.small', num_hiddens=256, ffn_num_hiddens=512, num_heads=4,\n", " num_layers=2, dropout=0.1, max_len=512, devices=devices)" ] }, { "cell_type": "markdown", "id": "2f4be336", "metadata": { "origin_pos": 15 }, "source": [ "## [**微调BERT的数据集**]\n", "\n", "对于SNLI数据集的下游任务自然语言推断,我们定义了一个定制的数据集类`SNLIBERTDataset`。在每个样本中,前提和假设形成一对文本序列,并被打包成一个BERT输入序列,如 :numref:`fig_bert-two-seqs`所示。回想 :numref:`subsec_bert_input_rep`,片段索引用于区分BERT输入序列中的前提和假设。利用预定义的BERT输入序列的最大长度(`max_len`),持续移除输入文本对中较长文本的最后一个标记,直到满足`max_len`。为了加速生成用于微调BERT的SNLI数据集,我们使用4个工作进程并行生成训练或测试样本。\n" ] }, { "cell_type": "code", "execution_count": 5, "id": "3c6424fb", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T07:03:08.341547Z", "iopub.status.busy": "2023-08-18T07:03:08.340816Z", "iopub.status.idle": "2023-08-18T07:03:08.359529Z", "shell.execute_reply": "2023-08-18T07:03:08.358243Z" }, "origin_pos": 17, "tab": [ "pytorch" ] }, "outputs": [], "source": [ "class SNLIBERTDataset(torch.utils.data.Dataset):\n", " def __init__(self, dataset, max_len, vocab=None):\n", " all_premise_hypothesis_tokens = [[\n", " p_tokens, h_tokens] for p_tokens, h_tokens in zip(\n", " *[d2l.tokenize([s.lower() for s in sentences])\n", " for sentences in dataset[:2]])]\n", "\n", " self.labels = torch.tensor(dataset[2])\n", " self.vocab = vocab\n", " self.max_len = max_len\n", " (self.all_token_ids, self.all_segments,\n", " self.valid_lens) = self._preprocess(all_premise_hypothesis_tokens)\n", " print('read ' + str(len(self.all_token_ids)) + ' examples')\n", "\n", " def _preprocess(self, all_premise_hypothesis_tokens):\n", " pool = multiprocessing.Pool(4) # 使用4个进程\n", " out = pool.map(self._mp_worker, all_premise_hypothesis_tokens)\n", " all_token_ids = [\n", " token_ids for token_ids, segments, valid_len in out]\n", " all_segments = [segments for token_ids, segments, valid_len in out]\n", " valid_lens = [valid_len for token_ids, segments, valid_len in out]\n", " return (torch.tensor(all_token_ids, dtype=torch.long),\n", " torch.tensor(all_segments, dtype=torch.long),\n", " torch.tensor(valid_lens))\n", "\n", " def _mp_worker(self, premise_hypothesis_tokens):\n", " p_tokens, h_tokens = premise_hypothesis_tokens\n", " self._truncate_pair_of_tokens(p_tokens, h_tokens)\n", " tokens, segments = d2l.get_tokens_and_segments(p_tokens, h_tokens)\n", " token_ids = self.vocab[tokens] + [self.vocab['']] \\\n", " * (self.max_len - len(tokens))\n", " segments = segments + [0] * (self.max_len - len(segments))\n", " valid_len = len(tokens)\n", " return token_ids, segments, valid_len\n", "\n", " def _truncate_pair_of_tokens(self, p_tokens, h_tokens):\n", " # 为BERT输入中的''、''和''词元保留位置\n", " while len(p_tokens) + len(h_tokens) > self.max_len - 3:\n", " if len(p_tokens) > len(h_tokens):\n", " p_tokens.pop()\n", " else:\n", " h_tokens.pop()\n", "\n", " def __getitem__(self, idx):\n", " return (self.all_token_ids[idx], self.all_segments[idx],\n", " self.valid_lens[idx]), self.labels[idx]\n", "\n", " def __len__(self):\n", " return len(self.all_token_ids)" ] }, { "cell_type": "markdown", "id": "1972357c", "metadata": { "origin_pos": 19 }, "source": [ "下载完SNLI数据集后,我们通过实例化`SNLIBERTDataset`类来[**生成训练和测试样本**]。这些样本将在自然语言推断的训练和测试期间进行小批量读取。\n" ] }, { "cell_type": "code", "execution_count": 6, "id": "ba13f731", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T07:03:08.365140Z", "iopub.status.busy": "2023-08-18T07:03:08.364679Z", "iopub.status.idle": "2023-08-18T07:04:03.295593Z", "shell.execute_reply": "2023-08-18T07:04:03.294685Z" }, "origin_pos": 21, "tab": [ "pytorch" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "read 549367 examples\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "read 9824 examples\n" ] } ], "source": [ "# 如果出现显存不足错误,请减少“batch_size”。在原始的BERT模型中,max_len=512\n", "batch_size, max_len, num_workers = 512, 128, d2l.get_dataloader_workers()\n", "data_dir = d2l.download_extract('SNLI')\n", "train_set = SNLIBERTDataset(d2l.read_snli(data_dir, True), max_len, vocab)\n", "test_set = SNLIBERTDataset(d2l.read_snli(data_dir, False), max_len, vocab)\n", "train_iter = torch.utils.data.DataLoader(train_set, batch_size, shuffle=True,\n", " num_workers=num_workers)\n", "test_iter = torch.utils.data.DataLoader(test_set, batch_size,\n", " num_workers=num_workers)" ] }, { "cell_type": "markdown", "id": "f6ae5ccc", "metadata": { "origin_pos": 23 }, "source": [ "## 微调BERT\n", "\n", "如 :numref:`fig_bert-two-seqs`所示,用于自然语言推断的微调BERT只需要一个额外的多层感知机,该多层感知机由两个全连接层组成(请参见下面`BERTClassifier`类中的`self.hidden`和`self.output`)。[**这个多层感知机将特殊的“<cls>”词元**]的BERT表示进行了转换,该词元同时编码前提和假设的信息(**为自然语言推断的三个输出**):蕴涵、矛盾和中性。\n" ] }, { "cell_type": "code", "execution_count": 7, "id": "8e26a41e", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T07:04:03.300395Z", "iopub.status.busy": "2023-08-18T07:04:03.299910Z", "iopub.status.idle": "2023-08-18T07:04:03.306353Z", "shell.execute_reply": "2023-08-18T07:04:03.305313Z" }, "origin_pos": 25, "tab": [ "pytorch" ] }, "outputs": [], "source": [ "class BERTClassifier(nn.Module):\n", " def __init__(self, bert):\n", " super(BERTClassifier, self).__init__()\n", " self.encoder = bert.encoder\n", " self.hidden = bert.hidden\n", " self.output = nn.Linear(256, 3)\n", "\n", " def forward(self, inputs):\n", " tokens_X, segments_X, valid_lens_x = inputs\n", " encoded_X = self.encoder(tokens_X, segments_X, valid_lens_x)\n", " return self.output(self.hidden(encoded_X[:, 0, :]))" ] }, { "cell_type": "markdown", "id": "1a8a1ecb", "metadata": { "origin_pos": 27 }, "source": [ "在下文中,预训练的BERT模型`bert`被送到用于下游应用的`BERTClassifier`实例`net`中。在BERT微调的常见实现中,只有额外的多层感知机(`net.output`)的输出层的参数将从零开始学习。预训练BERT编码器(`net.encoder`)和额外的多层感知机的隐藏层(`net.hidden`)的所有参数都将进行微调。\n" ] }, { "cell_type": "code", "execution_count": 8, "id": "b814689b", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T07:04:03.310896Z", "iopub.status.busy": "2023-08-18T07:04:03.310446Z", "iopub.status.idle": "2023-08-18T07:04:03.315332Z", "shell.execute_reply": "2023-08-18T07:04:03.314376Z" }, "origin_pos": 29, "tab": [ "pytorch" ] }, "outputs": [], "source": [ "net = BERTClassifier(bert)" ] }, { "cell_type": "markdown", "id": "7506eb91", "metadata": { "origin_pos": 30 }, "source": [ "回想一下,在 :numref:`sec_bert`中,`MaskLM`类和`NextSentencePred`类在其使用的多层感知机中都有一些参数。这些参数是预训练BERT模型`bert`中参数的一部分,因此是`net`中的参数的一部分。然而,这些参数仅用于计算预训练过程中的遮蔽语言模型损失和下一句预测损失。这两个损失函数与微调下游应用无关,因此当BERT微调时,`MaskLM`和`NextSentencePred`中采用的多层感知机的参数不会更新(陈旧的,staled)。\n", "\n", "为了允许具有陈旧梯度的参数,标志`ignore_stale_grad=True`在`step`函数`d2l.train_batch_ch13`中被设置。我们通过该函数使用SNLI的训练集(`train_iter`)和测试集(`test_iter`)对`net`模型进行训练和评估。由于计算资源有限,[**训练**]和测试精度可以进一步提高:我们把对它的讨论留在练习中。\n" ] }, { "cell_type": "code", "execution_count": 9, "id": "b98d38fc", "metadata": { "execution": { "iopub.execute_input": "2023-08-18T07:04:03.320237Z", "iopub.status.busy": "2023-08-18T07:04:03.319363Z", "iopub.status.idle": "2023-08-18T07:08:57.319142Z", "shell.execute_reply": "2023-08-18T07:08:57.318209Z" }, "origin_pos": 32, "tab": [ "pytorch" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "loss 0.520, train acc 0.790, test acc 0.779\n", "10442.5 examples/sec on [device(type='cuda', index=0), device(type='cuda', index=1)]\n" ] }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", " \n", " 2023-08-18T07:08:57.275417\n", " image/svg+xml\n", " \n", " \n", " Matplotlib v3.5.1, https://matplotlib.org/\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n" ], "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "lr, num_epochs = 1e-4, 5\n", "trainer = torch.optim.Adam(net.parameters(), lr=lr)\n", "loss = nn.CrossEntropyLoss(reduction='none')\n", "d2l.train_ch13(net, train_iter, test_iter, loss, trainer, num_epochs,\n", " devices)" ] }, { "cell_type": "markdown", "id": "8abd8300", "metadata": { "origin_pos": 34 }, "source": [ "## 小结\n", "\n", "* 我们可以针对下游应用对预训练的BERT模型进行微调,例如在SNLI数据集上进行自然语言推断。\n", "* 在微调过程中,BERT模型成为下游应用模型的一部分。仅与训练前损失相关的参数在微调期间不会更新。\n", "\n", "## 练习\n", "\n", "1. 如果您的计算资源允许,请微调一个更大的预训练BERT模型,该模型与原始的BERT基础模型一样大。修改`load_pretrained_model`函数中的参数设置:将“bert.small”替换为“bert.base”,将`num_hiddens=256`、`ffn_num_hiddens=512`、`num_heads=4`和`num_layers=2`的值分别增加到768、3072、12和12。通过增加微调迭代轮数(可能还会调优其他超参数),你可以获得高于0.86的测试精度吗?\n", "1. 如何根据一对序列的长度比值截断它们?将此对截断方法与`SNLIBERTDataset`类中使用的方法进行比较。它们的利弊是什么?\n" ] }, { "cell_type": "markdown", "id": "4a2eba9d", "metadata": { "origin_pos": 36, "tab": [ "pytorch" ] }, "source": [ "[Discussions](https://discuss.d2l.ai/t/5718)\n" ] } ], "metadata": { "language_info": { "name": "python" }, "required_libs": [] }, "nbformat": 4, "nbformat_minor": 5 }