Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
c7eb199b
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看板
提交
c7eb199b
编写于
1月 10, 2018
作者:
Y
Yang Yu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Init commit
上级
e5e206e2
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
68 addition
and
40 deletion
+68
-40
python/paddle/v2/fluid/tests/test_parallel_op.py
python/paddle/v2/fluid/tests/test_parallel_op.py
+68
-40
未找到文件。
python/paddle/v2/fluid/tests/test_parallel_op.py
浏览文件 @
c7eb199b
import
unittest
import
unittest
import
paddle.v2.fluid.layers
as
layers
import
paddle.v2.fluid
as
fluid
import
paddle.v2.fluid
as
fluid
from
paddle.v2.fluid.framework
import
Program
import
numpy
from
paddle.v2.fluid.executor
import
Executor
from
paddle.v2.fluid.backward
import
append_backward
import
numpy
as
np
class
BaseParallelForTest
(
unittest
.
TestCase
):
import
paddle.v2.fluid.core
as
core
def
main
(
self
,
callback
,
feed
,
fetch
):
cpu
=
fluid
.
CPUPlace
()
result_cpu
=
self
.
_main_impl_
(
class
ParallelOpTest
(
unittest
.
TestCase
):
callback
=
callback
,
def
setUp
(
self
):
feed
=
feed
,
x
=
layers
.
data
(
fetch
=
fetch
,
shape
=
[
-
1
,
30
,
40
],
place
=
cpu
,
dtype
=
'float32'
,
use_parallel
=
False
)
name
=
'x'
,
print
result_cpu
append_batch_size
=
False
,
stop_gradient
=
False
)
def
_main_impl_
(
self
,
callback
,
feed
,
fetch
,
place
,
use_parallel
=
False
):
main
=
fluid
.
Program
()
places
=
layers
.
get_places
(
device_count
=
4
)
startup
=
fluid
.
Program
()
pd
=
layers
.
ParallelDo
(
places
=
places
)
# Fix seed
main
.
random_seed
=
10
with
pd
.
do
():
startup
.
random_seed
=
10
data
=
pd
.
read_input
(
x
)
hidden
=
layers
.
fc
(
input
=
data
,
size
=
7
)
with
fluid
.
program_guard
(
main
,
startup
):
pd
.
write_output
(
hidden
)
generator
=
callback
()
data
=
pd
()
# Automatically insert parallel do if use_parallel = True
loss
=
layers
.
mean
(
x
=
data
)
if
use_parallel
:
sgd_optimizer
=
fluid
.
optimizer
.
SGD
(
learning_rate
=
0.001
)
places
=
fluid
.
layers
.
get_places
()
sgd_optimizer
.
minimize
(
loss
)
pd
=
fluid
.
layers
.
ParallelDo
(
places
)
data
=
next
(
generator
)
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
())
exe
.
run
(
fluid
.
default_startup_program
())
if
isinstance
(
data
,
fluid
.
Variable
):
exe
.
run
(
fluid
.
default_main_program
(),
data
=
[
data
]
feed
=
{
with
pd
.
do
():
x
.
name
:
np
.
random
.
uniform
(
0.1
,
0.6
,
ins
=
map
(
pd
.
read_input
,
data
)
(
20
,
30
,
40
)).
astype
(
"float32"
)
if
len
(
ins
)
==
1
:
})
ins
=
ins
[
0
]
generator
.
send
(
ins
)
# patch input
def
test_forward
(
self
):
loss
=
next
(
generator
)
pass
pd
.
write_output
(
loss
)
loss
=
pd
()
else
:
data
=
next
(
generator
)
generator
.
send
(
data
)
loss
=
next
(
generator
)
avg_loss
=
fluid
.
layers
.
mean
(
x
=
loss
)
fluid
.
backward
.
append_backward
(
loss
=
avg_loss
)
exe
=
fluid
.
Executor
(
place
)
exe
.
run
(
startup
)
return
exe
.
run
(
main
,
feed
=
feed
,
fetch_list
=
fetch
)
class
ParallelOpTest
(
BaseParallelForTest
):
def
test_simple_fc
(
self
):
def
__network__
():
x
=
fluid
.
layers
.
data
(
shape
=
[
784
],
dtype
=
'float32'
,
name
=
'img'
)
x
=
yield
x
hidden
=
fluid
.
layers
.
fc
(
input
=
x
,
size
=
200
,
param_attr
=
'fc1.w'
)
loss
=
fluid
.
layers
.
mean
(
x
=
hidden
)
yield
loss
self
.
main
(
callback
=
__network__
,
feed
=
{
'img'
:
numpy
.
random
.
random
(
size
=
(
128
,
784
)).
astype
(
'float32'
)
},
fetch
=
'fc1.w@GRAD'
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录