Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
33590b58
P
Paddle
项目概览
机器未来
/
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看板
提交
33590b58
编写于
1月 23, 2019
作者:
J
JiabinYang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
test=develop, move simple rnn cell to test_imperative
上级
05bbe4e1
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
113 addition
and
95 deletion
+113
-95
python/paddle/fluid/imperative/nn.py
python/paddle/fluid/imperative/nn.py
+1
-92
python/paddle/fluid/tests/unittests/test_imperative.py
python/paddle/fluid/tests/unittests/test_imperative.py
+91
-3
python/paddle/fluid/tests/unittests/test_imperative_ptb_rnn.py
...n/paddle/fluid/tests/unittests/test_imperative_ptb_rnn.py
+21
-0
未找到文件。
python/paddle/fluid/imperative/nn.py
浏览文件 @
33590b58
...
...
@@ -23,7 +23,7 @@ from ..framework import Variable, OpProtoHolder
from
..param_attr
import
ParamAttr
from
..initializer
import
Normal
,
Constant
__all__
=
[
'Conv2D'
,
'Pool2D'
,
'FC'
,
'SimpleRNNCell'
]
__all__
=
[
'Conv2D'
,
'Pool2D'
,
'FC'
]
class
Conv2D
(
layers
.
Layer
):
...
...
@@ -274,94 +274,3 @@ class FC(layers.Layer):
out
=
bias_out
# add activation
return
self
.
_helper
.
append_activation
(
out
)
class
SimpleRNNCell
(
layers
.
Layer
):
def
__init__
(
self
,
step_input_size
,
hidden_size
,
output_size
,
param_attr
):
super
(
SimpleRNNCell
,
self
).
__init__
()
self
.
step_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'
,
act
=
"tanh"
,
param_attr
=
param_attr
)
def
_build_once
(
self
,
inputs
,
pre_hidden
):
i2h_param_shape
=
[
self
.
step_input_size
,
self
.
hidden_size
]
h2h_param_shape
=
[
self
.
hidden_size
,
self
.
hidden_size
]
h2o_param_shape
=
[
self
.
output_size
,
self
.
hidden_size
]
self
.
_i2h_w
=
self
.
_helper
.
create_parameter
(
attr
=
self
.
_helper
.
param_attr
,
shape
=
i2h_param_shape
,
dtype
=
self
.
_dtype
,
is_bias
=
False
)
self
.
_h2h_w
=
self
.
_helper
.
create_parameter
(
attr
=
self
.
_helper
.
param_attr
,
shape
=
h2h_param_shape
,
dtype
=
self
.
_dtype
,
is_bias
=
False
)
self
.
_h2o_w
=
self
.
_helper
.
create_parameter
(
attr
=
self
.
_helper
.
param_attr
,
shape
=
h2o_param_shape
,
dtype
=
self
.
_dtype
,
is_bias
=
False
)
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
)
reduce_out
=
self
.
_helper
.
create_variable_for_type_inference
(
self
.
_dtype
)
self
.
_helper
.
append_op
(
type
=
"mul"
,
inputs
=
{
"X"
:
input
,
"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
=
"elementwise_add"
,
inputs
=
{
'X'
:
tmp_h2h
,
'Y'
:
tmp_i2h
},
outputs
=
{
'Out'
:
hidden
},
attrs
=
{
'axis'
:
-
1
,
'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"
:
1
,
"y_num_col_dims"
:
1
})
self
.
_helper
.
append_op
(
type
=
"softmax"
,
inputs
=
{
"X"
:
out
},
outputs
=
{
"Out"
:
softmax_out
},
attrs
=
{
"use_cudnn"
:
False
})
self
.
_helper
.
append_op
(
type
=
'reduce_sum'
,
inputs
=
{
'X'
:
softmax_out
},
outputs
=
{
'Out'
:
reduce_out
},
attrs
=
{
'dim'
:
None
,
'keep_dim'
:
False
,
'reduce_all'
:
True
})
return
reduce_out
,
hidden
python/paddle/fluid/tests/unittests/test_imperative.py
浏览文件 @
33590b58
...
...
@@ -20,9 +20,6 @@ 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
SimpleRNNCell
from
typing
import
List
,
Any
,
Tuple
from
test_imperative_base
import
new_program_scope
...
...
@@ -69,6 +66,97 @@ class MLP(fluid.imperative.Layer):
return
x
class
SimpleRNNCell
(
fluid
.
imperative
.
Layer
):
def
__init__
(
self
,
step_input_size
,
hidden_size
,
output_size
,
param_attr
):
super
(
SimpleRNNCell
,
self
).
__init__
()
self
.
step_input_size
=
step_input_size
self
.
hidden_size
=
hidden_size
self
.
output_size
=
output_size
self
.
_dype
=
core
.
VarDesc
.
VarType
.
FP32
from
paddle.fluid.layer_helper
import
LayerHelper
self
.
_helper
=
LayerHelper
(
'SimpleRNNCell'
,
act
=
"tanh"
,
param_attr
=
param_attr
)
def
_build_once
(
self
,
inputs
,
pre_hidden
):
i2h_param_shape
=
[
self
.
step_input_size
,
self
.
hidden_size
]
h2h_param_shape
=
[
self
.
hidden_size
,
self
.
hidden_size
]
h2o_param_shape
=
[
self
.
output_size
,
self
.
hidden_size
]
self
.
_i2h_w
=
self
.
_helper
.
create_parameter
(
attr
=
self
.
_helper
.
param_attr
,
shape
=
i2h_param_shape
,
dtype
=
self
.
_dtype
,
is_bias
=
False
)
self
.
_h2h_w
=
self
.
_helper
.
create_parameter
(
attr
=
self
.
_helper
.
param_attr
,
shape
=
h2h_param_shape
,
dtype
=
self
.
_dtype
,
is_bias
=
False
)
self
.
_h2o_w
=
self
.
_helper
.
create_parameter
(
attr
=
self
.
_helper
.
param_attr
,
shape
=
h2o_param_shape
,
dtype
=
self
.
_dtype
,
is_bias
=
False
)
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
)
reduce_out
=
self
.
_helper
.
create_variable_for_type_inference
(
self
.
_dtype
)
self
.
_helper
.
append_op
(
type
=
"mul"
,
inputs
=
{
"X"
:
input
,
"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
=
"elementwise_add"
,
inputs
=
{
'X'
:
tmp_h2h
,
'Y'
:
tmp_i2h
},
outputs
=
{
'Out'
:
hidden
},
attrs
=
{
'axis'
:
-
1
,
'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"
:
1
,
"y_num_col_dims"
:
1
})
self
.
_helper
.
append_op
(
type
=
"softmax"
,
inputs
=
{
"X"
:
out
},
outputs
=
{
"Out"
:
softmax_out
},
attrs
=
{
"use_cudnn"
:
False
})
self
.
_helper
.
append_op
(
type
=
'reduce_sum'
,
inputs
=
{
'X'
:
softmax_out
},
outputs
=
{
'Out'
:
reduce_out
},
attrs
=
{
'dim'
:
None
,
'keep_dim'
:
False
,
'reduce_all'
:
True
})
return
reduce_out
,
hidden
class
SimpleRNN
(
fluid
.
imperative
.
Layer
):
def
__init__
(
self
):
super
(
SimpleRNN
,
self
).
__init__
()
...
...
python/paddle/fluid/tests/unittests/test_imperative_ptb_rnn.py
0 → 100644
浏览文件 @
33590b58
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from
__future__
import
print_function
import
unittest
import
paddle.fluid.framework
as
framework
import
paddle.fluid.optimizer
as
optimizer
from
paddle.fluid.backward
import
append_backward
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录