Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
s920243400
PaddleDetection
提交
a994327f
P
PaddleDetection
项目概览
s920243400
/
PaddleDetection
与 Fork 源项目一致
Fork自
PaddlePaddle / PaddleDetection
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
a994327f
编写于
4月 03, 2018
作者:
Q
qiaolongfei
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add TestSGDOpOptimizeSelectedRows
上级
abb7deee
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
34 addition
and
29 deletion
+34
-29
python/paddle/fluid/tests/unittests/test_sgd_op.py
python/paddle/fluid/tests/unittests/test_sgd_op.py
+34
-29
未找到文件。
python/paddle/fluid/tests/unittests/test_sgd_op.py
浏览文件 @
a994327f
...
...
@@ -101,31 +101,50 @@ class TestSGDOpOptimizeSelectedRows(unittest.TestCase):
def
check_with_place
(
self
,
place
):
scope
=
core
.
Scope
()
row_width
=
12
# create and initialize Grad Variable
height
=
10
rows
=
[
0
,
4
,
7
]
row_numel
=
12
grad_height
=
10
grad_rows
=
[
0
,
4
,
7
]
grad_selected_rows
=
scope
.
var
(
'Grad'
).
get_selected_rows
()
grad_selected_rows
.
set_height
(
height
)
grad_selected_rows
.
set_rows
(
rows
)
np_array
=
np
.
ones
((
len
(
rows
),
row_numel
)).
astype
(
"float32"
)
np
_array
[
0
,
0
]
=
2.0
np
_array
[
2
,
8
]
=
4.0
grad_selected_rows
.
set_height
(
grad_
height
)
grad_selected_rows
.
set_rows
(
grad_
rows
)
grad_array
=
np
.
ones
((
len
(
grad_rows
),
row_width
)).
astype
(
"float32"
)
grad
_array
[
0
,
0
]
=
2.0
grad
_array
[
2
,
8
]
=
4.0
grad_tensor
=
grad_selected_rows
.
get_tensor
()
grad_tensor
.
set
(
np
_array
,
place
)
grad_tensor
.
set
(
grad
_array
,
place
)
# create and initialize Param Variable
param
=
scope
.
var
(
'Param'
).
get_tensor
()
param_array
=
np
.
full
((
height
,
row_numel
),
5.0
).
astype
(
"float32"
)
param
.
set
(
param_array
,
place
)
# create and initialize W Variable
param_rows
=
[
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
]
# init Param
w_selected_rows
=
scope
.
var
(
'Param'
).
get_selected_rows
()
w_selected_rows
.
set_height
(
len
(
param_rows
))
w_selected_rows
.
set_rows
(
param_rows
)
w_array
=
np
.
ones
((
len
(
param_rows
),
row_width
)).
astype
(
"float32"
)
for
i
in
range
(
len
(
param_rows
)):
w_array
[
i
]
*=
i
w_tensor
=
w_selected_rows
.
get_tensor
()
w_tensor
.
set
(
w_array
,
place
)
w_before_optimize
=
np
.
array
(
w_tensor
)
print
(
w_before_optimize
)
# create and initialize LeraningRate Variable
lr_value
=
0.1
lr
=
scope
.
var
(
'LearningRate'
).
get_tensor
()
lr_array
=
np
.
full
((
1
),
2.0
).
astype
(
"float32"
)
lr_array
=
np
.
full
((
1
),
lr_value
).
astype
(
"float32"
)
lr
.
set
(
lr_array
,
place
)
# optimize with Python
w_after_optimize
=
np
.
copy
(
w_before_optimize
)
for
index
,
id
in
enumerate
(
grad_rows
):
w_after_optimize
[
id
]
=
w_before_optimize
[
id
]
-
lr_value
*
grad_array
[
index
]
# create and run sgd operator
sgd_op
=
Operator
(
"sgd"
,
...
...
@@ -136,22 +155,8 @@ class TestSGDOpOptimizeSelectedRows(unittest.TestCase):
sgd_op
.
run
(
scope
,
place
)
# get and compare result
result_array
=
np
.
array
(
param
)
# rows[0] = 0, 5.0 - 2.0 * 2.0
self
.
assertAlmostEqual
(
1.0
,
result_array
[
rows
[
0
],
0
])
# rows[0] = 0, 5.0 - 2.0 * 1.0
self
.
assertAlmostEqual
(
3.0
,
result_array
[
rows
[
0
],
2
])
# 5.0 - 2.0 * 0.0
self
.
assertAlmostEqual
(
5.0
,
result_array
[
1
,
0
])
# rows[1] = 4, 5.0 - 2.0 * 1.0
self
.
assertAlmostEqual
(
3.0
,
result_array
[
rows
[
1
],
10
])
# 5.0 - 2.0 * 0.0
self
.
assertAlmostEqual
(
5.0
,
result_array
[
5
,
8
])
# rows[2] = 7, 5.0 - 2.0 * 1.0
self
.
assertAlmostEqual
(
3.0
,
result_array
[
rows
[
2
],
1
])
# rows[2] = 7, 5.0 - 2.0 * 4.0
self
.
assertAlmostEqual
(
-
3.0
,
result_array
[
rows
[
2
],
8
])
result_array
=
np
.
array
(
w_tensor
)
assert
(
result_array
==
w_after_optimize
).
all
()
def
test_sparse_sgd
(
self
):
places
=
[
core
.
CPUPlace
()]
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录