Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
56c54ccc
P
Paddle
项目概览
机器未来
/
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看板
未验证
提交
56c54ccc
编写于
4月 15, 2020
作者:
Z
zhupengyang
提交者:
GitHub
4月 15, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Op(prelu/relu/f.relu/f.log_softmax) error message enhancement (#23792)
上级
c2a60bb1
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
75 addition
and
52 deletion
+75
-52
paddle/fluid/operators/prelu_op.cc
paddle/fluid/operators/prelu_op.cc
+10
-5
python/paddle/fluid/tests/unittests/test_activation_op.py
python/paddle/fluid/tests/unittests/test_activation_op.py
+1
-1
python/paddle/fluid/tests/unittests/test_prelu_op.py
python/paddle/fluid/tests/unittests/test_prelu_op.py
+56
-43
python/paddle/nn/functional/activation.py
python/paddle/nn/functional/activation.py
+8
-3
未找到文件。
paddle/fluid/operators/prelu_op.cc
浏览文件 @
56c54ccc
...
@@ -31,10 +31,11 @@ class PReluOp : public framework::OperatorWithKernel {
...
@@ -31,10 +31,11 @@ class PReluOp : public framework::OperatorWithKernel {
auto
x_dim
=
ctx
->
GetInputDim
(
"X"
);
auto
x_dim
=
ctx
->
GetInputDim
(
"X"
);
std
::
string
mode
=
ctx
->
Attrs
().
Get
<
std
::
string
>
(
"mode"
);
std
::
string
mode
=
ctx
->
Attrs
().
Get
<
std
::
string
>
(
"mode"
);
if
(
mode
==
"all"
)
{
if
(
mode
==
"all"
)
{
PADDLE_ENFORCE_EQ
(
PADDLE_ENFORCE_EQ
(
product
(
ctx
->
GetInputDim
(
"Alpha"
)),
1
,
product
(
ctx
->
GetInputDim
(
"Alpha"
)),
1
,
platform
::
errors
::
InvalidArgument
(
platform
::
errors
::
InvalidArgument
(
"For mode 'all', size of weight Alpha must be one."
));
"For mode 'all', size of weight Alpha must be one. "
"But recevied alpha's size: %d."
,
product
(
ctx
->
GetInputDim
(
"Alpha"
))));
}
else
if
(
mode
==
"channel"
)
{
}
else
if
(
mode
==
"channel"
)
{
PADDLE_ENFORCE_EQ
(
product
(
ctx
->
GetInputDim
(
"Alpha"
)),
x_dim
[
1
],
PADDLE_ENFORCE_EQ
(
product
(
ctx
->
GetInputDim
(
"Alpha"
)),
x_dim
[
1
],
platform
::
errors
::
InvalidArgument
(
platform
::
errors
::
InvalidArgument
(
...
@@ -67,7 +68,11 @@ class PReluOp : public framework::OperatorWithKernel {
...
@@ -67,7 +68,11 @@ class PReluOp : public framework::OperatorWithKernel {
"x's size: %d."
,
"x's size: %d."
,
alpha_product
,
x_product
));
alpha_product
,
x_product
));
}
else
{
}
else
{
PADDLE_THROW
(
"Unkown mode %s"
,
mode
);
PADDLE_THROW
(
platform
::
errors
::
InvalidArgument
(
"Attr(mode) of prelu must be one of 'all', 'channel', or 'element'. "
"But recevied "
"mode: '%s'."
,
mode
));
}
}
ctx
->
ShareDim
(
"X"
,
/*->*/
"Out"
);
ctx
->
ShareDim
(
"X"
,
/*->*/
"Out"
);
ctx
->
ShareLoD
(
"X"
,
/*->*/
"Out"
);
ctx
->
ShareLoD
(
"X"
,
/*->*/
"Out"
);
...
...
python/paddle/fluid/tests/unittests/test_activation_op.py
浏览文件 @
56c54ccc
...
@@ -463,7 +463,7 @@ class TestReluOpError(unittest.TestCase):
...
@@ -463,7 +463,7 @@ class TestReluOpError(unittest.TestCase):
def
test_errors
(
self
):
def
test_errors
(
self
):
with
program_guard
(
Program
()):
with
program_guard
(
Program
()):
# The input type must be Variable.
# The input type must be Variable.
self
.
assertRaises
(
TypeError
,
fluid
.
layers
.
sqrt
,
1
)
self
.
assertRaises
(
TypeError
,
fluid
.
layers
.
relu
,
1
)
# The input dtype must be float16, float32, float64.
# The input dtype must be float16, float32, float64.
x_int32
=
fluid
.
data
(
name
=
'x_int32'
,
shape
=
[
12
,
10
],
dtype
=
'int32'
)
x_int32
=
fluid
.
data
(
name
=
'x_int32'
,
shape
=
[
12
,
10
],
dtype
=
'int32'
)
self
.
assertRaises
(
TypeError
,
fluid
.
layers
.
relu
,
x_int32
)
self
.
assertRaises
(
TypeError
,
fluid
.
layers
.
relu
,
x_int32
)
...
...
python/paddle/fluid/tests/unittests/test_prelu_op.py
浏览文件 @
56c54ccc
...
@@ -23,21 +23,18 @@ from paddle.fluid import Program, program_guard
...
@@ -23,21 +23,18 @@ from paddle.fluid import Program, program_guard
from
op_test
import
OpTest
,
skip_check_grad_ci
from
op_test
import
OpTest
,
skip_check_grad_ci
class
TestPRelu
API
Error
(
unittest
.
TestCase
):
class
TestPRelu
Op
Error
(
unittest
.
TestCase
):
def
test_errors
(
self
):
def
test_errors
(
self
):
with
fluid
.
program_guard
(
fluid
.
Program
(),
fluid
.
Program
()):
with
program_guard
(
Program
()):
layer
=
fluid
.
PRelu
(
# The input type must be Variable.
mode
=
'all'
,
self
.
assertRaises
(
TypeError
,
fluid
.
layers
.
prelu
,
0.1
,
'all'
)
param_attr
=
fluid
.
ParamAttr
(
# The input dtype must be float16, float32, float64.
initializer
=
fluid
.
initializer
.
Constant
(
1.0
)))
x_int32
=
fluid
.
data
(
name
=
'x_int32'
,
shape
=
[
12
,
10
],
dtype
=
'int32'
)
# the input must be Variable.
self
.
assertRaises
(
TypeError
,
fluid
.
layers
.
prelu
,
x_int32
,
'all'
)
x0
=
fluid
.
create_lod_tensor
(
# support the input dtype is float32
np
.
array
([
-
1
,
3
,
5
,
5
]),
[[
1
,
1
,
1
,
1
]],
fluid
.
CPUPlace
())
x_fp16
=
fluid
.
layers
.
data
(
self
.
assertRaises
(
TypeError
,
layer
,
x0
)
name
=
'x_fp16'
,
shape
=
[
12
,
10
],
dtype
=
'float32'
)
# the input dtype must be float32
fluid
.
layers
.
prelu
(
x_fp16
,
'all'
)
data_t
=
fluid
.
data
(
name
=
"input"
,
shape
=
[
5
,
200
,
100
,
100
],
dtype
=
"float64"
)
self
.
assertRaises
(
TypeError
,
layer
,
data_t
)
class
PReluTest
(
OpTest
):
class
PReluTest
(
OpTest
):
...
@@ -79,20 +76,18 @@ class PReluTest(OpTest):
...
@@ -79,20 +76,18 @@ class PReluTest(OpTest):
self
.
check_grad
([
'X'
,
'Alpha'
],
'Out'
)
self
.
check_grad
([
'X'
,
'Alpha'
],
'Out'
)
# TODO(minqiyang): Resume these test cases after fixing Python3 CI job issues
@
skip_check_grad_ci
(
if
six
.
PY2
:
@
skip_check_grad_ci
(
reason
=
"[skip shape check] Input(Alpha) must be 1-D and only has one data in 'all' mode"
reason
=
"[skip shape check] Input(Alpha) must be 1-D and only has one data in 'all' mode"
)
)
class
TestModeAll
(
PReluTest
):
class
TestModeAll
(
PReluTest
):
def
init_input_shape
(
self
):
def
init_input_shape
(
self
):
self
.
x_shape
=
(
2
,
3
,
4
,
5
)
self
.
x_shape
=
(
2
,
3
,
4
,
5
)
def
init_attr
(
self
):
def
init_attr
(
self
):
self
.
attrs
=
{
'mode'
:
"all"
}
self
.
attrs
=
{
'mode'
:
"all"
}
class
TestModeElt
(
PReluTest
):
class
TestModeElt
(
PReluTest
):
def
init_input_shape
(
self
):
def
init_input_shape
(
self
):
self
.
x_shape
=
(
3
,
2
,
5
,
10
)
self
.
x_shape
=
(
3
,
2
,
5
,
10
)
...
@@ -100,18 +95,36 @@ if six.PY2:
...
@@ -100,18 +95,36 @@ if six.PY2:
self
.
attrs
=
{
'mode'
:
"element"
}
self
.
attrs
=
{
'mode'
:
"element"
}
class
TestPReluOpError
(
unittest
.
TestCase
):
def
prelu_t
(
x
,
mode
,
param_attr
=
None
,
name
=
None
):
def
test_errors
(
self
):
helper
=
fluid
.
layer_helper
.
LayerHelper
(
'prelu'
,
**
locals
())
with
program_guard
(
Program
()):
alpha_shape
=
[
1
,
x
.
shape
[
1
],
1
,
1
]
# The input type must be Variable.
dtype
=
helper
.
input_dtype
(
input_param_name
=
'x'
)
self
.
assertRaises
(
TypeError
,
fluid
.
layers
.
prelu
,
1
,
'all'
)
alpha
=
helper
.
create_parameter
(
# The input dtype must be float16, float32, float64.
attr
=
helper
.
param_attr
,
x_int32
=
fluid
.
data
(
name
=
'x_int32'
,
shape
=
[
12
,
10
],
dtype
=
'int32'
)
shape
=
alpha_shape
,
self
.
assertRaises
(
TypeError
,
fluid
.
layers
.
prelu
,
x_int32
,
'all'
)
dtype
=
'float32'
,
# support the input dtype is float32
is_bias
=
False
,
x_fp16
=
fluid
.
layers
.
data
(
default_initializer
=
fluid
.
initializer
.
ConstantInitializer
(
0.25
))
name
=
'x_fp16'
,
shape
=
[
12
,
10
],
dtype
=
'float32'
)
out
=
helper
.
create_variable_for_type_inference
(
dtype
)
fluid
.
layers
.
prelu
(
x_fp16
,
'all'
)
helper
.
append_op
(
type
=
"prelu"
,
inputs
=
{
"X"
:
x
,
'Alpha'
:
alpha
},
attrs
=
{
"mode"
:
mode
},
outputs
=
{
"Out"
:
out
})
return
out
# error message test if mode is not one of 'all', 'channel', 'element'
class
TestModeError
(
unittest
.
TestCase
):
def
test_mode_error
(
self
):
main_program
=
Program
()
with
fluid
.
program_guard
(
main_program
,
Program
()):
x
=
fluid
.
data
(
name
=
'x'
,
shape
=
[
2
,
3
,
4
,
5
])
try
:
y
=
prelu_t
(
x
,
'any'
)
except
Exception
as
e
:
assert
(
e
.
args
[
0
].
find
(
'InvalidArgumentError'
)
!=
-
1
)
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
...
...
python/paddle/nn/functional/activation.py
浏览文件 @
56c54ccc
...
@@ -206,8 +206,10 @@ def relu(input, inplace=False, name=None):
...
@@ -206,8 +206,10 @@ def relu(input, inplace=False, name=None):
)
)
return
core
.
ops
.
relu
(
input
)
return
core
.
ops
.
relu
(
input
)
helper
=
LayerHelper
(
'relu'
,
**
locals
())
check_variable_and_dtype
(
input
,
'input'
,
[
'float16'
,
'float32'
,
'float64'
],
'relu'
)
helper
=
LayerHelper
(
'relu'
,
**
locals
())
outs
=
input
if
inplace
else
helper
.
create_variable_for_type_inference
(
outs
=
input
if
inplace
else
helper
.
create_variable_for_type_inference
(
input
.
dtype
)
input
.
dtype
)
helper
.
append_op
(
type
=
'relu'
,
inputs
=
{
'X'
:
[
input
]},
outputs
=
{
'Out'
:
outs
})
helper
.
append_op
(
type
=
'relu'
,
inputs
=
{
'X'
:
[
input
]},
outputs
=
{
'Out'
:
outs
})
...
@@ -263,7 +265,7 @@ def sigmoid(input, inplace=False, name=None):
...
@@ -263,7 +265,7 @@ def sigmoid(input, inplace=False, name=None):
)
)
return
core
.
ops
.
sigmoid
(
input
)
return
core
.
ops
.
sigmoid
(
input
)
check_variable_and_dtype
(
input
,
'
X
'
,
[
'float16'
,
'float32'
,
'float64'
],
check_variable_and_dtype
(
input
,
'
input
'
,
[
'float16'
,
'float32'
,
'float64'
],
'sigmoid'
)
'sigmoid'
)
helper
=
LayerHelper
(
"sigmoid"
,
**
locals
())
helper
=
LayerHelper
(
"sigmoid"
,
**
locals
())
outputs
=
helper
.
create_variable_for_type_inference
(
input
.
dtype
)
outputs
=
helper
.
create_variable_for_type_inference
(
input
.
dtype
)
...
@@ -329,8 +331,11 @@ def log_softmax(input, axis=None, dtype=None, name=None):
...
@@ -329,8 +331,11 @@ def log_softmax(input, axis=None, dtype=None, name=None):
False
)
False
)
return
core
.
ops
.
log
(
outs_softmax
)
return
core
.
ops
.
log
(
outs_softmax
)
helper
=
LayerHelper
(
"log_softmax"
,
**
locals
())
if
dtype
is
None
:
check_variable_and_dtype
(
input
,
'input'
,
[
'float16'
,
'float32'
,
'float64'
],
'log_softmax'
)
helper
=
LayerHelper
(
"log_softmax"
,
**
locals
())
outs_cast
=
input
outs_cast
=
input
if
dtype
is
not
None
:
if
dtype
is
not
None
:
outs_cast
=
helper
.
create_variable_for_type_inference
(
dtype
)
outs_cast
=
helper
.
create_variable_for_type_inference
(
dtype
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录