-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
[xdoctest] reformat example code with google style in No.297、298、302 #56861
Changes from 3 commits
a5ba675
ff26e08
24249b8
66ab764
6198629
1e822a9
7634ba6
8a263cf
1b833d6
8d001c1
64b3a81
b8ff2d0
e0652be
0a7770d
2e1672f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 是我看错了么,这个文件没恢复啊 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 恢复了,因为不需要改 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个文件仍然有 diff,需要 merge develop 确保没有 diff There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure?我是直接用的paddle官方的py文件替换的 |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
.. 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) | ||||||
SigureMo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
""" | ||||||
|
||||||
if dtype not in ['float32', 'float64']: | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个文件 #56826 已经改过了,需要恢复