test_manual_seed.py 1.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#   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

17 18
import numpy as np

L
Leo Chen 已提交
19
import paddle
20
import paddle.fluid as fluid
21
from paddle.tensor import random
22 23 24


class TestManualSeed(unittest.TestCase):
C
cnn 已提交
25
    def test_seed(self):
L
Leo Chen 已提交
26 27
        fluid.enable_dygraph()

C
cnn 已提交
28
        gen = paddle.seed(12312321111)
29
        x = random.gaussian([10], dtype="float32")
L
Leo Chen 已提交
30
        st1 = gen.get_state()
31
        x1 = random.gaussian([10], dtype="float32")
L
Leo Chen 已提交
32
        gen.set_state(st1)
33
        x2 = random.gaussian([10], dtype="float32")
L
Leo Chen 已提交
34
        gen.manual_seed(12312321111)
35
        x3 = random.gaussian([10], dtype="float32")
L
Leo Chen 已提交
36 37 38 39 40 41
        x_np = x.numpy()
        x1_np = x1.numpy()
        x2_np = x2.numpy()
        x3_np = x3.numpy()

        if not fluid.core.is_compiled_with_cuda():
42 43
            np.testing.assert_allclose(x1_np, x2_np, rtol=1e-05)
            np.testing.assert_allclose(x_np, x3_np, rtol=1e-05)
44 45 46 47


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