Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
513bc997
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
513bc997
编写于
9月 08, 2017
作者:
C
caoying03
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
softmax with cross entropy as a cost operator.
上级
2070bc93
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
139 addition
and
12 deletion
+139
-12
paddle/operators/softmax_with_cross_entropy_op.cc
paddle/operators/softmax_with_cross_entropy_op.cc
+102
-0
paddle/operators/softmax_with_cross_entropy_op.cu
paddle/operators/softmax_with_cross_entropy_op.cu
+25
-0
paddle/operators/softmax_with_cross_entropy_op.h
paddle/operators/softmax_with_cross_entropy_op.h
+11
-11
paddle/pybind/pybind.cc
paddle/pybind/pybind.cc
+1
-1
未找到文件。
paddle/operators/softmax_with_c
ost
_op.cc
→
paddle/operators/softmax_with_c
ross_entropy
_op.cc
浏览文件 @
513bc997
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
See the License for the specific language governing permissions and
:A
limitations under the License. */
limitations under the License. */
#include "paddle/operators/softmax_op.h"
#include "paddle/operators/softmax_
with_cross_entropy_
op.h"
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
class
SoftmaxWith
Loss
Op
:
public
framework
::
OperatorWithKernel
{
class
SoftmaxWith
CrossEntropy
Op
:
public
framework
::
OperatorWithKernel
{
public:
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
auto
logits
=
ctx
.
Input
<
Tensor
>
(
"logits"
);
auto
logits
=
ctx
.
Input
<
Tensor
>
(
"logits"
);
PADDLE_ENFORCE
(
logits
->
dims
().
size
()
==
2UL
,
PADDLE_ENFORCE
(
"The input of softmax_with_loss_op should be a 2-d tensor."
);
logits
->
dims
().
size
()
==
2UL
,
"The input of softmax_with_cross_entropy should be a 2-d tensor."
);
PADDLE_ENFORCE
(
ctx
.
Input
<
Tensor
>
(
"lables"
)
->
dims
().
size
()
==
1UL
,
PADDLE_ENFORCE
(
ctx
.
Input
<
Tensor
>
(
"lables"
)
->
dims
().
size
()
==
1UL
,
"The label should be a 1-d tensor."
);
"The label should be a 1-d tensor."
);
ctx
.
Output
<
Tensor
>
(
"
loss
"
)
->
Resize
({
logits
->
dims
()[
0
]});
ctx
.
Output
<
Tensor
>
(
"
Y
"
)
->
Resize
({
logits
->
dims
()[
0
]});
}
}
};
};
class
SoftmaxWithLossOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
class
SoftmaxWithCrossEntropyOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
public:
SoftmaxWith
Loss
OpMaker
(
framework
::
OpProto
*
proto
,
SoftmaxWith
CrossEntropy
OpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"logits"
,
AddInput
(
"logits"
,
"The unscaled log probabilities which is a 2-D tensor<float> with"
"The unscaled log probabilities which is a 2-D tensor<float> with"
"shape [N x K]. N is the batch_size, and K is the class number."
);
"shape [N x K]. N is the batch_size, and K is the class number."
);
AddInput
(
"label"
,
"The ground truth. A 1-D tensor<int> with shape N."
);
AddInput
(
"label"
,
"The ground truth. A 1-D tensor<int> with shape N."
);
AddOutput
(
"
loss
"
,
"A 1-D tensor<float> with shape N."
);
AddOutput
(
"
Y
"
,
"A 1-D tensor<float> with shape N."
);
AddComment
(
R"DOC(
AddComment
(
R"DOC(
Cross entropy loss with softmax are used as the output layer extensively. This
Cross entropy loss with softmax are used as the output layer extensively. This
operator computes the softmax normalized values for each row of the input
operator computes the softmax normalized values for each row of the input
...
@@ -60,12 +61,28 @@ and only one label.
...
@@ -60,12 +61,28 @@ and only one label.
}
}
};
};
class
SoftmaxWith
Loss
OpGrad
:
public
framework
::
OperatorWithKernel
{
class
SoftmaxWith
CrossEntropy
OpGrad
:
public
framework
::
OperatorWithKernel
{
public:
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{}
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
"Y"
),
"Input(Y) should be not null."
);
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
framework
::
GradVarName
(
"Y"
)),
"Input(Y@GRAD) should be not null."
);
PADDLE_ENFORCE_EQ
(
ctx
.
Input
<
Tensor
>
(
"Y"
)
->
dims
(),
ctx
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Y"
))
->
dims
(),
"Input(Y) and its gradients should have a same shape."
);
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
"labels"
),
"Input(lables) should be not null."
);
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
framework
::
GradVarName
(
"logits"
)),
"Input(logits@GRAD) should be not null."
);
PADDLE_ENFORCE_EQ
(
ctx
.
Input
<
Tensor
>
(
"logits"
)
->
dims
(),
ctx
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"logits"
))
->
dims
(),
"Input(logits) and its gradients should have a same shape."
);
}
};
};
}
// namespace operators
}
// namespace operators
...
@@ -73,10 +90,13 @@ class SoftmaxWithLossOpGrad : public framework::OperatorWithKernel {
...
@@ -73,10 +90,13 @@ class SoftmaxWithLossOpGrad : public framework::OperatorWithKernel {
namespace
ops
=
paddle
::
operators
;
namespace
ops
=
paddle
::
operators
;
REGISTER_OP
(
softmax
,
ops
::
SoftmaxWithLossOp
,
ops
::
SoftmaxWithLossOpMaker
,
REGISTER_OP
(
softmax_with_cross_entropy
,
ops
::
SoftmaxWithCrossEntropyOp
,
softmax_grad
,
ops
::
SoftmaxWithLossOpGrad
);
ops
::
SoftmaxWithCrossEntropyOpMaker
,
softmax_with_cross_entropy_grad
,
ops
::
SoftmaxWithCrossEntropyOpGrad
);
REGISTER_OP_CPU_KERNEL
(
REGISTER_OP_CPU_KERNEL
(
softmax
,
ops
::
SoftmaxWithLossKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
softmax_with_cross_entropy
,
ops
::
SoftmaxWithCrossEntropyKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
REGISTER_OP_CPU_KERNEL
(
REGISTER_OP_CPU_KERNEL
(
softmax_grad
,
softmax_
with_cross_entropy_
grad
,
ops
::
SoftmaxWith
Loss
GradKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
ops
::
SoftmaxWith
CrossEntropy
GradKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
paddle/operators/softmax_with_cross_entropy_op.cu
0 → 100644
浏览文件 @
513bc997
/* 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 "softmax_with_cross_entropy_op.h"
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_GPU_KERNEL
(
softmax_with_cross_entropy
,
ops
::
SoftmaxWithCrossEntropyKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
REGISTER_OP_GPU_KERNEL
(
softmax_with_cross_entropy_grad
,
ops
::
SoftmaxWithCrossEntropyGradKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
paddle/operators/softmax_with_c
ost
_op.h
→
paddle/operators/softmax_with_c
ross_entropy
_op.h
浏览文件 @
513bc997
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
See the License for the specific language governing permissions and
limitations under the License. */
limitations under the License. */
#pragma once
#pragma once
#include "paddle/framework/eigen.h"
#include "paddle/framework/eigen.h"
...
@@ -25,13 +25,13 @@ template <typename T, int MajorType = Eigen::RowMajor,
...
@@ -25,13 +25,13 @@ template <typename T, int MajorType = Eigen::RowMajor,
using
EigenMatrix
=
framework
::
EigenMatrix
<
T
,
MajorType
,
IndexType
>
;
using
EigenMatrix
=
framework
::
EigenMatrix
<
T
,
MajorType
,
IndexType
>
;
template
<
typename
Place
,
typename
T
>
template
<
typename
Place
,
typename
T
>
class
SoftmaxWith
Loss
Kernel
:
public
framework
::
OpKernel
{
class
SoftmaxWith
CrossEntropy
Kernel
:
public
framework
::
OpKernel
{
public:
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{}
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{}
};
};
template
<
typename
Place
,
typename
T
>
template
<
typename
Place
,
typename
T
>
class
SoftmaxWith
Loss
GradKernel
:
public
framework
::
OpKernel
{
class
SoftmaxWith
CrossEntropy
GradKernel
:
public
framework
::
OpKernel
{
public:
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{}
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{}
};
};
...
...
paddle/pybind/pybind.cc
浏览文件 @
513bc997
...
@@ -37,7 +37,7 @@ USE_OP(mul);
...
@@ -37,7 +37,7 @@ USE_OP(mul);
USE_OP
(
mean
);
USE_OP
(
mean
);
USE_OP
(
sigmoid
);
USE_OP
(
sigmoid
);
USE_OP
(
softmax
);
USE_OP
(
softmax
);
USE_OP
(
softmax_with_
loss
);
USE_OP
(
softmax_with_
cross_entropy
);
USE_OP
(
rowwise_add
);
USE_OP
(
rowwise_add
);
USE_OP
(
fill_zeros_like
);
USE_OP
(
fill_zeros_like
);
USE_NO_KERNEL_OP
(
recurrent
);
USE_NO_KERNEL_OP
(
recurrent
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录