Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
c11c83fb
P
Paddle
项目概览
Crayon鑫
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
c11c83fb
编写于
8月 22, 2020
作者:
H
hong19860320
提交者:
GitHub
8月 22, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add the parameter checking for softplus and fix the doc string (#26530)
上级
0ca10d31
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
111 addition
and
124 deletion
+111
-124
python/paddle/fluid/tests/unittests/test_activation_op.py
python/paddle/fluid/tests/unittests/test_activation_op.py
+3
-0
python/paddle/nn/functional/activation.py
python/paddle/nn/functional/activation.py
+58
-68
python/paddle/nn/layer/activation.py
python/paddle/nn/layer/activation.py
+50
-56
未找到文件。
python/paddle/fluid/tests/unittests/test_activation_op.py
浏览文件 @
c11c83fb
...
...
@@ -717,6 +717,9 @@ class TestSoftshrinkAPI(unittest.TestCase):
# The input dtype must be float16, float32, float64.
x_int32
=
paddle
.
data
(
name
=
'x_int32'
,
shape
=
[
12
,
10
],
dtype
=
'int32'
)
self
.
assertRaises
(
TypeError
,
F
.
softshrink
,
x_int32
)
# The threshold must be no less than zero
x_fp32
=
paddle
.
data
(
name
=
'x_fp32'
,
shape
=
[
12
,
10
],
dtype
=
'float32'
)
self
.
assertRaises
(
ValueError
,
F
.
softshrink
,
x_fp32
,
-
1.0
)
# support the input dtype is float16
x_fp16
=
paddle
.
data
(
name
=
'x_fp16'
,
shape
=
[
12
,
10
],
dtype
=
'float16'
)
F
.
softshrink
(
x_fp16
)
...
...
python/paddle/nn/functional/activation.py
浏览文件 @
c11c83fb
...
...
@@ -225,7 +225,7 @@ def hardtanh(x, min=-1.0, max=1.0, name=None):
x,
\\
text{otherwise}
\\
end{cases}
Arg
s:
Parameter
s:
x (Tensor): The input Tensor with data type float32, float64.
min (float, optional): The minimum value of the linear region range. Default is -1.
max (float, optional): The maximum value of the linear region range. Default is 1.
...
...
@@ -598,9 +598,9 @@ def relu6(x, name=None):
.. math::
\t
ext{relu6}(x) = \min(\
max(0,x), 6)
relu6(x) = min(
max(0,x), 6)
Arg
s:
Parameter
s:
x (Tensor): The input Tensor with data type float32, float64.
name (str, optional): Name for the operation (optional, default is None).
For more information, please refer to :ref:`api_guide_Name`.
...
...
@@ -609,18 +609,16 @@ def relu6(x, name=None):
A Tensor with the same data type and shape as ``x`` .
Examples:
.. code-block:: python
import paddle
import paddle.nn.functional as F
import numpy as np
paddle.disable_static()
import paddle
import paddle.nn.functional as F
import numpy as np
x = paddle.to_tensor(np.array([-1, 0.3, 6.5]))
out = F.relu6(x) # [0, 0.3, 6]
paddle.disable_static()
x = paddle.to_tensor(np.array([-1, 0.3, 6.5]))
out = F.relu6(x) # [0, 0.3, 6]
"""
threshold
=
6.0
if
in_dygraph_mode
():
...
...
@@ -646,11 +644,9 @@ def selu(x,
.. math::
\t
ext{selu}(x) = scale * (\max(0,x) + \min(0,
\a
lpha * (\exp(x) - 1))),
\\
with\,alpha=1.6732632423543772848170429916717 and
\\
scale=1.0507009873554804934193349852946
selu(x) = scale * (max(0,x) + min(0, alpha * (e^{x} - 1)))
Arg
s:
Parameter
s:
x (Tensor): The input Tensor with data type float32, float64.
scale (float, optional): The value of scale for selu. Default is 1.0507009873554804934193349852946
alpha (float, optional): The value of alpha for selu. Default is 1.6732632423543772848170429916717
...
...
@@ -661,18 +657,16 @@ def selu(x,
A Tensor with the same data type and shape as ``x`` .
Examples:
.. code-block:: python
import paddle
import paddle.nn.functional as F
import numpy as np
paddle.disable_static()
import paddle
import paddle.nn.functional as F
import numpy as np
x = paddle.to_tensor(np.array([[0, 1],[2, 3]]))
out = F.selu(x) # [[0, 1.050701],[2.101402, 3.152103]]
paddle.disable_static()
x = paddle.to_tensor(np.array([[0, 1],[2, 3]]))
out = F.selu(x) # [[0, 1.050701],[2.101402, 3.152103]]
"""
if
in_dygraph_mode
():
return
core
.
ops
.
selu
(
x
,
'scale'
,
scale
,
'alpha'
,
alpha
)
...
...
@@ -856,10 +850,10 @@ def softplus(x, beta=1, threshold=20, name=None):
.. math::
\t
ext{softplus}(x) =
\f
rac{1}{
\b
eta} * \log(1 + \exp(
\b
eta * x))
\\
\
t
ext{For numerical stability, the implementation reverts to the linear function when :}\,x
\t
imes
\b
eta > threshold.
softplus(x) =
\\
frac{1}{beta} *
\\
log(1 + e^{beta * x})
\\
\\
\
\
text{For numerical stability, the implementation reverts to the linear function when: beta * x > threshold.}
Arg
s:
Parameter
s:
x (Tensor): The input Tensor with data type float32, float64.
beta (float, optional): The value of beta for softplus. Default is 1
threshold (float, optional): The value of threshold for softplus. Default is 20
...
...
@@ -870,18 +864,16 @@ def softplus(x, beta=1, threshold=20, name=None):
A Tensor with the same data type and shape as ``x`` .
Examples:
.. code-block:: python
import paddle
import paddle.nn.functional as F
import numpy as np
paddle.disable_static()
import paddle
import paddle.nn.functional as F
import numpy as np
x = paddle.to_tensor(np.array([-0.4, -0.2, 0.1, 0.3]))
out = F.softplus(x) # [0.513015, 0.598139, 0.744397, 0.854355]
paddle.disable_static()
x = paddle.to_tensor(np.array([-0.4, -0.2, 0.1, 0.3]))
out = F.softplus(x) # [0.513015, 0.598139, 0.744397, 0.854355]
"""
if
in_dygraph_mode
():
return
core
.
ops
.
softplus
(
x
,
'beta'
,
beta
,
'threshold'
,
threshold
)
...
...
@@ -905,14 +897,13 @@ def softshrink(x, threshold=0.5, name=None):
.. math::
\t
ext{softshrink}(x) =
\b
egin{cases}
x - threshold, &
\t
ext{ if } x > threshold
\\
x + threshold, &
\t
ext{ if } x < -threshold
\\
0, &
\t
ext{ otherwise }
\end{cases}
softshrink(x)=
\\
begin{cases}
x - threshold,
\\
text{if } x > threshold
\\\\
x + threshold,
\\
text{if } x < -threshold
\\\\
0,
\\
text{otherwise}
\\
end{cases}
Arg
s:
Parameter
s:
x (Tensor): The input Tensor with data type float32, float64.
threshold (float, optional): The value of threshold(must be no less than zero) for softplus. Default is 0.5
name (str, optional): Name for the operation (optional, default is None).
...
...
@@ -922,19 +913,22 @@ def softshrink(x, threshold=0.5, name=None):
A Tensor with the same data type and shape as ``x`` .
Examples:
.. code-block:: python
import paddle
import paddle.nn.functional as F
import numpy as np
paddle.disable_static()
import paddle
import paddle.nn.functional as F
import numpy as np
x = paddle.to_tensor(np.array([-0.9, -0.2, 0.1, 0.8]))
out = F.softshrink(x) # [-0.4, 0, 0, 0.3]
paddle.disable_static()
x = paddle.to_tensor(np.array([-0.9, -0.2, 0.1, 0.8]))
out = F.softshrink(x) # [-0.4, 0, 0, 0.3]
"""
if
threshold
<
0
:
raise
ValueError
(
"The threshold must be no less than zero. Received: {}."
.
format
(
threshold
))
if
in_dygraph_mode
():
return
core
.
ops
.
softshrink
(
x
,
'lambda'
,
threshold
)
...
...
@@ -956,9 +950,9 @@ def softsign(x, name=None):
.. math::
\t
ext{softsign}(x) =
\f
rac{x}{1 + |x|}
softsign(x) =
\
\
frac{x}{1 + |x|}
Arg
s:
Parameter
s:
x (Tensor): The input Tensor with data type float32, float64.
name (str, optional): Name for the operation (optional, default is None).
For more information, please refer to :ref:`api_guide_Name`.
...
...
@@ -967,18 +961,16 @@ def softsign(x, name=None):
A Tensor with the same data type and shape as ``x`` .
Examples:
.. code-block:: python
import paddle
import paddle.nn.functional as F
import numpy as np
paddle.disable_static()
import paddle
import paddle.nn.functional as F
import numpy as np
x = paddle.to_tensor(np.array([-0.4, -0.2, 0.1, 0.3]))
out = F.softsign(x) # [-0.285714, -0.166667, 0.0909091, 0.230769]
paddle.disable_static()
x = paddle.to_tensor(np.array([-0.4, -0.2, 0.1, 0.3]))
out = F.softsign(x) # [-0.285714, -0.166667, 0.0909091, 0.230769]
"""
if
in_dygraph_mode
():
return
core
.
ops
.
softsign
(
x
)
...
...
@@ -997,7 +989,7 @@ def tanhshrink(x, name=None):
.. math::
\t
ext{tanhshrink}(x) = x -
\t
ext{tanh}
(x)
tanhshrink(x) = x - tanh
(x)
Args:
x (Tensor): The input Tensor with data type float32, float64.
...
...
@@ -1008,18 +1000,16 @@ def tanhshrink(x, name=None):
A Tensor with the same data type and shape as ``x`` .
Examples:
.. code-block:: python
import paddle
import paddle.nn.functional as F
import numpy as np
paddle.disable_static()
import paddle
import paddle.nn.functional as F
import numpy as np
x = paddle.to_tensor(np.array([-0.4, -0.2, 0.1, 0.3]))
out = F.tanhshrink(x) # [-0.020051, -0.00262468, 0.000332005, 0.00868739]
paddle.disable_static()
x = paddle.to_tensor(np.array([-0.4, -0.2, 0.1, 0.3]))
out = F.tanhshrink(x) # [-0.020051, -0.00262468, 0.000332005, 0.00868739]
"""
if
in_dygraph_mode
():
return
core
.
ops
.
tanh_shrink
(
x
)
...
...
python/paddle/nn/layer/activation.py
浏览文件 @
c11c83fb
...
...
@@ -513,7 +513,7 @@ class ReLU6(layers.Layer):
.. math::
\t
ext{ReLU6}(x) = \min(\
max(0,x), 6)
ReLU6(x) = min(
max(0,x), 6)
Parameters:
name (str, optional): Name for the operation (optional, default is None).
...
...
@@ -524,17 +524,16 @@ class ReLU6(layers.Layer):
- output: Tensor with the same shape as input.
Examples:
.. code-block:: python
import paddle
import numpy as np
import paddle
import numpy as np
paddle.disable_static()
paddle.disable_static()
x = paddle.to_tensor(np.array([-1, 0.3, 6.5]))
m = paddle.nn.ReLU6()
out = m(x) # [0, 0.3, 6]
x = paddle.to_tensor(np.array([-1, 0.3, 6.5]))
m = paddle.nn.ReLU6()
out = m(x) # [0, 0.3, 6]
"""
def
__init__
(
self
,
name
=
None
):
...
...
@@ -551,9 +550,7 @@ class SELU(layers.Layer):
.. math::
\t
ext{SELU}(x) = scale * (\max(0,x) + \min(0,
\a
lpha * (\exp(x) - 1))),
\\
with\,alpha=1.6732632423543772848170429916717 and
\\
scale=1.0507009873554804934193349852946
SELU(x) = scale * (max(0,x) + min(0, alpha * (e^{x} - 1)))
Parameters:
scale (float, optional): The value of scale for SELU. Default is 1.0507009873554804934193349852946
...
...
@@ -566,17 +563,16 @@ class SELU(layers.Layer):
- output: Tensor with the same shape as input.
Examples:
.. code-block:: python
import paddle
import numpy as np
import paddle
import numpy as np
paddle.disable_static()
paddle.disable_static()
x = paddle.to_tensor(np.array([[0, 1],[2, 3]]))
m = paddle.nn.SELU()
out = m(x) # [[0, 1.050701],[2.101402, 3.152103]]
x = paddle.to_tensor(np.array([[0, 1],[2, 3]]))
m = paddle.nn.SELU()
out = m(x) # [[0, 1.050701],[2.101402, 3.152103]]
"""
def
__init__
(
self
,
...
...
@@ -684,10 +680,12 @@ class Softplus(layers.Layer):
.. math::
\t
ext{Softplus}(x) =
\f
rac{1}{
\b
eta} * \log(1 + \exp(
\b
eta * x))
\\
\
t
ext{For numerical stability, the implementation reverts to the linear function when :}\,x
\t
imes
\b
eta > threshold.
Softplus(x) =
\\
frac{1}{beta} *
\\
log(1 + e^{beta * x})
\\
\\
\
\
text{For numerical stability, the implementation reverts to the linear function when: beta * x > threshold.}
Parameters:
beta (float, optional): The value of beta for Softplus. Default is 1
threshold (float, optional): The value of threshold for Softplus. Default is 20
name (str, optional): Name for the operation (optional, default is None).
For more information, please refer to :ref:`api_guide_Name`.
...
...
@@ -696,18 +694,16 @@ class Softplus(layers.Layer):
- output: Tensor with the same shape as input.
Examples:
.. code-block:: python
import paddle
import numpy as np
paddle.disable_static()
import paddle
import numpy as np
x = paddle.to_tensor(np.array([-0.4, -0.2, 0.1, 0.3]))
m = paddle.nn.Softplus()
out = m(x) # [0.513015, 0.598139, 0.744397, 0.854355]
paddle.disable_static()
x = paddle.to_tensor(np.array([-0.4, -0.2, 0.1, 0.3]))
m = paddle.nn.Softplus()
out = m(x) # [0.513015, 0.598139, 0.744397, 0.854355]
"""
def
__init__
(
self
,
beta
=
1
,
threshold
=
20
,
name
=
None
):
...
...
@@ -726,14 +722,14 @@ class Softshrink(layers.Layer):
.. math::
\t
ext{Softshrink}(x) =
\b
egin{cases}
x - threshold, &
\t
ext{ if } x > threshold
\\
x + threshold, &
\t
ext{ if } x < -threshold
\\
0, &
\t
ext{ otherwise }
\end{cases}
Softshrink(x)=
\\
begin{cases}
x - threshold,
\\
text{if } x > threshold
\\\\
x + threshold,
\\
text{if } x < -threshold
\\\\
0,
\\
text{otherwise}
\\
end{cases}
Parameters:
threshold (float, optional): The value of threshold(must be no less than zero) for softplus. Default is 0.5
name (str, optional): Name for the operation (optional, default is None).
For more information, please refer to :ref:`api_guide_Name`.
...
...
@@ -742,17 +738,16 @@ class Softshrink(layers.Layer):
- output: Tensor with the same shape as input.
Examples:
.. code-block:: python
import paddle
import numpy as np
import paddle
import numpy as np
paddle.disable_static()
paddle.disable_static()
x = paddle.to_tensor(np.array([-0.9, -0.2, 0.1, 0.8]))
m = paddle.nn.Softshrink()
out = m(x) # [-0.4, 0, 0, 0.3]
x = paddle.to_tensor(np.array([-0.9, -0.2, 0.1, 0.8]))
m = paddle.nn.Softshrink()
out = m(x) # [-0.4, 0, 0, 0.3]
"""
def
__init__
(
self
,
threshold
=
0.5
,
name
=
None
):
...
...
@@ -770,7 +765,7 @@ class Softsign(layers.Layer):
.. math::
\t
ext{Softsign}(x) =
\f
rac{x}{1 + |x|}
Softsign(x) =
\
\
frac{x}{1 + |x|}
Parameters:
name (str, optional): Name for the operation (optional, default is None).
...
...
@@ -781,17 +776,16 @@ class Softsign(layers.Layer):
- output: Tensor with the same shape as input.
Examples:
.. code-block:: python
import paddle
import numpy as np
import paddle
import numpy as np
paddle.disable_static()
paddle.disable_static()
x = paddle.to_tensor(np.array([-0.4, -0.2, 0.1, 0.3]))
m = paddle.nn.Softsign()
out = m(x) # [-0.285714, -0.166667, 0.0909091, 0.230769]
x = paddle.to_tensor(np.array([-0.4, -0.2, 0.1, 0.3]))
m = paddle.nn.Softsign()
out = m(x) # [-0.285714, -0.166667, 0.0909091, 0.230769]
"""
def
__init__
(
self
,
name
=
None
):
...
...
@@ -808,7 +802,7 @@ class Tanhshrink(layers.Layer):
.. math::
\t
ext{Tanhshrink}(x) = x -
\t
ext{Tanh}
(x)
Tanhshrink(x) = x - tanh
(x)
Parameters:
name (str, optional): Name for the operation (optional, default is None).
...
...
@@ -821,14 +815,14 @@ class Tanhshrink(layers.Layer):
Examples:
.. code-block:: python
import paddle
import numpy as np
import paddle
import numpy as np
paddle.disable_static()
paddle.disable_static()
x = paddle.to_tensor(np.array([-0.4, -0.2, 0.1, 0.3]))
m = paddle.nn.Tanhshrink()
out = m(x) # [-0.020051, -0.00262468, 0.000332005, 0.00868739]
x = paddle.to_tensor(np.array([-0.4, -0.2, 0.1, 0.3]))
m = paddle.nn.Tanhshrink()
out = m(x) # [-0.020051, -0.00262468, 0.000332005, 0.00868739]
"""
def
__init__
(
self
,
name
=
None
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录