第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 at time step . In other words, for a minibatch of sequence examples, each row of corresponds to one example at time step from the sequence. Next, denote by the hidden layer output of time step . Unlike with MLP, here we save the hidden layer output from the previous time step and introduce a new weight parameter 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:
英語のヒント
時間ステップで入力のミニバッチがあるとしよう。言い換えれば、個の系列の事例からなるミニバッチについて、の各行は系列の時間ステップにおける一つの事例に対応する。次に、時間ステップの隠れ層の出力をと表す。MLPとは違って、ここでは前の時間ステップの隠れ層の出力を保存し、前の時間ステップの隠れ層の出力を現在の時間ステップでどう使うかを表すために、新しい重みパラメータを導入する。具体的には、現在の時間ステップの隠れ層の出力の計算は、現在の時間ステップの入力と、前の時間ステップの隠れ層の出力とによって決まる。
(9.4.5)
Compared with (9.4.3), (9.4.5) adds one more term and thus instantiates (9.4.2). From the relationship between hidden layer outputs and 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)は項を一つ加えており、したがって(9.4.2)を具体化している。隣り合う時間ステップの隠れ層の出力との関係から、これらの変数は、ニューラルネットワークの現在の時間ステップの状態や記憶のように、現在の時間ステップまでの系列の履歴の情報をとらえて保持していたことがわかる。そのため、このような隠れ層の出力は隠れ状態と呼ばれる。隠れ状態は前の時間ステップと同じ定義を現在の時間ステップで使うので、(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 , the output of the output layer is similar to the computation in the MLP:
英語のヒント
RNNを組み立てる方法はいろいろある。(9.4.5)で定義される隠れ状態をもつものは、とてもよく見られる。時間ステップでは、出力層の出力はMLPでの計算と似ている。
(9.4.6)
Parameters of the RNN include the weights , and the bias of the hidden layer, together with the weights and the bias 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のパラメータには、隠れ層の重みとバイアス、それに出力層の重みとバイアスが含まれる。異なる時間ステップでも、RNNはいつもこれらのモデルパラメータを使うことは述べておく価値がある。そのため、RNNのパラメータ化の費用は、時間ステップの数が増えても大きくならない。
Fig. 9.4.1 illustrates the computational logic of an RNN at three adjacent time steps. At any time step , the computation of the hidden state can be treated as: (i) concatenating the input at the current time step and the hidden state at the previous time step ; (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 of the current time step . In this case, the model parameters are the concatenation of and , and a bias of , all from (9.4.5). The hidden state of the current time step , , will participate in computing the hidden state of the next time step . What is more, will also be fed into the fully connected output layer to compute the output of the current time step .
英語のヒント
Fig. 9.4.1図9.4.1
An RNN with a hidden state.
英語のヒント
隠れ状態をもつRNN。
We just mentioned that the calculation of for the hidden state is equivalent to matrix multiplication of the concatenation of and and the concatenation of and . 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).英語のヒント
隠れ状態についてのの計算は、との連結と、との連結との行列の積に等しいと述べたばかりである。これは数学的に証明できるが、以下では簡単なコード片を使って示すだけにする。まず、形状がそれぞれ(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]])