Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
001b62a4
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看板
提交
001b62a4
编写于
8月 08, 2017
作者:
S
superjom
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
finish simple rnn in python
上级
d9f97b02
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
68 addition
and
13 deletion
+68
-13
python/paddle/v2/framework/tests/test_recurrent_op.py
python/paddle/v2/framework/tests/test_recurrent_op.py
+68
-13
未找到文件。
python/paddle/v2/framework/tests/test_recurrent_op.py
浏览文件 @
001b62a4
...
@@ -2,9 +2,64 @@ import logging
...
@@ -2,9 +2,64 @@ import logging
import
paddle.v2.framework.core
as
core
import
paddle.v2.framework.core
as
core
import
unittest
import
unittest
import
numpy
as
np
import
numpy
as
np
import
paddle.v2.framework.create_op_creation_methods
as
creation
from
paddle.v2.framework.op
import
Operator
ops
=
creation
.
op_creations
def
py_sigmoid
(
x
):
return
1.
/
(
1
+
np
.
exp
(
-
x
))
class
PySimpleRNN
(
object
):
'''
A simple implementation of RNN based on numpy, to futhur test RecurrentOp's alogorithm
'''
def
__init__
(
self
,
input_dim
=
30
,
batch_size
=
50
,
weight_dim
=
15
,
sent_len
=
11
):
self
.
x
=
np
.
random
.
normal
(
size
=
(
sent_len
,
batch_size
,
input_dim
))
self
.
W
=
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
))
# memories
self
.
mems
=
[
np
.
zeros
(
shape
=
(
batch_size
,
input_dim
))
for
i
in
range
(
sent_len
)]
def
forward
(
self
):
xs
=
self
.
segment_inputs
()
for
step_id
in
range
(
self
.
x
.
shape
[
0
]):
self
.
step
(
step_id
,
xs
[
step_id
])
return
self
.
concat_outputs
()
def
segment_inputs
(
self
):
return
[
self
.
x
[
i
]
for
i
in
range
(
self
.
x
.
shape
[
0
])]
def
concat_outputs
(
self
):
return
np
.
array
(
self
.
mems
)
def
step
(
self
,
step_id
,
x
):
'''
run a step
'''
mem
=
self
.
mems
[
step_id
]
if
step_id
>
0
:
pre_mem
=
self
.
mems
[
step_id
-
1
]
else
:
pre_mem
=
self
.
h_boot
xW
=
np
.
matmul
(
x
,
self
.
W
)
hU
=
np
.
matmul
(
mem
,
self
.
U
)
sum
=
xW
+
hU
self
.
mems
[
step_id
]
=
py_sigmoid
(
sum
)
class
PySimpleRNNTest
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
rnn
=
PySimpleRNN
()
def
test_forward
(
self
):
output
=
self
.
rnn
.
forward
()
print
'output'
,
output
def
create_tensor
(
scope
,
name
,
shape
):
def
create_tensor
(
scope
,
name
,
shape
):
...
@@ -14,7 +69,7 @@ def create_tensor(scope, name, shape):
...
@@ -14,7 +69,7 @@ def create_tensor(scope, name, shape):
return
tensor
return
tensor
class
TestR
NN
(
unittest
.
TestCase
):
class
TestR
ecurrentOp
(
unittest
.
TestCase
):
'''
'''
Test RNNOp
Test RNNOp
...
@@ -28,7 +83,7 @@ class TestRNN(unittest.TestCase):
...
@@ -28,7 +83,7 @@ class TestRNN(unittest.TestCase):
memories:
memories:
- h
- h
outputs:
outputs:
- h
- h
'''
'''
input_dim
=
30
input_dim
=
30
...
@@ -36,7 +91,7 @@ class TestRNN(unittest.TestCase):
...
@@ -36,7 +91,7 @@ class TestRNN(unittest.TestCase):
weight_dim
=
15
weight_dim
=
15
sent_len
=
11
sent_len
=
11
def
init
(
self
):
def
forward
(
self
):
self
.
scope
=
core
.
Scope
()
self
.
scope
=
core
.
Scope
()
...
@@ -46,7 +101,6 @@ class TestRNN(unittest.TestCase):
...
@@ -46,7 +101,6 @@ class TestRNN(unittest.TestCase):
ctx
=
core
.
DeviceContext
.
create
(
core
.
CPUPlace
())
ctx
=
core
.
DeviceContext
.
create
(
core
.
CPUPlace
())
print
'infer_shape'
print
'infer_shape'
rnn_op
.
infer_shape
(
self
.
scope
)
rnn_op
.
infer_shape
(
self
.
scope
)
rnn_op
.
run
(
self
.
scope
,
ctx
)
rnn_op
.
run
(
self
.
scope
,
ctx
)
def
create_global_variables
(
self
):
def
create_global_variables
(
self
):
...
@@ -62,7 +116,7 @@ class TestRNN(unittest.TestCase):
...
@@ -62,7 +116,7 @@ class TestRNN(unittest.TestCase):
def
create_rnn_op
(
self
):
def
create_rnn_op
(
self
):
# create RNNOp
# create RNNOp
rnnop
=
ops
.
recurrent_op
(
rnnop
=
Operator
(
"recurrent_op"
,
# inputs
# inputs
inlinks
=
[
"x"
],
inlinks
=
[
"x"
],
boot_memories
=
[
"h_boot"
],
boot_memories
=
[
"h_boot"
],
...
@@ -81,17 +135,18 @@ class TestRNN(unittest.TestCase):
...
@@ -81,17 +135,18 @@ class TestRNN(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
=
ops
.
fc
(
X
=
"x@alias"
,
W
=
"W"
,
Y
=
"Wx"
)
x_fc_op
=
Operator
(
"fc"
,
X
=
"x@alias"
,
W
=
"W"
,
Y
=
"Wx"
)
h_fc_op
=
ops
.
fc
(
X
=
"h@pre"
,
W
=
"U"
,
Y
=
"Uh"
)
h_fc_op
=
Operator
(
"fc"
,
X
=
"h@pre"
,
W
=
"U"
,
Y
=
"Uh"
)
sum_op
=
ops
.
add_two
(
X
=
"Wx"
,
Y
=
"Uh"
,
Out
=
"sum"
)
sum_op
=
Operator
(
"add_two"
,
X
=
"Wx"
,
Y
=
"Uh"
,
Out
=
"sum"
)
sig_op
=
ops
.
sigmoid
(
X
=
"sum"
,
Y
=
"h@alias"
)
sig_op
=
Operator
(
"sigmoid"
,
X
=
"sum"
,
Y
=
"h@alias"
)
for
op
in
[
x_fc_op
,
h_fc_op
,
sum_op
,
sig_op
]:
for
op
in
[
x_fc_op
,
h_fc_op
,
sum_op
,
sig_op
]:
stepnet
.
add_op
(
op
)
stepnet
.
add_op
(
op
)
stepnet
.
complete_add_op
(
True
)
stepnet
.
complete_add_op
(
True
)
def
test_recurrent
(
self
):
def
test_forward
(
self
):
self
.
init
()
print
'test recurrent op forward'
self
.
forward
()
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录