Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
54c368db
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看板
未验证
提交
54c368db
编写于
9月 30, 2020
作者:
1
123malin
提交者:
GitHub
9月 30, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[API 2.0: doc] fix doc of nonzero (#27685)
* test=develop, update example
上级
69a3339a
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
49 addition
and
40 deletion
+49
-40
python/paddle/fluid/optimizer.py
python/paddle/fluid/optimizer.py
+27
-21
python/paddle/fluid/tests/unittests/test_nonzero_api.py
python/paddle/fluid/tests/unittests/test_nonzero_api.py
+8
-0
python/paddle/optimizer/__init__.py
python/paddle/optimizer/__init__.py
+6
-8
python/paddle/tensor/search.py
python/paddle/tensor/search.py
+8
-11
未找到文件。
python/paddle/fluid/optimizer.py
浏览文件 @
54c368db
...
...
@@ -4884,29 +4884,35 @@ class LookaheadOptimizer(object):
import paddle
import paddle.fluid as fluid
import numpy as np
import numpy.random as random
x = fluid.layers.data(name='x', shape=[2], dtype='float32'
)
label = fluid.layers.data(name="label", shape=[1], dtype="int64")
y = fluid.layers.fc(input=[x], size=2, act="softmax"
)
loss = fluid.layers.cross_entropy(input=y, label=label
)
loss = fluid.layers.mean(x=loss
)
sgd = fluid.optimizer.SGD(learning_rate=0.01
)
optimizer = fluid.optimizer.LookaheadOptimizer(sgd,
alpha=0.5,
k=5)
optimizer.minimize(loss)
main_program = fluid.default_main_program(
)
place = fluid.CPUPlace(
)
exe = fluid.Executor(place
)
exe.run(fluid.default_startup_program()
)
feeder = fluid.DataFeeder(feed_list=[x, label], place=place
)
paddle.enable_static(
)
x = fluid.layers.data(name='x', shape=[2], dtype='float32'
)
label = fluid.layers.data(name="label", shape=[1], dtype="int64"
)
y = fluid.layers.fc(input=[x], size=2, act="softmax"
)
loss = fluid.layers.cross_entropy(input=y, label=label
)
loss = fluid.layers.mean(x=loss)
sgd = fluid.optimizer.SGD(learning_rate=0.01)
optimizer = fluid.optimizer.LookaheadOptimizer(sgd,
alpha=0.5,
k=5
)
optimizer.minimize(loss
)
main_program = fluid.default_main_program(
)
place = fluid.CPUPlace(
)
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program()
)
step = 0
while(step < 10):
step += 1
exe.run(fluid.default_main_program(),
feed=feeder.feed(batch_data))
def train_reader(limit=5):
for i in range(limit):
yield random.random([2]).astype('float32'), random.random([1]).astype('int64')
feeder = fluid.DataFeeder(feed_list=[x, label], place=place)
reader = paddle.batch(paddle.reader.shuffle(train_reader, buf_size=50000),batch_size=1)
for batch_data in reader():
exe.run(fluid.default_main_program(),
feed=feeder.feed(batch_data))
"""
...
...
python/paddle/fluid/tests/unittests/test_nonzero_api.py
浏览文件 @
54c368db
...
...
@@ -76,6 +76,14 @@ class TestNonZeroAPI(unittest.TestCase):
expect_out
=
np
.
array
([[
0
],
[
1
]])
self
.
assertTrue
(
np
.
allclose
(
expect_out
,
np
.
array
(
res
)))
def
test_dygraph_api
(
self
):
data_x
=
np
.
array
([[
True
,
False
],
[
False
,
True
]])
with
fluid
.
dygraph
.
guard
():
x
=
fluid
.
dygraph
.
to_variable
(
data_x
)
z
=
paddle
.
nonzero
(
x
)
np_z
=
z
.
numpy
()
expect_out
=
np
.
array
([[
0
,
0
],
[
1
,
1
]])
if
__name__
==
"__main__"
:
unittest
.
main
()
python/paddle/optimizer/__init__.py
浏览文件 @
54c368db
...
...
@@ -15,19 +15,17 @@
__all__
=
[
'Adadelta'
,
'AdadeltaOptimizer'
,
'Adagrad'
,
'AdagradOptimizer'
,
'Adam'
,
'Adamax'
,
'AdamW'
,
'DecayedAdagrad'
,
'DecayedAdagradOptimizer'
,
'Dpsgd'
,
'DpsgdOptimizer'
,
'Ftrl'
,
'FtrlOptimizer'
,
'LookaheadOptimizer'
,
'ModelAverage'
,
'Momentum'
,
'MomentumOptimizer'
,
'RMSProp'
,
'SGD'
,
'SGDOptimizer'
,
'Optimizer'
,
'_LRScheduler'
,
'NoamLR'
,
'PiecewiseLR'
,
'NaturalExpLR'
,
'InverseTimeLR'
,
'PolynomialLR'
,
'LinearLrWarmup'
,
'ExponentialLR'
,
'MultiStepLR'
,
'StepLR'
,
'LambdaLR'
,
'ReduceLROnPlateau'
,
'CosineAnnealingLR'
'DpsgdOptimizer'
,
'Ftrl'
,
'FtrlOptimizer'
,
'Momentum'
,
'MomentumOptimizer'
,
'RMSProp'
,
'SGD'
,
'SGDOptimizer'
,
'Optimizer'
,
'_LRScheduler'
,
'NoamLR'
,
'PiecewiseLR'
,
'NaturalExpLR'
,
'InverseTimeLR'
,
'PolynomialLR'
,
'LinearLrWarmup'
,
'ExponentialLR'
,
'MultiStepLR'
,
'StepLR'
,
'LambdaLR'
,
'ReduceLROnPlateau'
,
'CosineAnnealingLR'
]
from
..fluid.optimizer
import
Momentum
,
Adagrad
,
Dpsgd
,
DecayedAdagrad
,
Ftrl
,
\
AdagradOptimizer
,
DpsgdOptimizer
,
DecayedAdagradOptimizer
,
\
FtrlOptimizer
,
AdadeltaOptimizer
,
ModelAverage
,
\
LookaheadOptimizer
FtrlOptimizer
,
AdadeltaOptimizer
from
.optimizer
import
Optimizer
from
.adam
import
Adam
...
...
python/paddle/tensor/search.py
浏览文件 @
54c368db
...
...
@@ -339,11 +339,8 @@ def index_select(x, index, axis=0, name=None):
return
out
def
nonzero
(
input
,
as_tuple
=
False
):
def
nonzero
(
x
,
as_tuple
=
False
):
"""
:alias_main: paddle.nonzero
:alias: paddle.nonzero,paddle.tensor.nonzero,paddle.tensor.search.nonzero
Return a tensor containing the indices of all non-zero elements of the `input`
tensor. If as_tuple is True, return a tuple of 1-D tensors, one for each dimension
in `input`, each containing the indices (in that dimension) of all non-zero elements
...
...
@@ -353,17 +350,17 @@ def nonzero(input, as_tuple=False):
a 1-D tensor tuple of length `n`, and the shape of each 1-D tensor is [z, 1].
Args:
inputs (Variable
): The input tensor variable.
x (Tensor
): The input tensor variable.
as_tuple (bool): Return type, Tensor or tuple of Tensor.
Returns:
Variable
. The data type is int64.
Tensor
. The data type is int64.
Examples:
.. code-block:: python
import paddle
paddle.disable_static()
import paddle
x1 = paddle.to_tensor([[1.0, 0.0, 0.0],
[0.0, 2.0, 0.0],
...
...
@@ -402,13 +399,13 @@ def nonzero(input, as_tuple=False):
#[]
"""
list_out
=
[]
shape
=
input
.
shape
shape
=
x
.
shape
rank
=
len
(
shape
)
if
in_dygraph_mode
():
outs
=
core
.
ops
.
where_index
(
input
)
outs
=
core
.
ops
.
where_index
(
x
)
else
:
outs
=
layers
.
where
(
input
)
outs
=
layers
.
where
(
x
)
if
not
as_tuple
:
return
outs
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录