Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
42c102a0
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
42c102a0
编写于
8月 07, 2017
作者:
C
caoying03
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
follow comments.
上级
ffafc5c9
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
111 addition
and
67 deletion
+111
-67
paddle/gserver/layers/PrintLayer.cpp
paddle/gserver/layers/PrintLayer.cpp
+1
-1
paddle/gserver/layers/SubNestedSequenceLayer.cpp
paddle/gserver/layers/SubNestedSequenceLayer.cpp
+60
-60
paddle/parameter/Argument.cpp
paddle/parameter/Argument.cpp
+20
-0
paddle/parameter/Argument.h
paddle/parameter/Argument.h
+24
-0
python/paddle/trainer_config_helpers/layers.py
python/paddle/trainer_config_helpers/layers.py
+6
-6
未找到文件。
paddle/gserver/layers/PrintLayer.cpp
浏览文件 @
42c102a0
...
...
@@ -29,7 +29,7 @@ public:
vals
.
push_back
(
s
.
str
());
}
size_t
pos
=
0
;
in
t
i
=
0
;
size_
t
i
=
0
;
std
::
ostringstream
s
;
const
std
::
string
&
format
=
config_
.
user_arg
();
while
(
true
)
{
...
...
paddle/gserver/layers/SubNestedSequenceLayer.cpp
浏览文件 @
42c102a0
...
...
@@ -31,22 +31,42 @@ public:
void
backward
(
const
UpdateCallback
&
callback
=
nullptr
)
override
;
private:
void
reorganizeSeqInfo
(
const
ICpuGpuVectorPtr
seqStartPos
,
const
ICpuGpuVectorPtr
subSeqStartPos
);
void
calSelectedCols
(
const
MatrixPtr
selectedIndices
,
const
std
::
vector
<
std
::
vector
<
int
>>
inputSeqInfo
);
void
buildOutputSeqInfo
();
/*
* This functions generates the indices of rows in a batch according to the
* indices of selected sub-sequence in each sequence.
*
* Examples:
* selectedIndices:
* [
* [0, 1, -1],
* [0, 1, 2],
* [0, -1, -1],
* [0, 2, 3],
* ]
* inputSeqInfo:
* [
* [0,3,4],
* [4,5,7,10,15],
* [15,20],
* [20,22,23,25,28]
* ]
*
* ths output is saved to private member rowIndice_;
* [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
* 16,17,18,19,20,21,22,23,24,25,26,27]
*/
std
::
vector
<
int
>
outSeqStartInfo_
;
std
::
vector
<
int
>
outSubSeqStartInfo_
;
void
calSelectedCols
(
const
MatrixPtr
selectedIndices
,
const
std
::
vector
<
std
::
vector
<
int
>>&
inputSeqInfo
)
;
// if the second input of this layer is on GPU memory, copy it to CPU memory.
MatrixPtr
selIdsCpu_
;
// reorganize sequenceStartPositions and subSequenceStartPositions altogether
// reorganized sequenceStartPositions and subSequenceStartPositions
// into a 2d vector to facilitate the sequence selection process.
std
::
vector
<
std
::
vector
<
int
>>
inputSeqInfo_
;
std
::
vector
<
std
::
vector
<
int
>>
inputSeqInfo
Vec
_
;
// the final seleted row indices in a batch,
// the final sele
c
ted row indices in a batch,
// rowIdx_ and selectedRows_ actually share a same memory.
IVectorPtr
rowIndice_
;
std
::
vector
<
int
>
selectedRows_
;
...
...
@@ -63,30 +83,13 @@ bool SubNestedSequenceLayer::init(const LayerMap& layerMap,
return
true
;
}
void
SubNestedSequenceLayer
::
reorganizeSeqInfo
(
const
ICpuGpuVectorPtr
seqStartPos
,
const
ICpuGpuVectorPtr
subSeqStartPos
)
{
int
*
seqStarts
=
seqStartPos
->
getMutableData
(
false
);
int
*
subSeqStarts
=
subSeqStartPos
->
getMutableData
(
false
);
int
seqNum
=
seqStartPos
->
getSize
()
-
1
;
inputSeqInfo_
.
resize
(
seqNum
,
std
::
vector
<
int
>
());
int
seqIdx
=
0
;
for
(
size_t
i
=
0
;
i
<
subSeqStartPos
->
getSize
();
++
i
)
{
inputSeqInfo_
[
seqIdx
].
push_back
(
subSeqStarts
[
i
]);
if
(
subSeqStarts
[
i
]
==
seqStarts
[
seqIdx
+
1
])
{
seqIdx
++
;
if
(
seqIdx
==
seqNum
)
return
;
inputSeqInfo_
[
seqIdx
].
push_back
(
subSeqStarts
[
i
]);
}
}
}
void
SubNestedSequenceLayer
::
calSelectedCols
(
const
MatrixPtr
selectedIndices
,
const
std
::
vector
<
std
::
vector
<
int
>>
inputSeqInfo
)
{
const
std
::
vector
<
std
::
vector
<
int
>>
&
inputSeqInfo
)
{
selectedRows_
.
clear
();
outSubSeqStartInfo_
.
resize
(
1
,
0
);
outSeqStartInfo_
.
resize
(
1
,
0
);
std
::
vector
<
int
>
outSeqStartInfo
(
1
,
0
);
std
::
vector
<
int
>
outSubSeqStartInfo
(
1
,
0
);
size_t
seqNum
=
selectedIndices
->
getHeight
();
size_t
beamSize
=
selectedIndices
->
getWidth
();
...
...
@@ -94,30 +97,35 @@ void SubNestedSequenceLayer::calSelectedCols(
for
(
size_t
j
=
0
;
j
<
beamSize
;
++
j
)
{
if
(
selectedIndices
->
getElement
(
i
,
j
)
==
-
1.
)
break
;
int
selSubSeqIdx
=
selectedIndices
->
getElement
(
i
,
j
);
CHECK_GT
(
inputSeqInfo_
[
i
].
size
()
-
1
,
selSubSeqIdx
);
CHECK_GT
(
inputSeqInfo
Vec
_
[
i
].
size
()
-
1
,
selSubSeqIdx
);
size_t
subSeqLen
=
inputSeqInfo_
[
i
][
selSubSeqIdx
+
1
]
-
inputSeqInfo
_
[
i
][
selSubSeqIdx
];
size_t
subSeqLen
=
inputSeqInfoVec_
[
i
][
selSubSeqIdx
+
1
]
-
inputSeqInfoVec
_
[
i
][
selSubSeqIdx
];
for
(
size_t
k
=
0
;
k
<
subSeqLen
;
++
k
)
selectedRows_
.
push_back
(
inputSeqInfo_
[
i
][
selSubSeqIdx
]
+
k
);
outSubSeqStartInfo
_
.
push_back
(
outSubSeqStartInfo_
.
back
()
+
subSeqLen
);
selectedRows_
.
push_back
(
inputSeqInfo
Vec
_
[
i
][
selSubSeqIdx
]
+
k
);
outSubSeqStartInfo
.
push_back
(
outSubSeqStartInfo
.
back
()
+
subSeqLen
);
}
outSeqStartInfo
_
.
push_back
(
outSubSeqStartInfo_
.
back
());
outSeqStartInfo
.
push_back
(
outSubSeqStartInfo
.
back
());
}
}
void
SubNestedSequenceLayer
::
buildOutputSeqInfo
()
{
Argument
&
output
=
getOutput
();
if
(
useGpu_
)
{
rowIndice_
=
IVector
::
create
(
selectedRows_
.
size
(),
useGpu_
);
rowIndice_
->
copyFrom
(
selectedRows_
.
data
(),
selectedRows_
.
size
());
}
else
{
rowIndice_
=
IVector
::
create
(
selectedRows_
.
data
(),
selectedRows_
.
size
(),
useGpu_
);
}
// create the sequence information for the output.
ICpuGpuVector
::
resizeOrCreate
(
output
.
sequenceStartPositions
,
outSeqStartInfo_
.
size
(),
false
);
output
.
sequenceStartPositions
->
copyFrom
(
outSeqStartInfo
_
.
data
(),
outSeqStartInfo_
.
size
(),
false
);
output
_
.
sequenceStartPositions
,
outSeqStartInfo
.
size
(),
false
);
output
_
.
sequenceStartPositions
->
copyFrom
(
outSeqStartInfo
.
data
(),
outSeqStartInfo
.
size
(),
false
);
ICpuGpuVector
::
resizeOrCreate
(
output
.
subSequenceStartPositions
,
outSubSeqStartInfo_
.
size
(),
false
);
output
.
subSequenceStartPositions
->
copyFrom
(
outSubSeqStartInfo
_
.
data
(),
outSubSeqStartInfo_
.
size
(),
false
);
output
_
.
subSequenceStartPositions
,
outSubSeqStartInfo
.
size
(),
false
);
output
_
.
subSequenceStartPositions
->
copyFrom
(
outSubSeqStartInfo
.
data
(),
outSubSeqStartInfo
.
size
(),
false
);
}
void
SubNestedSequenceLayer
::
forward
(
PassType
passType
)
{
...
...
@@ -131,7 +139,7 @@ void SubNestedSequenceLayer::forward(PassType passType) {
if
(
dynamic_cast
<
GpuMatrix
*>
(
selectedIndices
.
get
()))
{
/*
* Currently, the second input for this layer generated by
* Currently, the second input for this layer
is
generated by
* kmax_sequence_score_layer whose output is always stored on CPU,
* or a data_layer which canbe on GPU.
*
...
...
@@ -149,20 +157,12 @@ void SubNestedSequenceLayer::forward(PassType passType) {
selIdsCpu_
=
selectedIndices
;
}
reorganizeSeqInfo
(
inputSeq
.
sequenceStartPositions
,
inputSeq
.
subSequenceStartPositions
);
calSelectedCols
(
selIdsCpu_
,
inputSeqInfo
_
);
resetOutput
(
selectedRows_
.
size
(),
getSize
()
);
Argument
::
reorganizeSeqInfo
(
inputSeq
.
sequenceStartPositions
,
inputSeq
.
subSequenceStartPositions
,
inputSeqInfoVec
_
);
calSelectedCols
(
selIdsCpu_
,
inputSeqInfoVec_
);
if
(
useGpu_
)
{
rowIndice_
=
IVector
::
create
(
selectedRows_
.
size
(),
useGpu_
);
rowIndice_
->
copyFrom
(
selectedRows_
.
data
(),
selectedRows_
.
size
());
}
else
{
rowIndice_
=
IVector
::
create
(
selectedRows_
.
data
(),
selectedRows_
.
size
(),
useGpu_
);
}
buildOutputSeqInfo
();
resetOutput
(
selectedRows_
.
size
(),
getSize
());
getOutputValue
()
->
selectRows
(
*
getInputValue
(
0
),
*
rowIndice_
);
}
...
...
paddle/parameter/Argument.cpp
浏览文件 @
42c102a0
...
...
@@ -666,4 +666,24 @@ void Argument::subArgFrom(const Argument& input,
}
}
void
Argument
::
reorganizeSeqInfo
(
const
ICpuGpuVectorPtr
seqStartPos
,
const
ICpuGpuVectorPtr
subSeqStartPos
,
std
::
vector
<
std
::
vector
<
int
>>&
reorganizedSeqInfo
)
{
int
*
seqStarts
=
seqStartPos
->
getMutableData
(
false
);
int
*
subSeqStarts
=
subSeqStartPos
->
getMutableData
(
false
);
int
seqNum
=
seqStartPos
->
getSize
()
-
1
;
reorganizedSeqInfo
.
resize
(
seqNum
,
std
::
vector
<
int
>
());
int
seqIdx
=
0
;
for
(
size_t
i
=
0
;
i
<
subSeqStartPos
->
getSize
();
++
i
)
{
reorganizedSeqInfo
[
seqIdx
].
push_back
(
subSeqStarts
[
i
]);
if
(
subSeqStarts
[
i
]
==
seqStarts
[
seqIdx
+
1
])
{
seqIdx
++
;
if
(
seqIdx
==
seqNum
)
return
;
reorganizedSeqInfo
[
seqIdx
].
push_back
(
subSeqStarts
[
i
]);
}
}
}
}
// namespace paddle
paddle/parameter/Argument.h
浏览文件 @
42c102a0
...
...
@@ -317,6 +317,30 @@ struct Argument {
*/
void
printValueString
(
std
::
ostream
&
stream
,
const
std
::
string
&
prefix
=
""
)
const
;
/**
* @brief reorganizeSeqInfo will reorganize sequenceStartPositions and
* subSequenceStartPositions into a 2 dimensional arrary: reorganizedSeqInfo.
*
* @param seqStartPos: sequenceStartPositions of an Argument.
* @param subSeqStartPos: subSequenceStartPositions of an Argument.
* @param the reorganized sequence start position information.
*
* Examples:
* seqStartPos: [0, 4, 15, 20, 28]
* subSeqStartPos: [0, 3, 4, 5, 7, 10, 15, 20, 22, 23, 25, 28]
* reorganizedSeqInfo:
* [
* [0,3,4],
* [4,5,7,10,15],
* [15,20],
* [20,22,23,25,28]
* ]
*/
static
void
reorganizeSeqInfo
(
const
ICpuGpuVectorPtr
seqStartPos
,
const
ICpuGpuVectorPtr
subSeqStartPos
,
std
::
vector
<
std
::
vector
<
int
>>&
reorganizedSeqInfo
);
};
}
// namespace paddle
python/paddle/trainer_config_helpers/layers.py
浏览文件 @
42c102a0
...
...
@@ -6097,16 +6097,15 @@ def sub_nested_seq_layer(input, selected_indices, name=None):
The sub_nested_seq_layer accepts two inputs: the first one is a nested
sequence; the second one is a set of selceted indices in the nested sequence.
Then sub_nest_seq_layer trims the first nested sequence input according to
the selected indices to form a new output.
This layer is useful in beam training.
Then sub_nest_seq_layer trims the first nested sequence input according
to the selected indices to form a new output. This layer is useful in
beam training.
The example usage is:
.. code-block:: python
sub_nest_seq = sub_nested_seq_layer(input=[data, selected_indices])
sub_nest_seq = sub_nested_seq_layer(input=[data, selected_indices])
:param input: A nested sequence.
...
...
@@ -6118,6 +6117,7 @@ def sub_nested_seq_layer(input, selected_indices, name=None):
:return: LayerOutput object.
:rtype: LayerOutput
"""
assert
isinstance
(
input
,
LayerOutput
),
(
'The first input of '
'sub_nested_seq_layer must be a Paddle layer.'
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录