/
n_step_lstm.py
153 lines (112 loc) · 5.35 KB
/
n_step_lstm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from chainer.functions.connection import n_step_lstm as rnn
from chainer.links.connection import n_step_rnn
class NStepLSTMBase(n_step_rnn.NStepRNNBase):
"""Base link class for Stacked LSTM/BiLSTM links.
This link is base link class for :func:`chainer.links.NStepLSTM` and
:func:`chainer.links.NStepBiLSTM`.
This link's behavior depends on argument, ``use_bi_direction``.
Args:
n_layers (int): Number of layers.
in_size (int): Dimensionality of input vectors.
out_size (int): Dimensionality of hidden states and output vectors.
dropout (float): Dropout ratio.
use_bi_direction (bool): if ``True``, use Bi-directional LSTM.
.. seealso::
:func:`chainer.functions.n_step_lstm`
:func:`chainer.functions.n_step_bilstm`
"""
n_weights = 8
def forward(self, hx, cx, xs, **kwargs):
"""forward(self, hx, cx, xs)
Calculate all hidden states and cell states.
.. warning::
``train`` argument is not supported anymore since v2.
Instead, use ``chainer.using_config('train', train)``.
See :func:`chainer.using_config`.
Args:
hx (~chainer.Variable or None): Initial hidden states. If ``None``
is specified zero-vector is used. Its shape is ``(S, B, N)``
for uni-directional LSTM and ``(2S, B, N)`` for
bi-directional LSTM where ``S`` is the number of layers
and is equal to ``n_layers``, ``B`` is the mini-batch size,
and ``N`` is the dimension of the hidden units.
cx (~chainer.Variable or None): Initial cell states. If ``None``
is specified zero-vector is used.
It has the same shape as ``hx``.
xs (list of ~chainer.Variable): List of input sequences.
Each element ``xs[i]`` is a :class:`chainer.Variable` holding
a sequence. Its shape is ``(L_t, I)``, where ``L_t`` is the
length of a sequence for time ``t``, and ``I`` is the size of
the input and is equal to ``in_size``.
Returns:
tuple: This function returns a tuple containing three elements,
``hy``, ``cy`` and ``ys``.
- ``hy`` is an updated hidden states whose shape is the same as
``hx``.
- ``cy`` is an updated cell states whose shape is the same as
``cx``.
- ``ys`` is a list of :class:`~chainer.Variable` . Each element
``ys[t]`` holds hidden states of the last layer corresponding
to an input ``xs[t]``. Its shape is ``(L_t, N)`` for
uni-directional LSTM and ``(L_t, 2N)`` for bi-directional LSTM
where ``L_t`` is the length of a sequence for time ``t``,
and ``N`` is size of hidden units.
"""
(hy, cy), ys = self._call([hx, cx], xs, **kwargs)
return hy, cy, ys
class NStepLSTM(NStepLSTMBase):
"""__init__(self, n_layers, in_size, out_size, dropout)
Stacked Uni-directional LSTM for sequences.
This link is stacked version of Uni-directional LSTM for sequences.
It calculates hidden and cell states of all layer at end-of-string,
and all hidden states of the last layer for each time.
Unlike :func:`chainer.functions.n_step_lstm`, this function automatically
sort inputs in descending order by length, and transpose the sequence.
Users just need to call the link with a list of :class:`chainer.Variable`
holding sequences.
.. warning::
``use_cudnn`` argument is not supported anymore since v2.
Instead, use ``chainer.using_config('use_cudnn', use_cudnn)``.
See :func:`chainer.using_config`.
Args:
n_layers (int): Number of layers.
in_size (int): Dimensionality of input vectors.
out_size (int): Dimensionality of hidden states and output vectors.
dropout (float): Dropout ratio.
.. seealso::
:func:`chainer.functions.n_step_lstm`
"""
use_bi_direction = False
def rnn(self, *args):
return rnn.n_step_lstm(*args)
@property
def n_cells(self):
return 2
class NStepBiLSTM(NStepLSTMBase):
"""__init__(self, n_layers, in_size, out_size, dropout)
Stacked Bi-directional LSTM for sequences.
This link is stacked version of Bi-directional LSTM for sequences.
It calculates hidden and cell states of all layer at end-of-string,
and all hidden states of the last layer for each time.
Unlike :func:`chainer.functions.n_step_bilstm`, this function automatically
sort inputs in descending order by length, and transpose the sequence.
Users just need to call the link with a list of :class:`chainer.Variable`
holding sequences.
.. warning::
``use_cudnn`` argument is not supported anymore since v2.
Instead, use ``chainer.using_config('use_cudnn', use_cudnn)``.
See :func:`chainer.using_config`.
Args:
n_layers (int): Number of layers.
in_size (int): Dimensionality of input vectors.
out_size (int): Dimensionality of hidden states and output vectors.
dropout (float): Dropout ratio.
.. seealso::
:func:`chainer.functions.n_step_bilstm`
"""
use_bi_direction = True
def rnn(self, *args):
return rnn.n_step_bilstm(*args)
@property
def n_cells(self):
return 2