プリンキピア

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

9.4.2. Recurrent Neural Networks with Hidden States9.4.2 隠れ状態をもつリカレントニューラルネットワーク

Matters are entirely different when we have hidden states. Let’s look at the structure in some more detail.
英語のヒント
隠れ状態があると、事情はまったく違ってくる。構造をもう少し詳しく見てみよう。
Assume that we have a minibatch of inputs 𝐗t∈ℝn×d at time step t. In other words, for a minibatch of n sequence examples, each row of 𝐗t corresponds to one example at time step t from the sequence. Next, denote by 𝐇t∈ℝn×h the hidden layer output of time step t. Unlike with MLP, here we save the hidden layer output 𝐇t−1 from the previous time step and introduce a new weight parameter 𝐖hh∈ℝh×h to describe how to use the hidden layer output of the previous time step in the current time step. Specifically, the calculation of the hidden layer output of the current time step is determined by the input of the current time step together with the hidden layer output of the previous time step:
英語のヒント
時間ステップtで入力のミニバッチ𝐗t∈ℝn×dがあるとしよう。言い換えれば、n個の系列の事例からなるミニバッチについて、𝐗tの各行は系列の時間ステップtにおける一つの事例に対応する。次に、時間ステップtの隠れ層の出力を𝐇t∈ℝn×hと表す。MLPとは違って、ここでは前の時間ステップの隠れ層の出力𝐇t−1を保存し、前の時間ステップの隠れ層の出力を現在の時間ステップでどう使うかを表すために、新しい重みパラメータ𝐖hh∈ℝh×hを導入する。具体的には、現在の時間ステップの隠れ層の出力の計算は、現在の時間ステップの入力と、前の時間ステップの隠れ層の出力とによって決まる。
𝐇t=ϕ(𝐗t𝐖xh+𝐇t−1𝐖hh+𝐛h).(9.4.5)
Compared with (9.4.3), (9.4.5) adds one more term 𝐇t−1𝐖hh and thus instantiates (9.4.2). From the relationship between hidden layer outputs 𝐇t and 𝐇t−1 of adjacent time steps, we know that these variables captured and retained the sequence’s historical information up to their current time step, just like the state or memory of the neural network’s current time step. Therefore, such a hidden layer output is called a hidden state. Since the hidden state uses the same definition of the previous time step in the current time step, the computation of (9.4.5) is recurrent. Hence, as we said, neural networks with hidden states based on recurrent computation are named recurrent neural networks. Layers that perform the computation of (9.4.5) in RNNs are called recurrent layers.
英語のヒント
(9.4.3)と比べると、(9.4.5)は項𝐇t−1𝐖hhを一つ加えており、したがって(9.4.2)を具体化している。隣り合う時間ステップの隠れ層の出力𝐇tと𝐇t−1の関係から、これらの変数は、ニューラルネットワークの現在の時間ステップの状態や記憶のように、現在の時間ステップまでの系列の履歴の情報をとらえて保持していたことがわかる。そのため、このような隠れ層の出力は隠れ状態と呼ばれる。隠れ状態は前の時間ステップと同じ定義を現在の時間ステップで使うので、(9.4.5)の計算は再帰的である。したがって、前に述べたように、再帰的な計算にもとづく隠れ状態をもつニューラルネットワークは、リカレントニューラルネットワークと名づけられている。RNNで(9.4.5)の計算を行う層は、リカレント層と呼ばれる。
There are many different ways for constructing RNNs. Those with a hidden state defined by (9.4.5) are very common. For time step t, the output of the output layer is similar to the computation in the MLP:
英語のヒント
RNNを組み立てる方法はいろいろある。(9.4.5)で定義される隠れ状態をもつものは、とてもよく見られる。時間ステップtでは、出力層の出力はMLPでの計算と似ている。
𝐎t=𝐇t𝐖hq+𝐛q.(9.4.6)
Parameters of the RNN include the weights 𝐖xh∈ℝd×h,𝐖hh∈ℝh×h, and the bias 𝐛h∈ℝ1×h of the hidden layer, together with the weights 𝐖hq∈ℝh×q and the bias 𝐛q∈ℝ1×q of the output layer. It is worth mentioning that even at different time steps, RNNs always use these model parameters. Therefore, the parametrization cost of an RNN does not grow as the number of time steps increases.
英語のヒント
RNNのパラメータには、隠れ層の重み𝐖xh∈ℝd×h,𝐖hh∈ℝh×hとバイアス𝐛h∈ℝ1×h、それに出力層の重み𝐖hq∈ℝh×qとバイアス𝐛q∈ℝ1×qが含まれる。異なる時間ステップでも、RNNはいつもこれらのモデルパラメータを使うことは述べておく価値がある。そのため、RNNのパラメータ化の費用は、時間ステップの数が増えても大きくならない。
Fig. 9.4.1 illustrates the computational logic of an RNN at three adjacent time steps. At any time step t, the computation of the hidden state can be treated as: (i) concatenating the input 𝐗t at the current time step t and the hidden state 𝐇t−1 at the previous time step t−1; (ii) feeding the concatenation result into a fully connected layer with the activation function ϕ. The output of such a fully connected layer is the hidden state 𝐇t of the current time step t. In this case, the model parameters are the concatenation of 𝐖xh and 𝐖hh, and a bias of 𝐛h, all from (9.4.5). The hidden state of the current time step t, 𝐇t, will participate in computing the hidden state 𝐇t+1 of the next time step t+1. What is more, 𝐇t will also be fed into the fully connected output layer to compute the output 𝐎t of the current time step t.
英語のヒント
図9.4.1は、隣り合う三つの時間ステップでのRNNの計算の論理を示している。任意の時間ステップtで、隠れ状態の計算は次のように扱える。(i)現在の時間ステップtの入力𝐗tと、前の時間ステップt−1の隠れ状態𝐇t−1を連結する。(ii)連結の結果を、活性化関数ϕをもつ全結合層に入れる。こうした全結合層の出力が、現在の時間ステップtの隠れ状態𝐇tである。この場合、モデルパラメータは、𝐖xhと𝐖hhの連結とバイアス𝐛hであり、どれも(9.4.5)から来ている。現在の時間ステップtの隠れ状態𝐇tは、次の時間ステップt+1の隠れ状態𝐇t+1の計算に加わる。さらに、𝐇tは全結合の出力層にも入れられ、現在の時間ステップtの出力𝐎tを計算する。
Fig. 9.4.1図9.4.1
An RNN with a hidden state.
英語のヒント
隠れ状態をもつRNN。
We just mentioned that the calculation of 𝐗t𝐖xh+𝐇t−1𝐖hh for the hidden state is equivalent to matrix multiplication of the concatenation of 𝐗t and 𝐇t−1 and the concatenation of 𝐖xh and 𝐖hh. Though this can be proven mathematically, in the following we just use a simple code snippet as a demonstration. To begin with, we define matrices X, W_xh, H, and W_hh, whose shapes are (3, 1), (1, 4), (3, 4), and (4, 4), respectively. Multiplying X by W_xh, and H by W_hh, and then adding these two products, we obtain a matrix of shape (3, 4).
英語のヒント
隠れ状態についての𝐗t𝐖xh+𝐇t−1𝐖hhの計算は、𝐗tと𝐇t−1の連結と、𝐖xhと𝐖hhの連結との行列の積に等しいと述べたばかりである。これは数学的に証明できるが、以下では簡単なコード片を使って示すだけにする。まず、形状がそれぞれ(3, 1)、(1, 4)、(3, 4)、(4, 4)の行列X、W_xh、H、W_hhを定義する。XにW_xhを掛け、HにW_hhを掛け、この二つの積を足すと、形状(3, 4)の行列が得られる。
X, W_xh = torch.randn(3, 1), torch.randn(1, 4)
H, W_hh = torch.randn(3, 4), torch.randn(4, 4)
torch.matmul(X, W_xh) + torch.matmul(H, W_hh)
tensor([[ 1.2526,  0.0580, -3.3460, -0.2519],
        [-1.3064,  1.4132, -0.1435,  0.3482],
        [ 3.1495,  0.8172,  1.5167, -0.9038]])
Now we concatenate the matrices X and H along columns (axis 1), and the matrices W_xh and W_hh along rows (axis 0). These two concatenations result in matrices of shape (3, 5) and of shape (5, 4), respectively. Multiplying these two concatenated matrices, we obtain the same output matrix of shape (3, 4) as above.
英語のヒント
次に、行列XとHを列に沿って(軸1)連結し、行列W_xhとW_hhを行に沿って(軸0)連結する。この二つの連結により、それぞれ形状(3, 5)と形状(5, 4)の行列ができる。この二つの連結した行列を掛けると、上と同じ形状(3, 4)の出力の行列が得られる。
torch.matmul(torch.cat((X, H), 1), torch.cat((W_xh, W_hh), 0))
tensor([[ 1.2526,  0.0580, -3.3460, -0.2519],
        [-1.3064,  1.4132, -0.1435,  0.3482],
        [ 3.1495,  0.8172,  1.5167, -0.9038]])