test_renorm_op.py 4.1 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
# 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):
25

S
seemingwang 已提交
26
    def input_data(self):
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 39 40 41 42 43 44 45 46 47 48 49
        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()):
            #x = fluid.layers.data(name = 'x',shape=[-1, 2, 3])
            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())
            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]]])
50
        np.testing.assert_allclose(expected, np.array(res), rtol=1e-05)
S
seemingwang 已提交
51 52 53 54

    def test_dygraph_api(self):
        self.input_data()
        # case axis none
H
hong 已提交
55
        with fluid.dygraph.guard(fluid.CPUPlace()):
S
seemingwang 已提交
56 57 58 59 60 61 62
            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)
            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]]])
63
            np.testing.assert_allclose(expected, np.array(y), rtol=1e-05)
S
seemingwang 已提交
64 65
            z = paddle.mean(y)
            z.backward(retain_graph=True)
66 67 68 69
            expected_grad = np.array([[[0, 0.01394558, 0.02733333],
                                       [0, 0.01394558, 0.00683333]],
                                      [[0, 0.01045918, 0.00683333],
                                       [0, 0.01394558, 0.00683333]]])
70 71 72
            np.testing.assert_allclose(expected_grad,
                                       np.array(x.grad),
                                       rtol=1e-05)
S
seemingwang 已提交
73
        # #test exception:
S
seemingwang 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
        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)
            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]]])
94
            np.testing.assert_allclose(expected, np.array(y), rtol=1e-05)
S
seemingwang 已提交
95 96 97


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