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


class TestRandnOp(unittest.TestCase):
    def test_api(self):
26 27 28 29 30 31 32 33 34 35 36
        shape = [1000, 784]
        train_program = Program()
        startup_program = Program()
        with program_guard(train_program, startup_program):
            x1 = paddle.randn(shape, 'float32')
            x2 = paddle.randn(shape, 'float64')

            dim_1 = paddle.fill_constant([1], "int64", 20)
            dim_2 = paddle.fill_constant([1], "int32", 50)
            x3 = paddle.randn([dim_1, dim_2, 784])

37
            var_shape = paddle.static.data('X', [2], 'int32')
38 39 40 41
            x4 = paddle.randn(var_shape)

        place = paddle.CUDAPlace(0) if core.is_compiled_with_cuda(
        ) else paddle.CPUPlace()
42
        exe = paddle.static.Executor(place)
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
        res = exe.run(train_program,
                      feed={'X': np.array(
                          shape, dtype='int32')},
                      fetch_list=[x1, x2, x3, x4])

        for out in res:
            self.assertAlmostEqual(np.mean(out), .0, delta=0.1)
            self.assertAlmostEqual(np.std(out), 1., delta=0.1)


class TestRandnOpForDygraph(unittest.TestCase):
    def test_api(self):
        shape = [1000, 784]
        place = paddle.CUDAPlace(0) if core.is_compiled_with_cuda(
        ) else paddle.CPUPlace()
58 59 60
        paddle.disable_static(place)
        x1 = paddle.randn(shape, 'float32')
        x2 = paddle.randn(shape, 'float64')
61

62 63 64
        dim_1 = paddle.fill_constant([1], "int64", 20)
        dim_2 = paddle.fill_constant([1], "int32", 50)
        x3 = paddle.randn(shape=[dim_1, dim_2, 784])
65

Z
Zhou Wei 已提交
66
        var_shape = paddle.to_tensor(np.array(shape))
67
        x4 = paddle.randn(var_shape)
68

69 70 71 72
        for out in [x1, x2, x3, x4]:
            self.assertAlmostEqual(np.mean(out.numpy()), .0, delta=0.1)
            self.assertAlmostEqual(np.std(out.numpy()), 1., delta=0.1)
        paddle.enable_static()
G
guofei 已提交
73 74 75 76 77 78


class TestRandnOpError(unittest.TestCase):
    def test_error(self):
        with program_guard(Program(), Program()):
            # The argument shape's size of randn_op should not be 0.
79
            self.assertRaises(AssertionError, paddle.randn, [])
G
guofei 已提交
80 81

            # The argument shape's type of randn_op should be list or tuple.
82
            self.assertRaises(TypeError, paddle.randn, 1)
G
guofei 已提交
83 84

            # The argument dtype of randn_op should be float32 or float64.
85
            self.assertRaises(TypeError, paddle.randn, [1, 2], 'int32')
G
guofei 已提交
86 87 88 89


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