Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
s920243400
PaddleDetection
提交
f14a7966
P
PaddleDetection
项目概览
s920243400
/
PaddleDetection
与 Fork 源项目一致
Fork自
PaddlePaddle / PaddleDetection
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
f14a7966
编写于
9月 21, 2017
作者:
L
Liu Yiqun
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Initialize the sequence softmax operator.
上级
e53dc8a2
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
205 addition
and
1 deletion
+205
-1
paddle/operators/sequence_avg_pool_op.cc
paddle/operators/sequence_avg_pool_op.cc
+1
-1
paddle/operators/sequence_softmax_op.cc
paddle/operators/sequence_softmax_op.cc
+82
-0
paddle/operators/sequence_softmax_op.cu
paddle/operators/sequence_softmax_op.cu
+25
-0
paddle/operators/sequence_softmax_op.h
paddle/operators/sequence_softmax_op.h
+62
-0
python/paddle/v2/framework/tests/test_sequence_softmax_op.py
python/paddle/v2/framework/tests/test_sequence_softmax_op.py
+35
-0
未找到文件。
paddle/operators/sequence_avg_pool_op.cc
浏览文件 @
f14a7966
...
@@ -36,7 +36,7 @@ class SequenceAvgPoolOp : public framework::OperatorWithKernel {
...
@@ -36,7 +36,7 @@ class SequenceAvgPoolOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE_GE
(
PADDLE_ENFORCE_GE
(
dims
[
0
],
dims
[
0
],
/*batch size = */
static_cast
<
int64_t
>
(
lod
[
0
].
size
()
-
1
),
/*batch size = */
static_cast
<
int64_t
>
(
lod
[
0
].
size
()
-
1
),
"The first dimension of Input(X) must be large than batch size."
);
"The first dimension of Input(X) must be large
r
than batch size."
);
dims
[
0
]
=
lod
[
0
].
size
()
-
1
;
dims
[
0
]
=
lod
[
0
].
size
()
-
1
;
ctx
.
Output
<
framework
::
LoDTensor
>
(
"Out"
)
->
Resize
({
dims
});
ctx
.
Output
<
framework
::
LoDTensor
>
(
"Out"
)
->
Resize
({
dims
});
}
}
...
...
paddle/operators/sequence_softmax_op.cc
0 → 100644
浏览文件 @
f14a7966
/* 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/sequence_softmax_op.h"
namespace
paddle
{
namespace
operators
{
class
SequenceSoftmaxOp
:
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(X) of SequenceSoftmaxOp should not be null."
);
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
OutputVar
(
"Out"
),
"Output(Out) of SequenceSoftmaxOp should not be null."
);
auto
*
x
=
ctx
.
Input
<
framework
::
LoDTensor
>
(
"X"
);
auto
dims
=
x
->
dims
();
auto
lod
=
x
->
lod
();
PADDLE_ENFORCE_EQ
(
lod
.
size
(),
1UL
,
"Only support one level sequence now."
);
PADDLE_ENFORCE_GE
(
dims
[
0
],
/* batch_size */
static_cast
<
int64_t
>
(
lod
[
0
].
size
()
-
1
),
"The first dimension of Input(X) should be larger than batch size."
);
PADDLE_ENFORCE_EQ
(
x
->
numel
(),
static_cast
<
int64_t
>
(
lod
[
0
].
size
()
-
1
),
"The width of each timestep in Input(X) of "
"SequenceSoftmaxOp should be 1."
);
dims
[
0
]
=
lod
[
0
].
size
()
-
1
;
ctx
.
Output
<
framework
::
LoDTensor
>
(
"Out"
)
->
Resize
({
dims
});
}
};
class
SequenceSoftmaxOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
SequenceSoftmaxOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"(LoDTensor)"
);
AddOutput
(
"Out"
,
"(LoDTensor)"
);
AddComment
(
R"DOC(
Softmax of Sequence.
)DOC"
);
}
};
class
SequenceSoftmaxGradOp
:
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
(
sequence_softmax
,
ops
::
SequenceSoftmaxOp
,
ops
::
SequenceSoftmaxOpMaker
,
sequence_softmax_grad
,
ops
::
SequenceSoftmaxGradOp
);
REGISTER_OP_CPU_KERNEL
(
sequence_softmax
,
ops
::
SequenceSoftmaxKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
REGISTER_OP_CPU_KERNEL
(
sequence_softmax_grad
,
ops
::
SequenceSoftmaxGradKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
paddle/operators/sequence_softmax_op.cu
0 → 100644
浏览文件 @
f14a7966
/* 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/sequence_softmax_op.h"
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_GPU_KERNEL
(
sequence_softmax
,
ops
::
SequenceSoftmaxKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
)
REGISTER_OP_GPU_KERNEL
(
sequence_softmax_grad
,
ops
::
SequenceSoftmaxGradKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
paddle/operators/sequence_softmax_op.h
0 → 100644
浏览文件 @
f14a7966
/* 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/operators/math/softmax_function.h"
namespace
paddle
{
namespace
operators
{
using
Tensor
=
framework
::
Tensor
;
using
LoDTensor
=
framework
::
LoDTensor
;
template
<
typename
T
,
int
MajorType
=
Eigen
::
RowMajor
,
typename
IndexType
=
Eigen
::
DenseIndex
>
using
EigenVector
=
framework
::
EigenVector
<
T
,
MajorType
,
IndexType
>
;
template
<
typename
T
,
int
MajorType
=
Eigen
::
RowMajor
,
typename
IndexType
=
Eigen
::
DenseIndex
>
using
EigenMatrix
=
framework
::
EigenMatrix
<
T
,
MajorType
,
IndexType
>
;
template
<
typename
Place
,
typename
T
>
class
SequenceSoftmaxKernel
:
public
framework
::
OpKernel
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
x
=
ctx
.
Input
<
LoDTensor
>
(
"X"
);
auto
*
out
=
ctx
.
Output
<
LoDTensor
>
(
"Out"
);
auto
lod
=
x
->
lod
();
const
size_t
level
=
lod
.
size
();
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
for
(
int
i
=
0
;
i
<
static_cast
<
int
>
(
lod
[
level
].
size
())
-
1
;
++
i
)
{
int
start_pos
=
static_cast
<
int
>
(
lod
[
level
][
i
]);
int
end_pos
=
static_cast
<
int
>
(
lod
[
level
][
i
+
1
]);
Tensor
x_i
=
x
->
Slice
<
T
>
(
start_pos
,
end_pos
);
Tensor
out_i
=
out
->
Slice
<
T
>
(
start_pos
,
end_pos
);
math
::
SoftmaxFunctor
<
Place
,
T
>
()(
&
x_i
,
&
out_i
,
ctx
);
}
}
};
template
<
typename
Place
,
typename
T
>
class
SequenceSoftmaxGradKernel
:
public
framework
::
OpKernel
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{}
};
}
// namespace operators
}
// namespace paddle
python/paddle/v2/framework/tests/test_sequence_softmax_op.py
0 → 100644
浏览文件 @
f14a7966
import
unittest
import
numpy
as
np
from
op_test
import
OpTest
def
stable_softmax
(
x
):
"""Compute the softmax of vector x in a numerically stable way."""
shiftx
=
x
-
np
.
max
(
x
)
exps
=
np
.
exp
(
shiftx
)
return
exps
/
np
.
sum
(
exps
)
class
TestSequenceSoftmaxOp
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"sequence_softmax"
x
=
np
.
random
.
uniform
(
0.1
,
1
,
(
11
,
1
)).
astype
(
"float32"
)
lod
=
[[
0
,
4
,
5
,
8
,
11
]]
out
=
np
.
zeros
((
11
,
1
)).
astype
(
"float32"
)
for
i
in
range
(
4
):
sub_x
=
x
[
lod
[
0
][
i
]:
lod
[
0
][
i
+
1
],
:]
sub_x
=
sub_x
.
reshape
(
1
,
lod
[
0
][
i
+
1
]
-
lod
[
0
][
i
])
sub_out
=
stable_softmax
(
sub_x
)
out
[
lod
[
0
][
i
]:
lod
[
0
][
i
+
1
],
:]
=
sub_out
.
reshape
(
lod
[
0
][
i
+
1
]
-
lod
[
0
][
i
],
1
)
self
.
inputs
=
{
"X"
:
(
x
,
lod
)}
self
.
outputs
=
{
"Out"
:
out
}
def
test_check_output
(
self
):
self
.
check_output
()
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录