test_normalize.py 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#   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.

from __future__ import print_function

import unittest
import paddle
import paddle.nn.functional as F
import paddle.fluid as fluid
import paddle.fluid.core as core
import numpy as np
23
from paddle.fluid.framework import _test_eager_guard
24 25 26 27 28 29 30 31 32 33


def p_normalize(x, axis=1, p=2, epsilon=1e-12, keepdims=True):
    xp = np.power(np.abs(x), p)
    s = np.sum(xp, axis=axis, keepdims=keepdims)
    r = np.maximum(np.power(s, 1.0 / p), epsilon)
    return x / r


class TestNNFunctionalNormalize(unittest.TestCase):
34

35 36 37 38 39 40
    def setUp(self):
        self.input_np = np.random.random(size=(10, 10)).astype(np.float32)
        self.input_np2 = np.array([0.0, 0.0]).astype(np.float32)
        self.expected0 = p_normalize(self.input_np)
        self.expected1 = p_normalize(self.input_np, p=1.5)
        self.expected2 = p_normalize(self.input_np, axis=0)
41
        self.expected3 = p_normalize(self.input_np2, axis=0)
42 43

    def run_imperative(self):
44
        x = paddle.to_tensor(self.input_np)
45
        y = F.normalize(x)
46
        np.testing.assert_allclose(y.numpy(), self.expected0, rtol=1e-05)
47 48

        y = F.normalize(x, p=1.5)
49
        np.testing.assert_allclose(y.numpy(), self.expected1, rtol=1e-05)
50 51

        y = F.normalize(x, axis=0)
52
        np.testing.assert_allclose(y.numpy(), self.expected2, rtol=1e-05)
53

54 55
        x = paddle.to_tensor(self.input_np2)
        y = F.normalize(x, axis=0)
56
        np.testing.assert_allclose(y.numpy(), self.expected3, rtol=1e-05)
57

58 59
        self.assertRaises(BaseException, F.normalize, x)

60
    def run_static(self, use_gpu=False):
61 62
        x = paddle.fluid.data(name='input', shape=[10, 10], dtype='float32')
        x2 = paddle.fluid.data(name='input2', shape=[2], dtype='float32')
63 64 65 66
        result0 = F.normalize(x)
        result1 = F.normalize(x, p=1.5)
        result2 = F.normalize(x, axis=0)
        result3 = F.normalize(x, name='aaa')
67
        result4 = F.normalize(x2, axis=0)
68 69 70 71

        place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace()
        exe = fluid.Executor(place)
        exe.run(fluid.default_startup_program())
72 73 74 75 76
        static_result = exe.run(feed={
            "input": self.input_np,
            "input2": self.input_np2
        },
                                fetch_list=[result0, result1, result2, result4])
77

78 79 80
        np.testing.assert_allclose(static_result[0], self.expected0, rtol=1e-05)
        np.testing.assert_allclose(static_result[1], self.expected1, rtol=1e-05)
        np.testing.assert_allclose(static_result[2], self.expected2, rtol=1e-05)
81
        self.assertTrue('aaa' in result3.name)
82
        np.testing.assert_allclose(static_result[3], self.expected3, rtol=1e-05)
83
        self.assertRaises(ValueError, F.normalize, x2)
84 85 86 87 88 89 90 91 92

    def test_cpu(self):
        paddle.disable_static(place=paddle.fluid.CPUPlace())
        self.run_imperative()
        paddle.enable_static()

        with fluid.program_guard(fluid.Program()):
            self.run_static()

93 94 95 96 97 98
    def test_cpu_eager(self):
        with _test_eager_guard():
            paddle.disable_static(place=paddle.fluid.CPUPlace())
            self.run_imperative()
            paddle.enable_static()

99 100 101 102 103 104 105 106 107 108 109
    def test_gpu(self):
        if not fluid.core.is_compiled_with_cuda():
            return

        paddle.disable_static(place=paddle.fluid.CUDAPlace(0))
        self.run_imperative()
        paddle.enable_static()

        with fluid.program_guard(fluid.Program()):
            self.run_static(use_gpu=True)

110 111 112 113 114 115 116 117 118
    def test_gpu_eager(self):
        with _test_eager_guard():
            if not fluid.core.is_compiled_with_cuda():
                return

            paddle.disable_static(place=paddle.fluid.CUDAPlace(0))
            self.run_imperative()
            paddle.enable_static()

119 120 121

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