第15章自然言語処理:事前学習
15.3.2. Subsampling15.3.2 サブサンプリング
Text data typically have high-frequency words such as “the”, “a”, and “in”: they may even occur billions of times in very large corpora. However, these words often co-occur with many different words in context windows, providing little useful signals. For instance, consider the word “chip” in a context window: intuitively its co-occurrence with a low-frequency word “intel” is more useful in training than the co-occurrence with a high-frequency word “a”. Moreover, training with vast amounts of (high-frequency) words is slow. Thus, when training word embedding models, high-frequency words can be subsampled (Mikolov et al., 2013). Specifically, each indexed word in the dataset will be discarded with probability
英語のヒント
テキストデータには、ふつう「the」「a」「in」のような高頻度語がある。これらはとても大きなコーパスでは数十億回も現れることさえある。しかし、これらの語は文脈窓の中で多くの異なる語としばしば共起し、役に立つ信号をほとんど与えない。たとえば、文脈窓の中の単語「chip」を考えよう。直観的には、低頻度語「intel」との共起のほうが、高頻度語「a」との共起よりも訓練に役立つ。さらに、大量の(高頻度の)語で訓練すると遅い。したがって、単語埋め込みモデルを訓練するときは、高頻度語をサブサンプリングできる(Mikolov et al., 2013)。具体的には、データセットの中のインデックスをつけた各単語は、次の確率で捨てられる。
(15.3.1)
where is the ratio of the number of words to the total number of words in the dataset, and the constant is a hyperparameter ( in the experiment). We can see that only when the relative frequency can the (high-frequency) word be discarded, and the higher the relative frequency of the word, the greater the probability of being discarded.
英語のヒント
ここで、はデータセットの単語の総数に対する単語の数の比であり、定数はハイパーパラメータ(実験では)である。相対頻度がのときに限って(高頻度の)単語が捨てられうること、そして単語の相対頻度が高いほど捨てられる確率が大きいことがわかる。
#@save
def subsample(sentences, vocab):
"""Subsample high-frequency words."""
# Exclude unknown tokens ('<unk>')
sentences = [[token for token in line if vocab[token] != vocab.unk]
for line in sentences]
counter = collections.Counter([
token for line in sentences for token in line])
num_tokens = sum(counter.values())
# Return True if `token` is kept during subsampling
def keep(token):
return(random.uniform(0, 1) <
math.sqrt(1e-4 / counter[token] * num_tokens))
return ([[token for token in line if keep(token)] for line in sentences],
counter)
subsampled, counter = subsample(sentences, vocab)The following code snippet plots the histogram of the number of tokens per sentence before and after subsampling. As expected, subsampling significantly shortens sentences by dropping high-frequency words, which will lead to training speedup.
英語のヒント
次のコード片は、サブサンプリングの前と後での、文あたりのトークン数のヒストグラムを描く。予想どおり、サブサンプリングは高頻度語を落とすことで文を大幅に短くし、それが訓練の高速化につながる。
d2l.show_list_len_pair_hist(['origin', 'subsampled'], '# tokens per sentence',
'count', sentences, subsampled);For individual tokens, the sampling rate of the high-frequency word “the” is less than 1/20.
英語のヒント
個々のトークンについては、高頻度語「the」のサンプリング率は1/20未満である。
def compare_counts(token):
return (f'# of "{token}": '
f'before={sum([l.count(token) for l in sentences])}, '
f'after={sum([l.count(token) for l in subsampled])}')
compare_counts('the')'# of "the": before=50770, after=2010'In contrast, low-frequency words “join” are completely kept.
英語のヒント
対照的に、低頻度語「join」は完全に残される。
compare_counts('join')'# of "join": before=45, after=45'After subsampling, we map tokens to their indices for the corpus.
英語のヒント
サブサンプリングの後、コーパスのためにトークンをそのインデックスに対応させる。
corpus = [vocab[line] for line in subsampled]
corpus[:3][[], [4127, 3228, 1773], [3922, 1922, 4743, 2696]]