Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
e9a92e3e
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
e9a92e3e
编写于
8月 08, 2017
作者:
S
superjom
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add fc test
上级
001b62a4
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
47 addition
and
32 deletion
+47
-32
python/paddle/v2/framework/tests/test_fc_op.py
python/paddle/v2/framework/tests/test_fc_op.py
+20
-19
python/paddle/v2/framework/tests/test_recurrent_op.py
python/paddle/v2/framework/tests/test_recurrent_op.py
+27
-13
未找到文件。
python/paddle/v2/framework/tests/test_fc_op.py
浏览文件 @
e9a92e3e
import
paddle.v2.framework.core
as
core
import
unittest
import
numpy
import
numpy
as
np
import
paddle.v2.framework.core
as
core
from
paddle.v2.framework.op
import
Operator
class
TestFc
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
x_np_data
=
np
.
random
.
random
((
1000
,
784
))
self
.
W_np_data
=
np
.
random
.
random
((
784
,
100
))
def
test_fc
(
self
):
scope
=
core
.
Scope
()
place
=
core
.
CPUPlace
()
x
=
scope
.
new_var
(
"X"
)
x_tensor
=
x
.
get_tensor
()
x_tensor
.
set_dims
([
1000
,
784
])
x_tensor
.
alloc_float
(
place
)
x_tensor
=
scope
.
new_var
(
"X"
).
get_tensor
()
x_tensor
.
set_dims
(
self
.
x_np_data
.
shape
)
x_tensor
.
set
(
self
.
x_np_data
,
place
)
w
=
scope
.
new_var
(
"W"
)
w_tensor
=
w
.
get_tensor
()
w_tensor
.
set_dims
([
784
,
100
])
w_tensor
.
alloc_float
(
place
)
w_tensor
.
set
(
numpy
.
random
.
random
((
784
,
100
)).
astype
(
"float32"
),
place
)
# Set a real numpy array here.
# x_tensor.set(numpy.array([]))
W_tensor
=
scope
.
new_var
(
"W"
).
get_tensor
()
W_tensor
.
set_dims
(
self
.
W_np_data
.
shape
)
W_tensor
.
set
(
self
.
W_np_data
,
place
)
op
=
Operator
(
"fc"
,
X
=
"X"
,
Y
=
"Y"
,
W
=
"W"
)
...
...
@@ -30,15 +26,20 @@ class TestFc(unittest.TestCase):
if
scope
.
find_var
(
out
)
is
None
:
scope
.
new_var
(
out
).
get_tensor
()
tensor
=
scope
.
find_var
(
"Y"
).
get_tensor
()
Y_
tensor
=
scope
.
find_var
(
"Y"
).
get_tensor
()
op
.
infer_shape
(
scope
)
self
.
assertEqual
([
1000
,
100
],
tensor
.
shape
())
self
.
assertEqual
([
1000
,
100
],
Y_
tensor
.
shape
())
ctx
=
core
.
DeviceContext
.
create
(
place
)
op
.
run
(
scope
,
ctx
)
# After complete all ops, check Y is expect or not.
py_data
=
np
.
matmul
(
self
.
x_np_data
,
self
.
W_np_data
)
op_data
=
np
.
array
(
Y_tensor
)
print
py_data
-
op_data
self
.
assertTrue
(
np
.
allclose
(
py_data
,
op_data
))
if
__name__
==
'__main__'
:
...
...
python/paddle/v2/framework/tests/test_recurrent_op.py
浏览文件 @
e9a92e3e
...
...
@@ -6,8 +6,7 @@ from paddle.v2.framework.op import Operator
def
py_sigmoid
(
x
):
return
1.
/
(
1
+
np
.
exp
(
-
x
))
return
1.
/
(
1.
+
np
.
exp
(
-
x
))
class
PySimpleRNN
(
object
):
'''
...
...
@@ -62,10 +61,10 @@ class PySimpleRNNTest(unittest.TestCase):
print
'output'
,
output
def
create_tensor
(
scope
,
name
,
shape
):
def
create_tensor
(
scope
,
name
,
shape
,
np_data
):
tensor
=
scope
.
new_var
(
name
).
get_tensor
()
tensor
.
set_dims
(
shape
)
tensor
.
set
(
np
.
random
.
random
(
shape
)
,
core
.
CPUPlace
())
tensor
.
set
(
np
_data
,
core
.
CPUPlace
())
return
tensor
...
...
@@ -91,25 +90,36 @@ class TestRecurrentOp(unittest.TestCase):
weight_dim
=
15
sent_len
=
11
def
forward
(
self
):
def
setUp
(
self
):
self
.
py_rnn
=
PySimpleRNN
(
self
.
input_dim
,
self
.
batch_size
,
self
.
weight_dim
,
self
.
sent_len
)
self
.
scope
=
core
.
Scope
()
def
forward
(
self
):
self
.
scope
=
core
.
Scope
()
self
.
create_global_variables
()
self
.
create_step_net
()
rnn_op
=
self
.
create_rnn_op
()
ctx
=
core
.
DeviceContext
.
create
(
core
.
CPUPlace
())
print
'infer_shape'
rnn_op
.
infer_shape
(
self
.
scope
)
rnn_op
.
run
(
self
.
scope
,
ctx
)
return
np
.
array
(
self
.
scope
.
find_var
(
"h"
).
get_tensor
())
def
create_global_variables
(
self
):
# create inlink
x_np_data
=
self
.
py_rnn
.
x
create_tensor
(
self
.
scope
,
"x"
,
[
self
.
sent_len
,
self
.
batch_size
,
self
.
input_dim
])
create_tensor
(
self
.
scope
,
"W"
,
[
self
.
input_dim
,
self
.
input_dim
])
create_tensor
(
self
.
scope
,
"U"
,
[
self
.
input_dim
,
self
.
input_dim
])
create_tensor
(
self
.
scope
,
"h_boot"
,
[
self
.
batch_size
,
self
.
input_dim
])
[
self
.
sent_len
,
self
.
batch_size
,
self
.
input_dim
],
x_np_data
)
W_np_data
=
self
.
py_rnn
.
W
create_tensor
(
self
.
scope
,
"W"
,
[
self
.
input_dim
,
self
.
input_dim
],
W_np_data
)
U_np_data
=
self
.
py_rnn
.
U
create_tensor
(
self
.
scope
,
"U"
,
[
self
.
input_dim
,
self
.
input_dim
],
U_np_data
)
h_boot_np_data
=
self
.
py_rnn
.
h_boot
create_tensor
(
self
.
scope
,
"h_boot"
,
[
self
.
batch_size
,
self
.
input_dim
],
h_boot_np_data
)
self
.
scope
.
new_var
(
"step_scopes"
)
self
.
scope
.
new_var
(
"h@alias"
)
self
.
scope
.
new_var
(
"h"
)
...
...
@@ -146,8 +156,12 @@ class TestRecurrentOp(unittest.TestCase):
def
test_forward
(
self
):
print
'test recurrent op forward'
self
.
forward
()
pd_output
=
self
.
forward
()
py_output
=
self
.
py_rnn
.
forward
()
print
'pd_output'
,
pd_output
print
print
'py_output'
,
py_output
self
.
assertEqual
(
pd_output
.
shape
,
py_output
.
shape
)
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录