Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
cec234b1
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看板
未验证
提交
cec234b1
编写于
4月 10, 2020
作者:
S
silingtong123
提交者:
GitHub
4月 10, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
test=develop, error message of tree_conv OP enhancement (#23574)
上级
7277df47
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
112 addition
and
14 deletion
+112
-14
paddle/fluid/operators/tree_conv_op.cc
paddle/fluid/operators/tree_conv_op.cc
+62
-13
python/paddle/fluid/contrib/layers/nn.py
python/paddle/fluid/contrib/layers/nn.py
+3
-0
python/paddle/fluid/dygraph/nn.py
python/paddle/fluid/dygraph/nn.py
+2
-0
python/paddle/fluid/tests/unittests/test_tree_conv_op.py
python/paddle/fluid/tests/unittests/test_tree_conv_op.py
+45
-1
未找到文件。
paddle/fluid/operators/tree_conv_op.cc
浏览文件 @
cec234b1
...
@@ -60,40 +60,78 @@ class TreeConvOp : public framework::OperatorWithKernel {
...
@@ -60,40 +60,78 @@ class TreeConvOp : public framework::OperatorWithKernel {
public:
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
));
OP_INOUT_CHECK
(
ctx
->
HasInput
(
"NodesVector"
),
"Input"
,
"NodesVector"
,
"TreeConv"
);
OP_INOUT_CHECK
(
ctx
->
HasInput
(
"Filter"
),
"Input"
,
"Filter"
,
"TreeConv"
);
OP_INOUT_CHECK
(
ctx
->
HasInput
(
"EdgeSet"
),
"Input"
,
"EdgeSet"
,
"TreeConv"
);
OP_INOUT_CHECK
(
ctx
->
HasOutput
(
"Out"
),
"Output"
,
"Out"
,
"TreeConv"
);
auto
edge_dims
=
ctx
->
GetInputDim
(
"EdgeSet"
);
auto
edge_dims
=
ctx
->
GetInputDim
(
"EdgeSet"
);
auto
vector_dims
=
ctx
->
GetInputDim
(
"NodesVector"
);
auto
vector_dims
=
ctx
->
GetInputDim
(
"NodesVector"
);
auto
filter_dims
=
ctx
->
GetInputDim
(
"Filter"
);
auto
filter_dims
=
ctx
->
GetInputDim
(
"Filter"
);
if
(
ctx
->
IsRuntime
())
{
if
(
ctx
->
IsRuntime
())
{
PADDLE_ENFORCE_EQ
(
edge_dims
[
2
],
2
,
"Input(EdgeSet) dim[2] should be 2"
);
PADDLE_ENFORCE_EQ
(
edge_dims
[
2
],
2
,
platform
::
errors
::
InvalidArgument
(
"Input(EdgeSet) dim[2] should be 2. "
"But received Input(EdgeSet) dim[2] is %d."
,
edge_dims
[
2
]));
}
else
{
}
else
{
if
(
edge_dims
[
2
]
!=
-
1
)
{
if
(
edge_dims
[
2
]
!=
-
1
)
{
PADDLE_ENFORCE_EQ
(
edge_dims
[
2
],
2
,
"Input(EdgeSet) dim[2] should be 2"
);
PADDLE_ENFORCE_EQ
(
edge_dims
[
2
],
2
,
platform
::
errors
::
InvalidArgument
(
"Input(EdgeSet) dim[2] should be 2. "
"But received Input(EdgeSet) dim[2] is %d."
,
edge_dims
[
2
]));
}
}
}
}
PADDLE_ENFORCE_EQ
(
edge_dims
.
size
(),
3
,
PADDLE_ENFORCE_EQ
(
edge_dims
.
size
(),
3
,
"The dimension of EdgeSet Tensor should be 3"
);
platform
::
errors
::
InvalidArgument
(
PADDLE_ENFORCE_EQ
(
vector_dims
.
size
(),
3
,
"The dimension of EdgeSet Tensor should be 3. "
"The dimension of NodesVector Tensor should be 3"
);
"But received the dimension of EdgeSet Tensor is %d."
,
edge_dims
.
size
()));
PADDLE_ENFORCE_EQ
(
vector_dims
.
size
(),
3
,
platform
::
errors
::
InvalidArgument
(
"The dimension of NodesVector Tensor should be 3. "
"But received the dimension of NodesVector Tensor is %d."
,
vector_dims
.
size
()));
PADDLE_ENFORCE_EQ
(
filter_dims
.
size
(),
4
,
PADDLE_ENFORCE_EQ
(
filter_dims
.
size
(),
4
,
"The dimension of Filter Tensor should be 4"
);
platform
::
errors
::
InvalidArgument
(
"The dimension of Filter Tensor should be 4. "
"But received the dimension of Filter Tensor is %d."
,
filter_dims
.
size
()));
if
(
ctx
->
IsRuntime
())
{
if
(
ctx
->
IsRuntime
())
{
PADDLE_ENFORCE_EQ
(
filter_dims
[
1
],
3
,
"Input(Filter) dim[1] should be 3"
);
PADDLE_ENFORCE_EQ
(
filter_dims
[
1
],
3
,
platform
::
errors
::
InvalidArgument
(
"Input(Filter) dim[1] should be 3. "
"But received Input(Filter) dim[1] is %d."
,
filter_dims
[
1
]));
PADDLE_ENFORCE_EQ
(
PADDLE_ENFORCE_EQ
(
filter_dims
[
0
],
vector_dims
[
2
],
filter_dims
[
0
],
vector_dims
[
2
],
"Input(Filter) dim[0] must equal to Input(NodesVector) dim[2]"
);
platform
::
errors
::
InvalidArgument
(
"Input(Filter) dim[0] must equal to Input(NodesVector) dim[2]. "
"But received Input(Filter) dim[0] = %d, Input(NodesVector) "
"dim[2] = %d."
,
filter_dims
[
0
],
vector_dims
[
2
]));
}
else
{
}
else
{
if
(
filter_dims
[
1
]
!=
-
1
)
{
if
(
filter_dims
[
1
]
!=
-
1
)
{
PADDLE_ENFORCE_EQ
(
filter_dims
[
1
],
3
,
PADDLE_ENFORCE_EQ
(
filter_dims
[
1
],
3
,
"Input(Filter) dim[1] should be 3"
);
platform
::
errors
::
InvalidArgument
(
"Input(Filter) dim[1] should be 3. "
"But received Input(Filter) dim[1] is %d."
,
filter_dims
[
1
]));
}
}
if
(
filter_dims
[
0
]
!=
-
1
&&
vector_dims
[
2
]
!=
-
1
)
{
if
(
filter_dims
[
0
]
!=
-
1
&&
vector_dims
[
2
]
!=
-
1
)
{
PADDLE_ENFORCE_EQ
(
PADDLE_ENFORCE_EQ
(
filter_dims
[
0
],
vector_dims
[
2
],
filter_dims
[
0
],
vector_dims
[
2
],
"Input(Filter) dim[0] must equal to Input(NodesVector) dim[2]"
);
platform
::
errors
::
InvalidArgument
(
"Input(Filter) dim[0] must equal to Input(NodesVector) dim[2]. "
"But received Input(Filter) dim[0] = %d, Input(NodesVector) "
"dim[2] = %d."
,
filter_dims
[
0
],
vector_dims
[
2
]));
}
}
}
}
auto
output_dims
=
framework
::
make_ddim
(
auto
output_dims
=
framework
::
make_ddim
(
...
@@ -137,10 +175,21 @@ class TreeConvGradOp : public framework::OperatorWithKernel {
...
@@ -137,10 +175,21 @@ class TreeConvGradOp : public framework::OperatorWithKernel {
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
OP_INOUT_CHECK
(
ctx
->
HasInput
(
"Filter"
),
"Input"
,
"Filter"
,
"grad_TreeConv"
);
OP_INOUT_CHECK
(
ctx
->
HasInput
(
"EdgeSet"
),
"Input"
,
"EdgeSet"
,
"grad_TreeConv"
);
OP_INOUT_CHECK
(
ctx
->
HasInput
(
"NodesVector"
),
"Input"
,
"NodesVector"
,
"grad_TreeConv"
);
OP_INOUT_CHECK
(
ctx
->
HasInput
(
framework
::
GradVarName
(
"Out"
)),
"Input"
,
framework
::
GradVarName
(
"Out"
),
"grad_TreeConv"
);
OP_INOUT_CHECK
(
ctx
->
HasOutput
(
framework
::
GradVarName
(
"NodesVector"
)),
"Output"
,
framework
::
GradVarName
(
"NodesVector"
),
"grad_TreeConv"
);
OP_INOUT_CHECK
(
ctx
->
HasOutput
(
framework
::
GradVarName
(
"Filter"
)),
"Output"
,
framework
::
GradVarName
(
"Filter"
),
"grad_TreeConv"
);
auto
vectors_dims
=
ctx
->
GetInputDim
(
"NodesVector"
);
auto
vectors_dims
=
ctx
->
GetInputDim
(
"NodesVector"
);
auto
filter_dims
=
ctx
->
GetInputDim
(
"Filter"
);
auto
filter_dims
=
ctx
->
GetInputDim
(
"Filter"
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
framework
::
GradVarName
(
"Out"
)),
"the gradient of output(Out) must not be null"
);
if
(
ctx
->
HasOutput
(
framework
::
GradVarName
(
"Filter"
)))
{
if
(
ctx
->
HasOutput
(
framework
::
GradVarName
(
"Filter"
)))
{
ctx
->
SetOutputDim
(
framework
::
GradVarName
(
"Filter"
),
filter_dims
);
ctx
->
SetOutputDim
(
framework
::
GradVarName
(
"Filter"
),
filter_dims
);
}
}
...
...
python/paddle/fluid/contrib/layers/nn.py
浏览文件 @
cec234b1
...
@@ -419,6 +419,9 @@ def tree_conv(nodes_vector,
...
@@ -419,6 +419,9 @@ def tree_conv(nodes_vector,
# also output tensor could be pooling(the pooling in paper called global pooling)
# also output tensor could be pooling(the pooling in paper called global pooling)
pooled = fluid.layers.reduce_max(out_vector, dim=2) # global pooling
pooled = fluid.layers.reduce_max(out_vector, dim=2) # global pooling
"""
"""
check_type
(
nodes_vector
,
'nodes_vector'
,
(
Variable
),
'tree_conv'
)
check_type
(
edge_set
,
'edge_set'
,
(
Variable
),
'tree_conv'
)
helper
=
LayerHelper
(
"tree_conv"
,
**
locals
())
helper
=
LayerHelper
(
"tree_conv"
,
**
locals
())
dtype
=
helper
.
input_dtype
(
'nodes_vector'
)
dtype
=
helper
.
input_dtype
(
'nodes_vector'
)
feature_size
=
nodes_vector
.
shape
[
2
]
feature_size
=
nodes_vector
.
shape
[
2
]
...
...
python/paddle/fluid/dygraph/nn.py
浏览文件 @
cec234b1
...
@@ -2949,6 +2949,8 @@ class TreeConv(layers.Layer):
...
@@ -2949,6 +2949,8 @@ class TreeConv(layers.Layer):
is_bias
=
False
)
is_bias
=
False
)
def
forward
(
self
,
nodes_vector
,
edge_set
):
def
forward
(
self
,
nodes_vector
,
edge_set
):
check_type
(
nodes_vector
,
'nodes_vector'
,
(
Variable
),
'TreeConv'
)
check_type
(
edge_set
,
'edge_set'
,
(
Variable
),
'TreeConv'
)
if
self
.
_name
:
if
self
.
_name
:
out
=
self
.
create_variable
(
out
=
self
.
create_variable
(
name
=
self
.
_name
,
dtype
=
self
.
_dtype
,
persistable
=
False
)
name
=
self
.
_name
,
dtype
=
self
.
_dtype
,
persistable
=
False
)
...
...
python/paddle/fluid/tests/unittests/test_tree_conv_op.py
浏览文件 @
cec234b1
...
@@ -13,8 +13,10 @@
...
@@ -13,8 +13,10 @@
# limitations under the License.
# limitations under the License.
import
numpy
as
np
import
numpy
as
np
from
paddle.fluid.framework
import
program_guard
,
Program
from
op_test
import
OpTest
from
op_test
import
OpTest
import
unittest
import
paddle.fluid
as
fluid
def
collect_node_patch
(
og
,
max_depth
):
def
collect_node_patch
(
og
,
max_depth
):
...
@@ -118,3 +120,45 @@ class TestTreeConvOp(OpTest):
...
@@ -118,3 +120,45 @@ class TestTreeConvOp(OpTest):
],
],
axis
=
0
)
axis
=
0
)
return
vec
return
vec
class
TestTreeConv_OpError
(
unittest
.
TestCase
):
def
test_errors
(
self
):
with
program_guard
(
Program
(),
Program
()):
nodes_vector_1
=
np
.
random
.
random
((
10
,
5
)).
astype
(
"float32"
)
edge_set_1
=
fluid
.
layers
.
data
(
name
=
'edge_set_1'
,
shape
=
[
10
,
2
],
dtype
=
'float32'
)
# the nodes_vector of tree_conv must be Variable.
self
.
assertRaises
(
TypeError
,
fluid
.
contrib
.
layers
.
tree_conv
,
nodes_vector_1
,
edge_set_1
,
3
)
nodes_vector_2
=
fluid
.
layers
.
data
(
name
=
'vectors2'
,
shape
=
[
10
,
5
],
dtype
=
'float32'
)
edge_set_2
=
np
.
random
.
random
((
10
,
2
)).
astype
(
"float32"
)
# the edge_set of tree_conv must be Variable.
self
.
assertRaises
(
TypeError
,
fluid
.
contrib
.
layers
.
tree_conv
,
nodes_vector_2
,
edge_set_2
,
3
)
class
TestDygraphTreeConv_OpError
(
unittest
.
TestCase
):
def
test_errors
(
self
):
with
program_guard
(
Program
(),
Program
()):
TreeConv
=
fluid
.
dygraph
.
nn
.
TreeConv
(
feature_size
=
5
,
output_size
=
6
,
num_filters
=
1
,
max_depth
=
2
)
nodes_vector_1
=
np
.
random
.
random
((
10
,
5
)).
astype
(
"float32"
)
edge_set_1
=
fluid
.
layers
.
data
(
name
=
'edge_set_1'
,
shape
=
[
10
,
2
],
dtype
=
'float32'
)
# the nodes_vector of TreeConv must be Variable.
self
.
assertRaises
(
TypeError
,
TreeConv
,
nodes_vector_1
,
edge_set_1
,
3
)
nodes_vector_2
=
fluid
.
layers
.
data
(
name
=
'vectors2'
,
shape
=
[
10
,
5
],
dtype
=
'float32'
)
edge_set_2
=
np
.
random
.
random
((
10
,
2
)).
astype
(
"float32"
)
# the edge_set of TreeConv must be Variable.
self
.
assertRaises
(
TypeError
,
TreeConv
,
nodes_vector_2
,
edge_set_2
,
3
)
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录