test_random_seed.py 18.9 KB
Newer Older
Y
yaoxuefeng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#   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 os
import unittest
import paddle.fluid.generator as generator

import time  # temp for debug
import paddle.fluid as fluid
import numpy as np
import paddle
import paddle.fluid.core as core


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

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

        fluid.enable_dygraph()

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

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

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

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

Y
yaoxuefeng 已提交
59 60 61 62 63 64
        x_np = x.numpy()
        x1_np = x1.numpy()
        x2_np = x2.numpy()
        x3_np = x3.numpy()

        if not core.is_compiled_with_cuda():
65 66
            np.testing.assert_allclose(x1_np, x2_np, rtol=1e-05)
            np.testing.assert_allclose(x_np, x3_np, rtol=1e-05)
Y
yaoxuefeng 已提交
67 68 69 70

    def test_generator_uniform_random_static(self):
        fluid.disable_dygraph()

C
cnn 已提交
71
        gen = paddle.seed(123123143)
Y
yaoxuefeng 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

        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)
            out1 = exe.run(train_program,
                           feed={},
                           fetch_list=[result_1, result_2])
            #gen.set_state(cur_state)
            gen.manual_seed(123123143)
            out2 = exe.run(train_program,
                           feed={},
                           fetch_list=[result_1, result_2])

            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():
98 99
                np.testing.assert_allclose(out1_res1, out2_res1, rtol=1e-05)
                np.testing.assert_allclose(out1_res2, out2_res2, rtol=1e-05)
Y
yaoxuefeng 已提交
100 101
                self.assertTrue(not np.allclose(out1_res2, out1_res1))

102 103 104
    def test_gen_dropout_dygraph(self):
        fluid.enable_dygraph()

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

123 124
        if not core.is_compiled_with_cuda():
            print(">>>>>>> dropout dygraph >>>>>>>")
125
            np.testing.assert_allclose(y_np, y1_np, rtol=1e-05)
126 127 128 129

    def test_gen_dropout_static(self):
        fluid.disable_dygraph()

C
cnn 已提交
130
        gen = paddle.seed(123123143)
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

        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])
            #gen.set_state(cur_state)
            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 已提交
147

148 149
        if not core.is_compiled_with_cuda():
            print(">>>>>>> dropout static >>>>>>>")
150
            np.testing.assert_allclose(out1_np, out2_np, rtol=1e-05)
151 152 153 154 155

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

C
cnn 已提交
156
        gen = paddle.seed(12312321111)
157 158 159 160 161 162 163 164 165 166 167 168 169 170
        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 >>>>>>>")
171 172
            np.testing.assert_allclose(x1_np, x2_np, rtol=1e-05)
            np.testing.assert_allclose(x_np, x3_np, rtol=1e-05)
173 174 175 176

    def test_generator_gaussian_random_static(self):
        fluid.disable_dygraph()

C
cnn 已提交
177
        gen = paddle.seed(123123143)
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

        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)
            out1 = exe.run(train_program,
                           feed={},
                           fetch_list=[result_1, result_2])
            #gen.set_state(cur_state)
            gen.manual_seed(123123143)
            out2 = exe.run(train_program,
                           feed={},
                           fetch_list=[result_1, result_2])

            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 >>>>>>>")
205 206
                np.testing.assert_allclose(out1_res1, out2_res1, rtol=1e-05)
                np.testing.assert_allclose(out1_res2, out2_res2, rtol=1e-05)
207 208
                self.assertTrue(not np.allclose(out1_res2, out1_res1))

Y
yaoxuefeng 已提交
209 210 211 212 213 214
    def test_generator_randint_dygraph(self):
        """Test Generator seed."""
        gen = generator.Generator()

        fluid.enable_dygraph()

C
cnn 已提交
215
        gen = paddle.seed(12312321111)
216
        x = paddle.randint(low=10, shape=[10], dtype="int32")
Y
yaoxuefeng 已提交
217
        st1 = gen.get_state()
218
        x1 = paddle.randint(low=10, shape=[10], dtype="int32")
Y
yaoxuefeng 已提交
219
        gen.set_state(st1)
220
        x2 = paddle.randint(low=10, shape=[10], dtype="int32")
Y
yaoxuefeng 已提交
221
        gen.manual_seed(12312321111)
222
        x3 = paddle.randint(low=10, shape=[10], dtype="int32")
Y
yaoxuefeng 已提交
223 224 225 226
        x_np = x.numpy()
        x1_np = x1.numpy()
        x2_np = x2.numpy()
        x3_np = x3.numpy()
227

Y
yaoxuefeng 已提交
228
        if not core.is_compiled_with_cuda():
229
            print(">>>>>>> randint dygraph >>>>>>>")
230 231
            np.testing.assert_allclose(x1_np, x2_np, rtol=1e-05)
            np.testing.assert_allclose(x_np, x3_np, rtol=1e-05)
Y
yaoxuefeng 已提交
232

Z
zhangchunle 已提交
233
    def test_generator_uniform_random_static_1(self):
L
Leo Chen 已提交
234 235
        fluid.disable_dygraph()

C
cnn 已提交
236
        gen = paddle.seed(123123143)
L
Leo Chen 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255

        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)
            out1 = exe.run(train_program,
                           feed={},
                           fetch_list=[result_1, result_2])
            #gen.set_state(cur_state)
            gen.manual_seed(123123143)
            out2 = exe.run(train_program,
                           feed={},
                           fetch_list=[result_1, result_2])
256

L
Leo Chen 已提交
257 258 259 260 261 262
            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():
263 264
                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 已提交
265 266
                self.assertTrue(not np.allclose(out1_res2, out1_res1))

Z
zhangchunle 已提交
267
    def test_generator_randint_dygraph_1(self):
L
Leo Chen 已提交
268 269 270
        """Test Generator seed."""
        fluid.enable_dygraph()

C
cnn 已提交
271
        gen = paddle.seed(12312321111)
L
Leo Chen 已提交
272 273 274 275 276 277 278 279 280 281 282 283
        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():
284 285
            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 已提交
286 287

    def test_generator_ranint_static(self):
288 289
        fluid.disable_dygraph()

C
cnn 已提交
290
        gen = paddle.seed(123123143)
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317

        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)
            out1 = exe.run(train_program,
                           feed={},
                           fetch_list=[result_1, result_2])
            #gen.set_state(cur_state)
            gen.manual_seed(123123143)
            out2 = exe.run(train_program,
                           feed={},
                           fetch_list=[result_1, result_2])

            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 >>>>>>>")
318 319
                np.testing.assert_allclose(out1_res1, out2_res1, rtol=1e-05)
                np.testing.assert_allclose(out1_res2, out2_res2, rtol=1e-05)
320 321 322 323 324 325 326
                self.assertTrue(not np.allclose(out1_res2, out1_res1))

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

        fluid.enable_dygraph()

C
cnn 已提交
327
        gen = paddle.seed(12312321111)
328 329 330 331 332 333 334 335 336 337 338 339 340 341
        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 >>>>>>>")
342 343
            np.testing.assert_allclose(x1_np, x2_np, rtol=1e-05)
            np.testing.assert_allclose(x_np, x3_np, rtol=1e-05)
344 345 346 347 348

    def test_generator_randperm_static(self):

        fluid.disable_dygraph()

C
cnn 已提交
349
        paddle.seed(123123143)
350 351 352 353 354 355 356 357 358 359 360 361 362 363

        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)
            out1 = exe.run(train_program,
                           feed={},
                           fetch_list=[result_1, result_2])
L
Leo Chen 已提交
364

C
cnn 已提交
365
            paddle.seed(123123143)
366 367 368 369 370 371 372 373 374 375 376
            out2 = exe.run(train_program,
                           feed={},
                           fetch_list=[result_1, result_2])

            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 >>>>>>>")
377 378
                np.testing.assert_allclose(out1_res1, out2_res1, rtol=1e-05)
                np.testing.assert_allclose(out1_res2, out2_res2, rtol=1e-05)
379 380 381 382
                self.assertTrue(not np.allclose(out1_res2, out1_res1))

    def test_generator_sampling_id_dygraph(self):
        """Test Generator seed."""
C
cnn 已提交
383
        gen = paddle.seed(12312321111)
384 385 386 387

        fluid.enable_dygraph()

        gen.manual_seed(12312321111)
388 389 390 391
        x = fluid.layers.uniform_random([10, 10],
                                        dtype="float32",
                                        min=0.0,
                                        max=1.0)
392
        y = fluid.layers.sampling_id(x)
L
Leo Chen 已提交
393

394
        st1 = gen.get_state()
395 396 397 398
        x1 = fluid.layers.uniform_random([10, 10],
                                         dtype="float32",
                                         min=0.0,
                                         max=1.0)
399
        y1 = fluid.layers.sampling_id(x)
L
Leo Chen 已提交
400

401
        gen.set_state(st1)
402 403 404 405
        x2 = fluid.layers.uniform_random([10, 10],
                                         dtype="float32",
                                         min=0.0,
                                         max=1.0)
406
        y2 = fluid.layers.sampling_id(x)
L
Leo Chen 已提交
407

408
        gen.manual_seed(12312321111)
409 410 411 412
        x3 = fluid.layers.uniform_random([10, 10],
                                         dtype="float32",
                                         min=0.0,
                                         max=1.0)
413 414 415 416 417 418 419 420 421
        y3 = fluid.layers.sampling_id(x)

        x_np = y.numpy()
        x1_np = y1.numpy()
        x2_np = y2.numpy()
        x3_np = y3.numpy()

        if not core.is_compiled_with_cuda():
            print(">>>>>>> sampling id dygraph >>>>>>>")
422 423
            np.testing.assert_allclose(x1_np, x2_np, rtol=1e-05)
            np.testing.assert_allclose(x_np, x3_np, rtol=1e-05)
424

Z
zhangchunle 已提交
425
    def test_generator_randperm_static_1(self):
426 427 428

        fluid.disable_dygraph()

C
cnn 已提交
429
        paddle.seed(123123143)
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444

        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=[10, 10])
            result_1 = fluid.layers.sampling_id(x)
            result_2 = fluid.layers.sampling_id(x)

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

C
cnn 已提交
446
            paddle.seed(123123143)
447 448 449 450 451 452 453 454 455 456 457
            out2 = exe.run(train_program,
                           feed={},
                           fetch_list=[result_1, result_2])

            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 >>>>>>>")
458 459
                np.testing.assert_allclose(out1_res1, out2_res1, rtol=1e-05)
                np.testing.assert_allclose(out1_res2, out2_res2, rtol=1e-05)
460 461 462 463 464
                self.assertTrue(not np.allclose(out1_res2, out1_res1))

    def test_gen_TruncatedNormal_initializer(self):
        fluid.disable_dygraph()

C
cnn 已提交
465
        gen = paddle.seed(123123143)
466 467 468 469 470 471 472 473 474 475 476
        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,
477 478
                param_attr=fluid.initializer.TruncatedNormal(loc=0.0,
                                                             scale=2.0))
479 480 481
            result_2 = fluid.layers.fc(
                input=x,
                size=10,
482 483
                param_attr=fluid.initializer.TruncatedNormal(loc=0.0,
                                                             scale=2.0))
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504

            exe = fluid.Executor(fluid.CPUPlace())
            exe.run(startup_program)
            out1 = exe.run(train_program,
                           feed={},
                           fetch_list=[result_1, result_2])

        gen.manual_seed(123123143)
        with fluid.program_guard(train_program, startup_program):
            exe.run(startup_program)
            out2 = exe.run(train_program,
                           feed={},
                           fetch_list=[result_1, result_2])

        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 >>>>>>>")
505 506
            np.testing.assert_allclose(out1_res1, out2_res1, rtol=1e-05)
            np.testing.assert_allclose(out1_res2, out2_res2, rtol=1e-05)
507 508
            self.assertTrue(not np.allclose(out1_res2, out1_res1))

Y
yaoxuefeng 已提交
509 510 511

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