Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
98adc8f0
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看板
未验证
提交
98adc8f0
编写于
11月 23, 2020
作者:
L
Leo Chen
提交者:
GitHub
11月 23, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Dev/fix doc of some api (#28785)
* refine doc of bernoulli * fix some problems * fix unsqueeze * fix squeeze * fix doc
上级
f77a78cd
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
54 addition
and
63 deletion
+54
-63
python/paddle/amp/grad_scaler.py
python/paddle/amp/grad_scaler.py
+17
-9
python/paddle/nn/layer/loss.py
python/paddle/nn/layer/loss.py
+9
-11
python/paddle/tensor/manipulation.py
python/paddle/tensor/manipulation.py
+19
-35
python/paddle/tensor/random.py
python/paddle/tensor/random.py
+9
-8
未找到文件。
python/paddle/amp/grad_scaler.py
浏览文件 @
98adc8f0
...
...
@@ -54,12 +54,14 @@ class GradScaler(AmpScaler):
optimizer = paddle.optimizer.SGD(learning_rate=0.01, parameters=model.parameters())
scaler = paddle.amp.GradScaler(init_loss_scaling=1024)
data = paddle.rand([10, 3, 32, 32])
with paddle.amp.auto_cast():
conv = model(data)
loss = paddle.mean(conv)
scaled = scaler.scale(loss) # scale the loss
scaled.backward() # do backward
scaler.minimize(optimizer, scaled) # update parameters
scaled = scaler.scale(loss) # scale the loss
scaled.backward() # do backward
scaler.minimize(optimizer, scaled) # update parameters
"""
def
__init__
(
self
,
...
...
@@ -86,6 +88,7 @@ class GradScaler(AmpScaler):
The scaled tensor or original tensor.
Examples:
.. code-block:: python
import paddle
...
...
@@ -94,12 +97,14 @@ class GradScaler(AmpScaler):
optimizer = paddle.optimizer.SGD(learning_rate=0.01, parameters=model.parameters())
scaler = paddle.amp.GradScaler(init_loss_scaling=1024)
data = paddle.rand([10, 3, 32, 32])
with paddle.amp.auto_cast():
conv = model(data)
loss = paddle.mean(conv)
scaled = scaler.scale(loss) # scale the loss
scaled.backward() # do backward
scaler.minimize(optimizer, scaled) # update parameters
scaled = scaler.scale(loss) # scale the loss
scaled.backward() # do backward
scaler.minimize(optimizer, scaled) # update parameters
"""
return
super
(
GradScaler
,
self
).
scale
(
var
)
...
...
@@ -118,6 +123,7 @@ class GradScaler(AmpScaler):
kwargs: Keyword arguments, which will be forward to `optimizer.minimize()`.
Examples:
.. code-block:: python
import paddle
...
...
@@ -126,11 +132,13 @@ class GradScaler(AmpScaler):
optimizer = paddle.optimizer.SGD(learning_rate=0.01, parameters=model.parameters())
scaler = paddle.amp.GradScaler(init_loss_scaling=1024)
data = paddle.rand([10, 3, 32, 32])
with paddle.amp.auto_cast():
conv = model(data)
loss = paddle.mean(conv)
scaled = scaler.scale(loss) # scale the loss
scaled.backward() # do backward
scaler.minimize(optimizer, scaled) # update parameters
scaled = scaler.scale(loss) # scale the loss
scaled.backward() # do backward
scaler.minimize(optimizer, scaled) # update parameters
"""
return
super
(
GradScaler
,
self
).
minimize
(
optimizer
,
*
args
,
**
kwargs
)
python/paddle/nn/layer/loss.py
浏览文件 @
98adc8f0
...
...
@@ -491,31 +491,29 @@ class L1Loss(fluid.dygraph.Layer):
If `reduction` is ``'mean'`` or ``'sum'``, the shape of output loss is [1].
Examples:
.. code-block:: python
import paddle
import numpy as np
paddle.disable_static()
input_data = np.array([[1.5, 0.8], [0.2, 1.3]]).astype("float32")
label_data = np.array([[1.7, 1], [0.4, 0.5]]).astype("float32")
input = paddle.to_tensor(input_data)
label = paddle.to_tensor(label_data)
input = paddle.to_tensor([[1.5, 0.8], [0.2, 1.3]])
label = paddle.to_tensor([[1.7, 1.0], [0.4, 0.5]])
l1_loss = paddle.nn.loss.L1Loss()
output = l1_loss(input, label)
print(output
.numpy()
)
print(output)
# [0.35]
l1_loss = paddle.nn.loss.L1Loss(reduction='sum')
output = l1_loss(input, label)
print(output
.numpy()
)
print(output)
# [1.4]
l1_loss = paddle.nn.loss.L1Loss(reduction='none')
output = l1_loss(input, label)
print(output
.numpy()
)
print(output)
# [[0.20000005 0.19999999]
# [0.2 0.79999995]]
#
[0.2 0.79999995]]
"""
def
__init__
(
self
,
reduction
=
'mean'
,
name
=
None
):
...
...
@@ -1001,7 +999,7 @@ class SmoothL1Loss(fluid.dygraph.Layer):
is the same as the shape of input.
Returns:
The tensor
variable
storing the smooth_l1_loss of input and label.
The tensor storing the smooth_l1_loss of input and label.
Return type: Tensor.
...
...
python/paddle/tensor/manipulation.py
浏览文件 @
98adc8f0
...
...
@@ -354,9 +354,6 @@ def roll(x, shifts, axis=None, name=None):
def
stack
(
x
,
axis
=
0
,
name
=
None
):
"""
:alias_main: paddle.stack
:alias: paddle.stack, paddle.tensor.stack, paddle.tensor.manipulation.stack
This OP stacks all the input tensors ``x`` along ``axis`` dimemsion.
All tensors must be of the same shape and same dtype.
...
...
@@ -423,13 +420,12 @@ def stack(x, axis=0, name=None):
import paddle
paddle.disable_static()
x1 = paddle.to_tensor([[1.0, 2.0]])
x2 = paddle.to_tensor([[3.0, 4.0]])
x3 = paddle.to_tensor([[5.0, 6.0]])
out = paddle.stack([x1, x2, x3], axis=0)
print(out.shape) # [3, 1, 2]
print(out
.numpy()
)
print(out)
# [[[1., 2.]],
# [[3., 4.]],
# [[5., 6.]]]
...
...
@@ -459,34 +455,31 @@ def split(x, num_or_sections, axis=0, name=None):
Example:
.. code-block:: python
import numpy as np
import paddle
# x is a Tensor which shape is [3, 9, 5]
x_np = np.random.random([3, 9, 5]).astype("int32")
x = paddle.to_tensor(x_np)
# x is a Tensor of shape [3, 9, 5]
x = paddle.rand([3, 9, 5])
out0, out1, out2
2
= paddle.split(x, num_or_sections=3, axis=1)
# out0.shape
[3, 3, 5]
# out1.shape
[3, 3, 5]
# out2.shape
[3, 3, 5]
out0, out1, out2 = paddle.split(x, num_or_sections=3, axis=1)
print(out0.shape) #
[3, 3, 5]
print(out1.shape) #
[3, 3, 5]
print(out2.shape) #
[3, 3, 5]
out0, out1, out2 = paddle.split(x, num_or_sections=[2, 3, 4], axis=1)
# out0.shape
[3, 2, 5]
# out1.shape
[3, 3, 5]
# out2.shape
[3, 4, 5]
print(out0.shape) #
[3, 2, 5]
print(out1.shape) #
[3, 3, 5]
print(out2.shape) #
[3, 4, 5]
out0, out1, out2 = paddle.split(x, num_or_sections=[2, 3, -1], axis=1)
# out0.shape
[3, 2, 5]
# out1.shape
[3, 3, 5]
# out2.shape
[3, 4, 5]
print(out0.shape) #
[3, 2, 5]
print(out1.shape) #
[3, 3, 5]
print(out2.shape) #
[3, 4, 5]
# axis is negative, the real axis is (rank(x) + axis) which real
# value is 1.
# axis is negative, the real axis is (rank(x) + axis)=1
out0, out1, out2 = paddle.split(x, num_or_sections=3, axis=-2)
# out0.shape
[3, 3, 5]
# out1.shape
[3, 3, 5]
# out2.shape
[3, 3, 5]
print(out0.shape) #
[3, 3, 5]
print(out1.shape) #
[3, 3, 5]
print(out2.shape) #
[3, 3, 5]
"""
return
paddle
.
fluid
.
layers
.
split
(
input
=
x
,
num_or_sections
=
num_or_sections
,
dim
=
axis
,
name
=
name
)
...
...
@@ -494,9 +487,6 @@ def split(x, num_or_sections, axis=0, name=None):
def
squeeze
(
x
,
axis
=
None
,
name
=
None
):
"""
:alias_main: paddle.squeeze
:alias: paddle.squeeze, paddle.tensor.squeeze, paddle.tensor.manipulation.squeeze
This OP will squeeze the dimension(s) of size 1 of input tensor x's shape.
If axis is provided, it will remove the dimension(s) by given axis that of size 1.
...
...
@@ -552,12 +542,10 @@ def squeeze(x, axis=None, name=None):
.. code-block:: python
import paddle
paddle.disable_static()
x = paddle.rand([5, 1, 10])
output = paddle.squeeze(x, axis=1)
# output.shape
[5, 10]
print(output.shape) #
[5, 10]
"""
if
axis
is
None
:
...
...
@@ -695,9 +683,6 @@ def unique(x,
def
unsqueeze
(
x
,
axis
,
name
=
None
):
"""
:alias_main: paddle.unsqueeze
:alias: paddle.unsqueeze, paddle.tensor.unsqueeze, paddle.tensor.manipulation.unsqueeze
Insert single-dimensional entries to the shape of input Tensor ``x``. Takes one
required argument axis, a dimension or list of dimensions that will be inserted.
Dimension indices in axis are as seen in the output tensor.
...
...
@@ -718,7 +703,6 @@ def unsqueeze(x, axis, name=None):
import paddle
paddle.disable_static()
x = paddle.rand([5, 10])
print(x.shape) # [5, 10]
...
...
@@ -728,7 +712,7 @@ def unsqueeze(x, axis, name=None):
out2 = paddle.unsqueeze(x, axis=[0, 2])
print(out2.shape) # [1, 5, 1, 10]
axis = paddle.
fluid.dygraph.to_variable
([0, 1, 2])
axis = paddle.
to_tensor
([0, 1, 2])
out3 = paddle.unsqueeze(x, axis=axis)
print(out3.shape) # [1, 1, 1, 5, 10]
...
...
python/paddle/tensor/random.py
浏览文件 @
98adc8f0
...
...
@@ -59,17 +59,18 @@ def bernoulli(x, name=None):
import paddle
paddle.seed(100) # on CPU device
paddle.set_device('cpu') # on CPU device
paddle.seed(100)
x = paddle.rand([2,3])
print(x
.numpy()
)
# [[0.5535528
0.20714243 0.01162981]
#
[0.51577556 0.36369765 0.2609165
]]
print(x)
# [[0.5535528
1, 0.20714243, 0.01162981],
#
[0.51577556, 0.36369765, 0.26091650
]]
paddle.seed(200) # on CPU device
out = paddle.bernoulli(x)
print(out
.numpy()
)
# [[
0. 0. 0.]
#
[1. 1.
0.]]
print(out)
# [[
1., 0., 1.],
#
[0., 1.,
0.]]
"""
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录