test_lstmp_op.py 11.1 KB
Newer Older
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
#  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.
import unittest
import numpy as np
from op_test import OpTest

SIGMOID_THRESHOLD_MIN = -40.0
SIGMOID_THRESHOLD_MAX = 13.0
EXP_MAX_INPUT = 40.0


def identity(x):
    return x


def sigmoid(x):
    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))


def tanh(x):
    y = -2. * x
    y[y > EXP_MAX_INPUT] = EXP_MAX_INPUT
    return (2. / (1. + np.exp(y))) - 1.


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


ACTVATION = {
    'identity': identity,
    'sigmoid': sigmoid,
    'tanh': tanh,
    'relu': relu
}


# LSTM with recurrent projection Layer
def lstmp(
        input,  # T x 4D
        lod,  # 1 x N
        h0=None,  # N x D
        c0=None,  # N x D
        w_r=None,  # P x 5D
        w_rh=None,  # D x P
        w_b=None,  # 1 x 4D
        w_c=None,  # 1 x 3D
        is_reverse=False,
        act_gate=None,
        act_cell=None,
65 66
        act_cand=None,
        share_cell_act=True):
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    def _step(x, w_r, w_rh, w_c, r_pre, c_pre, act_gate, act_cell, act_cand):
        g = np.dot(r_pre, w_r)  # 1 x 4D
        g = g + x
        g = np.reshape(g, (1, g.size))
        c, g_i, g_f, g_o = np.split(g, 4, axis=1)
        if w_c is None:
            g_i = act_gate(g_i)  # 1 x D
            g_f = act_gate(g_f)  # 1 x D
        else:
            w_ic, w_fc, _ = np.split(w_c, 3, axis=1)
            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
        c = g_f * c_pre + g_i * act_cand(c)  # 1 x D

        if w_c is None:
            g_o = act_gate(g_o)  # 1 x D
        else:
            _, _, w_oc = np.split(w_c, 3, axis=1)
            g_o = act_gate(g_o + w_oc * c)  # 1 x D
        h = g_o * act_cell(c)
        # projection
        r = np.dot(h, w_rh)
89 90
        if share_cell_act:
            r = act_cell(r)
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
        return r, c

    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

    offset = lod[0]
    batch_size = len(offset) - 1
    # recurrent projection state
    projection = []
    cell = []
    input = _reverse(input, offset) if is_reverse else input
    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], :]
        r_pre = np.dot(h0[i], w_rh)  # 1 x P
113 114
        if share_cell_act:
            r_pre = act_cell(r_pre)
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
        c_pre = c0[i]  # 1 x D
        for j in range(seq_len):
            # compute one step
            r_pre, c_pre = _step(x[j], w_r, w_rh, w_c, r_pre, c_pre, act_gate,
                                 act_cell, act_cand)
            projection.append(r_pre.flatten())
            cell.append(c_pre.flatten())

    projection = np.array(projection).astype('float64')
    cell = np.array(cell).astype('float64')

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

    assert projection.shape == (input.shape[0], w_r.shape[0])  # T x P
    assert cell.shape == (input.shape[0], input.shape[1] / 4)  # T x D
    return projection, cell


class TestLstmOp(OpTest):
    def set_argument(self):
        self.lod = [[0, 2, 5, 7]]
        # hidden size
        self.D = 16
        # projection size
        self.P = 10

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

146
        self.share_cell_act = True
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
        self.has_initial_state = False
        self.is_reverse = False
        self.use_peepholes = True

    def setUp(self):
        self.set_argument()
        self.op_type = 'lstmp'

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

        x = np.random.normal(size=(T, 4 * self.D)).astype('float64')
        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')
        w = np.random.normal(size=(self.P, 4 * self.D)).astype('float64')
        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')

        w_b = b[:, 0:4 * self.D]
        w_c = b[:, 4 * self.D:] if self.use_peepholes else None
        w_rh = np.random.normal(size=(self.D, self.P)).astype('float64')
        r, c = lstmp(x, self.lod, h0, c0, w, w_rh, w_b, w_c, self.is_reverse,
                     ACTVATION[self.act_gate], ACTVATION[self.act_cell],
176
                     ACTVATION[self.act_cand], self.share_cell_act)
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

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

        self.inputs['Bias'] = b

        if self.has_initial_state:
            self.inputs['H0'] = h0
            self.inputs['C0'] = c0

        self.outputs = {
            'Projection': (r, self.lod),
            'Cell': (c, self.lod),
        }
        self.attrs = {
            'use_peepholes': self.use_peepholes,
            'is_reverse': self.is_reverse,
            'gate_activation': self.act_gate,
            'cell_activation': self.act_cell,
            'candidate_activation': self.act_cand
        }

    def test_check_output(self):
        self.check_output(atol=1e-8)

    def test_check_grad(self):
        # TODO(qingqing) remove folowing lines after the check_grad is refined.
        N = len(self.lod[0]) - 1
204
        self.outputs['OrderedP0'] = np.zeros((N, self.P)).astype('float64')
205
        self.outputs['BatchGate'] = np.zeros((N, 4 * self.D)).astype('float64')
206
        self.outputs['BatchHidden'] = np.zeros((N, self.D)).astype('float64')
207 208 209
        self.outputs['BatchCellPreAct'] = np.zeros(
            (N, self.D)).astype('float64')
        self.check_grad(
210 211
            ['Input', 'Weight', 'Bias'], ['Projection'],
            max_relative_error=5e-3)
212 213 214 215 216 217


class TestLstmOpHasInitial(TestLstmOp):
    def set_argument(self):
        self.lod = [[0, 2, 5, 7]]
        self.D = 16
218
        self.P = 5
219 220 221 222 223

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

224
        self.share_cell_act = True
225 226 227 228 229 230 231
        self.has_initial_state = True
        self.is_reverse = True
        self.use_peepholes = True

    def test_check_grad(self):
        # TODO(qingqing) remove folowing lines after the check_grad is refined.
        N = len(self.lod[0]) - 1
232
        self.outputs['OrderedP0'] = np.zeros((N, self.P)).astype('float64')
233
        self.outputs['BatchGate'] = np.zeros((N, 4 * self.D)).astype('float64')
234
        self.outputs['BatchHidden'] = np.zeros((N, self.D)).astype('float64')
235 236 237
        self.outputs['BatchCellPreAct'] = np.zeros(
            (N, self.D)).astype('float64')
        self.check_grad(
238 239
            ['Input', 'Weight', 'Bias', 'H0', 'C0'], ['Projection'],
            max_relative_error=5e-3)
240 241 242

    def test_check_grad_ingore_bias(self):
        N = len(self.lod[0]) - 1
243
        self.outputs['OrderedP0'] = np.zeros((N, self.P)).astype('float64')
244
        self.outputs['BatchGate'] = np.zeros((N, 4 * self.D)).astype('float64')
245
        self.outputs['BatchHidden'] = np.zeros((N, self.D)).astype('float64')
246 247 248
        self.outputs['BatchCellPreAct'] = np.zeros(
            (N, self.D)).astype('float64')
        self.check_grad(
249 250
            ['Input', 'Weight'], ['Projection'],
            max_relative_error=5e-3,
251 252 253 254
            no_grad_set=set('Bias'))

    def test_check_grad_ingore_weight(self):
        N = len(self.lod[0]) - 1
255
        self.outputs['OrderedP0'] = np.zeros((N, self.P)).astype('float64')
256
        self.outputs['BatchGate'] = np.zeros((N, 4 * self.D)).astype('float64')
257
        self.outputs['BatchHidden'] = np.zeros((N, self.D)).astype('float64')
258 259 260
        self.outputs['BatchCellPreAct'] = np.zeros(
            (N, self.D)).astype('float64')
        self.check_grad(
261 262
            ['Input', 'Bias'], ['Projection'],
            max_relative_error=5e-3,
263 264 265 266
            no_grad_set=set('Weight'))

    def test_check_grad_ingore_input(self):
        N = len(self.lod[0]) - 1
267
        self.outputs['OrderedP0'] = np.zeros((N, self.P)).astype('float64')
268
        self.outputs['BatchGate'] = np.zeros((N, 4 * self.D)).astype('float64')
269
        self.outputs['BatchHidden'] = np.zeros((N, self.D)).astype('float64')
270 271 272
        self.outputs['BatchCellPreAct'] = np.zeros(
            (N, self.D)).astype('float64')
        self.check_grad(
273 274
            ['Weight', 'Bias'], ['Projection'],
            max_relative_error=5e-3,
275 276 277 278
            no_grad_set=set('Input'))

    def test_check_grad_ingore_h0(self):
        N = len(self.lod[0]) - 1
279
        self.outputs['OrderedP0'] = np.zeros((N, self.P)).astype('float64')
280
        self.outputs['BatchGate'] = np.zeros((N, 4 * self.D)).astype('float64')
281
        self.outputs['BatchHidden'] = np.zeros((N, self.D)).astype('float64')
282 283 284
        self.outputs['BatchCellPreAct'] = np.zeros(
            (N, self.D)).astype('float64')
        self.check_grad(
285 286
            ['Input', 'Weight', 'Bias', 'C0'], ['Projection'],
            max_relative_error=5e-3,
287 288 289 290
            no_grad_set=set('H0'))

    def test_check_grad_ingore_c0(self):
        N = len(self.lod[0]) - 1
291
        self.outputs['OrderedP0'] = np.zeros((N, self.P)).astype('float64')
292
        self.outputs['BatchGate'] = np.zeros((N, 4 * self.D)).astype('float64')
293
        self.outputs['BatchHidden'] = np.zeros((N, self.D)).astype('float64')
294 295 296
        self.outputs['BatchCellPreAct'] = np.zeros(
            (N, self.D)).astype('float64')
        self.check_grad(
297 298
            ['Input', 'Weight', 'Bias', 'H0'], ['Projection'],
            max_relative_error=5e-3,
299 300 301 302 303 304 305 306 307 308 309 310 311
            no_grad_set=set('C0'))


class TestLstmOpRerverse(TestLstmOp):
    def set_argument(self):
        self.lod = [[0, 2, 5, 7]]
        self.D = 16
        self.P = 10

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

312
        self.share_cell_act = True
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
        self.has_initial_state = False
        self.is_reverse = True
        self.use_peepholes = True


class TestLstmOpNotUsePeepholes(TestLstmOp):
    def set_argument(self):
        self.lod = [[0, 2, 5, 7]]
        self.D = 16
        self.P = 10

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

328
        self.share_cell_act = True
329 330 331 332 333 334 335
        self.has_initial_state = False
        self.is_reverse = True
        self.use_peepholes = False


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