Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
3a49bae0
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
694
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
3a49bae0
编写于
9月 08, 2017
作者:
Y
yangyaming
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Finish forward for GPU and CPU and CPU backward.
上级
da66891b
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
268 addition
and
0 deletion
+268
-0
paddle/operators/modified_huber_loss_op.cc
paddle/operators/modified_huber_loss_op.cc
+99
-0
paddle/operators/modified_huber_loss_op.cu
paddle/operators/modified_huber_loss_op.cu
+41
-0
paddle/operators/modified_huber_loss_op.h
paddle/operators/modified_huber_loss_op.h
+126
-0
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
未找到文件。
paddle/operators/modified_huber_loss_op.cc
0 → 100644
浏览文件 @
3a49bae0
/* 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/modified_huber_loss_op.h"
namespace
paddle
{
namespace
operators
{
class
ModifiedHuberLossOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
context
)
const
override
{
PADDLE_ENFORCE_NOT_NULL
(
context
.
InputVar
(
"X"
),
"X must be initialized."
);
PADDLE_ENFORCE_NOT_NULL
(
context
.
InputVar
(
"Y"
),
"Y must be initialized."
);
auto
*
x
=
context
.
Input
<
Tensor
>
(
"X"
);
auto
*
y
=
context
.
Input
<
Tensor
>
(
"Y"
);
PADDLE_ENFORCE_EQ
(
x
->
dims
(),
y
->
dims
(),
"Dimensions of X and Y must be the same."
);
PADDLE_ENFORCE_EQ
(
framework
::
arity
(
x
->
dims
()),
2
,
"Tensor rank of X must be 2."
);
PADDLE_ENFORCE_EQ
(
x
->
dims
()[
1
],
1
,
"Second dimension of X must be 1."
);
context
.
Output
<
Tensor
>
(
"intermediate_val"
)
->
Resize
(
x
->
dims
());
context
.
Output
<
Tensor
>
(
"Out"
)
->
Resize
({
x
->
dims
()[
0
],
1
});
}
};
class
ModifiedHuberLossOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
ModifiedHuberLossOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
""
);
AddInput
(
"Y"
,
""
);
AddOutput
(
"intermediate_val"
,
""
).
AsIntermediate
();
AddOutput
(
"Out"
,
""
);
AddComment
(
""
);
}
};
class
ModifiedHuberLossGradOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
context
)
const
override
{
auto
*
x
=
context
.
Input
<
Tensor
>
(
"X"
);
auto
*
y
=
context
.
Input
<
Tensor
>
(
"Y"
);
auto
*
intermediate_val
=
context
.
Input
<
Tensor
>
(
"intermediate_val"
);
auto
*
out_grad
=
context
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
x_grad
=
context
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
y_grad
=
context
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"Y"
));
PADDLE_ENFORCE_NOT_NULL
(
x
,
"Input X must not be null."
);
PADDLE_ENFORCE_NOT_NULL
(
y
,
"Target Y must not be null."
);
PADDLE_ENFORCE_NOT_NULL
(
intermediate_val
,
"Intermediate value must not be null."
);
PADDLE_ENFORCE_NOT_NULL
(
out_grad
,
"Out gradient must not be null."
);
PADDLE_ENFORCE_EQ
(
intermediate_val
->
dims
(),
x
->
dims
(),
"Dimension of X and intermediate value must be the same."
);
PADDLE_ENFORCE_EQ
(
out_grad
->
dims
(),
x
->
dims
(),
"Dimension of Out gradient and X must be the same (N*1)."
);
if
(
x_grad
)
x_grad
->
Resize
(
x
->
dims
());
if
(
y_grad
)
y_grad
->
Resize
(
y
->
dims
());
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OP
(
modified_huber_loss
,
ops
::
ModifiedHuberLossOp
,
ops
::
ModifiedHuberLossOpMaker
,
modified_huber_loss_grad
,
ops
::
ModifiedHuberLossGradOp
);
REGISTER_OP_CPU_KERNEL
(
modified_huber_loss
,
ops
::
ModifiedHuberLossKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
REGISTER_OP_CPU_KERNEL
(
modified_huber_loss_grad
,
ops
::
ModifiedHuberLossGradCPUKernel
<
float
>
);
paddle/operators/modified_huber_loss_op.cu
0 → 100644
浏览文件 @
3a49bae0
/* 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/framework/op_registry.h"
#include "paddle/operators/modified_huber_loss_op.h"
namespace
paddle
{
namespace
operators
{
using
Tensor
=
framework
::
Tensor
;
template
<
typename
T
>
class
ModifiedHuberLossGradGPUKernel
:
public
framework
::
OpKernel
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
// auto* in0 = context.Input<Tensor>("X");
// auto* in1 = context.Input<Tensor>("Y");
// auto* in2 = context.Input<Tensor>("intermediate_val");
// auto* in3 = context.Input<Tensor>(framework::GradVarName("Out"));
// auto* out0 = context.Output<Tensor>(framework::GradVarName("X"));
// auto* out1 = context.Output<Tensor>(framework::GradVarName("X"));
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_GPU_KERNEL
(
modified_huber_loss
,
ops
::
ModifiedHuberLossKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
REGISTER_OP_GPU_KERNEL
(
modified_huber_loss_grad
,
ops
::
ModifiedHuberLossGradGPUKernel
<
float
>
);
paddle/operators/modified_huber_loss_op.h
0 → 100644
浏览文件 @
3a49bae0
/* 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"
#include "paddle/platform/hostdevice.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
T
>
struct
CheckLabelValue
{
HOSTDEVICE
T
operator
()(
const
T
&
val
)
const
{
PADDLE_ASSERT
(
val
==
static_cast
<
T
>
(
0
)
||
val
==
static_cast
<
T
>
(
1
));
}
};
template
<
typename
T
>
struct
ModifiedHuberLossForward
{
HOSTDEVICE
T
operator
()(
const
T
&
val
)
const
{
if
(
val
<
-
1
)
{
return
-
4
*
val
;
}
else
if
(
val
<
1
)
{
return
(
1
-
val
)
*
(
1
-
val
);
}
else
{
return
static_cast
<
T
>
(
0
);
}
}
};
template
<
typename
Place
,
typename
T
>
class
ModifiedHuberLossKernel
:
public
framework
::
OpKernel
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
in0
=
context
.
Input
<
Tensor
>
(
"X"
);
auto
*
in1
=
context
.
Input
<
Tensor
>
(
"Y"
);
auto
*
out0
=
context
.
Output
<
Tensor
>
(
"intermediate_val"
);
auto
*
out1
=
context
.
Output
<
Tensor
>
(
"Out"
);
out0
->
mutable_data
<
T
>
(
context
.
GetPlace
());
out1
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
place
=
context
.
GetEigenDevice
<
Place
>
();
auto
x
=
EigenVector
<
T
>::
Flatten
(
*
in0
);
auto
y
=
EigenVector
<
T
>::
Flatten
(
*
in1
);
// make sure value's of Y in {0, 1}
y
.
unaryExpr
(
CheckLabelValue
<
T
>
());
auto
inter_val
=
EigenVector
<
T
>::
Flatten
(
*
out0
);
// scale y to {-1, +1} and compute x * y
inter_val
.
device
(
place
)
=
x
*
(
2
*
y
-
static_cast
<
T
>
(
1
));
auto
loss
=
EigenVector
<
T
>::
Flatten
(
*
out1
);
loss
.
device
(
place
)
=
inter_val
.
unaryExpr
(
ModifiedHuberLossForward
<
T
>
());
}
};
// Use thrust lib to unify cpu and gpu
// CPU backward kernel
template
<
typename
T
>
class
ModifiedHuberLossGradCPUKernel
:
public
framework
::
OpKernel
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
in0
=
context
.
Input
<
Tensor
>
(
"X"
);
auto
*
in1
=
context
.
Input
<
Tensor
>
(
"Y"
);
auto
*
in2
=
context
.
Input
<
Tensor
>
(
"intermediate_val"
);
auto
*
in3
=
context
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
out0
=
context
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
out1
=
context
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
// loop inter_val (x<-1) (x<1) otherwise
const
T
*
p_inter_val
=
in2
->
data
<
T
>
();
const
T
*
p_out_grad
=
in3
->
data
<
T
>
();
size_t
counts
=
static_cast
<
size_t
>
(
framework
::
product
(
in2
->
dims
()));
if
(
out0
)
{
T
*
p_x_grad
=
out0
->
mutable_data
<
T
>
(
context
.
GetPlace
());
const
T
*
p_y
=
in1
->
data
<
T
>
();
ModifiedHuberLossBackward
(
p_inter_val
,
p_y
,
p_out_grad
,
p_x_grad
,
counts
);
}
if
(
out1
)
{
T
*
p_y_grad
=
out1
->
mutable_data
<
T
>
(
context
.
GetPlace
());
const
T
*
p_x
=
in0
->
data
<
T
>
();
ModifiedHuberLossBackward
(
p_inter_val
,
p_x
,
p_out_grad
,
p_y_grad
,
counts
);
}
}
protected:
void
ModifiedHuberLossBackward
(
const
T
*
p_inter_data
,
const
T
*
p_in_data
,
const
T
*
p_in_grad
,
T
*
p_out_grad
,
size_t
counts
)
const
{
for
(
size_t
i
=
0
;
i
<
counts
;
++
i
)
{
if
(
p_inter_data
[
i
]
<
-
1
)
{
p_out_grad
[
i
]
=
-
4
*
p_in_data
[
i
]
*
p_in_grad
[
i
];
}
else
if
(
p_inter_data
[
i
]
<
1
)
{
p_out_grad
[
i
]
=
-
2
*
(
1
-
p_inter_data
[
i
])
*
p_in_data
[
i
]
*
p_in_grad
[
i
];
}
else
{
p_out_grad
[
i
]
=
0
;
}
}
}
};
}
// namespace operators
}
// namespace paddle
paddle/pybind/pybind.cc
浏览文件 @
3a49bae0
...
...
@@ -50,6 +50,7 @@ USE_OP(cos_sim);
USE_CPU_ONLY_OP
(
gather
);
USE_CPU_ONLY_OP
(
scatter
);
USE_OP
(
squared_l2_distance
);
USE_OP
(
modified_huber_loss
);
namespace
paddle
{
namespace
framework
{
...
...
python/paddle/v2/framework/tests/CMakeLists.txt
浏览文件 @
3a49bae0
...
...
@@ -34,3 +34,4 @@ 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
)
py_test
(
test_modified_huber_loss_op SRCS test_modified_huber_loss_op.py
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录