Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
c11c83fb
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看板
未验证
提交
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,7 +609,6 @@ def relu6(x, name=None):
A Tensor with the same data type and shape as ``x`` .
Examples:
.. code-block:: python
import paddle
...
...
@@ -620,7 +619,6 @@ def relu6(x, name=None):
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,7 +657,6 @@ def selu(x,
A Tensor with the same data type and shape as ``x`` .
Examples:
.. code-block:: python
import paddle
...
...
@@ -672,7 +667,6 @@ def selu(x,
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,7 +864,6 @@ 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
...
...
@@ -881,7 +874,6 @@ def softplus(x, beta=1, threshold=20, name=None):
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,7 +913,6 @@ 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
...
...
@@ -933,8 +923,12 @@ def softshrink(x, threshold=0.5, name=None):
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,7 +961,6 @@ def softsign(x, name=None):
A Tensor with the same data type and shape as ``x`` .
Examples:
.. code-block:: python
import paddle
...
...
@@ -978,7 +971,6 @@ def softsign(x, name=None):
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,7 +1000,6 @@ def tanhshrink(x, name=None):
A Tensor with the same data type and shape as ``x`` .
Examples:
.. code-block:: python
import paddle
...
...
@@ -1019,7 +1010,6 @@ def tanhshrink(x, name=None):
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,7 +524,6 @@ class ReLU6(layers.Layer):
- output: Tensor with the same shape as input.
Examples:
.. code-block:: python
import paddle
...
...
@@ -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,7 +563,6 @@ class SELU(layers.Layer):
- output: Tensor with the same shape as input.
Examples:
.. code-block:: python
import paddle
...
...
@@ -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,7 +694,6 @@ class Softplus(layers.Layer):
- output: Tensor with the same shape as input.
Examples:
.. code-block:: python
import paddle
...
...
@@ -707,7 +704,6 @@ class Softplus(layers.Layer):
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,7 +738,6 @@ class Softshrink(layers.Layer):
- output: Tensor with the same shape as input.
Examples:
.. code-block:: python
import paddle
...
...
@@ -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,7 +776,6 @@ class Softsign(layers.Layer):
- output: Tensor with the same shape as input.
Examples:
.. code-block:: python
import paddle
...
...
@@ -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).
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录