From 7daffbf88d342c289239cf49d00d4fe8f46147ff Mon Sep 17 00:00:00 2001 From: yuchen202 <103028470+yuchen202@users.noreply.github.com> Date: Wed, 6 Sep 2023 10:30:53 +0800 Subject: [PATCH] =?UTF-8?q?[xdoctest]=20reformat=20example=20code=20with?= =?UTF-8?q?=20google=20style=20in=20No.297=E3=80=81298=E3=80=81302=20(#568?= =?UTF-8?q?61)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 更改相关文件 * Update ir.py * 恢复相关文件 * Update ir.py * Delete python/paddle/incubate/optimizer/modelaverage.py * Delete modelaverage.py * 尝试恢复文件 * Revert "尝试恢复文件" This reverts commit 8a263cfd4642614a48a30f648c61fc801331e550. * Revert "恢复相关文件" This reverts commit 24249b8191fb3901681ffa9d0e1ad79ef43454de. * Revert "Revert "尝试恢复文件"" This reverts commit 1b833d623770a851f202c68fff51e77723121a9d. * Revert "Revert "Revert "尝试恢复文件""" This reverts commit 64b3a816d1f0fef5ee9100480b8354749005a463. * Revert "Delete python/paddle/incubate/optimizer/modelaverage.py" This reverts commit 61986296bf48f7b9bef878bed6890c5dc2971481. * Revert "更改相关文件" This reverts commit a5ba675a948534401247b779d6a0fba0581d0628. * Apply suggestions from code review --------- Co-authored-by: Nyakku Shigure --- .../incubate/optimizer/functional/bfgs.py | 72 +++++++++---------- .../incubate/optimizer/functional/lbfgs.py | 72 +++++++++---------- python/paddle/incubate/passes/ir.py | 20 +++--- 3 files changed, 82 insertions(+), 82 deletions(-) diff --git a/python/paddle/incubate/optimizer/functional/bfgs.py b/python/paddle/incubate/optimizer/functional/bfgs.py index 9d98460e03c..bc244d9c19d 100644 --- a/python/paddle/incubate/optimizer/functional/bfgs.py +++ b/python/paddle/incubate/optimizer/functional/bfgs.py @@ -81,46 +81,46 @@ def minimize_bfgs( .. code-block:: python :name: code-example1 - # Example1: 1D Grid Parameters - import paddle - # Randomly simulate a batch of input data - inputs = paddle. normal(shape=(100, 1)) - labels = inputs * 2.0 - # define the loss function - def loss(w): - y = w * inputs - return paddle.nn.functional.square_error_cost(y, labels).mean() - # Initialize weight parameters - w = paddle.normal(shape=(1,)) - # Call the bfgs method to solve the weight that makes the loss the smallest, and update the parameters - for epoch in range(0, 10): - # Call the bfgs method to optimize the loss, note that the third parameter returned represents the weight - w_update = paddle.incubate.optimizer.functional.minimize_bfgs(loss, w)[2] - # Use paddle.assign to update parameters in place - paddle. assign(w_update, w) + >>> # Example1: 1D Grid Parameters + >>> import paddle + >>> # Randomly simulate a batch of input data + >>> inputs = paddle. normal(shape=(100, 1)) + >>> labels = inputs * 2.0 + >>> # define the loss function + >>> def loss(w): + ... y = w * inputs + ... return paddle.nn.functional.square_error_cost(y, labels).mean() + >>> # Initialize weight parameters + >>> w = paddle.normal(shape=(1,)) + >>> # Call the bfgs method to solve the weight that makes the loss the smallest, and update the parameters + >>> for epoch in range(0, 10): + ... # Call the bfgs method to optimize the loss, note that the third parameter returned represents the weight + ... w_update = paddle.incubate.optimizer.functional.minimize_bfgs(loss, w)[2] + ... # Use paddle.assign to update parameters in place + ... paddle. assign(w_update, w) .. code-block:: python :name: code-example2 - # Example2: Multidimensional Grid Parameters - import paddle - def flatten(x): - return x. flatten() - def unflatten(x): - return x.reshape((2,2)) - # Assume the network parameters are more than one dimension - def net(x): - assert len(x.shape) > 1 - return x.square().mean() - # function to be optimized - def bfgs_f(flatten_x): - return net(unflatten(flatten_x)) - x = paddle.rand([2,2]) - for i in range(0, 10): - # Flatten x before using minimize_bfgs - x_update = paddle.incubate.optimizer.functional.minimize_bfgs(bfgs_f, flatten(x))[2] - # unflatten x_update, then update parameters - paddle. assign(unflatten(x_update), x) + >>> # Example2: Multidimensional Grid Parameters + >>> import paddle + >>> def flatten(x): + ... return x. flatten() + >>> def unflatten(x): + ... return x.reshape((2,2)) + >>> # Assume the network parameters are more than one dimension + >>> def net(x): + ... assert len(x.shape) > 1 + ... return x.square().mean() + >>> # function to be optimized + >>> def bfgs_f(flatten_x): + ... return net(unflatten(flatten_x)) + >>> x = paddle.rand([2,2]) + >>> for i in range(0, 10): + ... # Flatten x before using minimize_bfgs + ... x_update = paddle.incubate.optimizer.functional.minimize_bfgs(bfgs_f, flatten(x))[2] + ... # unflatten x_update, then update parameters + ... paddle.assign(unflatten(x_update), x) """ if dtype not in ['float32', 'float64']: diff --git a/python/paddle/incubate/optimizer/functional/lbfgs.py b/python/paddle/incubate/optimizer/functional/lbfgs.py index af30efe44a8..fc482e4ca18 100644 --- a/python/paddle/incubate/optimizer/functional/lbfgs.py +++ b/python/paddle/incubate/optimizer/functional/lbfgs.py @@ -82,46 +82,46 @@ def minimize_lbfgs( .. code-block:: python :name: code-example1 - # Example1: 1D Grid Parameters - import paddle - # Randomly simulate a batch of input data - inputs = paddle. normal(shape=(100, 1)) - labels = inputs * 2.0 - # define the loss function - def loss(w): - y = w * inputs - return paddle.nn.functional.square_error_cost(y, labels).mean() - # Initialize weight parameters - w = paddle.normal(shape=(1,)) - # Call the bfgs method to solve the weight that makes the loss the smallest, and update the parameters - for epoch in range(0, 10): - # Call the bfgs method to optimize the loss, note that the third parameter returned represents the weight - w_update = paddle.incubate.optimizer.functional.minimize_bfgs(loss, w)[2] - # Use paddle.assign to update parameters in place - paddle. assign(w_update, w) + >>> # Example1: 1D Grid Parameters + >>> import paddle + >>> # Randomly simulate a batch of input data + >>> inputs = paddle. normal(shape=(100, 1)) + >>> labels = inputs * 2.0 + >>> # define the loss function + >>> def loss(w): + ... y = w * inputs + ... return paddle.nn.functional.square_error_cost(y, labels).mean() + >>> # Initialize weight parameters + >>> w = paddle.normal(shape=(1,)) + >>> # Call the bfgs method to solve the weight that makes the loss the smallest, and update the parameters + >>> for epoch in range(0, 10): + ... # Call the bfgs method to optimize the loss, note that the third parameter returned represents the weight + ... w_update = paddle.incubate.optimizer.functional.minimize_bfgs(loss, w)[2] + ... # Use paddle.assign to update parameters in place + ... paddle.assign(w_update, w) .. code-block:: python :name: code-example2 - # Example2: Multidimensional Grid Parameters - import paddle - def flatten(x): - return x. flatten() - def unflatten(x): - return x.reshape((2,2)) - # Assume the network parameters are more than one dimension - def net(x): - assert len(x.shape) > 1 - return x.square().mean() - # function to be optimized - def bfgs_f(flatten_x): - return net(unflatten(flatten_x)) - x = paddle.rand([2,2]) - for i in range(0, 10): - # Flatten x before using minimize_bfgs - x_update = paddle.incubate.optimizer.functional.minimize_bfgs(bfgs_f, flatten(x))[2] - # unflatten x_update, then update parameters - paddle. assign(unflatten(x_update), x) + >>> # Example2: Multidimensional Grid Parameters + >>> import paddle + >>> def flatten(x): + ... return x. flatten() + >>> def unflatten(x): + ... return x.reshape((2,2)) + >>> # Assume the network parameters are more than one dimension + >>> def net(x): + ... assert len(x.shape) > 1 + ... return x.square().mean() + >>> # function to be optimized + >>> def bfgs_f(flatten_x): + ... return net(unflatten(flatten_x)) + >>> x = paddle.rand([2,2]) + >>> for i in range(0, 10): + ... # Flatten x before using minimize_bfgs + ... x_update = paddle.incubate.optimizer.functional.minimize_bfgs(bfgs_f, flatten(x))[2] + ... # unflatten x_update, then update parameters + ... paddle.assign(unflatten(x_update), x) """ if dtype not in ['float32', 'float64']: diff --git a/python/paddle/incubate/passes/ir.py b/python/paddle/incubate/passes/ir.py index 0e292e51a0a..c657f20abcb 100644 --- a/python/paddle/incubate/passes/ir.py +++ b/python/paddle/incubate/passes/ir.py @@ -469,16 +469,16 @@ def RegisterPass(function=None, input_specs={}): Examples: .. code-block:: python - import paddle - from paddle.fluid.ir import RegisterPass - - @RegisterPass - def multi_add_to_addn(): - def pattern(x, y, z): - return paddle.add(paddle.add(x, y), z) - def replace(x, y, z): - return paddle.add_n([x, y, z]) - return pattern, replace + >>> import paddle + >>> from paddle.fluid.ir import RegisterPass + + >>> @RegisterPass + >>> def multi_add_to_addn(): + ... def pattern(x, y, z): + ... return paddle.add(paddle.add(x, y), z) + ... def replace(x, y, z): + ... return paddle.add_n([x, y, z]) + ... return pattern, replace """ def _is_pass_pair(check_pair): -- GitLab