プリンキピア

第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 wi in the dataset will be discarded with probability
英語のヒント
テキストデータには、ふつう「the」「a」「in」のような高頻度語がある。これらはとても大きなコーパスでは数十億回も現れることさえある。しかし、これらの語は文脈窓の中で多くの異なる語としばしば共起し、役に立つ信号をほとんど与えない。たとえば、文脈窓の中の単語「chip」を考えよう。直観的には、低頻度語「intel」との共起のほうが、高頻度語「a」との共起よりも訓練に役立つ。さらに、大量の(高頻度の)語で訓練すると遅い。したがって、単語埋め込みモデルを訓練するときは、高頻度語をサブサンプリングできる(Mikolov et al., 2013)。具体的には、データセットの中のインデックスをつけた各単語wiは、次の確率で捨てられる。
P(wi)=max(1−tf(wi),0),(15.3.1)
where f(wi) is the ratio of the number of words wi to the total number of words in the dataset, and the constant t is a hyperparameter (10−4 in the experiment). We can see that only when the relative frequency f(wi)>t can the (high-frequency) word wi be discarded, and the higher the relative frequency of the word, the greater the probability of being discarded.
英語のヒント
ここで、f(wi)はデータセットの単語の総数に対する単語wiの数の比であり、定数tはハイパーパラメータ(実験では10−4)である。相対頻度がf(wi)>tのときに限って(高頻度の)単語wiが捨てられうること、そして単語の相対頻度が高いほど捨てられる確率が大きいことがわかる。
#@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]]