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

change prepend_op to append_op in initializer #32177

Merged
merged 5 commits into from
Apr 26, 2021

Conversation

zhiqiu
Copy link
Contributor

@zhiqiu zhiqiu commented Apr 9, 2021

PR types

Breaking changes

PR changes

Others

Describe

change prepend_op to append_op in initializer to make dygraph and static graph consistent

Given the following code, with the same random SEED=123, the same parameter initialized in static graph and dynamic graph is different.

import paddle
paddle.set_device('cpu')
SEED = 123

weight_attr=paddle.framework.ParamAttr(name="linear_weight", learning_rate=1.0,
        trainable=False, regularizer=None, initializer=paddle.nn.initializer.TruncatedNormal(mean=0.0, std=2.0))
bias_attr=paddle.framework.ParamAttr(name="linear_bias", learning_rate=1.0,
        trainable=False, regularizer=None, initializer=paddle.nn.initializer.TruncatedNormal(mean=0.0, std=2.0))

if True:
    print('Test dynamic graph')
    paddle.seed(SEED)
    linear = paddle.nn.Linear(1, 1, weight_attr=weight_attr, bias_attr=bias_attr)

    print('linear.weight: ', linear.weight.numpy())
    print('linear.bias: ', linear.bias.numpy())

if True:
    print('Test static graph')
    paddle.enable_static()

    exe = paddle.static.Executor(paddle.CPUPlace())
    paddle.seed(SEED)

    linear = paddle.nn.Linear(1, 1, weight_attr=weight_attr, bias_attr=bias_attr)

    res = exe.run(paddle.static.default_startup_program(), fetch_list=['linear_weight', 'linear_bias'])

    print('linear.weight: ', res[0])
    print('linear.bias: ', res[1])
  • Before
Test dynamic graph
linear.weight:  [[1.594699]]
linear.bias:  [3.8483276]
Test static graph
linear.weight:  [[3.8483276]]
linear.bias:  [1.594699]
  • After
Test dynamic graph
linear.weight:  [[1.594699]]
linear.bias:  [3.8483276]
Test static graph
linear.weight:  [[1.594699]]
linear.bias:  [3.8483276]

@paddle-bot-old
Copy link

paddle-bot-old bot commented Apr 9, 2021

Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@zhiqiu zhiqiu closed this Apr 25, 2021
@zhiqiu zhiqiu reopened this Apr 25, 2021
@zhiqiu zhiqiu requested a review from Aurelius84 April 26, 2021 03:21
Copy link
Contributor

@Aurelius84 Aurelius84 left a comment

Choose a reason for hiding this comment

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

LGTM

@zhiqiu zhiqiu merged commit 8fec3c6 into PaddlePaddle:develop Apr 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants