Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
0d1bc8ab
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0d1bc8ab
编写于
8月 14, 2017
作者:
S
superjom
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix res
上级
b1b71eab
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
25 addition
and
63 deletion
+25
-63
python/paddle/v2/framework/tests/test_fc_op.py
python/paddle/v2/framework/tests/test_fc_op.py
+0
-44
python/paddle/v2/framework/tests/test_recurrent_op.py
python/paddle/v2/framework/tests/test_recurrent_op.py
+25
-19
未找到文件。
python/paddle/v2/framework/tests/test_fc_op.py
已删除
100644 → 0
浏览文件 @
b1b71eab
import
unittest
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_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_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"
)
for
out
in
op
.
outputs
():
if
scope
.
find_var
(
out
)
is
None
:
scope
.
new_var
(
out
).
get_tensor
()
Y_tensor
=
scope
.
find_var
(
"Y"
).
get_tensor
()
op
.
infer_shape
(
scope
)
self
.
assertEqual
([
1000
,
100
],
Y_tensor
.
shape
())
ctx
=
core
.
DeviceContext
.
create
(
place
)
op
.
run
(
scope
,
ctx
)
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__'
:
unittest
.
main
()
python/paddle/v2/framework/tests/test_recurrent_op.py
浏览文件 @
0d1bc8ab
...
@@ -8,22 +8,22 @@ from paddle.v2.framework.op import Operator
...
@@ -8,22 +8,22 @@ from paddle.v2.framework.op import Operator
def
py_sigmoid
(
x
):
def
py_sigmoid
(
x
):
return
1.
/
(
1.
+
np
.
exp
(
-
x
))
return
1.
/
(
1.
+
np
.
exp
(
-
x
))
class
PySimpleRNN
(
object
):
class
PySimpleRNN
(
object
):
'''
'''
A simple implementation of RNN based on numpy, to futhur test RecurrentOp's alogorithm
A simple implementation of RNN based on numpy, to futhur test RecurrentOp's alogorithm
'''
'''
def
__init__
(
self
,
input_dim
=
30
,
def
__init__
(
self
,
input_dim
=
30
,
batch_size
=
50
,
weight_dim
=
15
,
sent_len
=
11
):
batch_size
=
50
,
weight_dim
=
15
,
sent_len
=
11
):
self
.
x
=
np
.
random
.
normal
(
size
=
(
sent_len
,
batch_size
,
input_dim
))
self
.
x
=
np
.
random
.
normal
(
size
=
(
sent_len
,
batch_size
,
input_dim
))
self
.
W
=
np
.
random
.
normal
(
size
=
(
input_dim
,
input_dim
))
self
.
W
=
np
.
random
.
normal
(
size
=
(
input_dim
,
input_dim
))
self
.
U
=
np
.
random
.
normal
(
size
=
(
input_dim
,
input_dim
))
self
.
U
=
np
.
random
.
normal
(
size
=
(
input_dim
,
input_dim
))
self
.
h_boot
=
np
.
random
.
normal
(
size
=
(
batch_size
,
input_dim
))
self
.
h_boot
=
np
.
random
.
normal
(
size
=
(
batch_size
,
input_dim
))
# memories
# memories
self
.
mems
=
[
np
.
zeros
(
shape
=
(
batch_size
,
input_dim
))
for
i
in
range
(
sent_len
)]
self
.
mems
=
[
np
.
zeros
(
shape
=
(
batch_size
,
input_dim
))
for
i
in
range
(
sent_len
)
]
def
forward
(
self
):
def
forward
(
self
):
xs
=
self
.
segment_inputs
()
xs
=
self
.
segment_inputs
()
...
@@ -43,7 +43,7 @@ class PySimpleRNN(object):
...
@@ -43,7 +43,7 @@ class PySimpleRNN(object):
'''
'''
mem
=
self
.
mems
[
step_id
]
mem
=
self
.
mems
[
step_id
]
if
step_id
>
0
:
if
step_id
>
0
:
pre_mem
=
self
.
mems
[
step_id
-
1
]
pre_mem
=
self
.
mems
[
step_id
-
1
]
else
:
else
:
pre_mem
=
self
.
h_boot
pre_mem
=
self
.
h_boot
xW
=
np
.
matmul
(
x
,
self
.
W
)
xW
=
np
.
matmul
(
x
,
self
.
W
)
...
@@ -52,6 +52,7 @@ class PySimpleRNN(object):
...
@@ -52,6 +52,7 @@ class PySimpleRNN(object):
sum
=
xW
+
hU
sum
=
xW
+
hU
self
.
mems
[
step_id
]
=
py_sigmoid
(
sum
)
self
.
mems
[
step_id
]
=
py_sigmoid
(
sum
)
class
PySimpleRNNTest
(
unittest
.
TestCase
):
class
PySimpleRNNTest
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
rnn
=
PySimpleRNN
()
self
.
rnn
=
PySimpleRNN
()
...
@@ -91,11 +92,8 @@ class TestRecurrentOp(unittest.TestCase):
...
@@ -91,11 +92,8 @@ class TestRecurrentOp(unittest.TestCase):
sent_len
=
11
sent_len
=
11
def
setUp
(
self
):
def
setUp
(
self
):
self
.
py_rnn
=
PySimpleRNN
(
self
.
input_dim
,
self
.
py_rnn
=
PySimpleRNN
(
self
.
input_dim
,
self
.
batch_size
,
self
.
batch_size
,
self
.
weight_dim
,
self
.
sent_len
)
self
.
weight_dim
,
self
.
sent_len
)
def
forward
(
self
):
def
forward
(
self
):
self
.
scope
=
core
.
Scope
()
self
.
scope
=
core
.
Scope
()
...
@@ -111,22 +109,27 @@ class TestRecurrentOp(unittest.TestCase):
...
@@ -111,22 +109,27 @@ class TestRecurrentOp(unittest.TestCase):
# create inlink
# create inlink
x_np_data
=
self
.
py_rnn
.
x
x_np_data
=
self
.
py_rnn
.
x
create_tensor
(
self
.
scope
,
"x"
,
create_tensor
(
self
.
scope
,
"x"
,
[
self
.
sent_len
,
self
.
batch_size
,
self
.
input_dim
],
x_np_data
)
[
self
.
sent_len
,
self
.
batch_size
,
self
.
input_dim
],
x_np_data
)
W_np_data
=
self
.
py_rnn
.
W
W_np_data
=
self
.
py_rnn
.
W
create_tensor
(
self
.
scope
,
"W"
,
[
self
.
input_dim
,
self
.
input_dim
],
W_np_data
)
create_tensor
(
self
.
scope
,
"W"
,
[
self
.
input_dim
,
self
.
input_dim
],
W_np_data
)
U_np_data
=
self
.
py_rnn
.
U
U_np_data
=
self
.
py_rnn
.
U
create_tensor
(
self
.
scope
,
"U"
,
[
self
.
input_dim
,
self
.
input_dim
],
U_np_data
)
create_tensor
(
self
.
scope
,
"U"
,
[
self
.
input_dim
,
self
.
input_dim
],
U_np_data
)
h_boot_np_data
=
self
.
py_rnn
.
h_boot
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
)
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
(
"step_scopes"
)
self
.
scope
.
new_var
(
"h@alias"
)
self
.
scope
.
new_var
(
"h@alias"
)
self
.
scope
.
new_var
(
"h"
)
self
.
scope
.
new_var
(
"h"
)
def
create_rnn_op
(
self
):
def
create_rnn_op
(
self
):
# create RNNOp
# create RNNOp
rnnop
=
Operator
(
"recurrent_op"
,
rnnop
=
Operator
(
"recurrent_op"
,
# inputs
# inputs
inlinks
=
[
"x"
],
inlinks
=
[
"x"
],
boot_memories
=
[
"h_boot"
],
boot_memories
=
[
"h_boot"
],
...
@@ -145,8 +148,10 @@ class TestRecurrentOp(unittest.TestCase):
...
@@ -145,8 +148,10 @@ class TestRecurrentOp(unittest.TestCase):
var
=
self
.
scope
.
new_var
(
"stepnet"
)
var
=
self
.
scope
.
new_var
(
"stepnet"
)
stepnet
=
var
.
get_net
()
stepnet
=
var
.
get_net
()
x_fc_op
=
Operator
(
"fc"
,
X
=
"x@alias"
,
W
=
"W"
,
Y
=
"Wx"
)
# x_fc_op = Operator("fc", X="x@alias", W="W", Y="Wx")
h_fc_op
=
Operator
(
"fc"
,
X
=
"h@pre"
,
W
=
"U"
,
Y
=
"Uh"
)
# h_fc_op = Operator("fc", X="h@pre", W="U", Y="Uh")
x_fc_op
=
Operator
(
"mul"
,
X
=
"x@alias"
,
Y
=
"W"
,
Out
=
"Wx"
)
h_fc_op
=
Operator
(
"mul"
,
X
=
"h@pre"
,
Y
=
"U"
,
Out
=
"Uh"
)
sum_op
=
Operator
(
"add_two"
,
X
=
"Wx"
,
Y
=
"Uh"
,
Out
=
"sum"
)
sum_op
=
Operator
(
"add_two"
,
X
=
"Wx"
,
Y
=
"Uh"
,
Out
=
"sum"
)
sig_op
=
Operator
(
"sigmoid"
,
X
=
"sum"
,
Y
=
"h@alias"
)
sig_op
=
Operator
(
"sigmoid"
,
X
=
"sum"
,
Y
=
"h@alias"
)
...
@@ -163,5 +168,6 @@ class TestRecurrentOp(unittest.TestCase):
...
@@ -163,5 +168,6 @@ class TestRecurrentOp(unittest.TestCase):
print
'py_output'
,
py_output
print
'py_output'
,
py_output
self
.
assertEqual
(
pd_output
.
shape
,
py_output
.
shape
)
self
.
assertEqual
(
pd_output
.
shape
,
py_output
.
shape
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录