Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
f60f0eae
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
f60f0eae
编写于
9月 28, 2017
作者:
Y
Yu Yang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Using double precision to stablize lstm gradient check
上级
9fbf94b6
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
22 addition
and
19 deletion
+22
-19
paddle/operators/lstm_unit_op.cc
paddle/operators/lstm_unit_op.cc
+7
-6
paddle/operators/lstm_unit_op.cu
paddle/operators/lstm_unit_op.cu
+8
-6
paddle/operators/lstm_unit_op.h
paddle/operators/lstm_unit_op.h
+4
-4
python/paddle/v2/framework/tests/test_lstm_unit_op.py
python/paddle/v2/framework/tests/test_lstm_unit_op.py
+3
-3
未找到文件。
paddle/operators/lstm_unit_op.cc
浏览文件 @
f60f0eae
...
...
@@ -47,7 +47,6 @@ class LstmUnitOp : public framework::OperatorWithKernel {
}
};
template
<
typename
AttrType
>
class
LstmUnitOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
LstmUnitOpMaker
(
framework
::
OpProto
*
proto
,
...
...
@@ -68,7 +67,7 @@ Equation:
H = C * sigm(o)
)DOC"
);
AddAttr
<
AttrType
>
(
"forget_bias"
,
"The forget bias of Lstm Unit."
)
AddAttr
<
float
>
(
"forget_bias"
,
"The forget bias of Lstm Unit."
)
.
SetDefault
(
0.0
);
}
};
...
...
@@ -93,9 +92,11 @@ class LstmUnitGradOp : public framework::OperatorWithKernel {
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OP
(
lstm_unit
,
ops
::
LstmUnitOp
,
ops
::
LstmUnitOpMaker
<
float
>
,
lstm_unit_grad
,
ops
::
LstmUnitGradOp
);
REGISTER_OP
(
lstm_unit
,
ops
::
LstmUnitOp
,
ops
::
LstmUnitOpMaker
,
lstm_unit_grad
,
ops
::
LstmUnitGradOp
);
REGISTER_OP_CPU_KERNEL
(
lstm_unit
,
ops
::
LstmUnitKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
ops
::
LstmUnitKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
,
ops
::
LstmUnitKernel
<
paddle
::
platform
::
CPUPlace
,
double
>
);
REGISTER_OP_CPU_KERNEL
(
lstm_unit_grad
,
ops
::
LstmUnitGradKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
lstm_unit_grad
,
ops
::
LstmUnitGradKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
,
ops
::
LstmUnitGradKernel
<
paddle
::
platform
::
CPUPlace
,
double
>
);
paddle/operators/lstm_unit_op.cu
浏览文件 @
f60f0eae
...
...
@@ -89,7 +89,7 @@ __global__ void LSTMUnitGradientKernel(const int nthreads, const int dim,
}
}
template
<
typename
T
,
typename
AttrType
=
T
>
template
<
typename
T
>
class
LstmUnitOpCUDAKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
...
...
@@ -101,7 +101,7 @@ class LstmUnitOpCUDAKernel : public framework::OpKernel<T> {
auto
*
c_tensor
=
ctx
.
Output
<
framework
::
Tensor
>
(
"C"
);
auto
*
h_tensor
=
ctx
.
Output
<
framework
::
Tensor
>
(
"H"
);
auto
forget_bias
=
static_cast
<
T
>
(
ctx
.
Attr
<
AttrType
>
(
"forget_bias"
));
auto
forget_bias
=
static_cast
<
T
>
(
ctx
.
Attr
<
float
>
(
"forget_bias"
));
int
b_size
=
c_tensor
->
dims
()[
0
];
int
D
=
c_tensor
->
dims
()[
1
];
...
...
@@ -120,7 +120,7 @@ class LstmUnitOpCUDAKernel : public framework::OpKernel<T> {
}
};
template
<
typename
T
,
typename
AttrType
=
T
>
template
<
typename
T
>
class
LstmUnitGradOpCUDAKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
...
...
@@ -153,7 +153,7 @@ class LstmUnitGradOpCUDAKernel : public framework::OpKernel<T> {
int
N
=
c_tensor
->
dims
()[
0
];
int
D
=
c_tensor
->
dims
()[
1
];
auto
forget_bias
=
static_cast
<
T
>
(
ctx
.
Attr
<
AttrType
>
(
"forget_bias"
));
auto
forget_bias
=
static_cast
<
T
>
(
ctx
.
Attr
<
float
>
(
"forget_bias"
));
int
block
=
512
;
int
n
=
N
*
D
;
...
...
@@ -169,5 +169,7 @@ class LstmUnitGradOpCUDAKernel : public framework::OpKernel<T> {
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_GPU_KERNEL
(
lstm_unit
,
ops
::
LstmUnitOpCUDAKernel
<
float
>
);
REGISTER_OP_GPU_KERNEL
(
lstm_unit_grad
,
ops
::
LstmUnitGradOpCUDAKernel
<
float
>
);
REGISTER_OP_GPU_KERNEL
(
lstm_unit
,
ops
::
LstmUnitOpCUDAKernel
<
float
>
,
ops
::
LstmUnitOpCUDAKernel
<
double
>
);
REGISTER_OP_GPU_KERNEL
(
lstm_unit_grad
,
ops
::
LstmUnitGradOpCUDAKernel
<
float
>
,
ops
::
LstmUnitGradOpCUDAKernel
<
double
>
);
paddle/operators/lstm_unit_op.h
浏览文件 @
f60f0eae
...
...
@@ -32,7 +32,7 @@ inline T tanh(T x) {
return
2.
*
sigmoid
(
2.
*
x
)
-
1.
;
}
template
<
typename
Place
,
typename
T
,
typename
AttrType
=
T
>
template
<
typename
Place
,
typename
T
>
class
LstmUnitKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
...
...
@@ -44,7 +44,7 @@ class LstmUnitKernel : public framework::OpKernel<T> {
auto
*
c_tensor
=
ctx
.
Output
<
framework
::
Tensor
>
(
"C"
);
auto
*
h_tensor
=
ctx
.
Output
<
framework
::
Tensor
>
(
"H"
);
auto
forget_bias
=
static_cast
<
T
>
(
ctx
.
Attr
<
AttrType
>
(
"forget_bias"
));
auto
forget_bias
=
static_cast
<
T
>
(
ctx
.
Attr
<
float
>
(
"forget_bias"
));
int
b_size
=
c_tensor
->
dims
()[
0
];
int
D
=
c_tensor
->
dims
()[
1
];
...
...
@@ -75,7 +75,7 @@ class LstmUnitKernel : public framework::OpKernel<T> {
}
};
template
<
typename
Place
,
typename
T
,
typename
AttrType
=
T
>
template
<
typename
Place
,
typename
T
>
class
LstmUnitGradKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
...
...
@@ -108,7 +108,7 @@ class LstmUnitGradKernel : public framework::OpKernel<T> {
int
N
=
c_tensor
->
dims
()[
0
];
int
D
=
c_tensor
->
dims
()[
1
];
auto
forget_bias
=
static_cast
<
T
>
(
ctx
.
Attr
<
AttrType
>
(
"forget_bias"
));
auto
forget_bias
=
static_cast
<
T
>
(
ctx
.
Attr
<
float
>
(
"forget_bias"
));
for
(
int
n
=
0
;
n
<
N
;
++
n
)
{
for
(
int
d
=
0
;
d
<
D
;
++
d
)
{
...
...
python/paddle/v2/framework/tests/test_lstm_unit_op.py
浏览文件 @
f60f0eae
...
...
@@ -14,8 +14,8 @@ def tanh_np(x):
class
LstmUnitTest
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"lstm_unit"
x_np
=
np
.
random
.
normal
(
size
=
(
5
,
16
)).
astype
(
"float
32
"
)
c_np
=
np
.
random
.
normal
(
size
=
(
5
,
4
)).
astype
(
"float
32
"
)
x_np
=
np
.
random
.
normal
(
size
=
(
5
,
16
)).
astype
(
"float
64
"
)
c_np
=
np
.
random
.
normal
(
size
=
(
5
,
4
)).
astype
(
"float
64
"
)
i_np
,
f_np
,
o_np
,
j_np
=
np
.
split
(
x_np
,
4
,
axis
=
1
)
forget_bias_np
=
0.
self
.
attrs
=
{
'forget_bias'
:
0.
}
...
...
@@ -31,7 +31,7 @@ class LstmUnitTest(OpTest):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
'X'
,
'C_prev'
],
[
'C'
,
'H'
]
,
max_relative_error
=
0.01
)
self
.
check_grad
([
'X'
,
'C_prev'
],
[
'C'
,
'H'
])
if
__name__
==
"__main__"
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录