test_gru_unit_op.py 7.6 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 17
import math
import unittest
import numpy as np
18
import paddle.fluid as fluid
19
from op_test import OpTest
X
Xing Wu 已提交
20 21 22
from paddle import fluid
from paddle.fluid.layers import gru_unit
from paddle.fluid.framework import program_guard, Program
G
guosheng 已提交
23 24


25 26 27 28 29 30
class TestGRUUnitAPIError(unittest.TestCase):
    def test_errors(self):
        with fluid.program_guard(fluid.Program(), fluid.Program()):
            D = 5
            layer = fluid.dygraph.nn.GRUUnit(size=D * 3)
            # the input must be Variable.
31 32 33
            x0 = fluid.create_lod_tensor(
                np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.CPUPlace()
            )
34 35 36 37 38 39 40
            self.assertRaises(TypeError, layer, x0)
            # the input dtype must be float32 or float64
            x = fluid.data(name='x', shape=[-1, D * 3], dtype='float16')
            hidden = fluid.data(name='hidden', shape=[-1, D], dtype='float32')
            self.assertRaises(TypeError, layer, x, hidden)


41 42 43 44 45 46 47 48 49 50 51 52
class GRUActivationType(OpTest):
    identity = 0
    sigmoid = 1
    tanh = 2
    relu = 3


def identity(x):
    return x


def sigmoid(x):
53
    return 1.0 / (1.0 + np.exp(-x))
G
guosheng 已提交
54 55


56
def tanh(x):
57
    return 2.0 * sigmoid(2.0 * x) - 1.0
58 59 60 61


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


X
Xing Wu 已提交
64 65 66 67 68
class TestGRUUnitOpError(unittest.TestCase):
    def test_errors(self):
        with program_guard(Program(), Program()):
            batch_size = 5
            hidden_dim = 40
69 70 71 72 73 74
            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 已提交
75
            np_input = np.random.uniform(
76 77
                -0.1, 0.1, (batch_size, hidden_dim * 3)
            ).astype('float64')
X
Xing Wu 已提交
78
            np_pre_hidden = np.random.uniform(
79 80
                -0.1, 0.1, (batch_size, hidden_dim)
            ).astype('float64')
X
Xing Wu 已提交
81 82 83 84 85 86 87 88 89 90 91 92

            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():
93 94 95 96 97
                error_input = fluid.data(
                    name='error_input',
                    shape=[None, hidden_dim * 3],
                    dtype='int32',
                )
X
Xing Wu 已提交
98 99 100 101 102
                gru_unit(error_input, pre_hidden, hidden_dim * 3)

            self.assertRaises(TypeError, test_input_type)

            def test_pre_hidden_type():
103 104 105 106 107
                error_pre_hidden = fluid.data(
                    name='error_pre_hidden',
                    shape=[None, hidden_dim],
                    dtype='int32',
                )
X
Xing Wu 已提交
108 109 110 111 112
                gru_unit(input, error_pre_hidden, hidden_dim * 3)

            self.assertRaises(TypeError, test_pre_hidden_type)


G
guosheng 已提交
113
class TestGRUUnitOp(OpTest):
114
    batch_size = 5
Z
zhupengyang 已提交
115
    frame_size = 40
116 117 118 119 120 121 122
    activate = {
        GRUActivationType.identity: identity,
        GRUActivationType.sigmoid: sigmoid,
        GRUActivationType.tanh: tanh,
        GRUActivationType.relu: relu,
    }

Q
Qiao Longfei 已提交
123
    def set_inputs(self, origin_mode=False):
G
guosheng 已提交
124 125
        batch_size = self.batch_size
        frame_size = self.frame_size
126
        self.op_type = 'gru_unit'
G
guosheng 已提交
127
        self.inputs = {
128 129 130 131 132 133 134 135 136 137 138
            '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 已提交
139
        }
140 141
        self.attrs = {
            'activation': GRUActivationType.tanh,
Q
Qiao Longfei 已提交
142
            'gate_activation': GRUActivationType.sigmoid,
143
            'origin_mode': origin_mode,
144
        }
G
guosheng 已提交
145

Q
Qiao Longfei 已提交
146
    def set_outputs(self, origin_mode=False):
147
        # GRU calculations
G
guosheng 已提交
148 149
        batch_size = self.batch_size
        frame_size = self.frame_size
150 151 152
        x = self.inputs['Input']
        h_p = self.inputs['HiddenPrev']
        w = self.inputs['Weight']
153 154 155 156 157
        b = (
            self.inputs['Bias']
            if 'Bias' in self.inputs
            else np.zeros((1, frame_size * 3))
        )
G
guosheng 已提交
158
        g = x + np.tile(b, (batch_size, 1))
159 160 161 162 163 164
        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 已提交
165
        u = u_r[:, :frame_size]
166
        r = u_r[:, frame_size : frame_size * 2]
G
guosheng 已提交
167
        r_h_p = r * h_p
168 169 170 171 172 173
        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 已提交
174
        g = np.hstack((u_r, c))
Q
Qiao Longfei 已提交
175 176 177 178
        if origin_mode:
            h = (1 - u) * c + u * h_p
        else:
            h = u * c + (1 - u) * h_p
Y
Yu Yang 已提交
179
        self.outputs = {
R
ronnywang 已提交
180 181
            'Gate': g.astype(self.dtype),
            'ResetHiddenPrev': r_h_p.astype(self.dtype),
182
            'Hidden': h.astype(self.dtype),
Y
Yu Yang 已提交
183
        }
G
guosheng 已提交
184

G
guosheng 已提交
185
    def setUp(self):
186 187 188
        self.dtype = (
            'float32' if fluid.core.is_compiled_with_rocm() else 'float64'
        )
G
guosheng 已提交
189 190 191
        self.set_inputs()
        self.set_outputs()

G
guosheng 已提交
192 193 194
    def test_check_output(self):
        self.check_output()

G
guosheng 已提交
195
    def test_check_grad(self):
196
        self.check_grad(['Input', 'HiddenPrev', 'Weight'], ['Hidden'])
G
guosheng 已提交
197 198


Q
Qiao Longfei 已提交
199 200
class TestGRUUnitOpOriginMode(TestGRUUnitOp):
    def setUp(self):
201 202 203
        self.dtype = (
            'float32' if fluid.core.is_compiled_with_rocm() else 'float64'
        )
Q
Qiao Longfei 已提交
204
        self.set_inputs(origin_mode=True)
Q
Qiao Longfei 已提交
205 206 207
        self.set_outputs(origin_mode=True)


G
guosheng 已提交
208
class TestGRUUnitOpWithBias(TestGRUUnitOp):
Q
Qiao Longfei 已提交
209
    def set_inputs(self, origin_mode=False):
G
guosheng 已提交
210 211
        batch_size = self.batch_size
        frame_size = self.frame_size
212
        super().set_inputs()
G
guosheng 已提交
213
        self.inputs['Bias'] = np.random.uniform(
214 215
            -0.1, 0.1, (1, frame_size * 3)
        ).astype(self.dtype)
G
guosheng 已提交
216 217
        self.attrs = {
            'activation': GRUActivationType.identity,
Q
Qiao Longfei 已提交
218
            'gate_activation': GRUActivationType.sigmoid,
219
            'origin_mode': origin_mode,
G
guosheng 已提交
220 221
        }

G
guosheng 已提交
222
    def test_check_grad(self):
223 224 225
        self.check_grad(['Input', 'HiddenPrev', 'Weight', 'Bias'], ['Hidden'])

    def test_check_grad_ingore_input(self):
226 227 228 229 230
        self.check_grad(
            ['HiddenPrev', 'Weight', 'Bias'],
            ['Hidden'],
            no_grad_set=set('Input'),
        )
G
guosheng 已提交
231 232


Q
Qiao Longfei 已提交
233 234
class TestGRUUnitOpWithBiasOriginMode(TestGRUUnitOpWithBias):
    def setUp(self):
235 236 237
        self.dtype = (
            'float32' if fluid.core.is_compiled_with_rocm() else 'float64'
        )
Q
Qiao Longfei 已提交
238
        self.set_inputs(origin_mode=True)
Q
Qiao Longfei 已提交
239 240 241
        self.set_outputs(origin_mode=True)


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