Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
d3d16f76
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看板
提交
d3d16f76
编写于
3月 07, 2018
作者:
Y
ying
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
enhance reshape operator.
上级
ea4e6c7a
变更
6
显示空白变更内容
内联
并排
Showing
6 changed file
with
150 addition
and
59 deletion
+150
-59
paddle/fluid/operators/reshape_op.cc
paddle/fluid/operators/reshape_op.cc
+60
-37
paddle/fluid/operators/reshape_op.h
paddle/fluid/operators/reshape_op.h
+47
-1
python/paddle/fluid/tests/unittests/op_test.py
python/paddle/fluid/tests/unittests/op_test.py
+4
-4
python/paddle/fluid/tests/unittests/test_mine_hard_examples_op.py
...addle/fluid/tests/unittests/test_mine_hard_examples_op.py
+0
-0
python/paddle/fluid/tests/unittests/test_reshape_op.py
python/paddle/fluid/tests/unittests/test_reshape_op.py
+39
-17
python/paddle/fluid/tests/unittests/test_target_assign_op.py
python/paddle/fluid/tests/unittests/test_target_assign_op.py
+0
-0
未找到文件。
paddle/fluid/operators/reshape_op.cc
浏览文件 @
d3d16f76
...
@@ -31,47 +31,68 @@ class ReshapeOp : public framework::OperatorWithKernel {
...
@@ -31,47 +31,68 @@ class ReshapeOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
"Output(Out) of ReshapeOp should not be null."
);
"Output(Out) of ReshapeOp should not be null."
);
auto
shape
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"shape"
);
const
std
::
vector
<
int
>
&
shape
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"shape"
);
PADDLE_ENFORCE
(
shape
.
size
()
>
0
,
"Attr(shape) shouldn't be empty."
);
PADDLE_ENFORCE_EQ
(
shape
.
empty
(),
ctx
->
HasInput
(
"Shape"
),
"The shape information can only be set by Attr(shape) or "
"by Input(Shape). Attr(shape) and Input(Shape) cannot be "
"set at the same time."
);
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
if
(
ctx
->
HasInput
(
"Shape"
))
{
auto
shape_dims
=
ctx
->
GetInputDim
(
"Shape"
);
PADDLE_ENFORCE
(
shape_dims
.
size
()
==
2UL
&&
shape_dims
[
0
]
==
1UL
,
"The Input(Label) should be a 2-D tensor with the 1st "
"dimensions fixed to 1 (a row vector)."
);
// The actual output shape will be set at runtime, here temporially the
// the shape of output the same as the shape of input.
ctx
->
SetOutputDim
(
"Out"
,
x_dims
);
}
else
{
std
::
vector
<
int64_t
>
output_shape
;
ValidateShape
(
shape
,
framework
::
product
(
x_dims
),
output_shape
);
auto
out_dims
=
framework
::
make_ddim
(
output_shape
);
ctx
->
SetOutputDim
(
"Out"
,
out_dims
);
}
if
(
shape
[
0
]
==
x_dims
[
0
])
{
// Only pass LoD when the first dimension of output and input are the
// same.
ctx
->
ShareLoD
(
"X"
,
/*->*/
"Out"
);
}
}
private:
void
ValidateShape
(
const
std
::
vector
<
int
>
&
shape
,
const
int64_t
in_size
,
std
::
vector
<
int64_t
>
&
output_shape
)
const
{
std
::
vector
<
size_t
>
neg_dims_idx
;
std
::
vector
<
size_t
>
neg_dims_idx
;
// set some dimension to -1 if it is unknown
const
int
unknown_index
=
-
1
;
// only one dimension canbe set to -1, whose
const
int
unknown_size
=
-
1
;
// size will be automatically infered.
for
(
size_t
i
=
0
;
i
<
shape
.
size
();
++
i
)
{
for
(
size_t
i
=
0
;
i
<
shape
.
size
();
++
i
)
{
PADDLE_ENFORCE
(
shape
[
i
]
>
0
||
shape
[
i
]
==
unknown_size
,
PADDLE_ENFORCE
(
shape
[
i
]
>
1
||
shape
[
i
]
==
unknown_index
,
"Each dimension of Attr(shape) must be positive or %d."
,
"Each input dimension of Attr(shape) must be positive, or "
unknown_size
);
"only one input dimension can be -1."
);
if
(
shape
[
i
]
==
unknown_size
)
{
if
(
shape
[
i
]
==
unknown_index
)
neg_dims_idx
.
push_back
(
i
);
neg_dims_idx
.
push_back
(
i
);
PADDLE_ENFORCE
(
neg_dims_idx
.
size
()
<=
1
,
"Only one dimension of Attr(shape) can be unknown."
);
}
}
PADDLE_ENFORCE_LE
(
neg_dims_idx
.
size
(),
1
,
"Only one input dimension of Attr(shape) may be unknown."
);
int64_t
inferred_dim
=
0
;
if
(
neg_dims_idx
.
size
())
{
int64_t
capacity
=
std
::
accumulate
(
shape
.
begin
(),
shape
.
end
(),
1
,
std
::
multiplies
<
int
>
());
inferred_dim
=
in_size
/
(
-
capacity
);
}
}
int64_t
capacity
=
output_shape
.
resize
(
shape
.
size
(),
0
);
std
::
accumulate
(
shape
.
begin
(),
shape
.
end
(),
1
,
std
::
multiplies
<
int
>
());
std
::
transform
(
shape
.
begin
(),
shape
.
end
(),
output_shape
.
begin
(),
int64_t
in_size
=
framework
::
product
(
x_dims
);
if
(
neg_dims_idx
.
size
()
==
1
)
{
// dim infer
shape
[
neg_dims_idx
[
0
]]
=
in_size
/
(
-
capacity
);
// recalculate capacity
capacity
=
shape
[
neg_dims_idx
[
0
]]
*
(
-
capacity
);
}
// capacity check
PADDLE_ENFORCE
(
capacity
==
in_size
,
"The size of Input(X) mismatches with Attr(shape)."
);
// resize output
std
::
vector
<
int64_t
>
shape_int64
(
shape
.
size
(),
0
);
std
::
transform
(
shape
.
begin
(),
shape
.
end
(),
shape_int64
.
begin
(),
[](
int
a
)
{
return
static_cast
<
int64_t
>
(
a
);
});
[](
int
a
)
{
return
static_cast
<
int64_t
>
(
a
);
});
auto
out_dims
=
framework
::
make_ddim
(
shape_int64
);
if
(
neg_dims_idx
.
size
())
output_shape
[
neg_dims_idx
[
0
]]
=
inferred_dim
;
ctx
->
SetOutputDim
(
"Out"
,
out_dims
);
if
(
shape
[
0
]
==
x_dims
[
0
])
{
// Only pass LoD when the first dimension is equal between
// output and input.
ctx
->
ShareLoD
(
"X"
,
/*->*/
"Out"
);
}
}
}
};
};
...
@@ -80,10 +101,12 @@ class ReshapeOpMaker : public framework::OpProtoAndCheckerMaker {
...
@@ -80,10 +101,12 @@ class ReshapeOpMaker : public framework::OpProtoAndCheckerMaker {
ReshapeOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
ReshapeOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"The input tensor of reshape operator."
);
AddInput
(
"X"
,
"The input tensor of reshape operator."
);
AddInput
(
"Shape"
,
"a 1-D tensor that provides the shape information."
)
.
AsDispensable
();
AddOutput
(
"Out"
,
"The output tensor of reshape operator."
);
AddOutput
(
"Out"
,
"The output tensor of reshape operator."
);
AddAttr
<
std
::
vector
<
int
>>
(
"shape"
,
AddAttr
<
std
::
vector
<
int
>>
(
"shape"
,
"(vector<int>)
"
"(vector<int>)
Target shape of reshape operator."
)
"Target shape of reshape operator."
);
.
SetDefault
(
std
::
vector
<
int
>
()
);
AddComment
(
R"DOC(
AddComment
(
R"DOC(
Reshape Operator.
Reshape Operator.
...
...
paddle/fluid/operators/reshape_op.h
浏览文件 @
d3d16f76
...
@@ -26,11 +26,57 @@ class ReshapeKernel : public framework::OpKernel<T> {
...
@@ -26,11 +26,57 @@ class ReshapeKernel : public framework::OpKernel<T> {
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
{
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
{
auto
*
out
=
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
);
auto
*
out
=
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
);
auto
*
in
=
ctx
.
Input
<
framework
::
Tensor
>
(
"X"
);
auto
*
in
=
ctx
.
Input
<
framework
::
Tensor
>
(
"X"
);
auto
out_dims
=
out
->
dims
();
auto
*
shape
=
ctx
.
Input
<
framework
::
Tensor
>
(
"Shape"
);
framework
::
DDim
out_dims
;
if
(
shape
)
{
std
::
vector
<
int64_t
>
output_shape
;
ValidateShape
(
*
shape
,
framework
::
product
(
in
->
dims
()),
output_shape
);
for
(
auto
d
:
output_shape
)
std
::
cout
<<
d
<<
" "
;
std
::
cout
<<
std
::
endl
;
out_dims
=
framework
::
make_ddim
(
output_shape
);
}
else
{
out_dims
=
out
->
dims
();
}
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
framework
::
TensorCopy
(
*
in
,
ctx
.
GetPlace
(),
ctx
.
device_context
(),
out
);
framework
::
TensorCopy
(
*
in
,
ctx
.
GetPlace
(),
ctx
.
device_context
(),
out
);
out
->
Resize
(
out_dims
);
out
->
Resize
(
out_dims
);
}
}
private:
void
ValidateShape
(
const
framework
::
Tensor
&
shape
,
const
int64_t
in_size
,
std
::
vector
<
int64_t
>&
output_shape
)
const
{
std
::
vector
<
size_t
>
neg_dims_idx
;
const
int
unknown_index
=
-
1
;
// only one dimension canbe set to -1, whose
// size will be automatically infered.
const
int64_t
dimension
=
shape
.
dims
()[
1
];
std
::
cout
<<
"dimension ="
<<
dimension
<<
std
::
endl
;
const
T
*
shape_data
=
shape
.
data
<
T
>
();
for
(
int64_t
i
=
0
;
i
<
dimension
;
++
i
)
{
PADDLE_ENFORCE
(
shape_data
[
i
]
>
1
||
shape_data
[
i
]
==
unknown_index
,
"Each input dimension of Attr(shape) must be positive, or "
"only one input dimension can be -1."
);
if
(
shape_data
[
i
]
==
unknown_index
)
neg_dims_idx
.
push_back
(
i
);
}
PADDLE_ENFORCE_LE
(
neg_dims_idx
.
size
(),
1
,
"Only one input dimension of Attr(shape) can be unknown."
);
int64_t
capacity
=
1
;
output_shape
.
resize
(
dimension
,
0
);
for
(
int64_t
i
=
0
;
i
<
dimension
;
++
i
)
{
capacity
*=
shape_data
[
i
];
output_shape
[
i
]
=
static_cast
<
int64_t
>
(
shape_data
[
i
]);
}
if
(
neg_dims_idx
.
size
())
output_shape
[
neg_dims_idx
[
0
]]
=
in_size
/
(
-
capacity
);
}
};
};
template
<
typename
DeviceContext
,
typename
T
>
template
<
typename
DeviceContext
,
typename
T
>
...
...
python/paddle/fluid/tests/unittests/op_test.py
浏览文件 @
d3d16f76
...
@@ -334,7 +334,7 @@ class OpTest(unittest.TestCase):
...
@@ -334,7 +334,7 @@ class OpTest(unittest.TestCase):
np
.
allclose
(
np
.
allclose
(
actual_t
,
expect_t
,
atol
=
atol
),
actual_t
,
expect_t
,
atol
=
atol
),
"Output ("
+
out_name
+
") has diff at "
+
str
(
place
)
+
"Output ("
+
out_name
+
") has diff at "
+
str
(
place
)
+
str
(
actual_t
)
+
str
(
expect_t
))
str
(
actual_t
)
+
"
\n
"
+
str
(
expect_t
))
if
isinstance
(
expect
,
tuple
):
if
isinstance
(
expect
,
tuple
):
self
.
assertListEqual
(
actual
.
lod
(),
expect
[
1
],
self
.
assertListEqual
(
actual
.
lod
(),
expect
[
1
],
"Output ("
+
out_name
+
"Output ("
+
out_name
+
...
@@ -546,6 +546,6 @@ class OpTest(unittest.TestCase):
...
@@ -546,6 +546,6 @@ class OpTest(unittest.TestCase):
fetch_list
=
[
g
for
p
,
g
in
param_grad_list
]
fetch_list
=
[
g
for
p
,
g
in
param_grad_list
]
executor
=
Executor
(
place
)
executor
=
Executor
(
place
)
return
map
(
return
map
(
np
.
array
,
np
.
array
,
executor
.
run
(
prog
,
feed_dict
,
fetch_list
,
executor
.
run
(
prog
,
feed_dict
,
fetch_list
,
return_numpy
=
False
))
return_numpy
=
False
))
python/paddle/fluid/tests/unittests/test_mine_hard_examples_op.py
100755 → 100644
浏览文件 @
d3d16f76
文件模式从 100755 更改为 100644
python/paddle/fluid/tests/unittests/test_reshape_op.py
浏览文件 @
d3d16f76
...
@@ -14,29 +14,51 @@
...
@@ -14,29 +14,51 @@
import
unittest
import
unittest
import
numpy
as
np
import
numpy
as
np
from
op_test
import
OpTest
import
pdb
class
TestReshapeOp
(
OpTest
):
from
op_test
import
OpTest
def
setUp
(
self
):
self
.
op_type
=
"reshape"
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
10
,
20
)).
astype
(
"float32"
)}
self
.
attrs
=
{
'shape'
:
[
10
*
20
]}
self
.
outputs
=
{
'Out'
:
self
.
inputs
[
'X'
].
reshape
(
self
.
attrs
[
'shape'
])}
def
test_check_output
(
self
):
# class TestReshapeOp1(OpTest):
self
.
check_output
()
# def setUp(self):
# ori_shape = (2, 25)
# new_shape = [5, 10]
#
# self.op_type = "reshape"
# self.inputs = {"X": np.random.random(ori_shape).astype("float32")}
# self.attrs = {"shape": new_shape}
# self.outputs = {"Out": self.inputs["X"].reshape(new_shape)}
#
# def test_check_output(self):
# self.check_output()
#
# def test_check_grad(self):
# self.check_grad(["X"], "Out")
def
test_check_grad
(
self
):
# class TestReshapeOpDimInfer1(OpTest):
self
.
check_grad
([
"X"
],
"Out"
)
# def setUp(self):
# self.op_type = "reshape"
# self.inputs = {"X": np.random.random((5, 10)).astype("float32")}
# self.attrs = {"shape": [5, -1, 5]}
# self.outputs = {"Out": self.inputs["X"].reshape(self.attrs["shape"])}
#
# def test_check_output(self):
# self.check_output()
#
# def test_check_grad(self):
# self.check_grad(["X"], "Out")
class
TestReshapeOp
DimInfer
(
OpTest
):
class
TestReshapeOp
2
(
OpTest
):
def
setUp
(
self
):
def
setUp
(
self
):
ori_shape
=
(
2
,
25
)
new_shape
=
([
5
,
10
],
)
self
.
op_type
=
"reshape"
self
.
op_type
=
"reshape"
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
10
,
20
)).
astype
(
"float32"
)}
self
.
inputs
=
{
self
.
attrs
=
{
'shape'
:
[
4
,
-
1
,
5
]}
"X"
:
np
.
random
.
random
(
ori_shape
).
astype
(
"float32"
),
self
.
outputs
=
{
'Out'
:
self
.
inputs
[
'X'
].
reshape
(
self
.
attrs
[
'shape'
])}
"Shape"
:
np
.
array
(
new_shape
)
}
self
.
outputs
=
{
"Out"
:
self
.
inputs
[
"X"
].
reshape
(
new_shape
[
0
])}
def
test_check_output
(
self
):
def
test_check_output
(
self
):
self
.
check_output
()
self
.
check_output
()
...
@@ -45,5 +67,5 @@ class TestReshapeOpDimInfer(OpTest):
...
@@ -45,5 +67,5 @@ class TestReshapeOpDimInfer(OpTest):
self
.
check_grad
([
"X"
],
"Out"
)
self
.
check_grad
([
"X"
],
"Out"
)
if
__name__
==
'__main__'
:
if
__name__
==
"__main__"
:
unittest
.
main
()
unittest
.
main
()
python/paddle/fluid/tests/unittests/test_target_assign_op.py
100755 → 100644
浏览文件 @
d3d16f76
文件模式从 100755 更改为 100644
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录