test_gru_unit_op.py 7.7 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
class TestGRUUnitAPIError(unittest.TestCase):
26

27 28 29 30 31
    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.
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):
G
guosheng 已提交
53 54 55
    return 1. / (1. + np.exp(-x))


56 57 58 59 60 61
def tanh(x):
    return 2. * sigmoid(2. * x) - 1.


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


X
Xing Wu 已提交
64
class TestGRUUnitOpError(unittest.TestCase):
65

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

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

            self.assertRaises(TypeError, test_input_type)

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

            self.assertRaises(TypeError, test_pre_hidden_type)


G
guosheng 已提交
108
class TestGRUUnitOp(OpTest):
109
    batch_size = 5
Z
zhupengyang 已提交
110
    frame_size = 40
111 112 113 114 115 116 117
    activate = {
        GRUActivationType.identity: identity,
        GRUActivationType.sigmoid: sigmoid,
        GRUActivationType.tanh: tanh,
        GRUActivationType.relu: relu,
    }

Q
Qiao Longfei 已提交
118
    def set_inputs(self, origin_mode=False):
G
guosheng 已提交
119 120
        batch_size = self.batch_size
        frame_size = self.frame_size
121
        self.op_type = 'gru_unit'
G
guosheng 已提交
122
        self.inputs = {
123 124 125 126 127 128 129 130 131 132
            '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. / math.sqrt(frame_size),
                              1. / math.sqrt(frame_size),
                              (frame_size, frame_size * 3)).astype(self.dtype),
G
guosheng 已提交
133
        }
134 135
        self.attrs = {
            'activation': GRUActivationType.tanh,
Q
Qiao Longfei 已提交
136 137
            'gate_activation': GRUActivationType.sigmoid,
            'origin_mode': origin_mode
138
        }
G
guosheng 已提交
139

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

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

G
guosheng 已提交
179 180 181
    def test_check_output(self):
        self.check_output()

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


Q
Qiao Longfei 已提交
186
class TestGRUUnitOpOriginMode(TestGRUUnitOp):
187

Q
Qiao Longfei 已提交
188
    def setUp(self):
R
ronnywang 已提交
189 190
        self.dtype = 'float32' if fluid.core.is_compiled_with_rocm(
        ) else 'float64'
Q
Qiao Longfei 已提交
191
        self.set_inputs(origin_mode=True)
Q
Qiao Longfei 已提交
192 193 194
        self.set_outputs(origin_mode=True)


G
guosheng 已提交
195
class TestGRUUnitOpWithBias(TestGRUUnitOp):
196

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

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

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


Q
Qiao Longfei 已提交
217
class TestGRUUnitOpWithBiasOriginMode(TestGRUUnitOpWithBias):
218

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


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