Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
dc003fa3
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
dc003fa3
编写于
5月 11, 2023
作者:
L
lijialin03
提交者:
GitHub
5月 11, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
revise 'Examples' of LBFGS to create right docs(cn), test=docs_preview (#53375)
上级
38886829
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
113 addition
and
4 deletion
+113
-4
python/paddle/fluid/tests/unittests/test_lbfgs_class.py
python/paddle/fluid/tests/unittests/test_lbfgs_class.py
+30
-0
python/paddle/optimizer/lbfgs.py
python/paddle/optimizer/lbfgs.py
+83
-4
未找到文件。
python/paddle/fluid/tests/unittests/test_lbfgs_class.py
浏览文件 @
dc003fa3
...
...
@@ -555,6 +555,36 @@ class TestLbfgs(unittest.TestCase):
self
.
assertRaises
(
AssertionError
,
error_func3
)
def
test_error4
(
self
):
# test call minimize(loss)
paddle
.
disable_static
()
def
error_func4
():
inputs
=
np
.
random
.
rand
(
1
).
astype
(
np
.
float32
)
targets
=
paddle
.
to_tensor
([
inputs
*
2
])
inputs
=
paddle
.
to_tensor
(
inputs
)
extream_point
=
np
.
array
([
-
1
,
1
]).
astype
(
'float32'
)
def
func
(
extream_point
,
x
):
return
x
*
extream_point
[
0
]
+
5
*
x
*
extream_point
[
1
]
net
=
Net
(
extream_point
,
func
)
opt
=
lbfgs
.
LBFGS
(
learning_rate
=
1
,
max_iter
=
10
,
max_eval
=
None
,
tolerance_grad
=
1e-07
,
tolerance_change
=
1e-09
,
history_size
=
5
,
line_search_fn
=
'strong_wolfe'
,
parameters
=
net
.
parameters
(),
)
loss
=
train_step
(
inputs
,
targets
,
net
,
opt
)
opt
.
minimize
(
loss
)
self
.
assertRaises
(
NotImplementedError
,
error_func4
)
if
__name__
==
'__main__'
:
unittest
.
main
()
python/paddle/optimizer/lbfgs.py
浏览文件 @
dc003fa3
...
...
@@ -359,7 +359,6 @@ class LBFGS(Optimizer):
import paddle
import numpy as np
from paddle.incubate.optimizer import LBFGS
paddle.disable_static()
np.random.seed(0)
...
...
@@ -380,7 +379,7 @@ class LBFGS(Optimizer):
return self.w * x
net = Net()
opt = 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 closure():
outputs = net(inputs)
...
...
@@ -453,7 +452,46 @@ class LBFGS(Optimizer):
Return:
state, a dict holding current optimization state. Its content
differs between optimizer classes.
differs between optimizer classes.
Examples:
.. code-block:: python
import paddle
paddle.disable_static()
net = paddle.nn.Linear(10, 10)
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 closure():
outputs = net(inputs)
loss = paddle.nn.functional.mse_loss(outputs, targets)
opt.clear_grad()
loss.backward()
return loss
opt.step(closure)
inputs = paddle.rand([10, 10], dtype="float32")
targets = paddle.to_tensor([2 * x for x in inputs])
n_iter = 0
while n_iter < 20:
loss = train_step(inputs, targets)
n_iter = opt.state_dict()["state"]["func_evals"]
print("n_iter:", n_iter)
"""
packed_state
=
{}
...
...
@@ -512,9 +550,42 @@ class LBFGS(Optimizer):
@
framework
.
non_static_only
def
step
(
self
,
closure
):
"""Performs a single optimization step.
Args:
closure (callable): A closure that reevaluates the model
and returns the loss.
and returns the loss.
Examples:
.. code-block:: python
import paddle
paddle.disable_static()
inputs = paddle.rand([10, 10], dtype="float32")
targets = paddle.to_tensor([2 * x for x in inputs])
net = paddle.nn.Linear(10, 10)
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 closure():
outputs = net(inputs)
loss = paddle.nn.functional.mse_loss(outputs, targets)
print("loss:", loss.item())
opt.clear_grad()
loss.backward()
return loss
opt.step(closure)
"""
with
paddle
.
no_grad
():
...
...
@@ -699,3 +770,11 @@ class LBFGS(Optimizer):
state
[
'prev_loss'
]
=
prev_loss
return
orig_loss
def
minimize
(
self
,
loss
,
startup_program
=
None
,
parameters
=
None
,
no_grad_set
=
None
):
"""Empty method. LBFGS optimizer does not use this way to minimize ``loss``. Please refer 'Examples' of LBFGS() above for usage."""
raise
NotImplementedError
(
"LBFGS optimizer does not use this way to minimize loss. Please refer 'Examples' of LBFGS() for usage."
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录