test_ones_op.py 3.2 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 23
#   Copyright (c) 2018 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 numpy as np

import paddle
import paddle.fluid as fluid
import numpy as np


class ApiOnesTest(unittest.TestCase):
24

25
    def test_paddle_ones(self):
26
        with paddle.static.program_guard(paddle.static.Program()):
27
            ones = paddle.ones(shape=[10])
28
            place = paddle.CPUPlace()
29
            exe = paddle.static.Executor(place)
30
            result, = exe.run(fetch_list=[ones])
31
            expected_result = np.ones(10, dtype="float32")
32 33
        self.assertEqual((result == expected_result).all(), True)

34
        with paddle.static.program_guard(paddle.static.Program()):
35 36
            ones = paddle.ones(shape=[10], dtype="float64")
            place = paddle.CPUPlace()
37
            exe = paddle.static.Executor(place)
38 39 40 41
            result, = exe.run(fetch_list=[ones])
            expected_result = np.ones(10, dtype="float64")
        self.assertEqual((result == expected_result).all(), True)

42
        with paddle.static.program_guard(paddle.static.Program()):
43
            ones = paddle.ones(shape=[10], dtype="int64")
44
            place = paddle.CPUPlace()
45
            exe = paddle.static.Executor(place)
46 47 48 49
            result, = exe.run(fetch_list=[ones])
            expected_result = np.ones(10, dtype="int64")
        self.assertEqual((result == expected_result).all(), True)

50
    def test_fluid_ones(self):
51
        with paddle.static.program_guard(paddle.static.Program()):
52 53
            ones = fluid.layers.ones(shape=[10], dtype="int64")
            place = paddle.CPUPlace()
54
            exe = paddle.static.Executor(place)
55 56 57 58 59 60
            result, = exe.run(fetch_list=[ones])
            expected_result = np.ones(10, dtype="int64")
        self.assertEqual((result == expected_result).all(), True)


class ApiOnesZerosError(unittest.TestCase):
61

62
    def test_errors(self):
63

64
        def test_error1():
65
            with paddle.static.program_guard(paddle.static.Program()):
66
                ones = paddle.ones(shape=10, dtype="int64")
67

68
        self.assertRaises(TypeError, test_error1)
69 70

        def test_error2():
71
            with paddle.static.program_guard(paddle.static.Program()):
72
                ones = paddle.ones(shape=10)
73

74
        self.assertRaises(TypeError, test_error2)
75 76

        def test_error3():
77
            with paddle.static.program_guard(paddle.static.Program()):
78 79 80 81 82
                ones = fluid.layers.ones(shape=10, dtype="int64")

        self.assertRaises(TypeError, test_error3)

        def test_error4():
83
            with paddle.static.program_guard(paddle.static.Program()):
84 85 86 87 88 89 90
                ones = fluid.layers.ones(shape=[10], dtype="int8")

        self.assertRaises(TypeError, test_error4)


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