第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 (often 100s or 1000s of steps later). These inputs pass through 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層、出力層からなるネットワークを定義することに焦点を当ててきた。任意の時間ステップの入力と対応する出力のあいだに隠れ層が一つしかないにもかかわらず、これらのネットワークが深いと言える意味がある。最初の時間ステップの入力は、最後の時間ステップ(しばしば数百から数千ステップ後)の出力に影響しうる。これらの入力は、最終的な出力に届くまでに、再帰的な層を回当てはめられて通る。しかし、ある時間ステップの入力と、同じ時間ステップの出力のあいだの複雑な関係を表す能力も保ちたいことが多い。そのため、時間の方向だけでなく、入力から出力への方向にも深い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 , the first RNN produces a sequence of outputs, also of length . 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 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.
英語のヒント
Fig. 10.3.1図10.3.1
Architecture of a deep RNN.
英語のヒント
深いRNNの構造。
Formally, suppose that we have a minibatch input (number of examples ; number of inputs in each example ) at time step . At the same time step, let the hidden state of the hidden layer () be (number of hidden units ) and the output layer variable be (number of outputs: ). Setting , the hidden state of the hidden layer that uses the activation function is calculated as follows:
英語のヒント
正式には、時間ステップにミニバッチの入力(事例の数、各事例の入力の数)があるとする。同じ時間ステップで、の隠れ層()の隠れ状態を(隠れユニットの数)、出力層の変数を(出力の数:)とする。とおくと、活性化関数を使うの隠れ層の隠れ状態は次のように計算される。
(10.3.1)
where the weights and , together with the bias , are the model parameters of the hidden layer.
英語のヒント
ここで、重みとは、バイアスとともに、の隠れ層のモデルパラメータである。
At the end, the calculation of the output layer is only based on the hidden state of the final hidden layer:
英語のヒント
最後に、出力層の計算は、最後のの隠れ層の隠れ状態だけにもとづく。
(10.3.2)
where the weight and the bias are the model parameters of the output layer.
英語のヒント
ここで、重みとバイアスは、出力層のモデルパラメータである。
Just as with MLPs, the number of hidden layers and the number of hidden units are hyperparameters that we can tune. Common RNN layer widths () are in the range , and common depths () are in the range . 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と同じように、隠れ層の数と隠れユニットの数は、調整できるハイパーパラメータである。よく使われるRNNの層の幅()はの範囲にあり、よく使われる深さ()はの範囲にある。さらに、(10.3.1)の隠れ状態の計算をLSTMやGRUのものに置き換えれば、深いゲート付きRNNが簡単に得られる。
import torch
from torch import nn
from d2l import torch as d2l