Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
溜达小灰鼠
Paddle
提交
9edfd200
P
Paddle
项目概览
溜达小灰鼠
/
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
9edfd200
编写于
1月 10, 2017
作者:
X
xutianbing
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
use Daoyuan's SequenceArg.
上级
23ac0b78
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
28 addition
and
20 deletion
+28
-20
paddle/function/BufferArg.h
paddle/function/BufferArg.h
+2
-1
paddle/function/ContextProjectionOp.cpp
paddle/function/ContextProjectionOp.cpp
+13
-13
paddle/function/ContextProjectionOpTest.cpp
paddle/function/ContextProjectionOpTest.cpp
+2
-4
paddle/function/Function.cpp
paddle/function/Function.cpp
+6
-0
paddle/function/Function.h
paddle/function/Function.h
+4
-0
paddle/gserver/layers/ContextProjection.cpp
paddle/gserver/layers/ContextProjection.cpp
+1
-2
未找到文件。
paddle/function/BufferArg.h
浏览文件 @
9edfd200
...
...
@@ -190,7 +190,7 @@ private:
size_t
numSeqs_
;
};
// sequence data
// sequence data
{seqId(vec), buf(matrix)}
class
SequenceArg
:
public
BufferArg
{
public:
SequenceArg
(
void
*
buf
,
...
...
@@ -210,6 +210,7 @@ public:
void
*
getIdBuf
()
const
{
return
startPositions_
.
data
();
}
size_t
numSeqs
()
const
{
return
startPositions_
.
numSeqs
();
}
const
SequenceIdArg
&
getSequenceIds
()
const
{
return
startPositions_
;
}
private:
SequenceIdArg
startPositions_
;
...
...
paddle/function/ContextProjectionOp.cpp
浏览文件 @
9edfd200
...
...
@@ -17,7 +17,6 @@ limitations under the License. */
#include "paddle/math/Vector.h"
namespace
paddle
{
/**
* Context Projection Forward with CPU Matrix Device.
*
...
...
@@ -208,10 +207,10 @@ void ContextProjectionBackward<DEVICE_TYPE_CPU>(const CpuMatrix& out_grad_mat,
* Context Projection Backward Function.
* Update the weight gradient and input layer gradient with backprop
*
* \param inputs[0] input sequence.
* \param inputs[
1]
output layer grad.
* \param outputs[0] input layer grad.
* \param outputs[1] weight grad.
* \param inputs[0]
.seq
input sequence.
* \param inputs[
0].matrix
output layer grad.
* \param outputs[0]
input layer grad.
* \param outputs[1]
weight grad.
*/
template
<
DeviceType
Device
>
class
ContextProjectionBackwardFunc
:
public
FunctionBase
{
...
...
@@ -225,27 +224,28 @@ public:
}
void
calc
(
const
BufferArgs
&
inputs
,
const
BufferArgs
&
outputs
)
override
{
CHECK_EQ
((
size_t
)
2
,
inputs
.
size
());
CHECK_EQ
((
size_t
)
1
,
inputs
.
size
());
CHECK_EQ
((
size_t
)
2
,
outputs
.
size
());
CHECK
(
inputs
[
0
].
data
()
&&
inputs
[
1
].
data
());
CHECK_EQ
(
inputs
[
0
].
shape
().
ndims
(),
(
size_t
)
1
);
CHECK_EQ
(
inputs
[
1
].
shape
().
ndims
(),
(
size_t
)
2
);
const
auto
seqArg
=
dynamic_cast
<
const
SequenceArg
&>
(
inputs
[
0
]);
CHECK
(
seqArg
.
data
()
&&
inputs
[
0
].
data
());
CHECK_EQ
(
seqArg
.
shape
().
ndims
(),
(
size_t
)
2
);
CHECK_EQ
(
seqArg
.
getSequenceIds
().
shape
().
ndims
(),
(
size_t
)
1
);
CHECK_EQ
(
outputs
[
0
].
shape
().
ndims
(),
(
size_t
)
2
);
CHECK_EQ
(
outputs
[
1
].
shape
().
ndims
(),
(
size_t
)
2
);
/// dim of input grad == dim of weight
CHECK_EQ
(
outputs
[
0
].
shape
()[
1
],
outputs
[
1
].
shape
()[
1
]);
/// input and output grad has the same batch_size
CHECK_EQ
(
outputs
[
0
].
shape
()[
0
],
inputs
[
1
]
.
shape
()[
0
]);
CHECK_EQ
(
outputs
[
0
].
shape
()[
0
],
seqArg
.
shape
()[
0
]);
/// dim of output val = dim of input grad * context_length
CHECK_EQ
(
inputs
[
1
]
.
shape
()[
1
],
outputs
[
0
].
shape
()[
1
]
*
context_length_
);
CHECK_EQ
(
seqArg
.
shape
()[
1
],
outputs
[
0
].
shape
()[
1
]
*
context_length_
);
CHECK_EQ
(
outputs
[
0
].
getArgType
(),
ADD_TO
);
CHECK_EQ
(
outputs
[
1
].
getArgType
(),
ADD_TO
);
const
auto
seq_vec
=
inputs
[
0
]
.
vector
<
int
,
Device
>
();
const
auto
out_grad_mat
=
inputs
[
1
]
.
matrix
<
Device
>
();
const
auto
seq_vec
=
seqArg
.
getSequenceIds
()
.
vector
<
int
,
Device
>
();
const
auto
out_grad_mat
=
seqArg
.
matrix
<
Device
>
();
auto
in_grad_mat
=
!
outputs
[
0
].
data
()
?
typename
Tensor
<
real
,
Device
>::
Matrix
(
nullptr
,
0
,
0
)
...
...
paddle/function/ContextProjectionOpTest.cpp
浏览文件 @
9edfd200
...
...
@@ -122,8 +122,7 @@ void testMatrixProjectionBackward(int context_start,
BufferArgs
cpu_inputs
;
BufferArgs
cpu_outputs
;
cpu_inputs
.
addArg
(
*
cpu_seq
);
cpu_inputs
.
addArg
(
cpu_out_grad
);
cpu_inputs
.
addArg
(
cpu_out_grad
,
*
cpu_seq
);
cpu_outputs
.
addArg
(
cpu_in_grad
,
ADD_TO
);
cpu_outputs
.
addArg
(
cpu_w_grad
?
*
cpu_w_grad
:
CpuMatrix
(
nullptr
,
0
,
input_dim
),
ADD_TO
);
...
...
@@ -132,8 +131,7 @@ void testMatrixProjectionBackward(int context_start,
BufferArgs
gpu_inputs
;
BufferArgs
gpu_outputs
;
gpu_inputs
.
addArg
(
*
gpu_seq
);
gpu_inputs
.
addArg
(
gpu_out_grad
);
gpu_inputs
.
addArg
(
gpu_out_grad
,
*
gpu_seq
);
gpu_outputs
.
addArg
(
gpu_in_grad
,
ADD_TO
);
gpu_outputs
.
addArg
(
gpu_w_grad
?
*
gpu_w_grad
:
GpuMatrix
(
nullptr
,
0
,
input_dim
),
ADD_TO
);
...
...
paddle/function/Function.cpp
浏览文件 @
9edfd200
...
...
@@ -90,6 +90,12 @@ void BufferArgs::addArg(const GpuSparseMatrix& arg, ArgType argType) {
args_
.
push_back
(
std
::
make_shared
<
SparseMatrixArg
>
(
arg
,
argType
));
}
void
BufferArgs
::
addArg
(
const
Matrix
&
matrix
,
const
IVector
&
vector
,
ArgType
argType
)
{
args_
.
push_back
(
std
::
make_shared
<
SequenceArg
>
(
matrix
,
vector
,
argType
));
}
ClassRegistrar
<
FunctionBase
>
FunctionBase
::
funcRegistrar_
;
}
// namespace paddle
paddle/function/Function.h
浏览文件 @
9edfd200
...
...
@@ -77,6 +77,10 @@ public:
void
addArg
(
const
CpuSparseMatrix
&
arg
,
ArgType
argType
=
UNSPECIFIED
);
void
addArg
(
const
GpuSparseMatrix
&
arg
,
ArgType
argType
=
UNSPECIFIED
);
void
addArg
(
const
Matrix
&
matrix
,
const
IVector
&
vector
,
ArgType
argType
=
UNSPECIFIED
);
// get argument
const
BufferArg
&
operator
[](
size_t
num
)
const
{
CHECK_LT
(
num
,
args_
.
size
());
...
...
paddle/gserver/layers/ContextProjection.cpp
浏览文件 @
9edfd200
...
...
@@ -166,8 +166,7 @@ void ContextProjection::backward(const UpdateCallback& callback) {
BufferArgs
inputs
;
BufferArgs
outputs
;
inputs
.
addArg
(
*
in_
->
sequenceStartPositions
->
getVector
(
useGpu_
));
inputs
.
addArg
(
*
out_
->
grad
);
inputs
.
addArg
(
*
out_
->
grad
,
*
in_
->
sequenceStartPositions
->
getVector
(
useGpu_
));
outputs
.
addArg
(
CpuMatrix
(
in_
->
grad
?
in_
->
grad
->
getData
()
:
nullptr
,
batch_size
,
input_dim
),
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录