第9章リカレントニューラルネットワーク
9.2.5. Exploratory Language Statistics9.2.5 言語の探索的な統計
Using the real corpus and the
Vocab class defined over words, we can inspect basic statistics concerning word use in our corpus. Below, we construct a vocabulary from words used in The Time Machine and print the ten most frequently occurring of them.英語のヒント
実際のコーパスと、単語に対して定義した
Vocabクラスを使って、コーパスでの単語の使われ方に関する基本的な統計を調べられる。以下では、タイム・マシンで使われている単語から語彙を作り、最もよく現れる10語を表示する。words = text.split()
vocab = Vocab(words)
vocab.token_freqs[:10][('the', 2261),
('i', 1267),
('and', 1245),
('of', 1155),
('a', 816),
('to', 695),
('was', 552),
('in', 541),
('that', 443),
('my', 440)]Note that the ten most frequent words are not all that descriptive. You might even imagine that we might see a very similar list if we had chosen any book at random. Articles like “the” and “a”, pronouns like “i” and “my”, and prepositions like “of”, “to”, and “in” occur often because they serve common syntactic roles. Such words that are common but not particularly descriptive are often called stop words and, in previous generations of text classifiers based on so-called bag-of-words representations, they were most often filtered out. However, they carry meaning and it is not necessary to filter them out when working with modern RNN- and Transformer-based neural models. If you look further down the list, you will notice that word frequency decays quickly. The most frequent word is less than as common as the most popular. Word frequency tends to follow a power law distribution (specifically the Zipfian) as we go down the ranks. To get a better idea, we plot the figure of the word frequency.
英語のヒント
最も頻繁な10語は、あまり内容を表していないことに注意してほしい。どんな本を無作為に選んでも、とてもよく似たリストが見られると想像するかもしれない。「the」や「a」のような冠詞、「i」や「my」のような代名詞、「of」「to」「in」のような前置詞がよく現れるのは、よくある統語上の役割を果たすからである。こうした、よく現れるが特に内容を表すわけではない単語は、しばしばストップワードと呼ばれ、いわゆるバッグ・オブ・ワーズの表現にもとづく前の世代のテキスト分類器では、ほとんどの場合取り除かれていた。しかし、それらは意味を担っており、現代のRNNやTransformerにもとづくニューラルモデルを扱うときには、取り除く必要はない。リストをさらに下まで見ると、単語の頻度が急速に減っていくことに気づくだろう。頻度の順位がの単語は、最も人気のある単語の未満しか現れない。順位を下るにつれて、単語の頻度はべき乗則の分布(具体的にはジップ分布)に従う傾向がある。もっとよくつかむため、単語の頻度の図を描く。
freqs = [freq for token, freq in vocab.token_freqs]
d2l.plot(freqs, xlabel='token: x', ylabel='frequency: n(x)',
xscale='log', yscale='log')After dealing with the first few words as exceptions, all the remaining words roughly follow a straight line on a log–log plot. This phenomenon is captured by Zipf’s law, which states that the frequency of the most frequent word is:
英語のヒント
最初のいくつかの単語を例外として扱えば、残りの単語はすべて、両対数グラフ上でおおよそ直線に乗る。この現象はジップの法則でとらえられる。これは、頻度の順位がの単語の頻度が次のようになることを述べる。
(9.2.1)
which is equivalent to
英語のヒント
これは次と同値である。
(9.2.2)
where is the exponent that characterizes the distribution and is a constant. This should already give us pause for thought if we want to model words by counting statistics. After all, we will significantly overestimate the frequency of the tail, also known as the infrequent words. But what about the other word combinations, such as two consecutive words (bigrams), three consecutive words (trigrams), and beyond? Let’s see whether the bigram frequency behaves in the same manner as the single word (unigram) frequency.
英語のヒント
ここで、は分布を特徴づける指数、は定数である。単語を統計の数え上げでモデル化したいなら、これだけでもう立ち止まって考える理由になるはずだ。結局のところ、裾、つまりまれな単語の頻度を大きく過大評価してしまう。しかし、二つの連続する単語(バイグラム)、三つの連続する単語(トライグラム)、さらにその先といった、ほかの単語の組み合わせはどうだろうか。バイグラムの頻度が、単一の単語(ユニグラム)の頻度と同じようにふるまうか見てみよう。
bigram_tokens = ['--'.join(pair) for pair in zip(words[:-1], words[1:])]
bigram_vocab = Vocab(bigram_tokens)
bigram_vocab.token_freqs[:10][('of--the', 309),
('in--the', 169),
('i--had', 130),
('i--was', 112),
('and--the', 109),
('the--time', 102),
('it--was', 99),
('to--the', 85),
('as--i', 78),
('of--a', 73)]One thing is notable here. Out of the ten most frequent word pairs, nine are composed of both stop words and only one is relevant to the actual book—“the time”. Furthermore, let’s see whether the trigram frequency behaves in the same manner.
英語のヒント
ここで一つ注目すべきことがある。最も頻繁な10個の単語の組のうち、9個はどちらもストップワードからなり、実際の本に関係するのは一つ、「the time」だけである。さらに、トライグラムの頻度が同じようにふるまうかどうか見てみよう。
trigram_tokens = ['--'.join(triple) for triple in zip(
words[:-2], words[1:-1], words[2:])]
trigram_vocab = Vocab(trigram_tokens)
trigram_vocab.token_freqs[:10][('the--time--traveller', 59),
('the--time--machine', 30),
('the--medical--man', 24),
('it--seemed--to', 16),
('it--was--a', 15),
('here--and--there', 15),
('seemed--to--me', 14),
('i--did--not', 14),
('i--saw--the', 13),
('i--began--to', 13)]Now, let’s visualize the token frequency among these three models: unigrams, bigrams, and trigrams.
英語のヒント
では、ユニグラム、バイグラム、トライグラムという三つのモデルのあいだで、トークンの頻度を可視化しよう。
bigram_freqs = [freq for token, freq in bigram_vocab.token_freqs]
trigram_freqs = [freq for token, freq in trigram_vocab.token_freqs]
d2l.plot([freqs, bigram_freqs, trigram_freqs], xlabel='token: x',
ylabel='frequency: n(x)', xscale='log', yscale='log',
legend=['unigram', 'bigram', 'trigram'])This figure is quite exciting. First, beyond unigram words, sequences of words also appear to be following Zipf’s law, albeit with a smaller exponent in (9.2.1), depending on the sequence length. Second, the number of distinct -grams is not that large. This gives us hope that there is quite a lot of structure in language. Third, many -grams occur very rarely. This makes certain methods unsuitable for language modeling and motivates the use of deep learning models. We will discuss this in the next section.
英語のヒント
この図はかなりわくわくするものだ。第一に、ユニグラムの単語を超えて、単語の系列もジップの法則に従っているように見える。ただし、系列の長さによって、(9.2.1)の指数はより小さくなる。第二に、異なるグラムの数はそれほど大きくない。これは、言語にはかなり多くの構造があるという希望を与える。第三に、多くのグラムはとてもまれにしか現れない。このため、ある種の手法は言語のモデリングに向かず、深層学習のモデルを使う動機となる。これについては次の節で論じる。