test_gru_unit_op.py 3.4 KB
Newer Older
G
guosheng 已提交
1 2 3 4 5 6
import math
import unittest
import numpy as np
from op_test import OpTest


7 8 9 10 11 12 13 14 15 16 17 18
class GRUActivationType(OpTest):
    identity = 0
    sigmoid = 1
    tanh = 2
    relu = 3


def identity(x):
    return x


def sigmoid(x):
G
guosheng 已提交
19 20 21
    return 1. / (1. + np.exp(-x))


22 23 24 25 26 27
def tanh(x):
    return 2. * sigmoid(2. * x) - 1.


def relu(x):
    return np.maximum(x, 0)
G
guosheng 已提交
28 29 30


class TestGRUUnitOp(OpTest):
G
guosheng 已提交
31 32
    batch_size = 3
    frame_size = 5
33 34 35 36 37 38 39
    activate = {
        GRUActivationType.identity: identity,
        GRUActivationType.sigmoid: sigmoid,
        GRUActivationType.tanh: tanh,
        GRUActivationType.relu: relu,
    }

G
guosheng 已提交
40 41 42
    def set_inputs(self):
        batch_size = self.batch_size
        frame_size = self.frame_size
43
        self.op_type = 'gru_unit'
G
guosheng 已提交
44
        self.inputs = {
45
            'Input': np.random.uniform(
Y
Yu Yang 已提交
46
                -0.1, 0.1, (batch_size, frame_size * 3)).astype('float64'),
47
            'HiddenPrev': np.random.uniform(
Y
Yu Yang 已提交
48
                -0.1, 0.1, (batch_size, frame_size)).astype('float64'),
49
            'Weight': np.random.uniform(
G
guosheng 已提交
50
                -1. / math.sqrt(frame_size), 1. / math.sqrt(frame_size),
Y
Yu Yang 已提交
51
                (frame_size, frame_size * 3)).astype('float64'),
G
guosheng 已提交
52
        }
53 54 55 56
        self.attrs = {
            'activation': GRUActivationType.tanh,
            'gate_activation': GRUActivationType.sigmoid
        }
G
guosheng 已提交
57 58

    def set_outputs(self):
59
        # GRU calculations
G
guosheng 已提交
60 61
        batch_size = self.batch_size
        frame_size = self.frame_size
62 63 64
        x = self.inputs['Input']
        h_p = self.inputs['HiddenPrev']
        w = self.inputs['Weight']
G
guosheng 已提交
65 66
        b = self.inputs['Bias'] if self.inputs.has_key('Bias') else np.zeros(
            (1, frame_size * 3))
G
guosheng 已提交
67 68 69
        g = x + np.tile(b, (batch_size, 1))
        w_u_r = w.flatten()[:frame_size * frame_size * 2].reshape(
            (frame_size, frame_size * 2))
70 71
        u_r = self.activate[self.attrs['gate_activation']](np.dot(
            h_p, w_u_r) + g[:, :frame_size * 2])
G
guosheng 已提交
72 73 74 75 76
        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))
77 78
        c = self.activate[self.attrs['activation']](np.dot(r_h_p, w_c) +
                                                    g[:, frame_size * 2:])
G
guosheng 已提交
79
        g = np.hstack((u_r, c))
G
guosheng 已提交
80
        h = u * c + (1 - u) * h_p
Y
Yu Yang 已提交
81 82 83 84 85
        self.outputs = {
            'Gate': g.astype('float64'),
            'ResetHiddenPrev': r_h_p.astype('float64'),
            'Hidden': h.astype('float64')
        }
G
guosheng 已提交
86

G
guosheng 已提交
87 88 89 90
    def setUp(self):
        self.set_inputs()
        self.set_outputs()

G
guosheng 已提交
91 92 93
    def test_check_output(self):
        self.check_output()

G
guosheng 已提交
94 95
    def test_check_grad(self):
        self.check_grad(
G
guosheng 已提交
96
            ['Input', 'HiddenPrev', 'Weight'], ['Hidden'],
G
guosheng 已提交
97 98 99 100 101 102 103 104 105
            max_relative_error=0.007)


class TestGRUUnitOpWithBias(TestGRUUnitOp):
    def set_inputs(self):
        batch_size = self.batch_size
        frame_size = self.frame_size
        super(TestGRUUnitOpWithBias, self).set_inputs()
        self.inputs['Bias'] = np.random.uniform(
G
guosheng 已提交
106
            -0.1, 0.1, (1, frame_size * 3)).astype('float64')
G
guosheng 已提交
107 108 109 110 111
        self.attrs = {
            'activation': GRUActivationType.identity,
            'gate_activation': GRUActivationType.sigmoid
        }

G
guosheng 已提交
112 113
    def test_check_grad(self):
        self.check_grad(
114
            ['Input', 'HiddenPrev', 'Weight', 'Bias'], ['Hidden'],
G
guosheng 已提交
115 116 117 118 119
            max_relative_error=0.007)


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