プリンキピア

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

10.3. Deep Recurrent Neural Networks10.3 深いリカレントニューラルネットワーク

Up until now, we have focused on defining networks consisting of a sequence input, a single hidden RNN layer, and an output layer. Despite having just one hidden layer between the input at any time step and the corresponding output, there is a sense in which these networks are deep. Inputs from the first time step can influence the outputs at the final time step T (often 100s or 1000s of steps later). These inputs pass through T applications of the recurrent layer before reaching the final output. However, we often also wish to retain the ability to express complex relationships between the inputs at a given time step and the outputs at that same time step. Thus we often construct RNNs that are deep not only in the time direction but also in the input-to-output direction. This is precisely the notion of depth that we have already encountered in our development of MLPs and deep CNNs.
英語のヒント
これまでは、系列の入力、一つの隠れたRNN層、出力層からなるネットワークを定義することに焦点を当ててきた。任意の時間ステップの入力と対応する出力のあいだに隠れ層が一つしかないにもかかわらず、これらのネットワークが深いと言える意味がある。最初の時間ステップの入力は、最後の時間ステップT(しばしば数百から数千ステップ後)の出力に影響しうる。これらの入力は、最終的な出力に届くまでに、再帰的な層をT回当てはめられて通る。しかし、ある時間ステップの入力と、同じ時間ステップの出力のあいだの複雑な関係を表す能力も保ちたいことが多い。そのため、時間の方向だけでなく、入力から出力への方向にも深いRNNを組み立てることが多い。これはまさに、MLPや深いCNNを作る中ですでに出会った深さの概念である。
The standard method for building this sort of deep RNN is strikingly simple: we stack the RNNs on top of each other. Given a sequence of length T, the first RNN produces a sequence of outputs, also of length T. These, in turn, constitute the inputs to the next RNN layer. In this short section, we illustrate this design pattern and present a simple example for how to code up such stacked RNNs. Below, in Fig. 10.3.1, we illustrate a deep RNN with L hidden layers. Each hidden state operates on a sequential input and produces a sequential output. Moreover, any RNN cell (white box in Fig. 10.3.1) at each time step depends on both the same layer’s value at the previous time step and the previous layer’s value at the same time step.
英語のヒント
この種の深いRNNを作る標準的な方法は、驚くほど単純である。RNNを互いの上に積み重ねるのである。長さTの系列が与えられると、最初のRNNは、やはり長さTの出力の系列を生み出す。今度はこれらが、次のRNN層への入力になる。この短い節では、この設計パターンを示し、こうした積み重ねたRNNのコードを書く簡単な例を示す。以下の図10.3.1では、隠れ層がL個の深いRNNを示す。各隠れ状態は、系列の入力に働き、系列の出力を生み出す。さらに、各時間ステップのどのRNNセル(図10.3.1の白い箱)も、前の時間ステップの同じ層の値と、同じ時間ステップの前の層の値の両方に依存する。
Fig. 10.3.1図10.3.1
Architecture of a deep RNN.
英語のヒント
深いRNNの構造。
Formally, suppose that we have a minibatch input 𝐗t∈ℝn×d (number of examples =n; number of inputs in each example =d) at time step t. At the same time step, let the hidden state of the lth hidden layer (l=1,…,L) be 𝐇t(l)∈ℝn×h (number of hidden units =h) and the output layer variable be 𝐎t∈ℝn×q (number of outputs: q). Setting 𝐇t(0)=𝐗t, the hidden state of the lth hidden layer that uses the activation function ϕl is calculated as follows:
英語のヒント
正式には、時間ステップtにミニバッチの入力𝐗t∈ℝn×d(事例の数=n、各事例の入力の数=d)があるとする。同じ時間ステップで、lthの隠れ層(l=1,…,L)の隠れ状態を𝐇t(l)∈ℝn×h(隠れユニットの数=h)、出力層の変数を𝐎t∈ℝn×q(出力の数:q)とする。𝐇t(0)=𝐗tとおくと、活性化関数ϕlを使うlthの隠れ層の隠れ状態は次のように計算される。
𝐇t(l)=ϕl(𝐇t(l−1)𝐖xh(l)+𝐇t−1(l)𝐖hh(l)+𝐛h(l)),(10.3.1)
where the weights 𝐖xh(l)∈ℝh×h and 𝐖hh(l)∈ℝh×h, together with the bias 𝐛h(l)∈ℝ1×h, are the model parameters of the lth hidden layer.
英語のヒント
ここで、重み𝐖xh(l)∈ℝh×hと𝐖hh(l)∈ℝh×hは、バイアス𝐛h(l)∈ℝ1×hとともに、lthの隠れ層のモデルパラメータである。
At the end, the calculation of the output layer is only based on the hidden state of the final Lth hidden layer:
英語のヒント
最後に、出力層の計算は、最後のLthの隠れ層の隠れ状態だけにもとづく。
𝐎t=𝐇t(L)𝐖hq+𝐛q,(10.3.2)
where the weight 𝐖hq∈ℝh×q and the bias 𝐛q∈ℝ1×q are the model parameters of the output layer.
英語のヒント
ここで、重み𝐖hq∈ℝh×qとバイアス𝐛q∈ℝ1×qは、出力層のモデルパラメータである。
Just as with MLPs, the number of hidden layers L and the number of hidden units h are hyperparameters that we can tune. Common RNN layer widths (h) are in the range (64,2056), and common depths (L) are in the range (1,8). In addition, we can easily get a deep-gated RNN by replacing the hidden state computation in (10.3.1) with that from an LSTM or a GRU.
英語のヒント
MLPと同じように、隠れ層の数Lと隠れユニットの数hは、調整できるハイパーパラメータである。よく使われるRNNの層の幅(h)は(64,2056)の範囲にあり、よく使われる深さ(L)は(1,8)の範囲にある。さらに、(10.3.1)の隠れ状態の計算をLSTMやGRUのものに置き換えれば、深いゲート付きRNNが簡単に得られる。
import torch
from torch import nn
from d2l import torch as d2l