プリンキピア

第10章現代のリカレントニューラルネットワーク

10.4. Bidirectional Recurrent Neural Networks10.4 双方向リカレントニューラルネットワーク

So far, our working example of a sequence learning task has been language modeling, where we aim to predict the next token given all previous tokens in a sequence. In this scenario, we wish only to condition upon the leftward context, and thus the unidirectional chaining of a standard RNN seems appropriate. However, there are many other sequence learning tasks contexts where it is perfectly fine to condition the prediction at every time step on both the leftward and the rightward context. Consider, for example, part of speech detection. Why shouldn’t we take the context in both directions into account when assessing the part of speech associated with a given word?
英語のヒント
ここまで、系列学習の課題の作業例は言語のモデリングだった。そこでは、系列の前のトークンがすべて与えられたときに、次のトークンを予測することを目指す。この場面では、左側の文脈だけを条件としたいので、標準的なRNNの一方向の連鎖が適切に見える。しかし、ほかの多くの系列学習の課題の文脈では、各時間ステップの予測を左側と右側の両方の文脈を条件として行ってまったくかまわない。たとえば、品詞の検出を考えよう。ある単語に結びついた品詞を判断するときに、両方向の文脈を考慮に入れるべきでない理由があるだろうか。
Another common task—often useful as a pretraining exercise prior to fine-tuning a model on an actual task of interest—is to mask out random tokens in a text document and then to train a sequence model to predict the values of the missing tokens. Note that depending on what comes after the blank, the likely value of the missing token changes dramatically:
英語のヒント
もう一つのよくある課題は、テキスト文書の中のランダムなトークンを覆い隠し、系列モデルを訓練して欠けたトークンの値を予測させることである。これは、実際に関心のある課題でモデルを微調整する前の事前学習としてしばしば役に立つ。空欄の後に何が来るかによって、欠けたトークンのありそうな値は劇的に変わることに注意してほしい。
  • I am ___.
    英語のヒント
    「I am ___.」(私は__だ。)
  • I am ___ hungry.
    英語のヒント
    「I am ___ hungry.」(私は__おなかがすいている。)
  • I am ___ hungry, and I can eat half a pig.
    英語のヒント
    「I am ___ hungry, and I can eat half a pig.」(私は__おなかがすいていて、豚半頭だって食べられる。)
In the first sentence “happy” seems to be a likely candidate. The words “not” and “very” seem plausible in the second sentence, but “not” seems incompatible with the third sentences.
英語のヒント
最初の文では「happy(うれしい)」がありそうな候補に見える。2番目の文では「not(…ない)」や「very(とても)」がもっともらしく見えるが、「not」は3番目の文とは両立しないようだ。
Fortunately, a simple technique transforms any unidirectional RNN into a bidirectional RNN (Schuster and Paliwal, 1997). We simply implement two unidirectional RNN layers chained together in opposite directions and acting on the same input (Fig. 10.4.1). For the first RNN layer, the first input is 𝐱1 and the last input is 𝐱T, but for the second RNN layer, the first input is 𝐱T and the last input is 𝐱1. To produce the output of this bidirectional RNN layer, we simply concatenate together the corresponding outputs of the two underlying unidirectional RNN layers.
英語のヒント
幸い、単純な手法で、どんな一方向のRNNも双方向のRNNに変えられる(Schuster and Paliwal, 1997)。逆向きに連鎖させて同じ入力に働かせた、二つの一方向のRNN層を実装するだけでよい(図10.4.1)。1番目のRNN層では、最初の入力は𝐱1で最後の入力は𝐱Tだが、2番目のRNN層では、最初の入力は𝐱Tで最後の入力は𝐱1である。この双方向のRNN層の出力を生み出すには、もとの二つの一方向のRNN層の対応する出力を連結するだけでよい。
Fig. 10.4.1図10.4.1
Architecture of a bidirectional RNN.
英語のヒント
双方向RNNの構造。
Formally for any time step t, we consider a minibatch input 𝐗t∈ℝn×d (number of examples =n; number of inputs in each example =d) and let the hidden layer activation function be ϕ. In the bidirectional architecture, the forward and backward hidden states for this time step are 𝐇→t∈ℝn×h and 𝐇←t∈ℝn×h, respectively, where h is the number of hidden units. The forward and backward hidden state updates are as follows:
英語のヒント
正式には、任意の時間ステップtについて、ミニバッチの入力𝐗t∈ℝn×d(事例の数=n、各事例の入力の数=d)を考え、隠れ層の活性化関数をϕとする。双方向の構造では、この時間ステップの順方向と逆方向の隠れ状態はそれぞれ𝐇→t∈ℝn×hと𝐇←t∈ℝn×hである。ここでhは隠れユニットの数である。順方向と逆方向の隠れ状態の更新は次のとおりである。
𝐇→t=ϕ(𝐗t𝐖xh(f)+𝐇→t−1𝐖hh(f)+𝐛h(f)),𝐇←t=ϕ(𝐗t𝐖xh(b)+𝐇←t+1𝐖hh(b)+𝐛h(b)),(10.4.1)
where the weights 𝐖xh(f)∈ℝd×h,𝐖hh(f)∈ℝh×h,𝐖xh(b)∈ℝd×h, and 𝐖hh(b)∈ℝh×h, and the biases 𝐛h(f)∈ℝ1×h and 𝐛h(b)∈ℝ1×h are all the model parameters.
英語のヒント
ここで、重み𝐖xh(f)∈ℝd×h,𝐖hh(f)∈ℝh×h,𝐖xh(b)∈ℝd×h, and 𝐖hh(b)∈ℝh×hと、バイアス𝐛h(f)∈ℝ1×hおよび𝐛h(b)∈ℝ1×hは、すべてモデルパラメータである。
Next, we concatenate the forward and backward hidden states 𝐇→t and 𝐇←t to obtain the hidden state 𝐇t∈ℝn×2h for feeding into the output layer. In deep bidirectional RNNs with multiple hidden layers, such information is passed on as input to the next bidirectional layer. Last, the output layer computes the output 𝐎t∈ℝn×q (number of outputs =q):
英語のヒント
次に、順方向と逆方向の隠れ状態𝐇→tと𝐇←tを連結して、出力層に入れる隠れ状態𝐇t∈ℝn×2hを得る。複数の隠れ層をもつ深い双方向RNNでは、こうした情報は次の双方向の層への入力として渡される。最後に、出力層は出力𝐎t∈ℝn×q(出力の数=q)を計算する。
𝐎t=𝐇t𝐖hq+𝐛q.(10.4.2)
Here, the weight matrix 𝐖hq∈ℝ2h×q and the bias 𝐛q∈ℝ1×q are the model parameters of the output layer. While technically, the two directions can have different numbers of hidden units, this design choice is seldom made in practice. We now demonstrate a simple implementation of a bidirectional RNN.
英語のヒント
ここで、重み行列𝐖hq∈ℝ2h×qとバイアス𝐛q∈ℝ1×qは、出力層のモデルパラメータである。技術的には二つの方向で隠れユニットの数を変えることもできるが、実際にはこの設計の選択はめったにされない。では、双方向RNNの簡単な実装を示そう。
import torch
from torch import nn
from d2l import torch as d2l