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

import paddle
16

F
Feiyu Chan 已提交
17 18 19 20
paddle.framework.set_default_dtype("float64")

import unittest

21
import numpy as np
F
Feiyu Chan 已提交
22
from convert import convert_params_for_cell
23
from rnn_numpy import GRUCell, LSTMCell, SimpleRNNCell
F
Feiyu Chan 已提交
24 25 26 27


class TestSimpleRNNCell(unittest.TestCase):
    def __init__(self, bias=True, place="cpu"):
28
        super().__init__(methodName="runTest")
F
Feiyu Chan 已提交
29
        self.bias = bias
30 31 32
        self.place = (
            paddle.CPUPlace() if place == "cpu" else paddle.CUDAPlace(0)
        )
F
Feiyu Chan 已提交
33 34 35 36

    def setUp(self):
        paddle.disable_static(self.place)
        rnn1 = SimpleRNNCell(16, 32, bias=self.bias)
37 38 39
        rnn2 = paddle.nn.SimpleRNNCell(
            16, 32, bias_ih_attr=self.bias, bias_hh_attr=self.bias
        )
F
Feiyu Chan 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52
        convert_params_for_cell(rnn1, rnn2)

        self.rnn1 = rnn1
        self.rnn2 = rnn2

    def test_with_initial_state(self):
        rnn1 = self.rnn1
        rnn2 = self.rnn2

        x = np.random.randn(4, 16)
        prev_h = np.random.randn(4, 32)

        y1, h1 = rnn1(x, prev_h)
53
        y2, h2 = rnn2(paddle.to_tensor(x), paddle.to_tensor(prev_h))
F
Feiyu Chan 已提交
54 55 56 57 58 59 60 61 62
        np.testing.assert_allclose(h1, h2.numpy(), atol=1e-8, rtol=1e-5)

    def test_with_zero_state(self):
        rnn1 = self.rnn1
        rnn2 = self.rnn2

        x = np.random.randn(4, 16)

        y1, h1 = rnn1(x)
63
        y2, h2 = rnn2(paddle.to_tensor(x))
F
Feiyu Chan 已提交
64 65
        np.testing.assert_allclose(h1, h2.numpy(), atol=1e-8, rtol=1e-5)

66 67 68 69 70 71
    def test_errors(self):
        def test_zero_hidden_size():
            cell = paddle.nn.SimpleRNNCell(-1, 0)

        self.assertRaises(ValueError, test_zero_hidden_size)

F
Feiyu Chan 已提交
72 73 74
    def runTest(self):
        self.test_with_initial_state()
        self.test_with_zero_state()
75
        self.test_errors()
F
Feiyu Chan 已提交
76 77 78 79


class TestGRUCell(unittest.TestCase):
    def __init__(self, bias=True, place="cpu"):
80
        super().__init__(methodName="runTest")
F
Feiyu Chan 已提交
81
        self.bias = bias
82 83 84
        self.place = (
            paddle.CPUPlace() if place == "cpu" else paddle.CUDAPlace(0)
        )
F
Feiyu Chan 已提交
85 86 87 88

    def setUp(self):
        paddle.disable_static(self.place)
        rnn1 = GRUCell(16, 32, bias=self.bias)
89 90 91
        rnn2 = paddle.nn.GRUCell(
            16, 32, bias_ih_attr=self.bias, bias_hh_attr=self.bias
        )
F
Feiyu Chan 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104
        convert_params_for_cell(rnn1, rnn2)

        self.rnn1 = rnn1
        self.rnn2 = rnn2

    def test_with_initial_state(self):
        rnn1 = self.rnn1
        rnn2 = self.rnn2

        x = np.random.randn(4, 16)
        prev_h = np.random.randn(4, 32)

        y1, h1 = rnn1(x, prev_h)
105
        y2, h2 = rnn2(paddle.to_tensor(x), paddle.to_tensor(prev_h))
F
Feiyu Chan 已提交
106 107 108 109 110 111 112 113 114
        np.testing.assert_allclose(h1, h2.numpy(), atol=1e-8, rtol=1e-5)

    def test_with_zero_state(self):
        rnn1 = self.rnn1
        rnn2 = self.rnn2

        x = np.random.randn(4, 16)

        y1, h1 = rnn1(x)
115
        y2, h2 = rnn2(paddle.to_tensor(x))
F
Feiyu Chan 已提交
116 117
        np.testing.assert_allclose(h1, h2.numpy(), atol=1e-8, rtol=1e-5)

118 119 120 121 122 123
    def test_errors(self):
        def test_zero_hidden_size():
            cell = paddle.nn.GRUCell(-1, 0)

        self.assertRaises(ValueError, test_zero_hidden_size)

F
Feiyu Chan 已提交
124 125 126
    def runTest(self):
        self.test_with_initial_state()
        self.test_with_zero_state()
127
        self.test_errors()
F
Feiyu Chan 已提交
128 129 130 131


class TestLSTMCell(unittest.TestCase):
    def __init__(self, bias=True, place="cpu"):
132
        super().__init__(methodName="runTest")
F
Feiyu Chan 已提交
133
        self.bias = bias
134 135 136
        self.place = (
            paddle.CPUPlace() if place == "cpu" else paddle.CUDAPlace(0)
        )
F
Feiyu Chan 已提交
137 138 139

    def setUp(self):
        rnn1 = LSTMCell(16, 32, bias=self.bias)
140 141 142
        rnn2 = paddle.nn.LSTMCell(
            16, 32, bias_ih_attr=self.bias, bias_hh_attr=self.bias
        )
F
Feiyu Chan 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156
        convert_params_for_cell(rnn1, rnn2)

        self.rnn1 = rnn1
        self.rnn2 = rnn2

    def test_with_initial_state(self):
        rnn1 = self.rnn1
        rnn2 = self.rnn2

        x = np.random.randn(4, 16)
        prev_h = np.random.randn(4, 32)
        prev_c = np.random.randn(4, 32)

        y1, (h1, c1) = rnn1(x, (prev_h, prev_c))
157 158 159 160
        y2, (h2, c2) = rnn2(
            paddle.to_tensor(x),
            (paddle.to_tensor(prev_h), paddle.to_tensor(prev_c)),
        )
F
Feiyu Chan 已提交
161 162 163 164 165 166 167 168 169 170
        np.testing.assert_allclose(h1, h2.numpy(), atol=1e-8, rtol=1e-5)
        np.testing.assert_allclose(c1, c2.numpy(), atol=1e-8, rtol=1e-5)

    def test_with_zero_state(self):
        rnn1 = self.rnn1
        rnn2 = self.rnn2

        x = np.random.randn(4, 16)

        y1, (h1, c1) = rnn1(x)
171
        y2, (h2, c2) = rnn2(paddle.to_tensor(x))
F
Feiyu Chan 已提交
172 173 174
        np.testing.assert_allclose(h1, h2.numpy(), atol=1e-8, rtol=1e-5)
        np.testing.assert_allclose(c1, c2.numpy(), atol=1e-8, rtol=1e-5)

175 176 177 178 179 180
    def test_errors(self):
        def test_zero_hidden_size():
            cell = paddle.nn.LSTMCell(-1, 0)

        self.assertRaises(ValueError, test_zero_hidden_size)

F
Feiyu Chan 已提交
181 182 183
    def runTest(self):
        self.test_with_initial_state()
        self.test_with_zero_state()
184
        self.test_errors()
F
Feiyu Chan 已提交
185 186 187 188


def load_tests(loader, tests, pattern):
    suite = unittest.TestSuite()
189 190 191
    devices = (
        ["cpu", "gpu"] if paddle.fluid.is_compiled_with_cuda() else ["cpu"]
    )
F
Feiyu Chan 已提交
192 193 194 195 196
    for bias in [True, False]:
        for device in devices:
            for test_class in [TestSimpleRNNCell, TestGRUCell, TestLSTMCell]:
                suite.addTest(test_class(bias, device))
    return suite