Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
d94c936b
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
694
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
d94c936b
编写于
11月 07, 2017
作者:
D
dangqingqing
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Enhance unit testing.
1. user can disable peephole connections. 2. not calculate some gradients.
上级
d851dafe
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
167 addition
and
63 deletion
+167
-63
paddle/operators/lstm_op.cc
paddle/operators/lstm_op.cc
+6
-3
paddle/operators/lstm_op.h
paddle/operators/lstm_op.h
+9
-3
paddle/operators/math/detail/lstm_cpu_kernel.h
paddle/operators/math/detail/lstm_cpu_kernel.h
+22
-18
paddle/operators/math/detail/lstm_gpu_kernel.h
paddle/operators/math/detail/lstm_gpu_kernel.h
+8
-6
paddle/operators/math/sequence2batch.h
paddle/operators/math/sequence2batch.h
+4
-7
python/paddle/v2/framework/tests/test_lstm_op.py
python/paddle/v2/framework/tests/test_lstm_op.py
+117
-25
python/paddle/v2/optimizer.py
python/paddle/v2/optimizer.py
+1
-1
未找到文件。
paddle/operators/lstm_op.cc
浏览文件 @
d94c936b
...
...
@@ -164,16 +164,19 @@ class LSTMOpMaker : public framework::OpProtoAndCheckerMaker {
"(string, default: sigmoid)"
"The activation for input gate, forget gate and output "
"gate, `sigmoid` by default."
)
.
SetDefault
(
"sigmoid"
);
.
SetDefault
(
"sigmoid"
)
.
InEnum
({
"sigmoid"
,
"tanh"
,
"relu"
,
"identity"
});
AddAttr
<
std
::
string
>
(
"cell_activation"
,
"(string, default: tanh)"
"The activation for cell output, `tanh` by defalut."
)
.
SetDefault
(
"tanh"
);
.
SetDefault
(
"tanh"
)
.
InEnum
({
"sigmoid"
,
"tanh"
,
"relu"
,
"identity"
});
AddAttr
<
std
::
string
>
(
"candidate_activation"
,
"(string, default: tanh)"
"The activation for candidate hidden state, "
"`tanh` by default."
)
.
SetDefault
(
"tanh"
);
.
SetDefault
(
"tanh"
)
.
InEnum
({
"sigmoid"
,
"tanh"
,
"relu"
,
"identity"
});
AddComment
(
R"DOC(
Long-Short Term Memory (LSTM) Operator.
...
...
paddle/operators/lstm_op.h
浏览文件 @
d94c936b
...
...
@@ -69,7 +69,7 @@ class LSTMKernel : public framework::OpKernel<T> {
}
math
::
LstmMetaValue
<
T
>
lstm_value
;
if
(
bias
)
{
if
(
bias
&&
ctx
.
Attr
<
bool
>
(
"use_peepholes"
)
)
{
T
*
bias_data
=
const_cast
<
T
*>
(
bias
->
data
<
T
>
());
// the code style in LstmMetaValue will be updated later.
...
...
@@ -85,6 +85,7 @@ class LSTMKernel : public framework::OpKernel<T> {
Tensor
ordered_c0
;
if
(
cell_t0
)
{
math
::
CopyMatrixRowsFunctor
<
Place
,
T
>
row_shuffle
;
ordered_c0
.
mutable_data
<
T
>
(
cell_t0
->
dims
(),
ctx
.
GetPlace
());
const
size_t
*
order
=
batch_gate
->
lod
()[
2
].
data
();
row_shuffle
(
device_ctx
,
*
cell_t0
,
order
,
ordered_c0
,
true
);
lstm_value
.
prevStateValue
=
ordered_c0
.
data
<
T
>
();
...
...
@@ -124,6 +125,7 @@ class LSTMKernel : public framework::OpKernel<T> {
}
else
if
(
hidden_t0
)
{
math
::
CopyMatrixRowsFunctor
<
Place
,
T
>
row_shuffle
;
Tensor
ordered_h0
;
ordered_h0
.
mutable_data
<
T
>
(
hidden_t0
->
dims
(),
ctx
.
GetPlace
());
const
size_t
*
order
=
batch_gate
->
lod
()[
2
].
data
();
row_shuffle
(
device_ctx
,
*
hidden_t0
,
order
,
ordered_h0
,
true
);
math
::
matmul
<
Place
,
T
>
(
device_ctx
,
ordered_h0
,
false
,
*
weight
,
false
,
...
...
@@ -199,7 +201,7 @@ class LSTMGradKernel : public framework::OpKernel<T> {
PADDLE_ENFORCE_EQ
(
frame_size
,
out_dims
[
1
]);
math
::
LstmMetaValue
<
T
>
lstm_value
;
if
(
bias
)
{
if
(
bias
&&
ctx
.
Attr
<
bool
>
(
"use_peepholes"
)
)
{
T
*
bias_data
=
const_cast
<
T
*>
(
bias
->
data
<
T
>
());
lstm_value
.
checkIg
=
bias_data
+
4
*
frame_size
;
lstm_value
.
checkFg
=
lstm_value
.
checkIg
+
frame_size
;
...
...
@@ -211,9 +213,13 @@ class LSTMGradKernel : public framework::OpKernel<T> {
}
math
::
LstmMetaGrad
<
T
>
lstm_grad
;
if
(
bias
&&
bias_g
)
{
T
*
bias_g_data
=
const_cast
<
T
*>
(
bias_g
->
mutable_data
<
T
>
(
ctx
.
GetPlace
()
));
bias_g
->
mutable_data
<
T
>
(
ctx
.
GetPlace
(
));
zero
(
device_ctx
,
bias_g
,
static_cast
<
T
>
(
0.0
));
}
if
(
bias
&&
bias_g
&&
ctx
.
Attr
<
bool
>
(
"use_peepholes"
))
{
T
*
bias_g_data
=
bias_g
->
data
<
T
>
();
lstm_grad
.
checkIgGrad
=
bias_g_data
+
4
*
frame_size
;
lstm_grad
.
checkFgGrad
=
lstm_grad
.
checkIgGrad
+
frame_size
;
lstm_grad
.
checkOgGrad
=
lstm_grad
.
checkFgGrad
+
frame_size
;
...
...
paddle/operators/math/detail/lstm_cpu_kernel.h
浏览文件 @
d94c936b
...
...
@@ -52,9 +52,9 @@ void naive_lstm_forward_one_sequence(Op op, LstmMetaValue<T> value,
rValueIg
=
valueIg
[
i
];
rValueFg
=
valueFg
[
i
];
rValueOg
=
valueOg
[
i
];
rCheckI
=
value
.
checkIg
[
i
]
;
rCheckF
=
value
.
checkFg
[
i
]
;
rCheckO
=
value
.
checkOg
[
i
]
;
rCheckI
=
value
.
checkIg
?
value
.
checkIg
[
i
]
:
0
;
rCheckF
=
value
.
checkFg
?
value
.
checkFg
[
i
]
:
0
;
rCheckO
=
value
.
checkOg
?
value
.
checkOg
[
i
]
:
0
;
if
(
value
.
prevStateValue
)
{
rPrevState
=
value
.
prevStateValue
[
i
];
...
...
@@ -114,9 +114,9 @@ void naive_lstm_backward_one_sequence(Op op, LstmMetaValue<T> value,
rValueIg
=
valueIg
[
i
];
rValueFg
=
valueFg
[
i
];
rValueOg
=
valueOg
[
i
];
rCheckI
=
value
.
checkIg
[
i
]
;
rCheckF
=
value
.
checkFg
[
i
]
;
rCheckO
=
value
.
checkOg
[
i
]
;
rCheckI
=
value
.
checkIg
?
value
.
checkIg
[
i
]
:
0
;
rCheckF
=
value
.
checkFg
?
value
.
checkFg
[
i
]
:
0
;
rCheckO
=
value
.
checkOg
?
value
.
checkOg
[
i
]
:
0
;
rState
=
value
.
stateValue
[
i
];
rStateAtv
=
value
.
stateActiveValue
[
i
];
rOutputGrad
=
grad
.
outputGrad
[
i
];
...
...
@@ -155,9 +155,9 @@ void avx_lstm_forward_one_sequence(Op op, LstmMetaValue<T> value, int frameSize,
__m256
rValueIg
;
__m256
rValueFg
;
__m256
rValueOg
;
__m256
rCheckI
;
__m256
rCheckF
;
__m256
rCheckO
;
__m256
rCheckI
=
_mm256_set1_ps
(
0.0
f
)
;
__m256
rCheckF
=
_mm256_set1_ps
(
0.0
f
)
;
__m256
rCheckO
=
_mm256_set1_ps
(
0.0
f
)
;
__m256
rState
;
__m256
rPrevState
=
_mm256_set1_ps
(
0.0
f
);
__m256
rStateAtv
;
...
...
@@ -173,9 +173,11 @@ void avx_lstm_forward_one_sequence(Op op, LstmMetaValue<T> value, int frameSize,
rValueIg
=
valueIg
[
i
];
rValueFg
=
valueFg
[
i
];
rValueOg
=
valueOg
[
i
];
rCheckI
=
((
__m256
*
)
value
.
checkIg
)[
i
];
rCheckF
=
((
__m256
*
)
value
.
checkFg
)[
i
];
rCheckO
=
((
__m256
*
)
value
.
checkOg
)[
i
];
if
(
value
.
checkIg
)
{
rCheckI
=
((
__m256
*
)
value
.
checkIg
)[
i
];
rCheckF
=
((
__m256
*
)
value
.
checkFg
)[
i
];
rCheckO
=
((
__m256
*
)
value
.
checkOg
)[
i
];
}
if
(
value
.
prevStateValue
)
{
rPrevState
=
((
__m256
*
)
value
.
prevStateValue
)[
i
];
...
...
@@ -216,9 +218,9 @@ void avx_lstm_backward_one_sequence(Op op, LstmMetaValue<T> value,
__m256
rState
;
__m256
rStateAtv
;
__m256
rOutputGrad
;
__m256
rCheckI
;
__m256
rCheckF
;
__m256
rCheckO
;
__m256
rCheckI
=
_mm256_set1_ps
(
0.0
f
)
;
__m256
rCheckF
=
_mm256_set1_ps
(
0.0
f
)
;
__m256
rCheckO
=
_mm256_set1_ps
(
0.0
f
)
;
__m256
rCheckIGrad
;
__m256
rCheckFGrad
;
__m256
rCheckOGrad
;
...
...
@@ -237,9 +239,11 @@ void avx_lstm_backward_one_sequence(Op op, LstmMetaValue<T> value,
rValueIg
=
valueIg
[
i
];
rValueFg
=
valueFg
[
i
];
rValueOg
=
valueOg
[
i
];
rCheckI
=
((
__m256
*
)
value
.
checkIg
)[
i
];
rCheckF
=
((
__m256
*
)
value
.
checkFg
)[
i
];
rCheckO
=
((
__m256
*
)
value
.
checkOg
)[
i
];
if
(
value
.
checkIg
)
{
rCheckI
=
((
__m256
*
)
value
.
checkIg
)[
i
];
rCheckF
=
((
__m256
*
)
value
.
checkFg
)[
i
];
rCheckO
=
((
__m256
*
)
value
.
checkOg
)[
i
];
}
rState
=
((
__m256
*
)
value
.
stateValue
)[
i
];
rStateAtv
=
((
__m256
*
)
value
.
stateActiveValue
)[
i
];
rOutputGrad
=
((
__m256
*
)
grad
.
outputGrad
)[
i
];
...
...
paddle/operators/math/detail/lstm_gpu_kernel.h
浏览文件 @
d94c936b
...
...
@@ -55,9 +55,10 @@ __global__ void KeLstmForward(Op op, LstmMetaValue<T> value, int frameSize,
T
rValueIg
;
T
rValueFg
;
T
rValueOg
;
T
rCheckI
=
value
.
checkIg
[
frameIdx
];
T
rCheckF
=
value
.
checkFg
[
frameIdx
];
T
rCheckO
=
value
.
checkOg
[
frameIdx
];
T
rCheckI
=
value
.
checkIg
?
value
.
checkIg
[
frameIdx
]
:
0
;
T
rCheckF
=
value
.
checkFg
?
value
.
checkFg
[
frameIdx
]
:
0
;
T
rCheckO
=
value
.
checkOg
?
value
.
checkOg
[
frameIdx
]
:
0
;
rValueIn
=
value
.
gateValue
[
frameIdx
];
rValueIg
=
value
.
gateValue
[
frameIdx
+
frameSize
];
...
...
@@ -121,9 +122,10 @@ __global__ void KeLstmBackward(Op op, LstmMetaValue<T> value,
T
rStateGrad
;
T
rStateAtv
;
T
rOutputGrad
;
T
rCheckI
=
value
.
checkIg
[
frameIdx
];
T
rCheckF
=
value
.
checkFg
[
frameIdx
];
T
rCheckO
=
value
.
checkOg
[
frameIdx
];
T
rCheckI
=
value
.
checkIg
?
value
.
checkIg
[
frameIdx
]
:
0
;
T
rCheckF
=
value
.
checkFg
?
value
.
checkFg
[
frameIdx
]
:
0
;
T
rCheckO
=
value
.
checkOg
?
value
.
checkOg
[
frameIdx
]
:
0
;
T
rCheckIGrad
;
T
rCheckFGrad
;
T
rCheckOGrad
;
...
...
paddle/operators/math/sequence2batch.h
浏览文件 @
d94c936b
...
...
@@ -31,7 +31,7 @@ class CopyMatrixRowsFunctor {
// The indexed rows are based on the input index.
void
operator
()(
const
platform
::
DeviceContext
&
context
,
const
framework
::
Tensor
&
src
,
const
size_t
*
index
,
framework
::
Tensor
*
dst
,
bool
is_src_index
);
framework
::
Tensor
&
dst
,
bool
is_src_index
);
};
template
<
typename
Place
,
typename
T
>
...
...
@@ -57,7 +57,7 @@ class LoDTensor2BatchFunctor {
bool
is_reverse
=
false
)
const
{
if
(
!
is_cal_batch_lod
)
{
auto
lods
=
batch
.
lod
();
PADDLE_ENFORCE_
LE
(
lods
.
size
(),
2UL
);
PADDLE_ENFORCE_
GT
(
lods
.
size
(),
2UL
);
PADDLE_ENFORCE_EQ
(
lods
[
1
].
size
(),
static_cast
<
size_t
>
(
lod_tensor
.
dims
()[
0
]));
CopyMatrixRowsFunctor
<
Place
,
T
>
to_batch
;
...
...
@@ -68,8 +68,6 @@ class LoDTensor2BatchFunctor {
auto
lods
=
lod_tensor
.
lod
();
auto
lod
=
lods
[
0
];
PADDLE_ENFORCE_EQ
(
lods
.
size
(),
1UL
,
"Only support one level sequence now."
);
PADDLE_ENFORCE_EQ
(
lod_tensor
.
dims
()[
0
],
static_cast
<
int64_t
>
(
lod
.
size
()
-
1
));
std
::
vector
<
SeqInfo
>
seq_info
;
for
(
size_t
seq_id
=
0
;
seq_id
<
lod
.
size
()
-
1
;
++
seq_id
)
{
...
...
@@ -112,7 +110,7 @@ class LoDTensor2BatchFunctor {
int
num_batch
=
seq_info
[
0
].
length
;
batch_lods
[
0
].
resize
(
static_cast
<
size_t
>
(
num_batch
+
1
));
// batch_lods[1] is the raw index in the input LoDTensor
batch_lods
[
1
].
resize
(
static_cast
<
size_t
>
(
seq_info
.
size
()
));
batch_lods
[
1
].
resize
(
static_cast
<
size_t
>
(
lod_tensor
.
dims
()[
0
]
));
// batch_lods[2] is the sort order for the input LoDTensor.
batch_lods
[
2
].
resize
(
seq_info
.
size
());
...
...
@@ -152,8 +150,7 @@ class Batch2LoDTensorFunctor {
const
framework
::
LoDTensor
&
batch
,
framework
::
LoDTensor
&
lod_tensor
)
const
{
auto
in_lod
=
batch
.
lod
();
PADDLE_ENFORCE_LT
(
in_lod
.
size
(),
2UL
,
"The LoD size of input `batch` should be 2."
);
PADDLE_ENFORCE_GT
(
in_lod
.
size
(),
2UL
);
PADDLE_ENFORCE_EQ
(
in_lod
[
1
].
size
(),
static_cast
<
size_t
>
(
lod_tensor
.
dims
()[
0
]));
CopyMatrixRowsFunctor
<
Place
,
T
>
to_seq
;
...
...
python/paddle/v2/framework/tests/test_lstm_op.py
浏览文件 @
d94c936b
...
...
@@ -117,9 +117,9 @@ class TestLstmOp(OpTest):
self
.
act_cell
=
'tanh'
self
.
act_cand
=
'tanh'
self
.
has_initial_state
=
True
self
.
has_bias
=
True
self
.
has_initial_state
=
False
self
.
is_reverse
=
False
self
.
use_peepholes
=
True
def
setUp
(
self
):
self
.
set_argument
()
...
...
@@ -129,21 +129,27 @@ class TestLstmOp(OpTest):
N
=
len
(
self
.
lod
[
0
])
-
1
x
=
np
.
random
.
normal
(
size
=
(
T
,
4
*
self
.
D
)).
astype
(
'float64'
)
h0
=
np
.
zeros
((
N
,
self
.
D
)).
astype
(
'float64'
)
c0
=
np
.
zeros
((
N
,
self
.
D
)).
astype
(
'float64'
)
if
self
.
has_initial_state
:
h0
=
np
.
random
.
normal
(
size
=
(
N
,
self
.
D
)).
astype
(
'float64'
)
c0
=
np
.
random
.
normal
(
size
=
(
N
,
self
.
D
)).
astype
(
'float64'
)
else
:
h0
=
np
.
zeros
((
N
,
self
.
D
)).
astype
(
'float64'
)
c0
=
np
.
zeros
((
N
,
self
.
D
)).
astype
(
'float64'
)
w
=
np
.
random
.
normal
(
size
=
(
self
.
D
,
4
*
self
.
D
)).
astype
(
'float64'
)
b
=
np
.
random
.
normal
(
size
=
(
1
,
7
*
self
.
D
)).
astype
(
'float64'
)
if
self
.
use_peepholes
:
b
=
np
.
random
.
normal
(
size
=
(
1
,
7
*
self
.
D
)).
astype
(
'float64'
)
else
:
b
=
np
.
random
.
normal
(
size
=
(
1
,
4
*
self
.
D
)).
astype
(
'float64'
)
w_b
=
b
[:,
0
:
4
*
self
.
D
]
if
self
.
has_bias
else
None
w_c
=
b
[:,
4
*
self
.
D
:]
if
self
.
has_bia
s
else
None
w_b
=
b
[:,
0
:
4
*
self
.
D
]
w_c
=
b
[:,
4
*
self
.
D
:]
if
self
.
use_peephole
s
else
None
h
,
c
=
lstm
(
x
,
self
.
lod
,
h0
,
c0
,
w
,
w_b
,
w_c
,
self
.
is_reverse
,
ACTVATION
[
self
.
act_gate
],
ACTVATION
[
self
.
act_cell
],
ACTVATION
[
self
.
act_cand
])
self
.
inputs
=
{
'Input'
:
(
x
,
self
.
lod
),
'Weight'
:
w
}
if
self
.
has_bias
:
self
.
inputs
[
'Bias'
]
=
b
self
.
inputs
[
'Bias'
]
=
b
if
self
.
has_initial_state
:
self
.
inputs
[
'H0'
]
=
h0
...
...
@@ -154,18 +160,17 @@ class TestLstmOp(OpTest):
'Cell'
:
(
c
,
self
.
lod
),
}
self
.
attrs
=
{
'use_peepholes'
:
True
,
'use_peepholes'
:
self
.
use_peepholes
,
'is_reverse'
:
self
.
is_reverse
,
'gate_activation'
:
self
.
act_gate
,
'cell_activation'
:
self
.
act_cell
,
'candidate_activation'
:
self
.
act_cand
}
def
not_
test_check_output
(
self
):
def
test_check_output
(
self
):
self
.
check_output
(
atol
=
1e-8
)
#TODO(qingqing) add more unit testing case
def
not_test_check_grad
(
self
):
def
test_check_grad
(
self
):
# TODO(qingqing) remove folowing lines after the check_grad is refined.
N
=
len
(
self
.
lod
[
0
])
-
1
self
.
outputs
[
'BatchGate'
]
=
np
.
zeros
((
N
,
4
*
self
.
D
)).
astype
(
'float64'
)
...
...
@@ -174,8 +179,38 @@ class TestLstmOp(OpTest):
self
.
check_grad
(
[
'Input'
,
'Weight'
,
'Bias'
],
[
'Hidden'
],
max_relative_error
=
5e-4
)
def
test_check_grad_ingore_bias
(
self
):
N
=
len
(
self
.
lod
[
0
])
-
1
self
.
outputs
[
'BatchGate'
]
=
np
.
zeros
((
N
,
4
*
self
.
D
)).
astype
(
'float64'
)
self
.
outputs
[
'BatchCellPreAct'
]
=
np
.
zeros
(
(
N
,
self
.
D
)).
astype
(
'float64'
)
self
.
check_grad
(
[
'Input'
,
'Weight'
],
[
'Hidden'
],
max_relative_error
=
5e-4
,
no_grad_set
=
set
(
'Bias'
))
def
test_check_grad_ingore_weight
(
self
):
N
=
len
(
self
.
lod
[
0
])
-
1
self
.
outputs
[
'BatchGate'
]
=
np
.
zeros
((
N
,
4
*
self
.
D
)).
astype
(
'float64'
)
self
.
outputs
[
'BatchCellPreAct'
]
=
np
.
zeros
(
(
N
,
self
.
D
)).
astype
(
'float64'
)
self
.
check_grad
(
[
'Input'
,
'Bias'
],
[
'Hidden'
],
max_relative_error
=
5e-4
,
no_grad_set
=
set
(
'Weight'
))
def
test_check_grad_ingore_input
(
self
):
N
=
len
(
self
.
lod
[
0
])
-
1
self
.
outputs
[
'BatchGate'
]
=
np
.
zeros
((
N
,
4
*
self
.
D
)).
astype
(
'float64'
)
self
.
outputs
[
'BatchCellPreAct'
]
=
np
.
zeros
(
(
N
,
self
.
D
)).
astype
(
'float64'
)
self
.
check_grad
(
[
'Weight'
,
'Bias'
],
[
'Hidden'
],
max_relative_error
=
5e-4
,
no_grad_set
=
set
(
'Input'
))
class
TestLstmOpHas
No
Initial
(
TestLstmOp
):
class
TestLstmOpHasInitial
(
TestLstmOp
):
def
set_argument
(
self
):
self
.
lod
=
[[
0
,
2
,
5
,
7
]]
self
.
D
=
16
...
...
@@ -184,12 +219,52 @@ class TestLstmOpHasNoInitial(TestLstmOp):
self
.
act_cell
=
'tanh'
self
.
act_cand
=
'tanh'
self
.
has_initial_state
=
Fals
e
self
.
has_initial_state
=
Tru
e
self
.
is_reverse
=
True
self
.
has_bia
s
=
True
self
.
use_peephole
s
=
True
def
test_check_grad
(
self
):
# TODO(qingqing) remove folowing lines after the check_grad is refined.
N
=
len
(
self
.
lod
[
0
])
-
1
self
.
outputs
[
'BatchGate'
]
=
np
.
zeros
((
N
,
4
*
self
.
D
)).
astype
(
'float64'
)
self
.
outputs
[
'BatchCellPreAct'
]
=
np
.
zeros
(
(
N
,
self
.
D
)).
astype
(
'float64'
)
self
.
check_grad
(
[
'Input'
,
'Weight'
,
'Bias'
,
'H0'
,
'C0'
],
[
'Hidden'
],
max_relative_error
=
5e-4
)
class
TestLstmOpHasNoBias
(
TestLstmOp
):
# In order to speed up, skip following testing
def
test_check_grad_ingore_bias
(
self
):
return
def
test_check_grad_ingore_weight
(
self
):
return
def
test_check_grad_ingore_input
(
self
):
return
def
test_check_grad_ingore_h0
(
self
):
N
=
len
(
self
.
lod
[
0
])
-
1
self
.
outputs
[
'BatchGate'
]
=
np
.
zeros
((
N
,
4
*
self
.
D
)).
astype
(
'float64'
)
self
.
outputs
[
'BatchCellPreAct'
]
=
np
.
zeros
(
(
N
,
self
.
D
)).
astype
(
'float64'
)
self
.
check_grad
(
[
'Input'
,
'Weight'
,
'Bias'
,
'C0'
],
[
'Hidden'
],
max_relative_error
=
5e-4
,
no_grad_set
=
set
(
'H0'
))
def
test_check_grad_ingore_c0
(
self
):
N
=
len
(
self
.
lod
[
0
])
-
1
self
.
outputs
[
'BatchGate'
]
=
np
.
zeros
((
N
,
4
*
self
.
D
)).
astype
(
'float64'
)
self
.
outputs
[
'BatchCellPreAct'
]
=
np
.
zeros
(
(
N
,
self
.
D
)).
astype
(
'float64'
)
self
.
check_grad
(
[
'Input'
,
'Weight'
,
'Bias'
,
'H0'
],
[
'Hidden'
],
max_relative_error
=
5e-4
,
no_grad_set
=
set
(
'C0'
))
class
TestLstmOpRerverse
(
TestLstmOp
):
def
set_argument
(
self
):
self
.
lod
=
[[
0
,
2
,
5
,
7
]]
self
.
D
=
16
...
...
@@ -198,15 +273,22 @@ class TestLstmOpHasNoBias(TestLstmOp):
self
.
act_cell
=
'tanh'
self
.
act_cand
=
'tanh'
self
.
has_initial_state
=
Tru
e
self
.
is_reverse
=
Fals
e
self
.
has_bias
=
Fals
e
self
.
has_initial_state
=
Fals
e
self
.
is_reverse
=
Tru
e
self
.
use_peepholes
=
Tru
e
def
test_check_output
(
self
):
self
.
check_output
(
atol
=
1e-8
)
# In order to speed up, skip following testing
def
test_check_grad_ingore_bias
(
self
):
return
def
test_check_grad_ingore_weight
(
self
):
return
class
TestLstmOpRerverse
(
TestLstmOp
):
def
test_check_grad_ingore_input
(
self
):
return
class
TestLstmOpNotUsePeepholes
(
TestLstmOp
):
def
set_argument
(
self
):
self
.
lod
=
[[
0
,
2
,
5
,
7
]]
self
.
D
=
16
...
...
@@ -215,9 +297,19 @@ class TestLstmOpRerverse(TestLstmOp):
self
.
act_cell
=
'tanh'
self
.
act_cand
=
'tanh'
self
.
has_initial_state
=
Tru
e
self
.
has_initial_state
=
Fals
e
self
.
is_reverse
=
True
self
.
has_bias
=
True
self
.
use_peepholes
=
False
# In order to speed up, skip following testing
def
test_check_grad_ingore_bias
(
self
):
return
def
test_check_grad_ingore_weight
(
self
):
return
def
test_check_grad_ingore_input
(
self
):
return
if
__name__
==
'__main__'
:
...
...
python/paddle/v2/optimizer.py
浏览文件 @
d94c936b
...
...
@@ -102,7 +102,7 @@ class Momentum(Optimizer):
.. math::
v_{t} &= k * v_{t-1} -
\\
gamma_t
/
(g_{t} +
\\
lambda w_{t-1})
\\\\
v_{t} &= k * v_{t-1} -
\\
gamma_t (g_{t} +
\\
lambda w_{t-1})
\\\\
w_{t} &= w_{t-1} + v_{t}
\\\\
where, :math:`k` is momentum, :math:`
\\
lambda` is decay rate,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录