test_lstm_op.py 9.3 KB
Newer Older
D
dzhwinter 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
#  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
14 15 16 17
import unittest
import numpy as np
from op_test import OpTest

18 19 20 21
SIGMOID_THRESHOLD_MIN = -40.0
SIGMOID_THRESHOLD_MAX = 13.0
EXP_MAX_INPUT = 40.0

22 23 24 25 26 27

def identity(x):
    return x


def sigmoid(x):
28 29 30 31
    y = np.copy(x)
    y[x < SIGMOID_THRESHOLD_MIN] = SIGMOID_THRESHOLD_MIN
    y[x > SIGMOID_THRESHOLD_MAX] = SIGMOID_THRESHOLD_MAX
    return 1. / (1. + np.exp(-y))
32 33 34


def tanh(x):
35 36 37
    y = -2. * x
    y[y > EXP_MAX_INPUT] = EXP_MAX_INPUT
    return (2. / (1. + np.exp(y))) - 1.
38 39 40 41 42 43


def relu(x):
    return np.maximum(x, 0)


D
dangqingqing 已提交
44 45 46 47 48 49 50 51
ACTVATION = {
    'identity': identity,
    'sigmoid': sigmoid,
    'tanh': tanh,
    'relu': relu
}


52 53 54 55 56 57 58 59 60
def lstm(
        input,  # T x 4D
        lod,  # 1 x N
        h0=None,  # N x D
        c0=None,  # N x D
        w_h=None,  # D x 4D
        w_b=None,  # 1 x 4D
        w_c=None,  # 1 x 3D
        is_reverse=False,
D
dangqingqing 已提交
61 62 63 64
        act_gate=None,
        act_cell=None,
        act_cand=None):
    def _step(x, w_h, w_c, h_pre, c_pre, act_gate, act_cell, act_cand):
65 66 67
        g = np.dot(h_pre, w_h)  # 1 x 4D
        g = g + x
        g = np.reshape(g, (1, g.size))
D
dangqingqing 已提交
68
        c, g_i, g_f, g_o = np.split(g, 4, axis=1)
69
        if w_c is None:
D
dangqingqing 已提交
70 71
            g_i = act_gate(g_i)  # 1 x D
            g_f = act_gate(g_f)  # 1 x D
72 73
        else:
            w_ic, w_fc, w_oc = np.split(w_c, 3, axis=1)
D
dangqingqing 已提交
74 75
            g_i = act_gate(g_i + w_ic * c_pre)  # 1 x D
            g_f = act_gate(g_f + w_fc * c_pre)  # 1 x D
D
dangqingqing 已提交
76
        c = g_f * c_pre + g_i * act_cand(c)  # 1 x D
77 78

        if w_c is None:
D
dangqingqing 已提交
79
            g_o = act_gate(g_o)  # 1 x D
80 81
        else:
            _, _, w_oc = np.split(w_c, 3, axis=1)
D
dangqingqing 已提交
82 83
            g_o = act_gate(g_o + w_oc * c)  # 1 x D
        h = g_o * act_cell(c)
D
dangqingqing 已提交
84
        return h, c
85

D
dangqingqing 已提交
86 87 88 89 90 91 92
    def _reverse(x, lod):
        y = np.zeros_like(x)
        for i in range(len(lod) - 1):
            b, e = lod[i], lod[i + 1]
            y[b:e, :] = np.flip(x[b:e, :], 0)
        return y

93 94 95 96
    offset = lod[0]
    batch_size = len(offset) - 1
    hidden = []
    cell = []
D
dangqingqing 已提交
97
    input = _reverse(input, offset) if is_reverse else input
98 99 100 101 102 103 104
    if w_b is not None:
        input = input + np.tile(w_b, (offset[-1], 1))
    for i in range(batch_size):
        # compute one sequence
        seq_len = offset[i + 1] - offset[i]
        x = input[offset[i]:offset[i + 1], :]
        h_pre = h0[i]  # 1 x D
105
        c_pre = c0[i]  # 1 x D
106 107
        for j in range(seq_len):
            # compute one step
D
dangqingqing 已提交
108 109
            h_pre, c_pre = _step(x[j], w_h, w_c, h_pre, c_pre, act_gate,
                                 act_cell, act_cand)
110 111 112
            hidden.append(h_pre.flatten())
            cell.append(c_pre.flatten())

113 114
    hidden = np.array(hidden).astype('float64')
    cell = np.array(cell).astype('float64')
D
dangqingqing 已提交
115 116 117 118

    hidden = _reverse(hidden, offset) if is_reverse else hidden
    cell = _reverse(cell, offset) if is_reverse else cell

119 120
    assert hidden.shape == (input.shape[0], input.shape[1] / 4)
    assert cell.shape == (input.shape[0], input.shape[1] / 4)
D
dangqingqing 已提交
121
    return hidden, cell
122 123


D
dangqingqing 已提交
124
class TestLstmOp(OpTest):
125
    def set_argument(self):
126
        self.lod = [[0, 2, 5, 7]]
127 128
        self.D = 16

129 130 131
        self.act_gate = 'sigmoid'
        self.act_cell = 'tanh'
        self.act_cand = 'tanh'
D
dangqingqing 已提交
132

D
dangqingqing 已提交
133
        self.has_initial_state = False
D
dangqingqing 已提交
134
        self.is_reverse = False
D
dangqingqing 已提交
135
        self.use_peepholes = True
D
dangqingqing 已提交
136 137

    def setUp(self):
138
        self.set_argument()
139
        self.op_type = 'lstm'
D
dangqingqing 已提交
140 141 142 143

        T = self.lod[0][-1]
        N = len(self.lod[0]) - 1

144
        x = np.random.normal(size=(T, 4 * self.D)).astype('float64')
D
dangqingqing 已提交
145 146 147 148 149 150
        if self.has_initial_state:
            h0 = np.random.normal(size=(N, self.D)).astype('float64')
            c0 = np.random.normal(size=(N, self.D)).astype('float64')
        else:
            h0 = np.zeros((N, self.D)).astype('float64')
            c0 = np.zeros((N, self.D)).astype('float64')
151
        w = np.random.normal(size=(self.D, 4 * self.D)).astype('float64')
D
dangqingqing 已提交
152 153 154 155
        if self.use_peepholes:
            b = np.random.normal(size=(1, 7 * self.D)).astype('float64')
        else:
            b = np.random.normal(size=(1, 4 * self.D)).astype('float64')
D
dangqingqing 已提交
156

D
dangqingqing 已提交
157 158
        w_b = b[:, 0:4 * self.D]
        w_c = b[:, 4 * self.D:] if self.use_peepholes else None
D
dangqingqing 已提交
159 160 161
        h, c = lstm(x, self.lod, h0, c0, w, w_b, w_c, self.is_reverse,
                    ACTVATION[self.act_gate], ACTVATION[self.act_cell],
                    ACTVATION[self.act_cand])
162

163 164
        self.inputs = {'Input': (x, self.lod), 'Weight': w}

D
dangqingqing 已提交
165
        self.inputs['Bias'] = b
166

D
dangqingqing 已提交
167 168 169
        if self.has_initial_state:
            self.inputs['H0'] = h0
            self.inputs['C0'] = c0
170

171 172 173 174
        self.outputs = {
            'Hidden': (h, self.lod),
            'Cell': (c, self.lod),
        }
175
        self.attrs = {
D
dangqingqing 已提交
176
            'use_peepholes': self.use_peepholes,
177 178 179 180
            'is_reverse': self.is_reverse,
            'gate_activation': self.act_gate,
            'cell_activation': self.act_cell,
            'candidate_activation': self.act_cand
181 182
        }

D
dangqingqing 已提交
183
    def test_check_output(self):
D
dangqingqing 已提交
184
        self.check_output(atol=1e-8)
185

D
dangqingqing 已提交
186
    def test_check_grad(self):
D
dangqingqing 已提交
187 188 189 190 191
        # TODO(qingqing) remove folowing lines after the check_grad is refined.
        N = len(self.lod[0]) - 1
        self.outputs['BatchGate'] = np.zeros((N, 4 * self.D)).astype('float64')
        self.outputs['BatchCellPreAct'] = np.zeros(
            (N, self.D)).astype('float64')
192
        self.check_grad(
D
dangqingqing 已提交
193
            ['Input', 'Weight', 'Bias'], ['Hidden'], max_relative_error=5e-4)
194 195


D
dangqingqing 已提交
196
class TestLstmOpHasInitial(TestLstmOp):
197
    def set_argument(self):
198
        self.lod = [[0, 2, 5, 7]]
D
dangqingqing 已提交
199
        self.D = 16
200 201 202 203 204

        self.act_gate = 'sigmoid'
        self.act_cell = 'tanh'
        self.act_cand = 'tanh'

D
dangqingqing 已提交
205
        self.has_initial_state = True
206
        self.is_reverse = True
D
dangqingqing 已提交
207
        self.use_peepholes = True
208

D
dangqingqing 已提交
209 210 211 212 213 214 215 216 217
    def test_check_grad(self):
        # TODO(qingqing) remove folowing lines after the check_grad is refined.
        N = len(self.lod[0]) - 1
        self.outputs['BatchGate'] = np.zeros((N, 4 * self.D)).astype('float64')
        self.outputs['BatchCellPreAct'] = np.zeros(
            (N, self.D)).astype('float64')
        self.check_grad(
            ['Input', 'Weight', 'Bias', 'H0', 'C0'], ['Hidden'],
            max_relative_error=5e-4)
218

D
dangqingqing 已提交
219
    def test_check_grad_ingore_bias(self):
D
dangqingqing 已提交
220 221 222 223 224 225 226 227
        N = len(self.lod[0]) - 1
        self.outputs['BatchGate'] = np.zeros((N, 4 * self.D)).astype('float64')
        self.outputs['BatchCellPreAct'] = np.zeros(
            (N, self.D)).astype('float64')
        self.check_grad(
            ['Input', 'Weight'], ['Hidden'],
            max_relative_error=5e-4,
            no_grad_set=set('Bias'))
D
dangqingqing 已提交
228 229

    def test_check_grad_ingore_weight(self):
D
dangqingqing 已提交
230 231 232 233 234 235 236 237
        N = len(self.lod[0]) - 1
        self.outputs['BatchGate'] = np.zeros((N, 4 * self.D)).astype('float64')
        self.outputs['BatchCellPreAct'] = np.zeros(
            (N, self.D)).astype('float64')
        self.check_grad(
            ['Input', 'Bias'], ['Hidden'],
            max_relative_error=5e-4,
            no_grad_set=set('Weight'))
D
dangqingqing 已提交
238 239

    def test_check_grad_ingore_input(self):
D
dangqingqing 已提交
240 241 242 243 244 245 246 247
        N = len(self.lod[0]) - 1
        self.outputs['BatchGate'] = np.zeros((N, 4 * self.D)).astype('float64')
        self.outputs['BatchCellPreAct'] = np.zeros(
            (N, self.D)).astype('float64')
        self.check_grad(
            ['Weight', 'Bias'], ['Hidden'],
            max_relative_error=5e-4,
            no_grad_set=set('Input'))
D
dangqingqing 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270

    def test_check_grad_ingore_h0(self):
        N = len(self.lod[0]) - 1
        self.outputs['BatchGate'] = np.zeros((N, 4 * self.D)).astype('float64')
        self.outputs['BatchCellPreAct'] = np.zeros(
            (N, self.D)).astype('float64')
        self.check_grad(
            ['Input', 'Weight', 'Bias', 'C0'], ['Hidden'],
            max_relative_error=5e-4,
            no_grad_set=set('H0'))

    def test_check_grad_ingore_c0(self):
        N = len(self.lod[0]) - 1
        self.outputs['BatchGate'] = np.zeros((N, 4 * self.D)).astype('float64')
        self.outputs['BatchCellPreAct'] = np.zeros(
            (N, self.D)).astype('float64')
        self.check_grad(
            ['Input', 'Weight', 'Bias', 'H0'], ['Hidden'],
            max_relative_error=5e-4,
            no_grad_set=set('C0'))


class TestLstmOpRerverse(TestLstmOp):
271 272 273 274 275 276 277 278
    def set_argument(self):
        self.lod = [[0, 2, 5, 7]]
        self.D = 16

        self.act_gate = 'sigmoid'
        self.act_cell = 'tanh'
        self.act_cand = 'tanh'

D
dangqingqing 已提交
279 280 281
        self.has_initial_state = False
        self.is_reverse = True
        self.use_peepholes = True
282

D
dangqingqing 已提交
283 284

class TestLstmOpNotUsePeepholes(TestLstmOp):
285
    def set_argument(self):
286
        self.lod = [[0, 2, 5, 7]]
D
dangqingqing 已提交
287
        self.D = 16
288 289 290 291 292

        self.act_gate = 'sigmoid'
        self.act_cell = 'tanh'
        self.act_cand = 'tanh'

D
dangqingqing 已提交
293
        self.has_initial_state = False
294
        self.is_reverse = True
D
dangqingqing 已提交
295 296
        self.use_peepholes = False

297 298

if __name__ == '__main__':
299
    unittest.main()