Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"add init seed" #6221

Merged
merged 4 commits into from
Dec 5, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions python/paddle/v2/fluid/framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ def __init__(self):
self.desc = core.ProgramDesc()
self.blocks = [Block(self, 0)]
self.current_block_idx = 0
self._seed = 0

def __str__(self):
return self.to_string(True)
Expand Down Expand Up @@ -564,6 +565,16 @@ def parse_from_string(binary_str):
p.sync_with_cpp()
return p

@property
def random_seed(self):
return self._seed

@random_seed.setter
def random_seed(self, seed):
if not isinstance(seed, int):
raise ValueError("Seed must be a integer.")
self._seed = seed

def __repr__(self):
return str(self)

Expand Down
10 changes: 10 additions & 0 deletions python/paddle/v2/fluid/initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ def __call__(self, var, block):
assert isinstance(var, framework.Variable)
assert isinstance(block, framework.Block)
# Initialization Ops should be prepended and not appended
if block.program.random_seed != 0 and self._seed == 0:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't consider case that user need to overwrite program default rand seed with 0, just use if self._seed == 0 is fine, maybe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's true.

self._seed = block.program.random_seed
op = block.prepend_op(
type="uniform_random",
outputs={"Out": var},
Expand Down Expand Up @@ -180,6 +182,8 @@ def __call__(self, var, block):
assert isinstance(var, framework.Variable)
assert isinstance(block, framework.Block)
# Initialization Ops should be prepended and not appended
if block.program.random_seed != 0 and self._seed == 0:
self._seed = block.program.random_seed
op = block.prepend_op(
type="gaussian_random",
outputs={"Out": var},
Expand Down Expand Up @@ -255,6 +259,9 @@ def __call__(self, var, block):
fan_in = f_in if self._fan_in is None else self._fan_in
fan_out = f_out if self._fan_out is None else self._fan_out

if block.program.random_seed != 0 and self._seed == 0:
self._seed = block.program.random_seed

if self._uniform:
limit = np.sqrt(6.0 / float(fan_in + fan_out))
op = block.prepend_op(
Expand Down Expand Up @@ -338,6 +345,9 @@ def __call__(self, var, block):
# If fan_in is passed, use it
fan_in = f_in if self._fan_in is None else self._fan_in

if block.program.random_seed != 0 and self._seed == 0:
self._seed = block.program.random_seed

if self._uniform:
limit = np.sqrt(6.0 / float(fan_in))
op = block.prepend_op(
Expand Down
23 changes: 23 additions & 0 deletions python/paddle/v2/fluid/tests/test_initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,29 @@ def test_uniform_initializer_default_value(self):
self.assertAlmostEqual(init_op.attr('max'), 1.0, delta=DELTA)
self.assertEqual(init_op.attr('seed'), 0)

def test_uniform_initializer_random_seed(self):
"""Test the uniform initializer with manually setting seed
"""
program = framework.Program()
program.random_seed = 123
block = program.global_block()
block.create_parameter(
dtype="float32",
shape=[5, 10],
lod_level=0,
name="param",
initializer=initializer.UniformInitializer())
block.create_parameter(
dtype="float32",
shape=[5, 10],
lod_level=0,
name="param",
initializer=initializer.UniformInitializer(seed=456))
init_op = block.ops[1]
self.assertEqual(init_op.attr("seed"), 123)
init_op1 = block.ops[0]
self.assertEqual(init_op1.attr("seed"), 456)

def test_uniform_initializer(self):
"""Test uniform initializer with supplied attributes
"""
Expand Down