test_gru_unit_op.py 6.9 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# 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
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# 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.

G
guosheng 已提交
15 16
import math
import unittest
17

G
guosheng 已提交
18
import numpy as np
19
from op_test import OpTest
20 21 22

import paddle.fluid as fluid
from paddle.fluid.framework import Program, program_guard
X
Xing Wu 已提交
23
from paddle.fluid.layers import gru_unit
G
guosheng 已提交
24 25


26 27 28 29 30 31 32 33 34 35 36 37
class GRUActivationType(OpTest):
    identity = 0
    sigmoid = 1
    tanh = 2
    relu = 3


def identity(x):
    return x


def sigmoid(x):
38
    return 1.0 / (1.0 + np.exp(-x))
G
guosheng 已提交
39 40


41
def tanh(x):
42
    return 2.0 * sigmoid(2.0 * x) - 1.0
43 44 45 46


def relu(x):
    return np.maximum(x, 0)
G
guosheng 已提交
47 48


X
Xing Wu 已提交
49 50 51 52 53
class TestGRUUnitOpError(unittest.TestCase):
    def test_errors(self):
        with program_guard(Program(), Program()):
            batch_size = 5
            hidden_dim = 40
54 55 56 57 58 59
            input = fluid.data(
                name='input', shape=[None, hidden_dim * 3], dtype='float32'
            )
            pre_hidden = fluid.data(
                name='pre_hidden', shape=[None, hidden_dim], dtype='float32'
            )
X
Xing Wu 已提交
60
            np_input = np.random.uniform(
61 62
                -0.1, 0.1, (batch_size, hidden_dim * 3)
            ).astype('float64')
X
Xing Wu 已提交
63
            np_pre_hidden = np.random.uniform(
64 65
                -0.1, 0.1, (batch_size, hidden_dim)
            ).astype('float64')
X
Xing Wu 已提交
66 67 68 69 70 71 72 73 74 75 76 77

            def test_input_Variable():
                gru_unit(np_input, pre_hidden, hidden_dim * 3)

            self.assertRaises(TypeError, test_input_Variable)

            def test_pre_hidden_Variable():
                gru_unit(input, np_pre_hidden, hidden_dim * 3)

            self.assertRaises(TypeError, test_pre_hidden_Variable)

            def test_input_type():
78 79 80 81 82
                error_input = fluid.data(
                    name='error_input',
                    shape=[None, hidden_dim * 3],
                    dtype='int32',
                )
X
Xing Wu 已提交
83 84 85 86 87
                gru_unit(error_input, pre_hidden, hidden_dim * 3)

            self.assertRaises(TypeError, test_input_type)

            def test_pre_hidden_type():
88 89 90 91 92
                error_pre_hidden = fluid.data(
                    name='error_pre_hidden',
                    shape=[None, hidden_dim],
                    dtype='int32',
                )
X
Xing Wu 已提交
93 94 95 96 97
                gru_unit(input, error_pre_hidden, hidden_dim * 3)

            self.assertRaises(TypeError, test_pre_hidden_type)


G
guosheng 已提交
98
class TestGRUUnitOp(OpTest):
99
    batch_size = 5
Z
zhupengyang 已提交
100
    frame_size = 40
101 102 103 104 105 106 107
    activate = {
        GRUActivationType.identity: identity,
        GRUActivationType.sigmoid: sigmoid,
        GRUActivationType.tanh: tanh,
        GRUActivationType.relu: relu,
    }

Q
Qiao Longfei 已提交
108
    def set_inputs(self, origin_mode=False):
G
guosheng 已提交
109 110
        batch_size = self.batch_size
        frame_size = self.frame_size
111
        self.op_type = 'gru_unit'
G
guosheng 已提交
112
        self.inputs = {
113 114 115 116 117 118 119 120 121 122 123
            'Input': np.random.uniform(
                -0.1, 0.1, (batch_size, frame_size * 3)
            ).astype(self.dtype),
            'HiddenPrev': np.random.uniform(
                -0.1, 0.1, (batch_size, frame_size)
            ).astype(self.dtype),
            'Weight': np.random.uniform(
                -1.0 / math.sqrt(frame_size),
                1.0 / math.sqrt(frame_size),
                (frame_size, frame_size * 3),
            ).astype(self.dtype),
G
guosheng 已提交
124
        }
125 126
        self.attrs = {
            'activation': GRUActivationType.tanh,
Q
Qiao Longfei 已提交
127
            'gate_activation': GRUActivationType.sigmoid,
128
            'origin_mode': origin_mode,
129
        }
G
guosheng 已提交
130

Q
Qiao Longfei 已提交
131
    def set_outputs(self, origin_mode=False):
132
        # GRU calculations
G
guosheng 已提交
133 134
        batch_size = self.batch_size
        frame_size = self.frame_size
135 136 137
        x = self.inputs['Input']
        h_p = self.inputs['HiddenPrev']
        w = self.inputs['Weight']
138 139 140 141 142
        b = (
            self.inputs['Bias']
            if 'Bias' in self.inputs
            else np.zeros((1, frame_size * 3))
        )
G
guosheng 已提交
143
        g = x + np.tile(b, (batch_size, 1))
144 145 146 147 148 149
        w_u_r = w.flatten()[: frame_size * frame_size * 2].reshape(
            (frame_size, frame_size * 2)
        )
        u_r = self.activate[self.attrs['gate_activation']](
            np.dot(h_p, w_u_r) + g[:, : frame_size * 2]
        )
G
guosheng 已提交
150
        u = u_r[:, :frame_size]
151
        r = u_r[:, frame_size : frame_size * 2]
G
guosheng 已提交
152
        r_h_p = r * h_p
153 154 155 156 157 158
        w_c = w.flatten()[frame_size * frame_size * 2 :].reshape(
            (frame_size, frame_size)
        )
        c = self.activate[self.attrs['activation']](
            np.dot(r_h_p, w_c) + g[:, frame_size * 2 :]
        )
G
guosheng 已提交
159
        g = np.hstack((u_r, c))
Q
Qiao Longfei 已提交
160 161 162 163
        if origin_mode:
            h = (1 - u) * c + u * h_p
        else:
            h = u * c + (1 - u) * h_p
Y
Yu Yang 已提交
164
        self.outputs = {
R
ronnywang 已提交
165 166
            'Gate': g.astype(self.dtype),
            'ResetHiddenPrev': r_h_p.astype(self.dtype),
167
            'Hidden': h.astype(self.dtype),
Y
Yu Yang 已提交
168
        }
G
guosheng 已提交
169

G
guosheng 已提交
170
    def setUp(self):
171 172 173
        self.dtype = (
            'float32' if fluid.core.is_compiled_with_rocm() else 'float64'
        )
G
guosheng 已提交
174 175 176
        self.set_inputs()
        self.set_outputs()

G
guosheng 已提交
177 178 179
    def test_check_output(self):
        self.check_output()

G
guosheng 已提交
180
    def test_check_grad(self):
181
        self.check_grad(['Input', 'HiddenPrev', 'Weight'], ['Hidden'])
G
guosheng 已提交
182 183


Q
Qiao Longfei 已提交
184 185
class TestGRUUnitOpOriginMode(TestGRUUnitOp):
    def setUp(self):
186 187 188
        self.dtype = (
            'float32' if fluid.core.is_compiled_with_rocm() else 'float64'
        )
Q
Qiao Longfei 已提交
189
        self.set_inputs(origin_mode=True)
Q
Qiao Longfei 已提交
190 191 192
        self.set_outputs(origin_mode=True)


G
guosheng 已提交
193
class TestGRUUnitOpWithBias(TestGRUUnitOp):
Q
Qiao Longfei 已提交
194
    def set_inputs(self, origin_mode=False):
G
guosheng 已提交
195 196
        batch_size = self.batch_size
        frame_size = self.frame_size
197
        super().set_inputs()
G
guosheng 已提交
198
        self.inputs['Bias'] = np.random.uniform(
199 200
            -0.1, 0.1, (1, frame_size * 3)
        ).astype(self.dtype)
G
guosheng 已提交
201 202
        self.attrs = {
            'activation': GRUActivationType.identity,
Q
Qiao Longfei 已提交
203
            'gate_activation': GRUActivationType.sigmoid,
204
            'origin_mode': origin_mode,
G
guosheng 已提交
205 206
        }

G
guosheng 已提交
207
    def test_check_grad(self):
208 209 210
        self.check_grad(['Input', 'HiddenPrev', 'Weight', 'Bias'], ['Hidden'])

    def test_check_grad_ingore_input(self):
211 212 213 214 215
        self.check_grad(
            ['HiddenPrev', 'Weight', 'Bias'],
            ['Hidden'],
            no_grad_set=set('Input'),
        )
G
guosheng 已提交
216 217


Q
Qiao Longfei 已提交
218 219
class TestGRUUnitOpWithBiasOriginMode(TestGRUUnitOpWithBias):
    def setUp(self):
220 221 222
        self.dtype = (
            'float32' if fluid.core.is_compiled_with_rocm() else 'float64'
        )
Q
Qiao Longfei 已提交
223
        self.set_inputs(origin_mode=True)
Q
Qiao Longfei 已提交
224 225 226
        self.set_outputs(origin_mode=True)


G
guosheng 已提交
227 228
if __name__ == '__main__':
    unittest.main()