プリンキピア

第15章自然言語処理:事前学習

15.10.2. Representing Text with BERT15.10.2 BERTでテキストを表す

After pretraining BERT, we can use it to represent single text, text pairs, or any token in them. The following function returns the BERT (net) representations for all tokens in tokens_a and tokens_b.
英語のヒント
BERTを事前学習した後は、それを使って、単一のテキスト、テキストの対、またはその中の任意のトークンを表せる。次の関数は、tokens_aとtokens_bのすべてのトークンについて、BERT(net)の表現を返す。
def get_bert_encoding(net, tokens_a, tokens_b=None):
    tokens, segments = d2l.get_tokens_and_segments(tokens_a, tokens_b)
    token_ids = torch.tensor(vocab[tokens], device=devices[0]).unsqueeze(0)
    segments = torch.tensor(segments, device=devices[0]).unsqueeze(0)
    valid_len = torch.tensor(len(tokens), device=devices[0]).unsqueeze(0)
    encoded_X, _, _ = net(token_ids, segments, valid_len)
    return encoded_X
Consider the sentence “a crane is flying”. Recall the input representation of BERT as discussed in Section 15.8.4. After inserting special tokens “<cls>” (used for classification) and “<sep>” (used for separation), the BERT input sequence has a length of six. Since zero is the index of the “<cls>” token, encoded_text[:, 0, :] is the BERT representation of the entire input sentence. To evaluate the polysemy token “crane”, we also print out the first three elements of the BERT representation of the token.
英語のヒント
文「a crane is flying」(鶴が飛んでいる)を考えよう。15.8.4節で論じたBERTの入力表現を思い出そう。特殊トークン「<cls>」(分類に使う)と「<sep>」(区切りに使う)を挿入した後、BERTの入力系列の長さは6になる。0は「<cls>」トークンのインデックスなので、encoded_text[:, 0, :]は入力文全体のBERT表現である。多義のトークン「crane」を評価するため、そのトークンのBERT表現の最初の三つの要素も表示する。
tokens_a = ['a', 'crane', 'is', 'flying']
encoded_text = get_bert_encoding(net, tokens_a)
# Tokens: '<cls>', 'a', 'crane', 'is', 'flying', '<sep>'
encoded_text_cls = encoded_text[:, 0, :]
encoded_text_crane = encoded_text[:, 2, :]
encoded_text.shape, encoded_text_cls.shape, encoded_text_crane[0][:3]
(torch.Size([1, 6, 128]),
 torch.Size([1, 128]),
 tensor([0.8414, 1.4830, 0.8226], device='cuda:0', grad_fn=<SliceBackward0>))
Now consider a sentence pair “a crane driver came” and “he just left”. Similarly, encoded_pair[:, 0, :] is the encoded result of the entire sentence pair from the pretrained BERT. Note that the first three elements of the polysemy token “crane” are different from those when the context is different. This supports that BERT representations are context-sensitive.
英語のヒント
次に、文の対「a crane driver came」(クレーンの運転手が来た)と「he just left」(彼はちょうど去った)を考えよう。同じように、encoded_pair[:, 0, :]は、事前学習したBERTによる文の対全体の符号化の結果である。多義のトークン「crane」の最初の三つの要素が、文脈が異なるときのものとは違っていることに注意しよう。これは、BERTの表現が文脈に依存することを裏づけている。
tokens_a, tokens_b = ['a', 'crane', 'driver', 'came'], ['he', 'just', 'left']
encoded_pair = get_bert_encoding(net, tokens_a, tokens_b)
# Tokens: '<cls>', 'a', 'crane', 'driver', 'came', '<sep>', 'he', 'just',
# 'left', '<sep>'
encoded_pair_cls = encoded_pair[:, 0, :]
encoded_pair_crane = encoded_pair[:, 2, :]
encoded_pair.shape, encoded_pair_cls.shape, encoded_pair_crane[0][:3]
(torch.Size([1, 10, 128]),
 torch.Size([1, 128]),
 tensor([0.0430, 1.6132, 0.0437], device='cuda:0', grad_fn=<SliceBackward0>))
In Section 16, we will fine-tune a pretrained BERT model for downstream natural language processing applications.
英語のヒント
第16章では、下流の自然言語処理の応用のために、事前学習したBERTモデルを微調整する。