Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
f158829d
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看板
提交
f158829d
编写于
10月 11, 2018
作者:
S
sneaxiy
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
test=develop
上级
6330c140
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
151 addition
and
107 deletion
+151
-107
paddle/fluid/operators/rmsprop_op.h
paddle/fluid/operators/rmsprop_op.h
+16
-16
python/paddle/fluid/tests/unittests/test_rmsprop_op.py
python/paddle/fluid/tests/unittests/test_rmsprop_op.py
+135
-91
未找到文件。
paddle/fluid/operators/rmsprop_op.h
浏览文件 @
f158829d
...
@@ -131,21 +131,21 @@ template <typename DeviceContext, typename T>
...
@@ -131,21 +131,21 @@ template <typename DeviceContext, typename T>
class
RmspropOpKernel
:
public
framework
::
OpKernel
<
T
>
{
class
RmspropOpKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
using
Tensor
=
framework
::
LoDTensor
;
using
LoD
Tensor
=
framework
::
LoDTensor
;
auto
*
grad_var
=
ctx
.
InputVar
(
"Grad"
);
auto
*
grad_var
=
ctx
.
InputVar
(
"Grad"
);
auto
*
param_out
=
ctx
.
Output
<
Tensor
>
(
"ParamOut"
);
auto
*
param_out
=
ctx
.
Output
<
LoD
Tensor
>
(
"ParamOut"
);
auto
*
moment_out
=
ctx
.
Output
<
Tensor
>
(
"MomentOut"
);
auto
*
moment_out
=
ctx
.
Output
<
LoD
Tensor
>
(
"MomentOut"
);
auto
*
mean_square_out
=
ctx
.
Output
<
Tensor
>
(
"MeanSquareOut"
);
auto
*
mean_square_out
=
ctx
.
Output
<
LoD
Tensor
>
(
"MeanSquareOut"
);
auto
epsilon
=
static_cast
<
T
>
(
ctx
.
Attr
<
float
>
(
"epsilon"
));
auto
epsilon
=
static_cast
<
T
>
(
ctx
.
Attr
<
float
>
(
"epsilon"
));
auto
rho
=
static_cast
<
T
>
(
ctx
.
Attr
<
float
>
(
"decay"
));
auto
rho
=
static_cast
<
T
>
(
ctx
.
Attr
<
float
>
(
"decay"
));
auto
momentum
=
static_cast
<
T
>
(
ctx
.
Attr
<
float
>
(
"momentum"
));
auto
momentum
=
static_cast
<
T
>
(
ctx
.
Attr
<
float
>
(
"momentum"
));
bool
centered
=
ctx
.
Attr
<
bool
>
(
"centered"
);
bool
centered
=
ctx
.
Attr
<
bool
>
(
"centered"
);
auto
&
p_tensor
=
*
ctx
.
Input
<
Tensor
>
(
"Param"
);
auto
&
p_tensor
=
*
ctx
.
Input
<
LoD
Tensor
>
(
"Param"
);
auto
&
ms_tensor
=
*
ctx
.
Input
<
Tensor
>
(
"MeanSquare"
);
auto
&
ms_tensor
=
*
ctx
.
Input
<
LoD
Tensor
>
(
"MeanSquare"
);
auto
&
lr_tensor
=
*
ctx
.
Input
<
Tensor
>
(
"LearningRate"
);
auto
&
lr_tensor
=
*
ctx
.
Input
<
LoD
Tensor
>
(
"LearningRate"
);
auto
&
mom_tensor
=
*
ctx
.
Input
<
Tensor
>
(
"Moment"
);
auto
&
mom_tensor
=
*
ctx
.
Input
<
LoD
Tensor
>
(
"Moment"
);
PADDLE_ENFORCE_EQ
(
&
p_tensor
,
param_out
,
PADDLE_ENFORCE_EQ
(
&
p_tensor
,
param_out
,
"Param and ParamOut must be the same Tensor"
);
"Param and ParamOut must be the same Tensor"
);
...
@@ -157,8 +157,8 @@ class RmspropOpKernel : public framework::OpKernel<T> {
...
@@ -157,8 +157,8 @@ class RmspropOpKernel : public framework::OpKernel<T> {
auto
&
dev_ctx
=
ctx
.
template
device_context
<
DeviceContext
>();
auto
&
dev_ctx
=
ctx
.
template
device_context
<
DeviceContext
>();
size_t
limit
=
static_cast
<
size_t
>
(
ms_tensor
.
numel
());
size_t
limit
=
static_cast
<
size_t
>
(
ms_tensor
.
numel
());
if
(
grad_var
->
IsType
<
Tensor
>
())
{
if
(
grad_var
->
IsType
<
LoD
Tensor
>
())
{
auto
&
grad_tensor
=
grad_var
->
Get
<
Tensor
>
();
auto
&
grad_tensor
=
grad_var
->
Get
<
LoD
Tensor
>
();
if
(
std
::
is_same
<
DeviceContext
,
platform
::
CPUDeviceContext
>::
value
)
{
if
(
std
::
is_same
<
DeviceContext
,
platform
::
CPUDeviceContext
>::
value
)
{
auto
&
place
=
auto
&
place
=
...
@@ -176,9 +176,9 @@ class RmspropOpKernel : public framework::OpKernel<T> {
...
@@ -176,9 +176,9 @@ class RmspropOpKernel : public framework::OpKernel<T> {
ms_out
.
device
(
place
)
=
rho
*
ms
+
(
1
-
rho
)
*
g
*
g
;
ms_out
.
device
(
place
)
=
rho
*
ms
+
(
1
-
rho
)
*
g
*
g
;
if
(
centered
)
{
if
(
centered
)
{
auto
&
mg_tensor
=
*
ctx
.
Input
<
Tensor
>
(
"MeanGrad"
);
auto
&
mg_tensor
=
*
ctx
.
Input
<
LoD
Tensor
>
(
"MeanGrad"
);
auto
mg
=
EigenVector
<
T
>::
Flatten
(
mg_tensor
);
auto
mg
=
EigenVector
<
T
>::
Flatten
(
mg_tensor
);
auto
*
mean_grad_out
=
ctx
.
Output
<
Tensor
>
(
"MeanGradOut"
);
auto
*
mean_grad_out
=
ctx
.
Output
<
LoD
Tensor
>
(
"MeanGradOut"
);
PADDLE_ENFORCE
(
&
mg_tensor
,
mean_grad_out
,
PADDLE_ENFORCE
(
&
mg_tensor
,
mean_grad_out
,
"MeanGrad and MeanGradOut must be the same Tensor"
);
"MeanGrad and MeanGradOut must be the same Tensor"
);
auto
mg_out
=
EigenVector
<
T
>::
Flatten
(
*
mean_grad_out
);
auto
mg_out
=
EigenVector
<
T
>::
Flatten
(
*
mean_grad_out
);
...
@@ -196,8 +196,8 @@ class RmspropOpKernel : public framework::OpKernel<T> {
...
@@ -196,8 +196,8 @@ class RmspropOpKernel : public framework::OpKernel<T> {
DenseRmspropGradFunctor
<
T
>
grad_func
(
grad_tensor
.
data
<
T
>
());
DenseRmspropGradFunctor
<
T
>
grad_func
(
grad_tensor
.
data
<
T
>
());
platform
::
ForRange
<
DeviceContext
>
for_range
(
dev_ctx
,
limit
);
platform
::
ForRange
<
DeviceContext
>
for_range
(
dev_ctx
,
limit
);
if
(
centered
)
{
if
(
centered
)
{
auto
&
mg_tensor
=
*
ctx
.
Input
<
Tensor
>
(
"MeanGrad"
);
auto
&
mg_tensor
=
*
ctx
.
Input
<
LoD
Tensor
>
(
"MeanGrad"
);
auto
*
mean_grad_out
=
ctx
.
Output
<
Tensor
>
(
"MeanGradOut"
);
auto
*
mean_grad_out
=
ctx
.
Output
<
LoD
Tensor
>
(
"MeanGradOut"
);
PADDLE_ENFORCE
(
&
mg_tensor
,
mean_grad_out
,
PADDLE_ENFORCE
(
&
mg_tensor
,
mean_grad_out
,
"MeanGrad and MeanGradOut must be the same Tensor"
);
"MeanGrad and MeanGradOut must be the same Tensor"
);
for_range
(
CenteredRmspropFunctor
<
T
,
DenseRmspropGradFunctor
<
T
>>
(
for_range
(
CenteredRmspropFunctor
<
T
,
DenseRmspropGradFunctor
<
T
>>
(
...
@@ -241,8 +241,8 @@ class RmspropOpKernel : public framework::OpKernel<T> {
...
@@ -241,8 +241,8 @@ class RmspropOpKernel : public framework::OpKernel<T> {
row_numel
,
row_count
);
row_numel
,
row_count
);
if
(
centered
)
{
if
(
centered
)
{
auto
&
mg_tensor
=
*
ctx
.
Input
<
Tensor
>
(
"MeanGrad"
);
auto
&
mg_tensor
=
*
ctx
.
Input
<
LoD
Tensor
>
(
"MeanGrad"
);
auto
*
mean_grad_out
=
ctx
.
Output
<
Tensor
>
(
"MeanGradOut"
);
auto
*
mean_grad_out
=
ctx
.
Output
<
LoD
Tensor
>
(
"MeanGradOut"
);
PADDLE_ENFORCE
(
&
mg_tensor
,
mean_grad_out
,
PADDLE_ENFORCE
(
&
mg_tensor
,
mean_grad_out
,
"MeanGrad and MeanGradOut must be the same Tensor"
);
"MeanGrad and MeanGradOut must be the same Tensor"
);
for_range
(
CenteredRmspropFunctor
<
T
,
SparseRmspropGradFunctor
<
T
>>
(
for_range
(
CenteredRmspropFunctor
<
T
,
SparseRmspropGradFunctor
<
T
>>
(
...
...
python/paddle/fluid/tests/unittests/test_rmsprop_op.py
浏览文件 @
f158829d
...
@@ -19,29 +19,72 @@ import unittest
...
@@ -19,29 +19,72 @@ import unittest
import
numpy
as
np
import
numpy
as
np
import
paddle.fluid.core
as
core
import
paddle.fluid.core
as
core
from
paddle.fluid.op
import
Operator
from
paddle.fluid.op
import
Operator
import
paddle.fluid
as
fluid
def
create_selected_rows_and_tensor
(
scope
,
place
,
height
,
row_num
,
embedding_size
):
sr
=
scope
.
var
(
"@selected_rows@"
).
get_selected_rows
()
tensor
=
scope
.
var
(
"grad"
).
get_tensor
()
rows
=
np
.
random
.
random_integers
(
low
=
0
,
high
=
height
-
1
,
size
=
[
row_num
,
]).
astype
(
'int64'
)
sr_val
=
np
.
random
.
random
(
size
=
[
row_num
,
embedding_size
]).
astype
(
'float32'
)
sr
.
set_height
(
height
)
sr
.
set_rows
(
rows
)
sr
.
get_tensor
().
set
(
sr_val
,
place
)
tensor_val
=
np
.
zeros
(
shape
=
[
height
,
embedding_size
],
dtype
=
'float32'
)
for
i
in
range
(
row_num
):
row
=
rows
[
i
]
tensor_val
[
row
,
:]
=
tensor_val
[
row
,
:]
+
sr_val
[
i
,
:]
tensor
.
set
(
tensor_val
,
place
)
return
tensor_val
,
sr_val
class
TestBase
(
unittest
.
TestCase
):
class
TestBase
(
unittest
.
TestCase
):
def
setup
(
self
,
centered
,
epsilon
=
1e-6
):
def
setup
(
self
,
place
,
is_sparse
,
centered
,
size
,
row_num
=
None
,
epsilon
=
1e-6
):
np
.
random
.
seed
(
5
)
# fix seed
np
.
random
.
seed
(
5
)
# fix seed
self
.
scope
=
fluid
.
global_scope
()
self
.
place
=
place
self
.
param_name
=
"param"
self
.
param_name
=
"param"
self
.
param
=
np
.
random
.
random
(
(
123
,
321
)
).
astype
(
"float32"
)
self
.
param
=
np
.
random
.
random
(
size
).
astype
(
"float32"
)
self
.
mean_square_name
=
"mean_square"
self
.
mean_square_name
=
"mean_square"
self
.
mean_square
=
np
.
random
.
random
((
123
,
321
)).
astype
(
"float32"
)
self
.
mean_square
=
np
.
random
.
uniform
(
low
=
1
,
high
=
2
,
size
=
size
).
astype
(
"float32"
)
self
.
mean_grad_name
=
"mean_grad"
self
.
mean_grad_name
=
"mean_grad"
self
.
mean_grad
=
np
.
random
.
random
(
(
123
,
321
)
).
astype
(
"float32"
)
self
.
mean_grad
=
np
.
random
.
random
(
size
).
astype
(
"float32"
)
self
.
lr_name
=
"lr"
self
.
lr_name
=
"lr"
self
.
learning_rate
=
np
.
array
([
0.01
]).
astype
(
"float32"
)
self
.
learning_rate
=
np
.
array
([
0.01
]).
astype
(
"float32"
)
self
.
grad_name
=
"grad"
self
.
grad_name
=
"grad"
self
.
grad
=
np
.
random
.
random
((
123
,
321
)).
astype
(
"float32"
)
self
.
is_sparse
=
is_sparse
if
self
.
is_sparse
:
self
.
grad_sr_name
=
"@selected_rows@"
self
.
grad
,
self
.
grad_sr
=
create_selected_rows_and_tensor
(
self
.
scope
,
place
,
size
[
0
],
row_num
,
size
[
1
])
else
:
self
.
grad
=
np
.
random
.
random
(
size
).
astype
(
"float32"
)
grad_tensor
=
self
.
scope
.
var
(
self
.
grad_name
).
get_tensor
()
grad_tensor
.
set
(
self
.
grad
,
place
)
self
.
moment_name
=
"moment"
self
.
moment_name
=
"moment"
self
.
moment
=
np
.
zeros
((
123
,
321
)).
astype
(
"float32"
)
self
.
moment
=
np
.
random
.
uniform
(
low
=
0
,
high
=
1
,
size
=
size
).
astype
(
"float32"
)
self
.
epsilon
=
epsilon
self
.
epsilon
=
epsilon
self
.
decay
=
0.9
self
.
decay
=
0.9
...
@@ -61,118 +104,119 @@ class TestBase(unittest.TestCase):
...
@@ -61,118 +104,119 @@ class TestBase(unittest.TestCase):
self
.
param_out
=
self
.
param
-
self
.
moment_out
self
.
param_out
=
self
.
param
-
self
.
moment_out
def
check
(
self
,
actual_t
,
expect_t
,
place
,
out_name
,
atol
=
1e-5
,
equal_nan
=
False
):
self
.
assertTrue
(
np
.
allclose
(
actual_t
,
expect_t
,
atol
=
atol
,
equal_nan
=
equal_nan
),
"Output ("
+
out_name
+
") has diff at "
+
str
(
place
)
+
"
\n
Expect "
+
str
(
expect_t
)
+
"
\n
"
+
"But Got"
+
str
(
actual_t
))
class
TestRmspropOp
(
TestBase
):
def
check_with_place
(
self
,
place
,
centered
,
epsilon
):
self
.
setup
(
centered
,
epsilon
)
scope
=
core
.
Scope
()
# create and initialize Param Variable
# create and initialize Param Variable
param
=
scope
.
var
(
self
.
param_name
).
get_tensor
()
self
.
param_tensor
=
self
.
scope
.
var
(
self
.
param_name
).
get_tensor
()
param
.
set
(
self
.
param
,
place
)
self
.
param_tensor
.
set
(
self
.
param
,
place
)
mean_square
=
scope
.
var
(
self
.
mean_square_name
).
get_tensor
()
self
.
mean_square_tensor
=
self
.
scope
.
var
(
mean_square
.
set
(
self
.
mean_square
,
place
)
self
.
mean_square_name
).
get_tensor
()
self
.
mean_square_tensor
.
set
(
self
.
mean_square
,
place
)
lr
=
scope
.
var
(
self
.
lr_name
).
get_tensor
()
lr
=
s
elf
.
s
cope
.
var
(
self
.
lr_name
).
get_tensor
()
lr
.
set
(
self
.
learning_rate
,
place
)
lr
.
set
(
self
.
learning_rate
,
place
)
grad
=
scope
.
var
(
self
.
grad
_name
).
get_tensor
()
self
.
moment_tensor
=
self
.
scope
.
var
(
self
.
moment
_name
).
get_tensor
()
grad
.
set
(
self
.
grad
,
place
)
self
.
moment_tensor
.
set
(
self
.
moment
,
place
)
moment
=
scope
.
var
(
self
.
moment_name
).
get_tensor
()
if
self
.
centered
:
moment
.
set
(
self
.
moment
,
place
)
self
.
mean_grad_tensor
=
self
.
scope
.
var
(
self
.
mean_grad_name
).
get_tensor
()
self
.
mean_grad_tensor
.
set
(
self
.
mean_grad
,
place
)
# create and run sgd operator
def
check
(
self
,
actual_t
,
expect_t
,
place
,
out_name
,
atol
=
1e-5
):
self
.
assertTrue
(
np
.
allclose
(
actual_t
,
expect_t
,
atol
=
atol
),
"Output ("
+
out_name
+
") has diff at "
+
str
(
place
)
+
"
\n
Expect "
+
str
(
expect_t
)
+
"
\n
"
+
"But Got"
+
str
(
actual_t
))
if
self
.
centered
:
mean_grad
=
scope
.
var
(
self
.
mean_grad_name
).
get_tensor
()
class
TestRmspropOp
(
TestBase
):
mean_grad
.
set
(
self
.
mean_grad
,
place
)
def
check_with_place
(
self
,
place
,
rmsprop_op
=
Operator
(
is_sparse
,
"rmsprop"
,
centered
,
Param
=
self
.
param_name
,
size
,
Grad
=
self
.
grad_name
,
row_num
=
None
,
MeanSquare
=
self
.
mean_square_name
,
epsilon
=
1e-6
):
MeanGrad
=
self
.
mean_grad_name
,
self
.
setup
(
place
,
is_sparse
,
centered
,
size
,
row_num
,
epsilon
)
Moment
=
self
.
moment_name
,
self
.
run_and_check
()
LearningRate
=
self
.
lr_name
,
ParamOut
=
self
.
param_name
,
def
run_and_check
(
self
):
MeanSquareOut
=
self
.
mean_square_name
,
grad_name
=
self
.
grad_sr_name
if
self
.
is_sparse
else
self
.
grad_name
MomentOut
=
self
.
moment_name
,
MeanGradOut
=
self
.
mean_grad_name
,
kwargs
=
{
epsilon
=
self
.
epsilon
,
'Param'
:
self
.
param_name
,
decay
=
self
.
decay
,
'Grad'
:
grad_name
,
momentum
=
self
.
momentum
,
'MeanSquare'
:
self
.
mean_square_name
,
centered
=
True
)
'Moment'
:
self
.
moment_name
,
else
:
'LearningRate'
:
self
.
lr_name
,
rmsprop_op
=
Operator
(
'ParamOut'
:
self
.
param_name
,
"rmsprop"
,
'MeanSquareOut'
:
self
.
mean_square_name
,
Param
=
self
.
param_name
,
'MomentOut'
:
self
.
moment_name
,
Grad
=
self
.
grad_name
,
'epsilon'
:
self
.
epsilon
,
MeanSquare
=
self
.
mean_square_name
,
'decay'
:
self
.
decay
,
Moment
=
self
.
moment_name
,
'momentum'
:
self
.
momentum
,
LearningRate
=
self
.
lr_name
,
'centered'
:
self
.
centered
ParamOut
=
self
.
param_name
,
}
MeanSquareOut
=
self
.
mean_square_name
,
MomentOut
=
self
.
moment_name
,
epsilon
=
self
.
epsilon
,
decay
=
self
.
decay
,
momentum
=
self
.
momentum
,
centered
=
False
)
rmsprop_op
.
run
(
scope
,
place
)
atol
=
1e-5
equal_nan
=
False
if
self
.
centered
:
if
self
.
centered
:
atol
=
1e-3
kwargs
[
'MeanGrad'
]
=
self
.
mean_grad_name
equal_nan
=
True
kwargs
[
'MeanGradOut'
]
=
self
.
mean_grad_name
rmsprop_op
=
Operator
(
'rmsprop'
,
**
kwargs
)
atol
=
1e-6
rmsprop_op
.
run
(
self
.
scope
,
self
.
place
)
self
.
check
(
self
.
check
(
np
.
array
(
mean_square
),
self
.
ms_out
,
place
,
self
.
mean_square_name
)
np
.
array
(
self
.
mean_square_tensor
),
self
.
ms_out
,
self
.
place
,
self
.
mean_square_name
)
self
.
check
(
self
.
check
(
np
.
array
(
moment
),
np
.
array
(
self
.
moment_tensor
),
self
.
moment_out
,
self
.
moment_out
,
place
,
self
.
place
,
self
.
moment_name
,
self
.
moment_name
,
atol
=
atol
,
atol
=
atol
)
equal_nan
=
equal_nan
)
self
.
check
(
self
.
check
(
np
.
array
(
param
),
np
.
array
(
self
.
param_tensor
),
self
.
param_out
,
self
.
param_out
,
place
,
self
.
place
,
self
.
param_name
,
self
.
param_name
,
atol
=
atol
,
atol
=
atol
)
equal_nan
=
equal_nan
)
if
self
.
centered
:
if
self
.
centered
:
self
.
check
(
self
.
check
(
np
.
array
(
mean_grad
),
self
.
mg_out
,
place
,
self
.
mean_grad_name
)
np
.
array
(
self
.
mean_grad_tensor
),
self
.
mg_out
,
self
.
place
,
self
.
mean_grad_name
)
def
test_rmsprop
(
self
):
def
test_rmsprop
(
self
):
places
=
[
core
.
CPUPlace
()]
places
=
[
core
.
CPUPlace
()]
if
core
.
is_compiled_with_cuda
():
if
core
.
is_compiled_with_cuda
():
places
.
append
(
core
.
CUDAPlace
(
0
))
places
.
append
(
core
.
CUDAPlace
(
0
))
size
=
(
128
,
320
)
for
place
in
places
:
for
place
in
places
:
self
.
check_with_place
(
place
,
False
,
1e-6
)
for
centered
in
[
False
,
True
]:
self
.
check_with_place
(
place
,
False
,
1e-10
)
with
fluid
.
scope_guard
(
core
.
Scope
()):
self
.
check_with_place
(
place
,
True
,
1e-6
)
self
.
check_with_place
(
self
.
check_with_place
(
place
,
True
,
1e-10
)
place
,
is_sparse
=
False
,
centered
=
centered
,
size
=
size
)
with
fluid
.
scope_guard
(
core
.
Scope
()):
self
.
check_with_place
(
place
,
is_sparse
=
True
,
centered
=
centered
,
row_num
=
512
,
size
=
size
)
with
fluid
.
scope_guard
(
core
.
Scope
()):
self
.
check_with_place
(
place
,
is_sparse
=
True
,
centered
=
centered
,
row_num
=
60
,
size
=
size
)
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录