test_rnn_memory_helper_op.py 4.5 KB
Newer Older
D
dzhwinter 已提交
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
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.

Y
Yang Yang(Tony) 已提交
15 16
import unittest

Q
Qiao Longfei 已提交
17 18
from paddle.v2.fluid.framework import Program
from paddle.v2.fluid.executor import Executor
F
fengjiayi 已提交
19
from paddle.v2.fluid.backward import append_backward
Y
Yang Yang(Tony) 已提交
20
import numpy as np
Q
Qiao Longfei 已提交
21
import paddle.v2.fluid.core as core
Y
Yang Yang(Tony) 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40


class RNNMemoryHelperOpTest(unittest.TestCase):
    def setUp(self):
        self.program = Program()
        self.place = core.CPUPlace()

        self.X = self.program.global_block().create_var(
            name='X', shape=[2, 3], dtype='float32')
        self.Out = self.program.global_block().create_var(
            name='Out', shape=[2, 3], dtype='float32')
        self.program.global_block().append_op(
            type='rnn_memory_helper',
            inputs={"X": self.X},
            outputs={"Out": self.Out},
            attrs={})

    def test_forward(self):
        x_np = np.random.normal(size=(2, 3)).astype("float32")
D
dzhwinter 已提交
41
        self.feed_map = {'X': x_np}
Y
Yang Yang(Tony) 已提交
42 43 44 45 46
        self.fetch_list = [self.Out]
        exe = Executor(self.place)
        out = exe.run(self.program,
                      feed=self.feed_map,
                      fetch_list=self.fetch_list)
D
dzhwinter 已提交
47
        self.assertTrue(np.allclose(out[0], x_np, rtol=1e-5))
Y
Yang Yang(Tony) 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76


class RNNMemoryHelperGradOpTest(unittest.TestCase):
    def setUp(self):
        self.program = Program()
        self.place = core.CPUPlace()

        self.input_names = ['X', 'Out', 'Out@GRAD']
        self.input_vars = {
            name: self.program.global_block().create_var(
                name=name, shape=[2, 3], dtype='float32')
            for name in self.input_names
        }

        self.output_names = ['X@GRAD']
        self.output_vars = {
            name: self.program.global_block().create_var(
                name=name, shape=[2, 3], dtype='float32')
            for name in self.output_names
        }

        self.program.global_block().append_op(
            type='rnn_memory_helper_grad',
            inputs=self.input_vars,
            outputs=self.output_vars,
            attrs={})

    def test_backward(self):
        self.feed_map = {
D
dzhwinter 已提交
77
            name: np.random.normal(size=(2, 3)).astype("float32")
Y
Yang Yang(Tony) 已提交
78 79 80 81 82 83 84 85
            for name in self.input_names
        }
        self.fetch_list = [self.output_vars['X@GRAD']]

        exe = Executor(self.place)
        out = exe.run(self.program,
                      feed=self.feed_map,
                      fetch_list=self.fetch_list)
D
dzhwinter 已提交
86
        np.isclose(out[0], self.feed_map['Out@GRAD'], rtol=1e-5)
Y
Yang Yang(Tony) 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119


class RNNMemoryHelperGradOpWithoutInputTest(unittest.TestCase):
    def setUp(self):
        self.program = Program()
        self.fake_program = Program()
        self.place = core.CPUPlace()

        self.input_names = ['X', 'Out']
        self.input_vars = {
            name: self.program.global_block().create_var(
                name=name, shape=[2, 3], dtype='float32')
            for name in self.input_names
        }
        self.input_vars["Out@GRAD"] = \
            self.fake_program.global_block().create_var(
                name="Out@GRAD", shape=[2, 3], dtype='float32')

        self.output_names = ['X@GRAD']
        self.output_vars = {
            name: self.program.global_block().create_var(
                name=name, shape=[2, 3], dtype='float32')
            for name in self.output_names
        }

        self.program.global_block().append_op(
            type='rnn_memory_helper_grad',
            inputs=self.input_vars,
            outputs=self.output_vars,
            attrs={})

    def test_backward(self):
        self.feed_map = {
D
dzhwinter 已提交
120
            name: np.random.normal(size=(2, 3)).astype("float32")
Y
Yang Yang(Tony) 已提交
121 122 123 124 125 126 127 128
            for name in ['X', 'Out']
        }
        self.fetch_list = [self.output_vars['X@GRAD']]

        exe = Executor(self.place)
        out = exe.run(self.program,
                      feed=self.feed_map,
                      fetch_list=self.fetch_list)
D
dzhwinter 已提交
129 130 131
        self.assertTrue(
            np.allclose(
                out[0], np.zeros(shape=(2, 3)).astype("float32"), rtol=1e-5))
Y
Yang Yang(Tony) 已提交
132 133 134 135


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