Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
29fa73bc
P
Paddle
项目概览
Crayon鑫
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
29fa73bc
编写于
8月 06, 2017
作者:
C
caoying03
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix unittest.
上级
83ce2dce
变更
2
展开全部
隐藏空白更改
内联
并排
Showing
2 changed file
with
1975 addition
and
2010 deletion
+1975
-2010
paddle/gserver/layers/SubNestedSequenceLayer.cpp
paddle/gserver/layers/SubNestedSequenceLayer.cpp
+16
-79
paddle/gserver/tests/test_LayerGrad.cpp
paddle/gserver/tests/test_LayerGrad.cpp
+1959
-1931
未找到文件。
paddle/gserver/layers/SubNestedSequenceLayer.cpp
浏览文件 @
29fa73bc
...
...
@@ -31,13 +31,9 @@ public:
void
backward
(
const
UpdateCallback
&
callback
=
nullptr
)
override
;
private:
void
checkInputs
(
const
Argument
&
inputSeq
,
const
Argument
&
seqScores
);
void
calSelectedCols
(
const
Argument
&
scores
,
const
int
*
subSeqStartPos
,
size_t
topK
);
void
partialSortIndex
(
const
std
::
vector
<
real
>&
values
,
int
k
,
std
::
vector
<
size_t
>&
indices
);
void
calSelectedCols
(
const
MatrixPtr
scores
,
const
int
*
seqStartPos
,
const
int
*
subSeqStartPos
);
void
buildOutputSeqInfo
();
std
::
vector
<
int
>
outSeqStartInfo_
;
...
...
@@ -61,74 +57,12 @@ bool SubNestedSequenceLayer::init(const LayerMap& layerMap,
return
true
;
}
void
SubNestedSequenceLayer
::
checkInputs
(
const
Argument
&
inputSeq
,
const
Argument
&
seqScores
)
{
CHECK
(
inputSeq
.
hasSubseq
())
<<
"The first input of SubNestSequence layer "
<<
"must be a nested sequence."
;
CHECK
(
seqScores
.
hasSeq
())
<<
"The second input of SubNestSequence layer must be a sequence."
;
CHECK_EQ
(
seqScores
.
value
->
getWidth
(),
1U
)
<<
"The second input of SubNestedSequenceLayer is scores "
<<
"over each sequence in a nested sequence, "
<<
"so its size should be 1."
;
CHECK_EQ
(
inputSeq
.
getNumSubSequences
(),
seqScores
.
value
->
getHeight
())
<<
"The second input of SubNestedSequenceLayer is scores "
<<
"over each sequence in a nested sequence, so its height should be "
<<
"equal to number of sequence in the first input."
;
}
void
SubNestedSequenceLayer
::
partialSortIndex
(
const
std
::
vector
<
real
>&
values
,
int
k
,
std
::
vector
<
size_t
>&
indices
)
{
CHECK_GE
(
values
.
size
(),
k
);
indices
.
resize
(
values
.
size
(),
0
);
std
::
iota
(
begin
(
indices
),
end
(
indices
),
0U
);
std
::
partial_sort
(
begin
(
indices
),
begin
(
indices
)
+
k
,
end
(
indices
),
[
&
](
size_t
a
,
size_t
b
)
{
return
values
[
a
]
>
values
[
b
];
});
}
void
SubNestedSequenceLayer
::
calSelectedCols
(
const
Argument
&
scores
,
const
int
*
subSeqStartPos
,
size_t
topK
)
{
void
SubNestedSequenceLayer
::
calSelectedCols
(
const
MatrixPtr
selected_indices
,
const
int
*
seqStartPos
,
const
int
*
subSeqStartPos
)
{
selectedRows_
.
clear
();
outSubSeqStartInfo_
.
resize
(
1
,
0
);
outSeqStartInfo_
.
resize
(
1
,
0
);
real
*
seqScores
=
nullptr
;
if
(
useGpu_
)
{
Matrix
::
resizeOrCreate
(
scoreOverInputSeq_
,
scores
.
value
->
getHeight
(),
scores
.
value
->
getWidth
(),
false
/* trans */
,
false
/* useGpu */
);
scoreOverInputSeq_
->
copyFrom
(
*
scores
.
value
);
seqScores
=
scoreOverInputSeq_
->
getData
();
}
else
{
seqScores
=
scores
.
value
->
getData
();
}
int
*
scoreSeqStartPos
=
scores
.
sequenceStartPositions
->
getMutableData
(
false
);
for
(
int
i
=
0
;
i
<
scores
.
getNumSequences
();
++
i
)
{
int
seqLen
=
scoreSeqStartPos
[
i
+
1
]
-
scoreSeqStartPos
[
i
];
int
selectedSeqNum
=
std
::
min
(
static_cast
<
int
>
(
config_
.
top_k
()),
seqLen
);
std
::
vector
<
size_t
>
sortedIdx
;
partialSortIndex
(
std
::
vector
<
real
>
(
seqScores
+
scoreSeqStartPos
[
i
],
seqScores
+
scoreSeqStartPos
[
i
+
1
]),
selectedSeqNum
,
sortedIdx
);
for
(
int
j
=
0
;
j
<
selectedSeqNum
;
++
j
)
{
int
begPos
=
subSeqStartPos
[
scoreSeqStartPos
[
i
]
+
sortedIdx
[
j
]];
int
endPos
=
subSeqStartPos
[
scoreSeqStartPos
[
i
]
+
sortedIdx
[
j
]
+
1
];
for
(
int
m
=
begPos
;
m
<
endPos
;
++
m
)
selectedRows_
.
push_back
(
m
);
outSubSeqStartInfo_
.
push_back
(
outSubSeqStartInfo_
.
back
()
+
endPos
-
begPos
);
}
outSeqStartInfo_
.
push_back
(
outSubSeqStartInfo_
.
back
());
}
}
void
SubNestedSequenceLayer
::
buildOutputSeqInfo
()
{
...
...
@@ -147,14 +81,17 @@ void SubNestedSequenceLayer::buildOutputSeqInfo() {
void
SubNestedSequenceLayer
::
forward
(
PassType
passType
)
{
Layer
::
forward
(
passType
);
const
Argument
&
inputSeq
=
getInput
(
0
);
const
Argument
&
seqScores
=
getInput
(
1
);
const
MatrixPtr
selected_indices
=
getInputValue
(
1
);
CHECK
(
inputSeq
.
hasSubseq
())
<<
"The first input of SubNestSequence layer "
<<
"must be a nested sequence."
;
CHECK_EQ
(
inputSeq
.
getNumSequences
(),
selected_indices
->
getHeight
());
checkInputs
(
inputSeq
,
seqScores
);
calSelectedCols
(
selected_indices
,
inputSeq
.
sequenceStartPositions
->
getMutableData
(
false
),
inputSeq
.
subSequenceStartPositions
->
getMutableData
(
false
));
calSelectedCols
(
seqScores
,
inputSeq
.
subSequenceStartPositions
->
getMutableData
(
false
),
config_
.
top_k
());
resetOutput
(
selectedRows_
.
size
(),
getSize
());
buildOutputSeqInfo
();
...
...
@@ -170,10 +107,10 @@ void SubNestedSequenceLayer::forward(PassType passType) {
}
void
SubNestedSequenceLayer
::
backward
(
const
UpdateCallback
&
callback
)
{
MatrixPtr
input
Grad1
=
getInputGrad
(
0
);
MatrixPtr
input
SeqGrad
=
getInputGrad
(
0
);
MatrixPtr
outputGrad
=
getOutputGrad
();
if
(
input
Grad1
)
outputGrad
->
addToRows
(
*
inputGrad1
,
*
rowIndice_
);
if
(
input
SeqGrad
)
outputGrad
->
addToRows
(
*
inputSeqGrad
,
*
rowIndice_
);
}
}
// namespace paddle
paddle/gserver/tests/test_LayerGrad.cpp
浏览文件 @
29fa73bc
此差异已折叠。
点击以展开。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录