chainer.functions.n_step_gru¶
- chainer.functions.n_step_gru(n_layers, dropout_ratio, hx, ws, bs, xs)[source]¶
Stacked Uni-directional Gated Recurrent Unit function.
This function calculates stacked Uni-directional GRU with sequences. This function gets an initial hidden state \(h_0\), an input sequence \(x\), weight matrices \(W\), and bias vectors \(b\). This function calculates hidden states \(h_t\) for each time \(t\) from input \(x_t\).
\[\begin{split}r_t &= \sigma(W_0 x_t + W_3 h_{t-1} + b_0 + b_3) \\ z_t &= \sigma(W_1 x_t + W_4 h_{t-1} + b_1 + b_4) \\ h'_t &= \tanh(W_2 x_t + b_2 + r_t \cdot (W_5 h_{t-1} + b_5)) \\ h_t &= (1 - z_t) \cdot h'_t + z_t \cdot h_{t-1}\end{split}\]As the function accepts a sequence, it calculates \(h_t\) for all \(t\) with one call. Six weight matrices and six bias vectors are required for each layers. So, when \(S\) layers exists, you need to prepare \(6S\) weight matrices and \(6S\) bias vectors.
If the number of layers
n_layers
is greather than \(1\), input ofk
-th layer is hidden stateh_t
ofk-1
-th layer. Note that all input variables except first layer may have different shape from the first layer.- Parameters
n_layers (int) – Number of layers.
dropout_ratio (float) – Dropout ratio.
hx (Variable) – Variable holding stacked hidden states. Its shape is
(S, B, N)
whereS
is number of layers and is equal ton_layers
,B
is mini-batch size, andN
is dimension of hidden units.ws (list of list of
Variable
) – Weight matrices.ws[i]
represents weights for i-th layer. Eachws[i]
is a list containing six matrices.ws[i][j]
is corresponding withW_j
in the equation. Onlyws[0][j]
where0 <= j < 3
is(N, I)
shape as they are multiplied with input variables. All other matrices has(N, N)
shape.bs (list of list of
Variable
) – Bias vectors.bs[i]
represnents biases for i-th layer. Eachbs[i]
is a list containing six vectors.bs[i][j]
is corresponding withb_j
in the equation. Shape of each matrix is(N,)
whereN
is dimension of hidden units.xs (list of
Variable
) – A list ofVariable
holding input values. Each elementxs[t]
holds input value for timet
. Its shape is(B_t, I)
, whereB_t
is mini-batch size for timet
, andI
is size of input units. Note that this function supports variable length sequences. When sequneces has different lengths, sort sequences in descending order by length, and transpose the sorted sequence.transpose_sequence()
transpose a list ofVariable()
holding sequence. Soxs
needs to satisfyxs[t].shape[0] >= xs[t + 1].shape[0]
.
- Returns
This function returns a tuple containing two elements,
hy
andys
.hy
is an updated hidden states whose shape is same ashx
.ys
is a list ofVariable
. Each elementys[t]
holds hidden states of the last layer corresponding to an inputxs[t]
. Its shape is(B_t, N)
whereB_t
is mini-batch size for timet
, andN
is size of hidden units. Note thatB_t
is the same value asxs[t]
.
- Return type