test_gru_unit_op.py 7.4 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.

15 16
from __future__ import print_function

G
guosheng 已提交
17 18 19
import math
import unittest
import numpy as np
20
import paddle.fluid as fluid
21
from op_test import OpTest
X
Xing Wu 已提交
22 23 24
from paddle import fluid
from paddle.fluid.layers import gru_unit
from paddle.fluid.framework import program_guard, Program
G
guosheng 已提交
25 26


27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
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.
            x0 = fluid.create_lod_tensor(
                np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.CPUPlace())
            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)


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


def identity(x):
    return x


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


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


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


X
Xing Wu 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
class TestGRUUnitOpError(unittest.TestCase):
    def test_errors(self):
        with program_guard(Program(), Program()):
            batch_size = 5
            hidden_dim = 40
            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')
            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():
                error_input = fluid.data(
                    name='error_input',
                    shape=[None, hidden_dim * 3],
                    dtype='int32')
                gru_unit(error_input, pre_hidden, hidden_dim * 3)

            self.assertRaises(TypeError, test_input_type)

            def test_pre_hidden_type():
                error_pre_hidden = fluid.data(
                    name='error_pre_hidden',
                    shape=[None, hidden_dim],
                    dtype='int32')
                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
            'Input': np.random.uniform(
R
ronnywang 已提交
124
                -0.1, 0.1, (batch_size, frame_size * 3)).astype(self.dtype),
125
            'HiddenPrev': np.random.uniform(
R
ronnywang 已提交
126
                -0.1, 0.1, (batch_size, frame_size)).astype(self.dtype),
127
            'Weight': np.random.uniform(
G
guosheng 已提交
128
                -1. / math.sqrt(frame_size), 1. / math.sqrt(frame_size),
R
ronnywang 已提交
129
                (frame_size, frame_size * 3)).astype(self.dtype),
G
guosheng 已提交
130
        }
131 132
        self.attrs = {
            'activation': GRUActivationType.tanh,
Q
Qiao Longfei 已提交
133 134
            'gate_activation': GRUActivationType.sigmoid,
            'origin_mode': origin_mode
135
        }
G
guosheng 已提交
136

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

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

G
guosheng 已提交
175 176 177
    def test_check_output(self):
        self.check_output()

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


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


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

G
guosheng 已提交
203
    def test_check_grad(self):
204 205 206
        self.check_grad(['Input', 'HiddenPrev', 'Weight', 'Bias'], ['Hidden'])

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


Q
Qiao Longfei 已提交
212 213
class TestGRUUnitOpWithBiasOriginMode(TestGRUUnitOpWithBias):
    def setUp(self):
R
ronnywang 已提交
214 215
        self.dtype = 'float32' if fluid.core.is_compiled_with_rocm(
        ) else 'float64'
Q
Qiao Longfei 已提交
216
        self.set_inputs(origin_mode=True)
Q
Qiao Longfei 已提交
217 218 219
        self.set_outputs(origin_mode=True)


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