Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
3e13b912
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,发现更多精彩内容 >>
提交
3e13b912
编写于
9月 05, 2017
作者:
C
caoying03
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add softmax_with_cost_op.
上级
843a8b1e
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
145 addition
and
0 deletion
+145
-0
paddle/operators/softmax_with_cost_op.cc
paddle/operators/softmax_with_cost_op.cc
+82
-0
paddle/operators/softmax_with_cost_op.h
paddle/operators/softmax_with_cost_op.h
+40
-0
paddle/pybind/pybind.cc
paddle/pybind/pybind.cc
+1
-0
python/paddle/v2/framework/tests/test_softmax_with_cost_op.py
...on/paddle/v2/framework/tests/test_softmax_with_cost_op.py
+22
-0
未找到文件。
paddle/operators/softmax_with_cost_op.cc
0 → 100644
浏览文件 @
3e13b912
/* 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
:A
limitations under the License. */
#include "paddle/operators/softmax_op.h"
namespace
paddle
{
namespace
operators
{
class
SoftmaxWithLossOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
auto
logits
=
ctx
.
Input
<
Tensor
>
(
"logits"
);
PADDLE_ENFORCE
(
logits
->
dims
().
size
()
==
2UL
,
"The input of softmax_with_loss_op should be a 2-d tensor."
);
PADDLE_ENFORCE
(
ctx
.
Input
<
Tensor
>
(
"lables"
)
->
dims
().
size
()
==
1UL
,
"The label should be a 1-d tensor."
);
ctx
.
Output
<
Tensor
>
(
"loss"
)
->
Resize
({
logits
->
dims
()[
0
]});
}
};
class
SoftmaxWithLossOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
SoftmaxWithLossOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"logits"
,
"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."
);
AddInput
(
"label"
,
"The ground truth. A 1-D tensor<int> with shape N."
);
AddOutput
(
"loss"
,
"A 1-D tensor<float> with shape N."
);
AddComment
(
R"DOC(
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
tensor, after which cross-entropy loss is then computed. This provides a more
numerically stable gradient.
Because this operators performs a softmax on logits internally, it expects
unscaled logits. Please do not call this op with the output of softmax operator,
which will produce incorrect results.
This operators expects mutually exclusive hard labels, each sample in a batch
is in exactly one class with probabilities 1. Each sample in the batch with one
and only one label.
)DOC"
);
}
};
class
SoftmaxWithLossOpGrad
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OP
(
softmax
,
ops
::
SoftmaxWithLossOp
,
ops
::
SoftmaxWithLossOpMaker
,
softmax_grad
,
ops
::
SoftmaxWithLossOpGrad
);
REGISTER_OP_CPU_KERNEL
(
softmax
,
ops
::
SoftmaxWithLossKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
REGISTER_OP_CPU_KERNEL
(
softmax_grad
,
ops
::
SoftmaxWithLossGradKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
paddle/operators/softmax_with_cost_op.h
0 → 100644
浏览文件 @
3e13b912
/* 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
Place
,
typename
T
>
class
SoftmaxWithLossKernel
:
public
framework
::
OpKernel
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{}
};
template
<
typename
Place
,
typename
T
>
class
SoftmaxWithLossGradKernel
:
public
framework
::
OpKernel
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{}
};
}
// namespace operators
}
// namespace paddle
paddle/pybind/pybind.cc
浏览文件 @
3e13b912
...
...
@@ -37,6 +37,7 @@ USE_OP(mul);
USE_OP
(
mean
);
USE_OP
(
sigmoid
);
USE_OP
(
softmax
);
USE_OP
(
softmax_with_loss
);
USE_OP
(
rowwise_add
);
USE_OP
(
fill_zeros_like
);
USE_NO_KERNEL_OP
(
recurrent
);
...
...
python/paddle/v2/framework/tests/test_softmax_with_cost_op.py
0 → 100644
浏览文件 @
3e13b912
import
unittest
import
numpy
as
np
from
gradient_checker
import
GradientChecker
,
create_op
from
op_test_util
import
OpTestMeta
class
TestSoftmaxWithLossOp
(
unittest
.
TestCase
):
__metaclass__
=
OpTestMeta
def
setUp
(
self
):
pass
class
SoftmaxWithLossGradOpTest
(
GradientChecker
):
def
test_softmax
(
self
):
pass
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录