test_random_seed.py 15.1 KB
Newer Older
Y
yaoxuefeng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#   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.
"""Test cloud role maker."""

import unittest

import numpy as np
19

Y
yaoxuefeng 已提交
20
import paddle
21
import paddle.fluid as fluid
Y
yaoxuefeng 已提交
22
import paddle.fluid.core as core
23
import paddle.fluid.generator as generator
Y
yaoxuefeng 已提交
24 25 26


class TestGeneratorSeed(unittest.TestCase):
L
Leo Chen 已提交
27 28 29
    #     """
    #     Test cases for cpu generator seed.
    #     """
Y
yaoxuefeng 已提交
30 31 32 33 34 35

    def test_generator_uniform_random_dygraph(self):
        """Test Generator seed."""

        fluid.enable_dygraph()

C
cnn 已提交
36
        gen = paddle.seed(12312321111)
Y
yaoxuefeng 已提交
37
        x = fluid.layers.uniform_random([10], dtype="float32", min=0.0, max=1.0)
L
Leo Chen 已提交
38

Y
yaoxuefeng 已提交
39
        st1 = gen.get_state()
40 41 42
        x1 = fluid.layers.uniform_random(
            [10], dtype="float32", min=0.0, max=1.0
        )
L
Leo Chen 已提交
43

Y
yaoxuefeng 已提交
44
        gen.set_state(st1)
L
Leo Chen 已提交
45
        print(gen.get_state())
46 47 48
        x2 = fluid.layers.uniform_random(
            [10], dtype="float32", min=0.0, max=1.0
        )
L
Leo Chen 已提交
49

C
cnn 已提交
50
        paddle.seed(12312321111)
51 52 53
        x3 = fluid.layers.uniform_random(
            [10], dtype="float32", min=0.0, max=1.0
        )
L
Leo Chen 已提交
54

Y
yaoxuefeng 已提交
55 56 57 58 59 60
        x_np = x.numpy()
        x1_np = x1.numpy()
        x2_np = x2.numpy()
        x3_np = x3.numpy()

        if not core.is_compiled_with_cuda():
61 62
            np.testing.assert_allclose(x1_np, x2_np, rtol=1e-05)
            np.testing.assert_allclose(x_np, x3_np, rtol=1e-05)
Y
yaoxuefeng 已提交
63 64 65 66

    def test_generator_uniform_random_static(self):
        fluid.disable_dygraph()

C
cnn 已提交
67
        gen = paddle.seed(123123143)
Y
yaoxuefeng 已提交
68 69 70 71 72 73 74 75 76 77 78

        startup_program = fluid.Program()
        train_program = fluid.Program()
        with fluid.program_guard(train_program, startup_program):
            # example 1:
            # attr shape is a list which doesn't contain tensor Variable.
            result_1 = fluid.layers.uniform_random(shape=[3, 4])
            result_2 = fluid.layers.uniform_random(shape=[3, 4])

            exe = fluid.Executor(fluid.CPUPlace())
            exe.run(startup_program)
79 80 81 82
            out1 = exe.run(
                train_program, feed={}, fetch_list=[result_1, result_2]
            )
            # gen.set_state(cur_state)
Y
yaoxuefeng 已提交
83
            gen.manual_seed(123123143)
84 85 86
            out2 = exe.run(
                train_program, feed={}, fetch_list=[result_1, result_2]
            )
Y
yaoxuefeng 已提交
87 88 89 90 91 92 93

            out1_res1 = np.array(out1[0])
            out1_res2 = np.array(out1[1])
            out2_res1 = np.array(out2[0])
            out2_res2 = np.array(out2[1])

            if not core.is_compiled_with_cuda():
94 95
                np.testing.assert_allclose(out1_res1, out2_res1, rtol=1e-05)
                np.testing.assert_allclose(out1_res2, out2_res2, rtol=1e-05)
Y
yaoxuefeng 已提交
96 97
                self.assertTrue(not np.allclose(out1_res2, out1_res1))

98 99 100
    def test_gen_dropout_dygraph(self):
        fluid.enable_dygraph()

C
cnn 已提交
101
        gen = paddle.seed(111111111)
102 103
        st = gen.get_state()
        # x = np.arange(1,101).reshape(2,50).astype("float32")
104 105 106
        x = fluid.layers.uniform_random(
            [2, 10], dtype="float32", min=0.0, max=1.0
        )
107 108
        y = fluid.layers.dropout(x, 0.5)
        gen.manual_seed(111111111)
109 110 111 112
        # gen.set_state(st)
        x1 = fluid.layers.uniform_random(
            [2, 10], dtype="float32", min=0.0, max=1.0
        )
113 114 115
        y1 = fluid.layers.dropout(x1, 0.5)
        y_np = y.numpy()
        y1_np = y1.numpy()
L
Leo Chen 已提交
116

117 118
        if not core.is_compiled_with_cuda():
            print(">>>>>>> dropout dygraph >>>>>>>")
119
            np.testing.assert_allclose(y_np, y1_np, rtol=1e-05)
120 121 122 123

    def test_gen_dropout_static(self):
        fluid.disable_dygraph()

C
cnn 已提交
124
        gen = paddle.seed(123123143)
125 126 127 128 129 130 131 132 133 134 135

        startup_program = fluid.Program()
        train_program = fluid.Program()
        with fluid.program_guard(train_program, startup_program):
            # example 1:
            # attr shape is a list which doesn't contain tensor Variable.
            x_1 = fluid.layers.uniform_random(shape=[2, 10])
            y_1 = fluid.layers.dropout(x_1, 0.5)
            exe = fluid.Executor(fluid.CPUPlace())
            exe.run(startup_program)
            out1 = exe.run(train_program, feed={}, fetch_list=[y_1])
136
            # gen.set_state(cur_state)
137 138 139 140
            gen.manual_seed(123123143)
            out2 = exe.run(train_program, feed={}, fetch_list=[y_1])
        out1_np = np.array(out1[0])
        out2_np = np.array(out2[0])
L
Leo Chen 已提交
141

142 143
        if not core.is_compiled_with_cuda():
            print(">>>>>>> dropout static >>>>>>>")
144
            np.testing.assert_allclose(out1_np, out2_np, rtol=1e-05)
145 146 147 148 149

    def test_generator_gaussian_random_dygraph(self):
        """Test Generator seed."""
        fluid.enable_dygraph()

C
cnn 已提交
150
        gen = paddle.seed(12312321111)
151 152 153 154 155 156 157 158 159 160 161 162 163 164
        x = fluid.layers.gaussian_random([10], dtype="float32")
        st1 = gen.get_state()
        x1 = fluid.layers.gaussian_random([10], dtype="float32")
        gen.set_state(st1)
        x2 = fluid.layers.gaussian_random([10], dtype="float32")
        gen.manual_seed(12312321111)
        x3 = fluid.layers.gaussian_random([10], dtype="float32")
        x_np = x.numpy()
        x1_np = x1.numpy()
        x2_np = x2.numpy()
        x3_np = x3.numpy()

        if not core.is_compiled_with_cuda():
            print(">>>>>>> gaussian random dygraph >>>>>>>")
165 166
            np.testing.assert_allclose(x1_np, x2_np, rtol=1e-05)
            np.testing.assert_allclose(x_np, x3_np, rtol=1e-05)
167 168 169 170

    def test_generator_gaussian_random_static(self):
        fluid.disable_dygraph()

C
cnn 已提交
171
        gen = paddle.seed(123123143)
172 173 174 175 176 177 178 179 180 181 182

        startup_program = fluid.Program()
        train_program = fluid.Program()
        with fluid.program_guard(train_program, startup_program):
            # example 1:
            # attr shape is a list which doesn't contain tensor Variable.
            result_1 = fluid.layers.gaussian_random(shape=[3, 4])
            result_2 = fluid.layers.gaussian_random(shape=[3, 4])

            exe = fluid.Executor(fluid.CPUPlace())
            exe.run(startup_program)
183 184 185 186
            out1 = exe.run(
                train_program, feed={}, fetch_list=[result_1, result_2]
            )
            # gen.set_state(cur_state)
187
            gen.manual_seed(123123143)
188 189 190
            out2 = exe.run(
                train_program, feed={}, fetch_list=[result_1, result_2]
            )
191 192 193 194 195 196 197 198

            out1_res1 = np.array(out1[0])
            out1_res2 = np.array(out1[1])
            out2_res1 = np.array(out2[0])
            out2_res2 = np.array(out2[1])

            if not core.is_compiled_with_cuda():
                print(">>>>>>> gaussian random static >>>>>>>")
199 200
                np.testing.assert_allclose(out1_res1, out2_res1, rtol=1e-05)
                np.testing.assert_allclose(out1_res2, out2_res2, rtol=1e-05)
201 202
                self.assertTrue(not np.allclose(out1_res2, out1_res1))

Y
yaoxuefeng 已提交
203 204 205 206 207 208
    def test_generator_randint_dygraph(self):
        """Test Generator seed."""
        gen = generator.Generator()

        fluid.enable_dygraph()

C
cnn 已提交
209
        gen = paddle.seed(12312321111)
210
        x = paddle.randint(low=10, shape=[10], dtype="int32")
Y
yaoxuefeng 已提交
211
        st1 = gen.get_state()
212
        x1 = paddle.randint(low=10, shape=[10], dtype="int32")
Y
yaoxuefeng 已提交
213
        gen.set_state(st1)
214
        x2 = paddle.randint(low=10, shape=[10], dtype="int32")
Y
yaoxuefeng 已提交
215
        gen.manual_seed(12312321111)
216
        x3 = paddle.randint(low=10, shape=[10], dtype="int32")
Y
yaoxuefeng 已提交
217 218 219 220
        x_np = x.numpy()
        x1_np = x1.numpy()
        x2_np = x2.numpy()
        x3_np = x3.numpy()
221

Y
yaoxuefeng 已提交
222
        if not core.is_compiled_with_cuda():
223
            print(">>>>>>> randint dygraph >>>>>>>")
224 225
            np.testing.assert_allclose(x1_np, x2_np, rtol=1e-05)
            np.testing.assert_allclose(x_np, x3_np, rtol=1e-05)
Y
yaoxuefeng 已提交
226

Z
zhangchunle 已提交
227
    def test_generator_uniform_random_static_1(self):
L
Leo Chen 已提交
228 229
        fluid.disable_dygraph()

C
cnn 已提交
230
        gen = paddle.seed(123123143)
L
Leo Chen 已提交
231 232 233 234 235 236 237 238 239 240 241

        startup_program = fluid.Program()
        train_program = fluid.Program()
        with fluid.program_guard(train_program, startup_program):
            # example 1:
            # attr shape is a list which doesn't contain tensor Variable.
            result_1 = fluid.layers.uniform_random(shape=[3, 4])
            result_2 = fluid.layers.uniform_random(shape=[3, 4])

            exe = fluid.Executor(fluid.CPUPlace())
            exe.run(startup_program)
242 243 244 245
            out1 = exe.run(
                train_program, feed={}, fetch_list=[result_1, result_2]
            )
            # gen.set_state(cur_state)
L
Leo Chen 已提交
246
            gen.manual_seed(123123143)
247 248 249
            out2 = exe.run(
                train_program, feed={}, fetch_list=[result_1, result_2]
            )
250

L
Leo Chen 已提交
251 252 253 254 255 256
            out1_res1 = np.array(out1[0])
            out1_res2 = np.array(out1[1])
            out2_res1 = np.array(out2[0])
            out2_res2 = np.array(out2[1])

            if not core.is_compiled_with_cuda():
257 258
                np.testing.assert_allclose(out1_res1, out2_res1, rtol=1e-05)
                np.testing.assert_allclose(out1_res2, out2_res2, rtol=1e-05)
L
Leo Chen 已提交
259 260
                self.assertTrue(not np.allclose(out1_res2, out1_res1))

Z
zhangchunle 已提交
261
    def test_generator_randint_dygraph_1(self):
L
Leo Chen 已提交
262 263 264
        """Test Generator seed."""
        fluid.enable_dygraph()

C
cnn 已提交
265
        gen = paddle.seed(12312321111)
L
Leo Chen 已提交
266 267 268 269 270 271 272 273 274 275 276 277
        x = paddle.randint(low=1)
        st1 = gen.get_state()
        x1 = paddle.randint(low=1)
        gen.set_state(st1)
        x2 = paddle.randint(low=1)
        gen.manual_seed(12312321111)
        x3 = paddle.randint(low=1)
        x_np = x.numpy()
        x1_np = x1.numpy()
        x2_np = x2.numpy()
        x3_np = x3.numpy()
        if not core.is_compiled_with_cuda():
278 279
            np.testing.assert_allclose(x1_np, x2_np, rtol=1e-05)
            np.testing.assert_allclose(x_np, x3_np, rtol=1e-05)
L
Leo Chen 已提交
280 281

    def test_generator_ranint_static(self):
282 283
        fluid.disable_dygraph()

C
cnn 已提交
284
        gen = paddle.seed(123123143)
285 286 287 288 289 290 291 292 293 294 295

        startup_program = fluid.Program()
        train_program = fluid.Program()
        with fluid.program_guard(train_program, startup_program):
            # example 1:
            # attr shape is a list which doesn't contain tensor Variable.
            result_1 = paddle.randint(low=10, shape=[3, 4])
            result_2 = paddle.randint(low=10, shape=[3, 4])

            exe = fluid.Executor(fluid.CPUPlace())
            exe.run(startup_program)
296 297 298 299
            out1 = exe.run(
                train_program, feed={}, fetch_list=[result_1, result_2]
            )
            # gen.set_state(cur_state)
300
            gen.manual_seed(123123143)
301 302 303
            out2 = exe.run(
                train_program, feed={}, fetch_list=[result_1, result_2]
            )
304 305 306 307 308 309 310 311

            out1_res1 = np.array(out1[0])
            out1_res2 = np.array(out1[1])
            out2_res1 = np.array(out2[0])
            out2_res2 = np.array(out2[1])

            if not core.is_compiled_with_cuda():
                print(">>>>>>> randint static >>>>>>>")
312 313
                np.testing.assert_allclose(out1_res1, out2_res1, rtol=1e-05)
                np.testing.assert_allclose(out1_res2, out2_res2, rtol=1e-05)
314 315 316 317 318 319 320
                self.assertTrue(not np.allclose(out1_res2, out1_res1))

    def test_generator_randperm_dygraph(self):
        """Test Generator seed."""

        fluid.enable_dygraph()

C
cnn 已提交
321
        gen = paddle.seed(12312321111)
322 323 324 325 326 327 328 329 330 331 332 333 334 335
        x = paddle.randperm(10)
        st1 = gen.get_state()
        x1 = paddle.randperm(10)
        gen.set_state(st1)
        x2 = paddle.randperm(10)
        gen.manual_seed(12312321111)
        x3 = paddle.randperm(10)
        x_np = x.numpy()
        x1_np = x1.numpy()
        x2_np = x2.numpy()
        x3_np = x3.numpy()

        if not core.is_compiled_with_cuda():
            print(">>>>>>> randperm dygraph >>>>>>>")
336 337
            np.testing.assert_allclose(x1_np, x2_np, rtol=1e-05)
            np.testing.assert_allclose(x_np, x3_np, rtol=1e-05)
338 339 340 341 342

    def test_generator_randperm_static(self):

        fluid.disable_dygraph()

C
cnn 已提交
343
        paddle.seed(123123143)
344 345 346 347 348 349 350 351 352 353 354

        startup_program = fluid.Program()
        train_program = fluid.Program()
        with fluid.program_guard(train_program, startup_program):
            # example 1:
            # attr shape is a list which doesn't contain tensor Variable.
            result_1 = paddle.randperm(10)
            result_2 = paddle.randperm(10)

            exe = fluid.Executor(fluid.CPUPlace())
            exe.run(startup_program)
355 356 357
            out1 = exe.run(
                train_program, feed={}, fetch_list=[result_1, result_2]
            )
L
Leo Chen 已提交
358

C
cnn 已提交
359
            paddle.seed(123123143)
360 361 362
            out2 = exe.run(
                train_program, feed={}, fetch_list=[result_1, result_2]
            )
363 364 365 366 367 368 369 370

            out1_res1 = np.array(out1[0])
            out1_res2 = np.array(out1[1])
            out2_res1 = np.array(out2[0])
            out2_res2 = np.array(out2[1])

            if not core.is_compiled_with_cuda():
                print(">>>>>>> randperm static >>>>>>>")
371 372
                np.testing.assert_allclose(out1_res1, out2_res1, rtol=1e-05)
                np.testing.assert_allclose(out1_res2, out2_res2, rtol=1e-05)
373 374 375 376 377
                self.assertTrue(not np.allclose(out1_res2, out1_res1))

    def test_gen_TruncatedNormal_initializer(self):
        fluid.disable_dygraph()

C
cnn 已提交
378
        gen = paddle.seed(123123143)
379 380 381 382 383 384 385 386 387 388 389
        cur_state = gen.get_state()

        startup_program = fluid.Program()
        train_program = fluid.Program()
        with fluid.program_guard(train_program, startup_program):
            # example 1:
            # attr shape is a list which doesn't contain tensor Variable.
            x = fluid.layers.uniform_random(shape=[2, 10])
            result_1 = fluid.layers.fc(
                input=x,
                size=10,
390 391 392 393
                param_attr=fluid.initializer.TruncatedNormal(
                    loc=0.0, scale=2.0
                ),
            )
394 395 396
            result_2 = fluid.layers.fc(
                input=x,
                size=10,
397 398 399 400
                param_attr=fluid.initializer.TruncatedNormal(
                    loc=0.0, scale=2.0
                ),
            )
401 402 403

            exe = fluid.Executor(fluid.CPUPlace())
            exe.run(startup_program)
404 405 406
            out1 = exe.run(
                train_program, feed={}, fetch_list=[result_1, result_2]
            )
407 408 409 410

        gen.manual_seed(123123143)
        with fluid.program_guard(train_program, startup_program):
            exe.run(startup_program)
411 412 413
            out2 = exe.run(
                train_program, feed={}, fetch_list=[result_1, result_2]
            )
414 415 416 417 418 419 420 421

        out1_res1 = np.array(out1[0])
        out1_res2 = np.array(out1[1])
        out2_res1 = np.array(out2[0])
        out2_res2 = np.array(out2[1])

        if not core.is_compiled_with_cuda():
            print(">>>>>>> sampling id static >>>>>>>")
422 423
            np.testing.assert_allclose(out1_res1, out2_res1, rtol=1e-05)
            np.testing.assert_allclose(out1_res2, out2_res2, rtol=1e-05)
424 425
            self.assertTrue(not np.allclose(out1_res2, out1_res1))

Y
yaoxuefeng 已提交
426 427 428

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