プリンキピア

第9章リカレントニューラルネットワーク

9.3.1. Learning Language Models9.3.1 言語モデルを学習する

The obvious question is how we should model a document, or even a sequence of tokens. Suppose that we tokenize text data at the word level. Let’s start by applying basic probability rules:
英語のヒント
すぐに浮かぶ疑問は、文書、あるいはトークンの系列でさえ、どうモデル化すべきかである。テキストデータを単語の水準でトークン化するとしよう。基本的な確率の規則を当てはめることから始めよう。
P(x1,x2,…,xT)=∏t=1TP(xt∣x1,…,xt−1).(9.3.2)
For example, the probability of a text sequence containing four words would be given as:
英語のヒント
たとえば、四つの単語を含むテキスト系列の確率は、次のように与えられる。
P(deep,learning,is,fun)=P(deep)P(learning∣deep)P(is∣deep,learning)P(fun∣deep,learning,is).(9.3.3)

9.3.1.1. Markov Models and n-grams9.3.1.1 マルコフモデルとnグラム

Among those sequence model analyses in Section 9.1, let’s apply Markov models to language modeling. A distribution over sequences satisfies the Markov property of first order if P(xt+1∣xt,…,x1)=P(xt+1∣xt). Higher orders correspond to longer dependencies. This leads to a number of approximations that we could apply to model a sequence:
英語のヒント
9.1節での系列モデルの分析のうち、マルコフモデルを言語のモデリングに当てはめてみよう。系列上の分布は、P(xt+1∣xt,…,x1)=P(xt+1∣xt)のとき1次のマルコフ性を満たす。より高い次数は、より長い依存関係に対応する。これにより、系列をモデル化するのに当てはめられるいくつもの近似が得られる。
P(x1,x2,x3,x4)=P(x1)P(x2)P(x3)P(x4),P(x1,x2,x3,x4)=P(x1)P(x2∣x1)P(x3∣x2)P(x4∣x3),P(x1,x2,x3,x4)=P(x1)P(x2∣x1)P(x3∣x1,x2)P(x4∣x2,x3).(9.3.4)
The probability formulae that involve one, two, and three variables are typically referred to as unigram, bigram, and trigram models, respectively. In order to compute the language model, we need to calculate the probability of words and the conditional probability of a word given the previous few words. Note that such probabilities are language model parameters.
英語のヒント
一つ、二つ、三つの変数を含む確率の式は、それぞれふつうユニグラム、バイグラム、トライグラムのモデルと呼ばれる。言語モデルを計算するには、単語の確率と、直前のいくつかの単語が与えられたときの単語の条件付き確率を計算する必要がある。そうした確率は言語モデルのパラメータであることに注意してほしい。

9.3.1.2. Word Frequency9.3.1.2 単語の頻度

Here, we assume that the training dataset is a large text corpus, such as all Wikipedia entries, Project Gutenberg, and all text posted on the web. The probability of words can be calculated from the relative word frequency of a given word in the training dataset. For example, the estimate Pˆ(deep) can be calculated as the probability of any sentence starting with the word “deep”. A slightly less accurate approach would be to count all occurrences of the word “deep” and divide it by the total number of words in the corpus. This works fairly well, particularly for frequent words. Moving on, we could attempt to estimate
英語のヒント
ここでは、訓練データセットが、すべてのウィキペディアの項目、プロジェクト・グーテンベルク、ウェブに投稿されたすべてのテキストのような、大きなテキストコーパスだとする。単語の確率は、訓練データセットにおけるその単語の相対頻度から計算できる。たとえば、推定値Pˆ(deep)は、どれかの文が単語「deep」で始まる確率として計算できる。少し正確さの劣るやり方は、単語「deep」が現れたすべての回数を数え、それをコーパスの単語の総数で割ることだろう。これは、特に頻繁な単語ではかなりうまく働く。先へ進んで、次を推定しようとすることもできる。
Pˆ(learning∣deep)=n(deep, learning)n(deep),(9.3.5)
where n(x) and n(x,x′) are the number of occurrences of singletons and consecutive word pairs, respectively. Unfortunately, estimating the probability of a word pair is somewhat more difficult, since the occurrences of “deep learning” are a lot less frequent. In particular, for some unusual word combinations it may be tricky to find enough occurrences to get accurate estimates. As suggested by the empirical results in Section 9.2.5, things take a turn for the worse for three-word combinations and beyond. There will be many plausible three-word combinations that we likely will not see in our dataset. Unless we provide some solution to assign such word combinations a nonzero count, we will not be able to use them in a language model. If the dataset is small or if the words are very rare, we might not find even a single one of them.
英語のヒント
ここで、n(x)とn(x,x′)は、それぞれ単独の単語と連続する単語の組が現れた回数である。残念ながら、単語の組の確率を推定するのはいくらか難しい。「deep learning」の出現はずっと少ないからである。特に、珍しい単語の組み合わせでは、正確な推定値を得るのに十分な出現を見つけるのが厄介かもしれない。9.2.5節の経験的な結果が示すように、3語の組み合わせやそれより長いものでは、事態はさらに悪くなる。データセットではおそらく見かけない、もっともらしい3語の組み合わせがたくさんあるだろう。そうした単語の組み合わせに0でない数を割り当てる何らかの解決策を用意しない限り、それらを言語モデルで使えない。データセットが小さかったり、単語がとてもまれだったりすれば、その一つさえ見つからないかもしれない。

9.3.1.3. Laplace Smoothing9.3.1.3 ラプラス平滑化

A common strategy is to perform some form of Laplace smoothing. The solution is to add a small constant to all counts. Denote by n the total number of words in the training set and m the number of unique words. This solution helps with singletons, e.g., via
英語のヒント
よくある戦略は、何らかの形のラプラス平滑化を行うことである。その解決策は、すべての数に小さな定数を加えることである。訓練集合の単語の総数をn、一意な単語の数をmと表す。この解決策は、たとえば次によって、単独の単語に役立つ。
Pˆ(x)=n(x)+ϵ1/mn+ϵ1,Pˆ(x′∣x)=n(x,x′)+ϵ2Pˆ(x′)n(x)+ϵ2,Pˆ(x″∣x,x′)=n(x,x′,x″)+ϵ3Pˆ(x″)n(x,x′)+ϵ3.(9.3.6)
Here ϵ1,ϵ2, and ϵ3 are hyperparameters. Take ϵ1 as an example: when ϵ1=0, no smoothing is applied; when ϵ1 approaches positive infinity, Pˆ(x) approaches the uniform probability 1/m. The above is a rather primitive variant of what other techniques can accomplish (Wood et al., 2011).
英語のヒント
ここで、ϵ1,ϵ2とϵ3はハイパーパラメータである。ϵ1を例にとろう。ϵ1=0のときは平滑化は行われない。ϵ1が正の無限大に近づくと、Pˆ(x)は一様な確率1/mに近づく。以上は、ほかの手法が達成できることのかなり原始的な変種である(Wood et al., 2011)。
Unfortunately, models like this get unwieldy rather quickly for the following reasons. First, as discussed in Section 9.2.5, many n-grams occur very rarely, making Laplace smoothing rather unsuitable for language modeling. Second, we need to store all counts. Third, this entirely ignores the meaning of the words. For instance, “cat” and “feline” should occur in related contexts. It is quite difficult to adjust such models to additional contexts, whereas, deep learning based language models are well suited to take this into account. Last, long word sequences are almost certain to be novel, hence a model that simply counts the frequency of previously seen word sequences is bound to perform poorly there. Therefore, we focus on using neural networks for language modeling in the rest of the chapter.
英語のヒント
残念ながら、このようなモデルは次の理由でかなりすぐに扱いにくくなる。第一に、9.2.5節で論じたように、多くのnグラムはとてもまれにしか現れず、ラプラス平滑化は言語のモデリングにかなり向かない。第二に、すべての数を保存する必要がある。第三に、これは単語の意味をまったく無視している。たとえば、「cat(猫)」と「feline(ネコ科の動物)」は関連する文脈で現れるはずである。こうしたモデルを追加の文脈に合わせるのはかなり難しいが、深層学習にもとづく言語モデルはこれを考慮に入れるのに向いている。最後に、長い単語の系列はほぼ確実に新しいものなので、前に見た単語の系列の頻度を単に数えるモデルは、そこで性能が悪くなるに決まっている。そのため、この章の残りでは、言語のモデリングにニューラルネットワークを使うことに焦点を当てる。