Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
a4df3f5b
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
695
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
a4df3f5b
编写于
8月 30, 2017
作者:
Y
yangyaming
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Finish framework of squared_l2_distance_op.
上级
75e16bd3
变更
9
显示空白变更内容
内联
并排
Showing
9 changed file
with
227 addition
and
6 deletion
+227
-6
paddle/operators/CMakeLists.txt
paddle/operators/CMakeLists.txt
+2
-0
paddle/operators/squared_l2_distance_op.cc
paddle/operators/squared_l2_distance_op.cc
+82
-0
paddle/operators/squared_l2_distance_op.cu
paddle/operators/squared_l2_distance_op.cu
+25
-0
paddle/operators/squared_l2_distance_op.h
paddle/operators/squared_l2_distance_op.h
+84
-0
paddle/pybind/CMakeLists.txt
paddle/pybind/CMakeLists.txt
+2
-1
paddle/pybind/pybind.cc
paddle/pybind/pybind.cc
+1
-0
python/paddle/v2/framework/tests/CMakeLists.txt
python/paddle/v2/framework/tests/CMakeLists.txt
+1
-0
python/paddle/v2/framework/tests/op_test_util.py
python/paddle/v2/framework/tests/op_test_util.py
+5
-5
python/paddle/v2/framework/tests/test_squared_l2_distance_op.py
.../paddle/v2/framework/tests/test_squared_l2_distance_op.py
+25
-0
未找到文件。
paddle/operators/CMakeLists.txt
浏览文件 @
a4df3f5b
...
...
@@ -73,3 +73,5 @@ op_library(uniform_random_op SRCS uniform_random_op.cc uniform_random_op.cu)
op_library
(
lookup_table_op SRCS lookup_table_op.cc lookup_table_op.cu
)
op_library
(
scale_op SRCS scale_op.cc scale_op.cu DEPS net_op
)
op_library
(
minus_op SRCS minus_op.cc minus_op.cu DEPS scale_op
)
op_library
(
squared_l2_distance_op SRCS squared_l2_distance_op.cc squared_l2_distance_op.cu
)
paddle/operators/squared_l2_distance_op.cc
0 → 100644
浏览文件 @
a4df3f5b
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
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/operators/squared_l2_distance_op.h"
namespace
paddle
{
namespace
operators
{
class
SquaredL2DistanceOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
"X"
),
"Input of SquaredL2DistanceOp "
"must be initialized."
);
PADDLE_ENFORCE_EQ
(
ctx
.
Input
<
Tensor
>
(
"X"
)
->
dims
(),
ctx
.
Input
<
Tensor
>
(
"Y"
)
->
dims
(),
"Dimensions of SquaredL2DistanceOp's two inputs "
"must be same."
)
framework
::
DDim
dims
=
ctx
.
Input
<
Tensor
>
(
"X"
)
->
dims
();
ctx
.
Output
<
Tensor
>
(
"sub_result"
)
->
Resize
(
dims
);
ctx
.
Output
<
Tensor
>
(
"Out"
)
->
Resize
(
framework
::
make_ddim
({
dims
[
0
],
1
}));
}
};
class
SquaredL2DistanceOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
SquaredL2DistanceOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"Input value."
);
AddInput
(
"Y"
,
"Target value."
);
AddOutput
(
"sub_result"
,
"Buffering substraction result which "
"will be reused in backward."
)
.
AsIntermediate
();
AddOutput
(
"Out"
,
"Squared l2 distance between input and target."
);
AddComment
(
R"DOC(
SquaredL2DistanceOp will cacluate the squared L2 distances for
input and target. Number of distance value equals to the
first dimension of input.
)DOC"
);
}
};
class
SquaredL2DistanceGradOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
))
->
Resize
(
ctx
.
Input
<
Tensor
>
(
"X"
)
->
dims
());
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OP
(
squared_l2_distance
,
ops
::
SquaredL2DistanceOp
,
ops
::
SquaredL2DistanceOpMaker
,
squared_l2_distance_grad
,
ops
::
SquaredL2DistanceGradOp
);
REGISTER_OP_CPU_KERNEL
(
squared_l2_distance
,
ops
::
SquaredL2DistanceKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
REGISTER_OP_CPU_KERNEL
(
squared_l2_distance_grad
,
ops
::
SquaredL2DistanceGradKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
paddle/operators/squared_l2_distance_op.cu
0 → 100644
浏览文件 @
a4df3f5b
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
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. */
#define EIGEN_USE_GPU
#include "paddle/operators/squared_l2_distance_op.h"
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_GPU_KERNEL
(
squared_l2_distance
,
ops
::
SquaredL2DistanceKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
REGISTER_OP_GPU_KERNEL
(
squared_l2_distance_grad
,
ops
::
SquaredL2DistanceGradKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
paddle/operators/squared_l2_distance_op.h
0 → 100644
浏览文件 @
a4df3f5b
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
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 "paddle/framework/eigen.h"
#include "paddle/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
T
,
int
MajorType
=
Eigen
::
RowMajor
,
typename
IndexType
=
Eigen
::
DenseIndex
>
using
EigenVector
=
framework
::
EigenVector
<
T
,
MajorType
,
IndexType
>
;
template
<
typename
Place
,
typename
T
>
class
SquaredL2DistanceKernel
:
public
framework
::
OpKernel
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
input0
=
context
.
Input
<
Tensor
>
(
"X"
);
auto
*
input1
=
context
.
Input
<
Tensor
>
(
"Y"
);
auto
*
output0
=
context
.
Output
<
Tensor
>
(
"sub_result"
);
auto
*
output1
=
context
.
Output
<
Tensor
>
(
"Out"
);
output0
->
mutable_data
<
T
>
(
context
.
GetPlace
());
output1
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
X
=
EigenMatrix
<
T
>::
From
(
*
input0
);
auto
Y
=
EigenMatrix
<
T
>::
From
(
*
input1
);
auto
subResult
=
EigenMatrix
<
T
>::
From
(
*
output0
);
auto
Z
=
EigenMatrix
<
T
>::
From
(
*
output1
);
auto
place
=
context
.
GetEigenDevice
<
Place
>
();
// buffer the substraction result
subResult
.
device
(
place
)
=
X
-
Y
;
const
auto
&
inDims
=
X
.
dimensions
();
const
auto
&
subResMat
=
subResult
.
reshape
(
Eigen
::
array
<
int
,
2
>
(
{
static_cast
<
int
>
(
inDims
[
0
]),
static_cast
<
int
>
(
X
.
size
()
/
inDims
[
0
])}));
Z
.
device
(
place
)
=
subResMat
.
pow
(
2
).
sum
(
Eigen
::
array
<
int
,
1
>
({
1
}));
}
};
template
<
typename
Place
,
typename
T
>
class
SquaredL2DistanceGradKernel
:
public
framework
::
OpKernel
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
input0
=
context
.
Input
<
Tensor
>
(
"sub_result"
);
auto
*
OG
=
context
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
IG
=
context
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
IG
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
subResult
=
EigenMatrix
<
T
>::
From
(
*
input0
);
auto
outGrad
=
EigenMatrix
<
T
>::
From
(
*
OG
);
auto
inGrad
=
EigenMatrix
<
T
>::
From
(
*
IG
);
const
auto
&
subResDims
=
subResult
.
dimensions
();
int
firstDim
=
static_cast
<
int
>
(
subResDims
[
0
]);
int
cols
=
subResult
.
size
()
/
firstDim
;
const
auto
subResMat
=
subResult
.
reshape
(
Eigen
::
array
<
int
,
2
>
({
firstDim
,
cols
}));
// create a matrix view for input gradient tensor
auto
inGradMat
=
inGrad
.
reshape
(
Eigen
::
array
<
int
,
2
>
({
firstDim
,
cols
}));
inGradMat
.
device
(
context
.
GetEigenDevice
<
Place
>
())
=
2
*
(
outGrad
.
broadcast
(
Eigen
::
array
<
int
,
2
>
({
1
,
cols
})))
*
subResMat
;
}
};
}
// namespace operators
}
// namespace paddle
paddle/pybind/CMakeLists.txt
浏览文件 @
a4df3f5b
...
...
@@ -18,5 +18,6 @@ cc_library(paddle_pybind SHARED
fill_zeros_like_op
lookup_table_op
scale_op
minus_op
)
minus_op
squared_l2_distance_op
)
endif
(
WITH_PYTHON
)
paddle/pybind/pybind.cc
浏览文件 @
a4df3f5b
...
...
@@ -48,6 +48,7 @@ USE_OP_ITSELF(identity);
USE_OP
(
minus
);
USE_CPU_ONLY_OP
(
gather
);
USE_CPU_ONLY_OP
(
scatter
);
USE_OP
(
squared_l2_distance
);
namespace
paddle
{
namespace
framework
{
...
...
python/paddle/v2/framework/tests/CMakeLists.txt
浏览文件 @
a4df3f5b
...
...
@@ -32,3 +32,4 @@ py_test(test_gradient_checker SRCS test_gradient_checker.py)
py_test
(
test_lookup_table SRCS test_lookup_table.py
)
py_test
(
test_scale_and_identity_op SRCS test_scale_and_identity_op.py
)
py_test
(
mnist SRCS mnist.py
)
py_test
(
test_squared_l2_distance_op SRCS test_squared_l2_distance_op.py
)
python/paddle/v2/framework/tests/op_test_util.py
浏览文件 @
a4df3f5b
...
...
@@ -66,7 +66,7 @@ class OpTestMeta(type):
self
.
assertTrue
(
numpy
.
allclose
(
actual
,
expect
,
atol
=
1e-05
),
"output name: "
+
out_name
+
"has diff"
)
"output name: "
+
out_name
+
"
has diff"
)
obj
.
test_all
=
test_all
return
obj
python/paddle/v2/framework/tests/test_squared_l2_distance_op.py
0 → 100644
浏览文件 @
a4df3f5b
import
unittest
from
op_test_util
import
OpTestMeta
from
gradient_checker
import
GradientChecker
,
create_op
import
numpy
as
np
class
TestSquaredL2DistanceOp
(
unittest
.
TestCase
):
__metaclass__
=
OpTestMeta
def
setUp
(
self
):
self
.
type
=
'squared_l2_distance'
self
.
inputs
=
{
'X'
:
np
.
random
.
uniform
(
0.1
,
1.
,
(
2
,
3
)).
astype
(
'float32'
),
'Y'
:
np
.
random
.
uniform
(
0.1
,
1.
,
(
2
,
3
)).
astype
(
'float32'
)
}
subRes
=
self
.
inputs
[
'X'
]
-
self
.
inputs
[
'Y'
]
output
=
subRes
*
subRes
self
.
outputs
=
{
'sub_result'
:
subRes
,
'Out'
:
np
.
expand_dims
(
output
.
sum
(
1
),
1
)
}
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录