未验证 提交 9dfd5d60 编写于 作者: C Candy2Tang 提交者: GitHub

[xdoctest][task 108] Reformat example code with google style in...

[xdoctest][task 108] Reformat example code with google style in python/paddle/optimizer/lbfgs.py (#56224)

* [xdoctest][task 108] test=docs_preview

* Apply suggestions from code review

---------
Co-authored-by: NNyakku Shigure <sigure.qaq@gmail.com>
上级 61dd9844
...@@ -25,7 +25,7 @@ __all__ = [] ...@@ -25,7 +25,7 @@ __all__ = []
def _cubic_interpolate(x1, f1, g1, x2, f2, g2, bounds=None): def _cubic_interpolate(x1, f1, g1, x2, f2, g2, bounds=None):
r"""Cubic interpolation between (x1, f1, g1) and (x2, f2, g2). r"""Cubic interpolation between (x1, f1, g1) and (x2, f2, g2).
Use two points and their gradient to determine a cubic function and get the minimun point Use two points and their gradient to determine a cubic function and get the minimum point
between them in the cubic curve. between them in the cubic curve.
Reference: Reference:
...@@ -38,7 +38,7 @@ def _cubic_interpolate(x1, f1, g1, x2, f2, g2, bounds=None): ...@@ -38,7 +38,7 @@ def _cubic_interpolate(x1, f1, g1, x2, f2, g2, bounds=None):
bounds: bounds of interpolation area bounds: bounds of interpolation area
Returns: Returns:
min_pos: the minimun point between the specified points in the cubic curve. min_pos: the minimum point between the specified points in the cubic curve.
""" """
# Compute bounds of interpolation area # Compute bounds of interpolation area
if bounds is not None: if bounds is not None:
...@@ -338,14 +338,14 @@ class LBFGS(Optimizer): ...@@ -338,14 +338,14 @@ class LBFGS(Optimizer):
parameters (list|tuple, optional): List/Tuple of ``Tensor`` names to update to minimize ``loss``. \ parameters (list|tuple, optional): List/Tuple of ``Tensor`` names to update to minimize ``loss``. \
This parameter is required in dygraph mode. The default value is None. This parameter is required in dygraph mode. The default value is None.
weight_decay (float|WeightDecayRegularizer, optional): The strategy of regularization. \ weight_decay (float|WeightDecayRegularizer, optional): The strategy of regularization. \
It canbe a float value as coeff of L2 regularization or \ It can be a float value as coeff of L2 regularization or \
:ref:`api_fluid_regularizer_L1Decay`, :ref:`api_fluid_regularizer_L2Decay`. :ref:`api_fluid_regularizer_L1Decay`, :ref:`api_fluid_regularizer_L2Decay`.
If a parameter has set regularizer using :ref:`api_fluid_ParamAttr` already, \ If a parameter has set regularizer using :ref:`api_fluid_ParamAttr` already, \
the regularization setting here in optimizer will be ignored for this parameter. \ the regularization setting here in optimizer will be ignored for this parameter. \
Otherwise, the regularization setting here in optimizer will take effect. \ Otherwise, the regularization setting here in optimizer will take effect. \
Default None, meaning there is no regularization. Default None, meaning there is no regularization.
grad_clip (GradientClipBase, optional): Gradient cliping strategy, it's an instance of \ grad_clip (GradientClipBase, optional): Gradient clipping strategy, it's an instance of \
some derived class of ``GradientClipBase`` . There are three cliping strategies \ some derived class of ``GradientClipBase`` . There are three clipping strategies \
( :ref:`api_fluid_clip_GradientClipByGlobalNorm` , :ref:`api_fluid_clip_GradientClipByNorm` , \ ( :ref:`api_fluid_clip_GradientClipByGlobalNorm` , :ref:`api_fluid_clip_GradientClipByNorm` , \
:ref:`api_fluid_clip_GradientClipByValue` ). Default None, meaning there is no gradient clipping. :ref:`api_fluid_clip_GradientClipByValue` ). Default None, meaning there is no gradient clipping.
name (str, optional): Normally there is no need for user to set this property. name (str, optional): Normally there is no need for user to set this property.
...@@ -358,45 +358,43 @@ class LBFGS(Optimizer): ...@@ -358,45 +358,43 @@ class LBFGS(Optimizer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
import numpy as np >>> import numpy as np
paddle.disable_static() >>> paddle.disable_static()
np.random.seed(0) >>> np.random.seed(0)
np_w = np.random.rand(1).astype(np.float32) >>> np_w = np.random.rand(1).astype(np.float32)
np_x = np.random.rand(1).astype(np.float32) >>> np_x = np.random.rand(1).astype(np.float32)
inputs = [np.random.rand(1).astype(np.float32) for i in range(10)] >>> inputs = [np.random.rand(1).astype(np.float32) for i in range(10)]
# y = 2x >>> # y = 2x
targets = [2 * x for x in inputs] >>> targets = [2 * x for x in inputs]
class Net(paddle.nn.Layer): >>> class Net(paddle.nn.Layer):
def __init__(self): ... def __init__(self):
super().__init__() ... super().__init__()
w = paddle.to_tensor(np_w) ... w = paddle.to_tensor(np_w)
self.w = paddle.create_parameter(shape=w.shape, dtype=w.dtype, default_initializer=paddle.nn.initializer.Assign(w)) ... self.w = paddle.create_parameter(shape=w.shape, dtype=w.dtype, default_initializer=paddle.nn.initializer.Assign(w))
...
def forward(self, x): ... def forward(self, x):
return self.w * x ... return self.w * x
...
net = Net() >>> net = Net()
opt = paddle.optimizer.LBFGS(learning_rate=1, max_iter=1, max_eval=None, tolerance_grad=1e-07, tolerance_change=1e-09, history_size=100, line_search_fn='strong_wolfe', parameters=net.parameters()) >>> opt = paddle.optimizer.LBFGS(learning_rate=1, max_iter=1, max_eval=None, tolerance_grad=1e-07, tolerance_change=1e-09, history_size=100, line_search_fn='strong_wolfe', parameters=net.parameters())
def train_step(inputs, targets): >>> def train_step(inputs, targets):
def closure(): ... def closure():
outputs = net(inputs) ... outputs = net(inputs)
loss = paddle.nn.functional.mse_loss(outputs, targets) ... loss = paddle.nn.functional.mse_loss(outputs, targets)
print('loss: ', loss.item()) ... print('loss: ', loss.item())
opt.clear_grad() ... opt.clear_grad()
loss.backward() ... loss.backward()
return loss ... return loss
opt.step(closure) ... opt.step(closure)
...
>>> for input, target in zip(inputs, targets):
for input, target in zip(inputs, targets): ... input = paddle.to_tensor(input)
input = paddle.to_tensor(input) ... target = paddle.to_tensor(target)
target = paddle.to_tensor(target) ... train_step(input, target)
train_step(input, target)
""" """
def __init__( def __init__(
...@@ -458,41 +456,41 @@ class LBFGS(Optimizer): ...@@ -458,41 +456,41 @@ class LBFGS(Optimizer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
paddle.disable_static() >>> paddle.disable_static()
net = paddle.nn.Linear(10, 10) >>> net = paddle.nn.Linear(10, 10)
opt = paddle.optimizer.LBFGS( >>> opt = paddle.optimizer.LBFGS(
learning_rate=1, ... learning_rate=1,
max_iter=1, ... max_iter=1,
max_eval=None, ... max_eval=None,
tolerance_grad=1e-07, ... tolerance_grad=1e-07,
tolerance_change=1e-09, ... tolerance_change=1e-09,
history_size=100, ... history_size=100,
line_search_fn='strong_wolfe', ... line_search_fn='strong_wolfe',
parameters=net.parameters(), ... parameters=net.parameters(),
) >>> )
def train_step(inputs, targets): >>> def train_step(inputs, targets):
def closure(): ... def closure():
outputs = net(inputs) ... outputs = net(inputs)
loss = paddle.nn.functional.mse_loss(outputs, targets) ... loss = paddle.nn.functional.mse_loss(outputs, targets)
opt.clear_grad() ... opt.clear_grad()
loss.backward() ... loss.backward()
return loss ... return loss
...
opt.step(closure) ... opt.step(closure)
...
inputs = paddle.rand([10, 10], dtype="float32") >>> inputs = paddle.rand([10, 10], dtype="float32")
targets = paddle.to_tensor([2 * x for x in inputs]) >>> targets = paddle.to_tensor([2 * x for x in inputs])
n_iter = 0 >>> n_iter = 0
while n_iter < 20: >>> while n_iter < 20:
loss = train_step(inputs, targets) ... loss = train_step(inputs, targets)
n_iter = opt.state_dict()["state"]["func_evals"] ... n_iter = opt.state_dict()["state"]["func_evals"]
print("n_iter:", n_iter) ... print("n_iter:", n_iter)
...
""" """
packed_state = {} packed_state = {}
...@@ -559,34 +557,34 @@ class LBFGS(Optimizer): ...@@ -559,34 +557,34 @@ class LBFGS(Optimizer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
paddle.disable_static() >>> paddle.disable_static()
inputs = paddle.rand([10, 10], dtype="float32") >>> inputs = paddle.rand([10, 10], dtype="float32")
targets = paddle.to_tensor([2 * x for x in inputs]) >>> targets = paddle.to_tensor([2 * x for x in inputs])
net = paddle.nn.Linear(10, 10) >>> net = paddle.nn.Linear(10, 10)
opt = paddle.optimizer.LBFGS( >>> opt = paddle.optimizer.LBFGS(
learning_rate=1, ... learning_rate=1,
max_iter=1, ... max_iter=1,
max_eval=None, ... max_eval=None,
tolerance_grad=1e-07, ... tolerance_grad=1e-07,
tolerance_change=1e-09, ... tolerance_change=1e-09,
history_size=100, ... history_size=100,
line_search_fn='strong_wolfe', ... line_search_fn='strong_wolfe',
parameters=net.parameters(), ... parameters=net.parameters(),
) >>> )
def closure(): >>> def closure():
outputs = net(inputs) ... outputs = net(inputs)
loss = paddle.nn.functional.mse_loss(outputs, targets) ... loss = paddle.nn.functional.mse_loss(outputs, targets)
print("loss:", loss.item()) ... print("loss:", loss.item())
opt.clear_grad() ... opt.clear_grad()
loss.backward() ... loss.backward()
return loss ... return loss
...
opt.step(closure) >>> opt.step(closure)
""" """
with paddle.no_grad(): with paddle.no_grad():
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册