test_renorm_op.py 4.3 KB
Newer Older
S
seemingwang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
# 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 unittest
import paddle
import numpy as np
import paddle.fluid as fluid
from paddle.fluid import Program, program_guard

paddle.set_device('cpu')


class TestRenormAPI(unittest.TestCase):
    def input_data(self):
26 27 28
        self.data_x = np.array(
            [[[2.0, 2, -2], [3, 0.3, 3]], [[2, -8, 2], [3.1, 3.7, 3]]]
        )
S
seemingwang 已提交
29 30 31 32 33 34 35 36 37 38
        self.p = 1.0
        self.dim = 2
        self.max_norm = 2.05

    def test_renorm_api(self):
        paddle.enable_static()
        self.input_data()

        # case 1:
        with program_guard(Program(), Program()):
39
            # x = fluid.layers.data(name = 'x',shape=[-1, 2, 3])
S
seemingwang 已提交
40 41 42
            x = paddle.static.data(name="x", shape=[-1, 2, 3], dtype='float64')
            z = paddle.renorm(x, self.p, self.dim, self.max_norm)
            exe = fluid.Executor(fluid.CPUPlace())
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
            (res,) = exe.run(
                feed={"x": self.data_x}, fetch_list=[z], return_numpy=False
            )
        expected = np.array(
            [
                [
                    [0.40594056, 0.29285714, -0.41000000],
                    [0.60891086, 0.04392857, 0.61500001],
                ],
                [
                    [0.40594056, -1.17142856, 0.41000000],
                    [0.62920785, 0.54178572, 0.61500001],
                ],
            ]
        )
58
        np.testing.assert_allclose(expected, np.array(res), rtol=1e-05)
S
seemingwang 已提交
59 60 61 62

    def test_dygraph_api(self):
        self.input_data()
        # case axis none
H
hong 已提交
63
        with fluid.dygraph.guard(fluid.CPUPlace()):
S
seemingwang 已提交
64 65 66
            input = [[[2.0, 2, -2], [3, 0.3, 3]], [[2, -8, 2], [3.1, 3.7, 3]]]
            x = paddle.to_tensor(input, stop_gradient=False)
            y = paddle.renorm(x, 1.0, 2, 2.05)
67 68 69 70 71 72 73 74 75 76 77 78
            expected = np.array(
                [
                    [
                        [0.40594056, 0.29285714, -0.41000000],
                        [0.60891086, 0.04392857, 0.61500001],
                    ],
                    [
                        [0.40594056, -1.17142856, 0.41000000],
                        [0.62920785, 0.54178572, 0.61500001],
                    ],
                ]
            )
79
            np.testing.assert_allclose(expected, np.array(y), rtol=1e-05)
S
seemingwang 已提交
80 81
            z = paddle.mean(y)
            z.backward(retain_graph=True)
82 83 84 85 86 87 88 89 90
            expected_grad = np.array(
                [
                    [[0, 0.01394558, 0.02733333], [0, 0.01394558, 0.00683333]],
                    [[0, 0.01045918, 0.00683333], [0, 0.01394558, 0.00683333]],
                ]
            )
            np.testing.assert_allclose(
                expected_grad, np.array(x.grad), rtol=1e-05
            )
S
seemingwang 已提交
91
        # #test exception:
S
seemingwang 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
        with fluid.dygraph.guard():
            input = [[[2.0, 2, -2], [3, 0.3, 3]], [[2, -8, 2], [3.1, 3.7, 3]]]
            x = paddle.to_tensor(input, stop_gradient=False)
            exp = False
            try:
                paddle.renorm(x, 1.0, 8, 2.05)
            except:
                exp = True
            self.assertTrue(exp)
            exp = False
            try:
                paddle.renorm(x, 1.0, -4, 2.05)
            except:
                exp = True
            self.assertTrue(exp)
            y = paddle.renorm(x, 1.0, -1, 2.05)
108 109 110 111 112 113 114 115 116 117 118 119
            expected = np.array(
                [
                    [
                        [0.40594056, 0.29285714, -0.41000000],
                        [0.60891086, 0.04392857, 0.61500001],
                    ],
                    [
                        [0.40594056, -1.17142856, 0.41000000],
                        [0.62920785, 0.54178572, 0.61500001],
                    ],
                ]
            )
120
            np.testing.assert_allclose(expected, np.array(y), rtol=1e-05)
S
seemingwang 已提交
121 122 123


if __name__ == '__main__':
H
hong 已提交
124
    paddle.enable_static()
S
seemingwang 已提交
125
    unittest.main()