Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
af1cee5a
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
af1cee5a
编写于
1月 18, 2019
作者:
J
JiabinYang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
change in 1/18
上级
a360f143
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
46 addition
and
16 deletion
+46
-16
python/paddle/fluid/imperative/nn.py
python/paddle/fluid/imperative/nn.py
+44
-12
python/paddle/fluid/tests/unittests/test_imperative.py
python/paddle/fluid/tests/unittests/test_imperative.py
+2
-4
未找到文件。
python/paddle/fluid/imperative/nn.py
浏览文件 @
af1cee5a
...
...
@@ -257,12 +257,14 @@ class SimpleRNNCell(layers.Layer):
output_size
,
param_attr
,
dtype
=
core
.
VarDesc
.
VarType
.
FP32
):
super
(
SimpleRNNCell
,
self
).
__init__
()
self
.
input_size
=
step_input_size
self
.
hidden_size
=
hidden_size
self
.
output_size
=
output_size
self
.
_dype
=
core
.
VarDesc
.
VarType
.
FP32
from
..layer_helper
import
LayerHelper
self
.
_helper
=
LayerHelper
(
'SimpleRNNCell'
,
param_attr
=
param_attr
)
self
.
_helper
=
LayerHelper
(
'SimpleRNNCell'
,
act
=
"tanh"
,
param_attr
=
param_attr
)
def
_build_once
(
self
,
inputs
):
i2h_param_shape
=
[
self
.
step_input_size
,
self
.
hidden_size
]
...
...
@@ -284,20 +286,50 @@ class SimpleRNNCell(layers.Layer):
dtype
=
self
.
_dtype
,
is_bias
=
False
)
def
forward
(
self
,
inputs
):
input
=
inputs
[
0
]
pre_hidden
=
inputs
[
1
]
out
=
self
.
_helper
.
create_variable_for_type_inference
(
self
.
_dtype
)
hidden
=
self
.
_helper
.
create_variable_for_type_inference
(
self
.
_dype
)
def
forward
(
self
,
input
,
pre_hidden
):
tmp_i2h
=
self
.
_helper
.
create_variable_for_type_inference
(
self
.
_dtype
)
tmp_h2h
=
self
.
_helper
.
create_variable_for_type_inference
(
self
.
_dtype
)
hidden
=
self
.
_helper
.
create_variable_for_type_inference
(
self
.
_dype
)
out
=
self
.
_helper
.
create_variable_for_type_inference
(
self
.
_dype
)
softmax_out
=
self
.
_helper
.
create_variable_for_type_inference
(
self
.
_dtype
)
self
.
_helper
.
append_op
(
type
=
"mul"
,
inputs
=
{
"X"
:
input
,
"Y"
:
self
.
_w
},
"Y"
:
self
.
_i2h_w
},
outputs
=
{
"Out"
:
tmp_i2h
},
attrs
=
{
"x_num_col_dims"
:
1
,
"y_num_col_dims"
:
1
})
self
.
_helper
.
append_op
(
type
=
"mul"
,
inputs
=
{
"X"
:
pre_hidden
,
"Y"
:
self
.
_h2h_w
},
outputs
=
{
"Out"
:
tmp_h2h
},
attrs
=
{
"x_num_col_dims"
:
1
,
"y_num_col_dims"
:
1
})
self
.
_helper
.
append_op
(
type
=
'sum'
,
inputs
=
{
'X'
:
[
tmp_i2h
,
tmp_h2h
]},
outputs
=
{
'Out'
:
hidden
},
attrs
=
{
'use_mkldnn'
:
False
})
hidden
=
self
.
_helper
.
append_activation
(
hidden
)
self
.
_helper
.
append_op
(
type
=
"mul"
,
inputs
=
{
"X"
:
hidden
,
"Y"
:
self
.
_h2o_w
},
outputs
=
{
"Out"
:
out
},
attrs
=
{
"x_num_col_dims"
:
self
.
_num_flatten_dims
,
"y_num_col_dims"
:
1
})
attrs
=
{
"x_num_col_dims"
:
1
,
"y_num_col_dims"
:
1
})
self
.
_helper
.
append_op
(
type
=
"softmax"
,
inputs
=
{
"X"
:
out
},
outputs
=
{
"Out"
:
softmax_out
},
attrs
=
{
"use_cudnn"
:
False
})
return
1
return
softmax_out
,
hidden
python/paddle/fluid/tests/unittests/test_imperative.py
浏览文件 @
af1cee5a
...
...
@@ -19,7 +19,7 @@ import sys
import
paddle.fluid
as
fluid
from
paddle.fluid
import
core
from
paddle.fluid.imperative.nn
import
FC
from
paddle.fluid.imperative.nn
import
FC
,
SimpleRNNCell
from
test_imperative_base
import
new_program_scope
...
...
@@ -70,9 +70,7 @@ class SimpleRNN(fluid.imperative.Layer):
def
__init__
(
self
,
inputs
):
super
(
SimpleRNN
,
self
).
__init__
()
self
.
seq_len
=
input
.
shape
[
0
]
self
.
_fc1
=
FC
(
3
,
fluid
.
ParamAttr
(
initializer
=
fluid
.
initializer
.
Constant
(
value
=
0.1
)))
self
.
cell
=
SimpleRNNCell
(
input
.
shape
[
1
],
out
)
def
forward
(
self
,
inputs
):
for
i
in
range
(
self
.
seq_len
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录