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

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

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

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


class ApiOnesZerosError(unittest.TestCase):
    def test_errors(self):
        def test_error1():
62
            with paddle.static.program_guard(paddle.static.Program()):
63
                ones = paddle.ones(shape=10, dtype="int64")
64

65
        self.assertRaises(TypeError, test_error1)
66 67

        def test_error2():
68
            with paddle.static.program_guard(paddle.static.Program()):
69
                ones = paddle.ones(shape=10)
70

71
        self.assertRaises(TypeError, test_error2)
72 73

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

        self.assertRaises(TypeError, test_error3)

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

        self.assertRaises(TypeError, test_error4)


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