Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
4760f285
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看板
提交
4760f285
编写于
6月 04, 2018
作者:
Y
Yibing Liu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add the argsort operator
上级
e0a8c584
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
218 addition
and
0 deletion
+218
-0
paddle/fluid/operators/argsort_op.cc
paddle/fluid/operators/argsort_op.cc
+83
-0
paddle/fluid/operators/argsort_op.h
paddle/fluid/operators/argsort_op.h
+86
-0
python/paddle/fluid/tests/unittests/test_argsort_op.py
python/paddle/fluid/tests/unittests/test_argsort_op.py
+49
-0
未找到文件。
paddle/fluid/operators/argsort_op.cc
0 → 100644
浏览文件 @
4760f285
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/fluid/operators/argsort_op.h"
namespace
paddle
{
namespace
operators
{
class
ArgsortOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"Input(X) of ArgsortOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
"Output(Out) of ArgsortOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Indices"
),
"Output(Indices) of ArgsortOp should not be null."
);
auto
in_dims
=
ctx
->
GetInputDim
(
"X"
);
int
axis
=
static_cast
<
int
>
(
ctx
->
Attrs
().
Get
<
int
>
(
"axis"
));
auto
num_dims
=
in_dims
.
size
();
PADDLE_ENFORCE
(
axis
<
num_dims
,
"Attr(axis) %d of ArgsortOp is out of bounds for Input(X) "
"dimension %d."
,
axis
,
num_dims
);
PADDLE_ENFORCE
(
axis
>=
0
||
axis
==
-
1
,
"Attr(axis) %d of ArgsortOp must be nonnegative or equal to "
"-1."
,
axis
);
ctx
->
SetOutputDim
(
"Out"
,
in_dims
);
ctx
->
SetOutputDim
(
"Indices"
,
in_dims
);
ctx
->
ShareLoD
(
"X"
,
"Out"
);
ctx
->
ShareLoD
(
"X"
,
"Indices"
);
}
};
class
ArgsortOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
void
Make
()
override
{
AddInput
(
"X"
,
"(Tensor) The input of Argsort op."
);
AddOutput
(
"Out"
,
"(Tensor) The sorted tensor of Argsort op."
);
AddOutput
(
"Indices"
,
"(Tensor) The indices of a tensor giving the sorted order."
);
AddComment
(
R"DOC(
Argsort operator
Performs sorting on the input tensor along the given axis and outputs two
tensors, Output(Out) and Output(Indices). They reserve the same shape
with Input(X), and Output(Out) represents the sorted tensor while
Output(Indices) gives the sorted order along the given axis Attr(axis).
)DOC"
);
AddAttr
<
int
>
(
"axis"
,
"(int, default -1) The axis along which to sort the tensor, "
"default -1, the last dimension."
)
.
SetDefault
(
-
1
);
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OPERATOR
(
argsort
,
ops
::
ArgsortOp
,
ops
::
ArgsortOpMaker
,
paddle
::
framework
::
EmptyGradOpMaker
);
REGISTER_OP_CPU_KERNEL
(
argsort
,
ops
::
ArgsortKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
,
ops
::
ArgsortKernel
<
paddle
::
platform
::
CPUPlace
,
double
>
);
paddle/fluid/operators/argsort_op.h
0 → 100644
浏览文件 @
4760f285
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
namespace
paddle
{
namespace
operators
{
using
Tensor
=
framework
::
Tensor
;
template
<
typename
T
,
int
MajorType
=
Eigen
::
RowMajor
,
typename
IndexType
=
Eigen
::
DenseIndex
>
using
EigenMatrix
=
framework
::
EigenMatrix
<
T
,
MajorType
,
IndexType
>
;
template
<
typename
DeviceContext
,
typename
T
>
class
ArgsortKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
input
=
ctx
.
Input
<
Tensor
>
(
"X"
);
auto
*
output
=
ctx
.
Output
<
Tensor
>
(
"Out"
);
auto
*
indices
=
ctx
.
Output
<
Tensor
>
(
"Indices"
);
int
axis
=
static_cast
<
int
>
(
ctx
.
Attr
<
int
>
(
"axis"
));
auto
in_dims
=
input
->
dims
();
axis
=
(
axis
==
-
1
)
?
(
in_dims
.
size
()
-
1
)
:
axis
;
const
T
*
in_data
=
input
->
data
<
T
>
();
T
*
out_data
=
output
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
int64_t
*
idx_data
=
indices
->
mutable_data
<
int64_t
>
(
ctx
.
GetPlace
());
int64_t
part_dims_prod
=
input
->
numel
()
/
in_dims
[
axis
];
for
(
int64_t
i
=
0
;
i
<
part_dims_prod
;
++
i
)
{
int64_t
idx
=
i
;
std
::
vector
<
int64_t
>
idx_vec
(
in_dims
.
size
(),
0
);
for
(
int64_t
dim
=
in_dims
.
size
()
-
1
;
dim
>=
0
;
--
dim
)
{
if
(
dim
!=
axis
)
{
idx_vec
[
dim
]
=
idx
%
in_dims
[
dim
];
idx
/=
in_dims
[
dim
];
}
}
std
::
vector
<
std
::
pair
<
T
,
int64_t
>>
in_vec
;
std
::
vector
<
int64_t
>
org_index_vec
(
in_dims
[
axis
],
0
);
for
(
int64_t
j
=
0
;
j
<
in_dims
[
axis
];
++
j
)
{
idx_vec
[
axis
]
=
j
;
int64_t
index
=
idx_vec
[
0
];
for
(
int64_t
dim
=
0
;
dim
<
in_dims
.
size
()
-
1
;
++
dim
)
{
index
=
index
*
in_dims
[
dim
+
1
]
+
idx_vec
[
dim
+
1
];
}
in_vec
.
push_back
(
std
::
pair
<
T
,
int64_t
>
(
in_data
[
index
],
j
));
org_index_vec
[
j
]
=
index
;
}
std
::
sort
(
in_vec
.
begin
(),
in_vec
.
end
(),
[](
const
std
::
pair
<
T
,
int64_t
>&
v1
,
const
std
::
pair
<
T
,
int64_t
>&
v2
)
{
return
v1
.
first
<
v2
.
first
;
});
for
(
size_t
j
=
0
;
j
<
org_index_vec
.
size
();
++
j
)
{
int64_t
index
=
org_index_vec
[
j
];
out_data
[
index
]
=
in_vec
[
j
].
first
;
idx_data
[
index
]
=
in_vec
[
j
].
second
;
}
}
}
};
}
// namespace operators
}
// namespace paddle
python/paddle/fluid/tests/unittests/test_argsort_op.py
0 → 100644
浏览文件 @
4760f285
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
unittest
import
numpy
as
np
from
op_test
import
OpTest
class
TestArgsortOp
(
OpTest
):
def
setUp
(
self
):
self
.
init_axis
()
x
=
np
.
random
.
random
((
2
,
3
,
4
,
5
)).
astype
(
"float32"
)
self
.
indices
=
np
.
argsort
(
x
,
kind
=
'quicksort'
,
axis
=
self
.
axis
)
self
.
out
=
np
.
sort
(
x
,
kind
=
'quicksort'
,
axis
=
self
.
axis
)
self
.
op_type
=
"argsort"
self
.
inputs
=
{
'X'
:
x
}
self
.
attrs
=
{
'axis'
:
self
.
axis
}
self
.
outputs
=
{
'Indices'
:
self
.
indices
,
'Out'
:
self
.
out
}
def
init_axis
(
self
):
self
.
axis
=
-
1
def
test_check_output
(
self
):
self
.
check_output
()
class
TestArgsortOpAxis0
(
TestArgsortOp
):
def
init_axis
(
self
):
self
.
axis
=
0
class
TestArgsortOpAxis1
(
TestArgsortOp
):
def
init_axis
(
self
):
self
.
axis
=
1
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录