diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index 81ecb448ecf856b2766b676c4178fbe7fb26e480..2dd755ecafd890289dd6f6aae76da86a0d69f7d9 100644 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -36,6 +36,7 @@ batch = batch.batch import paddle.sysconfig import paddle.tensor import paddle.nn +import paddle.framework import paddle.imperative # TODO: define alias in tensor and framework directory @@ -198,7 +199,7 @@ from .tensor.search import nonzero #DEFINE_ALIAS from .tensor.search import sort #DEFINE_ALIAS # from .framework.framework import set_default_dtype #DEFINE_ALIAS # from .framework.framework import get_default_dtype #DEFINE_ALIAS -# from .framework.random import manual_seed #DEFINE_ALIAS +from .framework.random import manual_seed #DEFINE_ALIAS # from .framework import append_backward #DEFINE_ALIAS # from .framework import gradients #DEFINE_ALIAS # from .framework import Executor #DEFINE_ALIAS diff --git a/python/paddle/fluid/framework.py b/python/paddle/fluid/framework.py index 50f26c45df07206b6f434e275d16940a28b5bad4..f5e26711d262f80eec6b064398c2397d7e4a17fe 100644 --- a/python/paddle/fluid/framework.py +++ b/python/paddle/fluid/framework.py @@ -66,6 +66,8 @@ _dygraph_tracer_ = None _dygraph_current_expected_place_ = None _current_device = None +global_prog_seed = 0 + def require_version(min_version, max_version=None): """ @@ -3653,7 +3655,8 @@ class Program(object): self.desc = core.ProgramDesc() self.blocks = [Block(self, 0)] self.current_block_idx = 0 - self._seed = 0 + global global_prog_seed + self._seed = global_prog_seed self._current_role = core.op_proto_and_checker_maker.OpRole.Forward self.__op_role_var = [] @@ -3692,6 +3695,33 @@ class Program(object): # appending gradients times self._appending_grad_times = 0 + def global_seed(self, seed=0): + """ + Set global seed for Program + + Returns: + None. + + Examples: + .. code-block:: python + + import paddle.fluid as fluid + + prog = fluid.default_main_program() + print(prog.random_seed) + ## 0 + ## the default random seed is 0 + + prog.global_seed(102) + prog1 = fluid.default_main_program() + print(prog1.random_seed) + ## 102 + ## the random seed is 102 + """ + global global_prog_seed + global_prog_seed = seed + self._seed = global_prog_seed + @property def _op_role(self): """ diff --git a/python/paddle/fluid/tests/unittests/test_manual_seed.py b/python/paddle/fluid/tests/unittests/test_manual_seed.py new file mode 100644 index 0000000000000000000000000000000000000000..747026622e4653491feecb852dde67a19b8ff666 --- /dev/null +++ b/python/paddle/fluid/tests/unittests/test_manual_seed.py @@ -0,0 +1,45 @@ +# 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. + +from __future__ import print_function +import unittest + +import paddle.fluid as fluid +from paddle.framework import manual_seed +from paddle.fluid.framework import Program, default_main_program, default_startup_program + + +class TestManualSeed(unittest.TestCase): + def test_manual_seed(self): + local_program = Program() + local_main_prog = default_main_program() + local_start_prog = default_startup_program() + + self.assertEqual(0, local_program.random_seed) + self.assertEqual(0, local_main_prog.random_seed) + self.assertEqual(0, local_start_prog.random_seed) + + manual_seed(102) + global_program1 = Program() + global_program2 = Program() + global_main_prog = default_main_program() + global_start_prog = default_startup_program() + self.assertEqual(102, global_program1.random_seed) + self.assertEqual(102, global_program2.random_seed) + self.assertEqual(102, global_main_prog.random_seed) + self.assertEqual(102, global_start_prog.random_seed) + + +if __name__ == '__main__': + unittest.main() diff --git a/python/paddle/framework/__init__.py b/python/paddle/framework/__init__.py index 0932fe46c59ba5b356a74398b6854d5fd88d2793..873b22f4f4dcce4460e29dd5300993ceddc200d2 100644 --- a/python/paddle/framework/__init__.py +++ b/python/paddle/framework/__init__.py @@ -38,3 +38,6 @@ # 'WeightNormParamAttr', # 'Model', # 'Sequential'] + +from . import random +from .random import manual_seed diff --git a/python/paddle/framework/random.py b/python/paddle/framework/random.py index 7b3be421e475a945afa501ca4839061501b3df82..c0e7b3e21dd6cc8fc6ad9fa8817cbe3595ebed50 100644 --- a/python/paddle/framework/random.py +++ b/python/paddle/framework/random.py @@ -13,4 +13,28 @@ # limitations under the License. # TODO: define random api -# __all__ = ['manual_seed'] +import paddle.fluid as fluid + +__all__ = ['manual_seed'] + + +def manual_seed(seed): + """ + Set global manual seed for program + + Args: + manual_seed(int): random seed for program + + Returns: + None. + + Examples: + .. code-block:: python + + from paddle.framework import manual_seed + manual_seed(102) + """ + fluid.default_main_program().random_seed = seed + fluid.default_startup_program().random_seed = seed + program = fluid.Program() + program.global_seed(seed) diff --git a/python/setup.py.in b/python/setup.py.in index 76776ab50a1d1e8e214dd93d6da4040c4205bd38..36851adde19054cb9f42b7ea492f8f547e30f121 100644 --- a/python/setup.py.in +++ b/python/setup.py.in @@ -141,6 +141,7 @@ packages=['paddle', 'paddle.distributed', 'paddle.fluid', 'paddle.tensor', + 'paddle.framework', 'paddle.fluid.dygraph', 'paddle.tensor', 'paddle.fluid.dygraph.dygraph_to_static',