PyTorch教程-15.9。预训练 BERT 的数据集

电子说

1.2w人已加入

描述

为了预训练第 15.8 节中实现的 BERT 模型,我们需要以理想的格式生成数据集,以促进两项预训练任务:掩码语言建模和下一句预测。一方面,原始的 BERT 模型是在两个巨大的语料库 BookCorpus 和英文维基百科(参见第15.8.5 节)的串联上进行预训练的,这使得本书的大多数读者难以运行。另一方面,现成的预训练 BERT 模型可能不适合医学等特定领域的应用。因此,在自定义数据集上预训练 BERT 变得越来越流行。为了便于演示 BERT 预训练,我们使用较小的语料库 WikiText-2 ( Merity et al. , 2016 )。

与 15.3节用于预训练word2vec的PTB数据集相比,WikiText-2(i)保留了原有的标点符号,适合下一句预测;(ii) 保留原始案例和编号;(iii) 大两倍以上。

 

import os
import random
import torch
from d2l import torch as d2l

 

 

import os
import random
from mxnet import gluon, np, npx
from d2l import mxnet as d2l

npx.set_np()

 

在 WikiText-2 数据集中,每一行代表一个段落,其中在任何标点符号及其前面的标记之间插入空格。保留至少两句话的段落。为了简单起见,为了拆分句子,我们只使用句点作为分隔符。我们将在本节末尾的练习中讨论更复杂的句子拆分技术。

 

#@save
d2l.DATA_HUB['wikitext-2'] = (
  'https://s3.amazonaws.com/research.metamind.io/wikitext/'
  'wikitext-2-v1.zip', '3c914d17d80b1459be871a5039ac23e752a53cbe')

#@save
def _read_wiki(data_dir):
  file_name = os.path.join(data_dir, 'wiki.train.tokens')
  with open(file_name, 'r') as f:
    lines = f.readlines()
  # Uppercase letters are converted to lowercase ones
  paragraphs = [line.strip().lower().split(' . ')
         for line in lines if len(line.split(' . ')) >= 2]
  random.shuffle(paragraphs)
  return paragraphs

 

 

#@save
d2l.DATA_HUB['wikitext-2'] = (
  'https://s3.amazonaws.com/research.metamind.io/wikitext/'
  'wikitext-2-v1.zip', '3c914d17d80b1459be871a5039ac23e752a53cbe')

#@save
def _read_wiki(data_dir):
  file_name = os.path.join(data_dir, 'wiki.train.tokens')
  with open(file_name, 'r') as f:
    lines = f.readlines()
  # Uppercase letters are converted to lowercase ones
  paragraphs = [line.strip().lower().split(' . ')
         for line in lines if len(line.split(' . ')) >= 2]
  random.shuffle(paragraphs)
  return paragraphs

 

15.9.1。为预训练任务定义辅助函数

下面,我们首先为两个 BERT 预训练任务实现辅助函数:下一句预测和掩码语言建模。这些辅助函数将在稍后将原始文本语料库转换为理想格式的数据集以预训练 BERT 时调用。

15.9.1.1。生成下一句预测任务

根据15.8.5.2 节的描述,该 _get_next_sentence函数为二元分类任务生成一个训练样例。

 

#@save
def _get_next_sentence(sentence, next_sentence, paragraphs):
  if random.random() < 0.5:
    is_next = True
  else:
    # `paragraphs` is a list of lists of lists
    next_sentence = random.choice(random.choice(paragraphs))
    is_next = False
  return sentence, next_sentence, is_next

 

 

#@save
def _get_next_sentence(sentence, next_sentence, paragraphs):
  if random.random() < 0.5:
    is_next = True
  else:
    # `paragraphs` is a list of lists of lists
    next_sentence = random.choice(random.choice(paragraphs))
    is_next = False
  return sentence, next_sentence, is_next

 

以下函数paragraph通过调用该 _get_next_sentence函数从输入生成用于下一句预测的训练示例。这paragraph是一个句子列表,其中每个句子都是一个标记列表。该参数 max_len指定预训练期间 BERT 输入序列的最大长度。

 

#@save
def _get_nsp_data_from_paragraph(paragraph, paragraphs, vocab, max_len):
  nsp_data_from_paragraph = []
  for i in range(len(paragraph) - 1):
    tokens_a, tokens_b, is_next = _get_next_sentence(
      paragraph[i], paragraph[i + 1], paragraphs)
    # Consider 1 '' token and 2 '' tokens
    if len(tokens_a) + len(tokens_b) + 3 > max_len:
      continue
    tokens, segments = d2l.get_tokens_and_segments(tokens_a, tokens_b)
    nsp_data_from_paragraph.append((tokens, segments, is_next))
  return nsp_data_from_paragraph

 

 

#@save
def _get_nsp_data_from_paragraph(paragraph, paragraphs, vocab, max_len):
  nsp_data_from_paragraph = []
  for i in range(len(paragraph) - 1):
    tokens_a, tokens_b, is_next = _get_next_sentence(
      paragraph[i], paragraph[i + 1], paragraphs)
    # Consider 1 '' token and 2 '' tokens
    if len(tokens_a) + len(tokens_b) + 3 > max_len:
      continue
    tokens, segments = d2l.get_tokens_and_segments(tokens_a, tokens_b)
    nsp_data_from_paragraph.append((tokens, segments, is_next))
  return nsp_data_from_paragraph

 

15.9.1.2。生成掩码语言建模任务

为了从 BERT 输入序列为掩码语言建模任务生成训练示例,我们定义了以下 _replace_mlm_tokens函数。在它的输入中,tokens是代表BERT输入序列的token列表,candidate_pred_positions 是BERT输入序列的token索引列表,不包括特殊token(masked语言建模任务中不预测特殊token),num_mlm_preds表示预测(召回 15% 的随机标记来预测)。遵循第 15.8.5.1 节中屏蔽语言建模任务的定义 ,在每个预测位置,输入可能被特殊的“”标记或随机标记替换,或者保持不变。最后,该函数返回可能替换后的输入标记、发生预测的标记索引以及这些预测的标签。

 

#@save
def _replace_mlm_tokens(tokens, candidate_pred_positions, num_mlm_preds,
            vocab):
  # For the input of a masked language model, make a new copy of tokens and
  # replace some of them by '' or random tokens
  mlm_input_tokens = [token for token in tokens]
  pred_positions_and_labels = []
  # Shuffle for getting 15% random tokens for prediction in the masked
  # language modeling task
  random.shuffle(candidate_pred_positions)
  for mlm_pred_position in candidate_pred_positions:
    if len(pred_positions_and_labels) >= num_mlm_preds:
      break
    masked_token = None
    # 80% of the time: replace the word with the '' token
    if random.random() < 0.8:
      masked_token = ''
    else:
      # 10% of the time: keep the word unchanged
      if random.random() < 0.5:
        masked_token = tokens[mlm_pred_position]
      # 10% of the time: replace the word with a random word
      else:
        masked_token = random.choice(vocab.idx_to_token)
    mlm_input_tokens[mlm_pred_position] = masked_token
    pred_positions_and_labels.append(
      (mlm_pred_position, tokens[mlm_pred_position]))
  return mlm_input_tokens, pred_positions_and_labels

 

 

#@save
def _replace_mlm_tokens(tokens, candidate_pred_positions, num_mlm_preds,
            vocab):
  # For the input of a masked language model, make a new copy of tokens and
  # replace some of them by '' or random tokens
  mlm_input_tokens = [token for token in tokens]
  pred_positions_and_labels = []
  # Shuffle for getting 15% random tokens for prediction in the masked
  # language modeling task
  random.shuffle(candidate_pred_positions)
  for mlm_pred_position in candidate_pred_positions:
    if len(pred_positions_and_labels) >= num_mlm_preds:
      break
    masked_token = None
    # 80% of the time: replace the word with the '' token
    if random.random() < 0.8:
      masked_token = ''
    else:
      # 10% of the time: keep the word unchanged
      if random.random() < 0.5:
        masked_token = tokens[mlm_pred_position]
      # 10% of the time: replace the word with a random word
      else:
        masked_token = random.choice(vocab.idx_to_token)
    mlm_input_tokens[mlm_pred_position] = masked_token
    pred_positions_and_labels.append(
      (mlm_pred_position, tokens[mlm_pred_position]))
  return mlm_input_tokens, pred_positions_and_labels

 

通过调用上述_replace_mlm_tokens函数,以下函数将 BERT 输入序列 ( tokens) 作为输入并返回输入标记的索引(在可能的标记替换之后,如第15.8.5.1 节所述)、发生预测的标记索引和标签这些预测的指标。

 

#@save
def _get_mlm_data_from_tokens(tokens, vocab):
  candidate_pred_positions = []
  # `tokens` is a list of strings
  for i, token in enumerate(tokens):
    # Special tokens are not predicted in the masked language modeling
    # task
    if token in ['', '']:
      continue
    candidate_pred_positions.append(i)
  # 15% of random tokens are predicted in the masked language modeling task
  num_mlm_preds = max(1, round(len(tokens) * 0.15))
  mlm_input_tokens, pred_positions_and_labels = _replace_mlm_tokens(
    tokens, candidate_pred_positions, num_mlm_preds, vocab)
  pred_positions_and_labels = sorted(pred_positions_and_labels,
                    key=lambda x: x[0])
  pred_positions = [v[0] for v in pred_positions_and_labels]
  mlm_pred_labels = [v[1] for v in pred_positions_and_labels]
  return vocab[mlm_input_tokens], pred_positions, vocab[mlm_pred_labels]

 

 

#@save
def _get_mlm_data_from_tokens(tokens, vocab):
  candidate_pred_positions = []
  # `tokens` is a list of strings
  for i, token in enumerate(tokens):
    # Special tokens are not predicted in the masked language modeling
    # task
    if token in ['', '']:
      continue
    candidate_pred_positions.append(i)
  # 15% of random tokens are predicted in the masked language modeling task
  num_mlm_preds = max(1, round(len(tokens) * 0.15))
  mlm_input_tokens, pred_positions_and_labels = _replace_mlm_tokens(
    tokens, candidate_pred_positions, num_mlm_preds, vocab)
  pred_positions_and_labels = sorted(pred_positions_and_labels,
                    key=lambda x: x[0])
  pred_positions = [v[0] for v in pred_positions_and_labels]
  mlm_pred_labels = [v[1] for v in pred_positions_and_labels]
  return vocab[mlm_input_tokens], pred_positions, vocab[mlm_pred_labels]

 

15.9.2。将文本转换为预训练数据集

现在我们几乎准备好定制一个Dataset用于预训练 BERT 的类。在此之前,我们仍然需要定义一个辅助函数 _pad_bert_inputs来将特殊的“”标记附加到输入中。它的参数examples包含辅助函数 _get_nsp_data_from_paragraph和_get_mlm_data_from_tokens两个预训练任务的输出。

 

#@save
def _pad_bert_inputs(examples, max_len, vocab):
  max_num_mlm_preds = round(max_len * 0.15)
  all_token_ids, all_segments, valid_lens, = [], [], []
  all_pred_positions, all_mlm_weights, all_mlm_labels = [], [], []
  nsp_labels = []
  for (token_ids, pred_positions, mlm_pred_label_ids, segments,
     is_next) in examples:
    all_token_ids.append(torch.tensor(token_ids + [vocab['']] * (
      max_len - len(token_ids)), dtype=torch.long))
    all_segments.append(torch.tensor(segments + [0] * (
      max_len - len(segments)), dtype=torch.long))
    # `valid_lens` excludes count of '' tokens
    valid_lens.append(torch.tensor(len(token_ids), dtype=torch.float32))
    all_pred_positions.append(torch.tensor(pred_positions + [0] * (
      max_num_mlm_preds - len(pred_positions)), dtype=torch.long))
    # Predictions of padded tokens will be filtered out in the loss via
    # multiplication of 0 weights
    all_mlm_weights.append(
      torch.tensor([1.0] * len(mlm_pred_label_ids) + [0.0] * (
        max_num_mlm_preds - len(pred_positions)),
        dtype=torch.float32))
    all_mlm_labels.append(torch.tensor(mlm_pred_label_ids + [0] * (
      max_num_mlm_preds - len(mlm_pred_label_ids)), dtype=torch.long))
    nsp_labels.append(torch.tensor(is_next, dtype=torch.long))
  return (all_token_ids, all_segments, valid_lens, all_pred_positions,
      all_mlm_weights, all_mlm_labels, nsp_labels)

 

 

#@save
def _pad_bert_inputs(examples, max_len, vocab):
  max_num_mlm_preds = round(max_len * 0.15)
  all_token_ids, all_segments, valid_lens, = [], [], []
  all_pred_positions, all_mlm_weights, all_mlm_labels = [], [], []
  nsp_labels = []
  for (token_ids, pred_positions, mlm_pred_label_ids, segments,
     is_next) in examples:
    all_token_ids.append(np.array(token_ids + [vocab['']] * (
      max_len - len(token_ids)), dtype='int32'))
    all_segments.append(np.array(segments + [0] * (
      max_len - len(segments)), dtype='int32'))
    # `valid_lens` excludes count of '' tokens
    valid_lens.append(np.array(len(token_ids), dtype='float32'))
    all_pred_positions.append(np.array(pred_positions + [0] * (
      max_num_mlm_preds - len(pred_positions)), dtype='int32'))
    # Predictions of padded tokens will be filtered out in the loss via
    # multiplication of 0 weights
    all_mlm_weights.append(
      np.array([1.0] * len(mlm_pred_label_ids) + [0.0] * (
        max_num_mlm_preds - len(pred_positions)), dtype='float32'))
    all_mlm_labels.append(np.array(mlm_pred_label_ids + [0] * (
      max_num_mlm_preds - len(mlm_pred_label_ids)), dtype='int32'))
    nsp_labels.append(np.array(is_next))
  return (all_token_ids, all_segments, valid_lens, all_pred_positions,
      all_mlm_weights, all_mlm_labels, nsp_labels)

 

将两个预训练任务生成训练样例的辅助函数和填充输入的辅助函数放在一起,我们自定义如下类_WikiTextDataset作为预训练 BERT 的 WikiText-2 数据集。通过实现该 __getitem__功能,我们可以任意访问从 WikiText-2 语料库中的一对句子生成的预训练(掩码语言建模和下一句预测)示例。

原始 BERT 模型使用词汇量为 30000 的 WordPiece 嵌入( Wu et al. , 2016 )。WordPiece 的标记化方法是对15.6.2 节中原始字节对编码算法的轻微修改。为简单起见,我们使用该d2l.tokenize函数进行标记化。过滤掉出现次数少于五次的不常见标记。

 

#@save
class _WikiTextDataset(torch.utils.data.Dataset):
  def __init__(self, paragraphs, max_len):
    # Input `paragraphs[i]` is a list of sentence strings representing a
    # paragraph; while output `paragraphs[i]` is a list of sentences
    # representing a paragraph, where each sentence is a list of tokens
    paragraphs = [d2l.tokenize(
      paragraph, token='word') for paragraph in paragraphs]
    sentences = [sentence for paragraph in paragraphs
           for sentence in paragraph]
    self.vocab = d2l.Vocab(sentences, min_freq=5, reserved_tokens=[
      '', '', '', ''])
    # Get data for the next sentence prediction task
    examples = []
    for paragraph in paragraphs:
      examples.extend(_get_nsp_data_from_paragraph(
        paragraph, paragraphs, self.vocab, max_len))
    # Get data for the masked language model task
    examples = [(_get_mlm_data_from_tokens(tokens, self.vocab)
           + (segments, is_next))
           for tokens, segments, is_next in examples]
    # Pad inputs
    (self.all_token_ids, self.all_segments, self.valid_lens,
     self.all_pred_positions, self.all_mlm_weights,
     self.all_mlm_labels, self.nsp_labels) = _pad_bert_inputs(
      examples, max_len, self.vocab)

  def __getitem__(self, idx):
    return (self.all_token_ids[idx], self.all_segments[idx],
        self.valid_lens[idx], self.all_pred_positions[idx],
        self.all_mlm_weights[idx], self.all_mlm_labels[idx],
        self.nsp_labels[idx])

  def __len__(self):
    return len(self.all_token_ids)

 

 

#@save
class _WikiTextDataset(gluon.data.Dataset):
  def __init__(self, paragraphs, max_len):
    # Input `paragraphs[i]` is a list of sentence strings representing a
    # paragraph; while output `paragraphs[i]` is a list of sentences
    # representing a paragraph, where each sentence is a list of tokens
    paragraphs = [d2l.tokenize(
      paragraph, token='word') for paragraph in paragraphs]
    sentences = [sentence for paragraph in paragraphs
           for sentence in paragraph]
    self.vocab = d2l.Vocab(sentences, min_freq=5, reserved_tokens=[
      '', '', '', ''])
    # Get data for the next sentence prediction task
    examples = []
    for paragraph in paragraphs:
      examples.extend(_get_nsp_data_from_paragraph(
        paragraph, paragraphs, self.vocab, max_len))
    # Get data for the masked language model task
    examples = [(_get_mlm_data_from_tokens(tokens, self.vocab)
           + (segments, is_next))
           for tokens, segments, is_next in examples]
    # Pad inputs
    (self.all_token_ids, self.all_segments, self.valid_lens,
     self.all_pred_positions, self.all_mlm_weights,
     self.all_mlm_labels, self.nsp_labels) = _pad_bert_inputs(
      examples, max_len, self.vocab)

  def __getitem__(self, idx):
    return (self.all_token_ids[idx], self.all_segments[idx],
        self.valid_lens[idx], self.all_pred_positions[idx],
        self.all_mlm_weights[idx], self.all_mlm_labels[idx],
        self.nsp_labels[idx])

  def __len__(self):
    return len(self.all_token_ids)

 

通过使用_read_wiki函数和_WikiTextDataset类,我们定义了以下内容load_data_wiki来下载 WikiText-2 数据集并从中生成预训练示例。

 

#@save
def load_data_wiki(batch_size, max_len):
  """Load the WikiText-2 dataset."""
  num_workers = d2l.get_dataloader_workers()
  data_dir = d2l.download_extract('wikitext-2', 'wikitext-2')
  paragraphs = _read_wiki(data_dir)
  train_set = _WikiTextDataset(paragraphs, max_len)
  train_iter = torch.utils.data.DataLoader(train_set, batch_size,
                    shuffle=True, num_workers=num_workers)
  return train_iter, train_set.vocab

 

 

#@save
def load_data_wiki(batch_size, max_len):
  """Load the WikiText-2 dataset."""
  num_workers = d2l.get_dataloader_workers()
  data_dir = d2l.download_extract('wikitext-2', 'wikitext-2')
  paragraphs = _read_wiki(data_dir)
  train_set = _WikiTextDataset(paragraphs, max_len)
  train_iter = gluon.data.DataLoader(train_set, batch_size, shuffle=True,
                    num_workers=num_workers)
  return train_iter, train_set.vocab

 

将批量大小设置为 512,将 BERT 输入序列的最大长度设置为 64,我们打印出 BERT 预训练示例的小批量形状。请注意,在每个 BERT 输入序列中,10 (64×0.15) 位置是为掩码语言建模任务预测的。

 

batch_size, max_len = 512, 64
train_iter, vocab = load_data_wiki(batch_size, max_len)

for (tokens_X, segments_X, valid_lens_x, pred_positions_X, mlm_weights_X,
   mlm_Y, nsp_y) in train_iter:
  print(tokens_X.shape, segments_X.shape, valid_lens_x.shape,
     pred_positions_X.shape, mlm_weights_X.shape, mlm_Y.shape,
     nsp_y.shape)
  break

 

 

Downloading ../data/wikitext-2-v1.zip from https://s3.amazonaws.com/research.metamind.io/wikitext/wikitext-2-v1.zip...
torch.Size([512, 64]) torch.Size([512, 64]) torch.Size([512]) torch.Size([512, 10]) torch.Size([512, 10]) torch.Size([512, 10]) torch.Size([512])

 

 

batch_size, max_len = 512, 64
train_iter, vocab = load_data_wiki(batch_size, max_len)

for (tokens_X, segments_X, valid_lens_x, pred_positions_X, mlm_weights_X,
   mlm_Y, nsp_y) in train_iter:
  print(tokens_X.shape, segments_X.shape, valid_lens_x.shape,
     pred_positions_X.shape, mlm_weights_X.shape, mlm_Y.shape,
     nsp_y.shape)
  break

 

 

(512, 64) (512, 64) (512,) (512, 10) (512, 10) (512, 10) (512,)

 

最后,让我们看一下词汇量。即使在过滤掉不常见的标记后,它仍然比 PTB 数据集大两倍以上。

 

len(vocab)

 

 

20256

 

 

len(vocab)

 

 

20256

 

15.9.3。概括

与 PTB 数据集相比,WikiText-2 数据集保留了原始标点符号、大小写和数字,并且大了一倍多。

我们可以任意访问从 WikiText-2 语料库中的一对句子生成的预训练(掩码语言建模和下一句预测)示例。

15.9.4。练习

为简单起见,句点用作拆分句子的唯一分隔符。尝试其他句子拆分技术,例如 spaCy 和 NLTK。以 NLTK 为例。您需要先安装 NLTK:. 在代码中,首先. 然后,下载 Punkt 句子分词器: 。要拆分诸如 之类的句子 ,调用 将返回两个句子字符串的列表:。pip install nltkimport nltknltk.download('punkt')sentences = 'This is great ! Why not ?'nltk.tokenize.sent_tokenize(sentences)['This is great !', 'Why not ?']

如果我们不过滤掉任何不常见的标记,词汇表的大小是多少?

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分