Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wmsofts
Paddle
提交
632ad5c9
P
Paddle
项目概览
wmsofts
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
0
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
632ad5c9
编写于
2月 27, 2017
作者:
Q
qiaolongfei
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
support sequence_rnn_multi_input
上级
3d28291d
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
178 addition
and
60 deletion
+178
-60
demo/mnist/api_train_v2.py
demo/mnist/api_train_v2.py
+0
-3
python/paddle/trainer/config_parser.py
python/paddle/trainer/config_parser.py
+4
-2
python/paddle/v2/layer.py
python/paddle/v2/layer.py
+26
-4
python/paddle/v2/tests/CMakeLists.txt
python/paddle/v2/tests/CMakeLists.txt
+5
-1
python/paddle/v2/tests/test_layer.py
python/paddle/v2/tests/test_layer.py
+0
-50
python/paddle/v2/tests/test_rnn_layer.py
python/paddle/v2/tests/test_rnn_layer.py
+143
-0
未找到文件。
demo/mnist/api_train_v2.py
浏览文件 @
632ad5c9
...
...
@@ -3,9 +3,6 @@ import paddle.v2 as paddle
import
mnist_util
import
pudb
pudb
.
set_trace
()
def
train_reader
():
train_file
=
'./data/raw_data/train'
...
...
python/paddle/trainer/config_parser.py
浏览文件 @
632ad5c9
...
...
@@ -3474,6 +3474,8 @@ def update_g_config():
for
name
in
g_config
.
model_config
.
output_layer_names
:
assert
name
in
g_layer_map
,
\
'input name "%s" does not correspond to a layer name'
%
name
for
hook
in
_parse_config_hooks
:
hook
()
return
g_config
...
...
@@ -3485,8 +3487,8 @@ def parse_config(trainer_config, config_arg_str):
passed to config script as a dictionary CONFIG_ARGS
'''
init_config_environment
()
for
hook
in
_parse_config_hooks
:
hook
()
#
for hook in _parse_config_hooks:
#
hook()
config_args
=
{}
...
...
python/paddle/v2/layer.py
浏览文件 @
632ad5c9
...
...
@@ -124,11 +124,13 @@ class Layer(object):
return
self
.
to_proto_impl
(
**
kwargs
)
# memory may have the same name with some layer
if
isinstance
(
self
,
MemoryV2
)
or
isinstance
(
self
,
LayerOutputV2
)
:
if
isinstance
(
self
,
MemoryV2
):
return
self
.
to_proto_impl
(
**
kwargs
)
# store v1 API's layer_output in context with the key of it's name.
if
self
.
name
not
in
context
:
context
[
self
.
name
]
=
self
.
to_proto_impl
(
**
kwargs
)
return
context
[
self
.
name
]
def
to_proto_impl
(
self
,
**
kwargs
):
...
...
@@ -200,8 +202,19 @@ class MemoryV2(Layer):
def
__init__
(
self
,
name
,
size
,
**
kwargs
):
self
.
name
=
name
self
.
size
=
size
self
.
__kwargs__
=
kwargs
super
(
MemoryV2
,
self
).
__init__
(
name
=
name
,
parent_layers
=
dict
())
parent_names
=
[
'boot_layer'
]
parent_layers
=
dict
()
other_kwargs
=
dict
()
for
pname
in
parent_names
:
if
kwargs
.
has_key
(
pname
):
parent_layers
[
pname
]
=
kwargs
[
pname
]
for
key
in
kwargs
.
keys
():
if
key
not
in
parent_names
:
other_kwargs
[
key
]
=
kwargs
[
key
]
super
(
MemoryV2
,
self
).
__init__
(
name
=
name
,
parent_layers
=
parent_layers
)
self
.
__kwargs__
=
other_kwargs
def
to_proto_impl
(
self
,
**
kwargs
):
args
=
dict
()
...
...
@@ -209,10 +222,16 @@ class MemoryV2(Layer):
args
[
each
]
=
kwargs
[
each
]
for
each
in
self
.
__kwargs__
:
args
[
each
]
=
self
.
__kwargs__
[
each
]
return
conf_helps
.
memory
(
name
=
self
.
name
,
size
=
self
.
size
,
**
args
)
class
LayerOutputV2
(
Layer
):
"""
LayerOutputV2 is used to store the result of LayerOutput in v1 api.
It will not store it's parents because layer_output has been parsed already.
"""
def
__init__
(
self
,
layer_output
):
assert
isinstance
(
layer_output
,
conf_helps
.
LayerOutput
)
self
.
layer_output
=
layer_output
...
...
@@ -239,8 +258,11 @@ class RecurrentGroupV2(Layer):
super
(
RecurrentGroupV2
,
self
).
__init__
(
name
=
name
,
parent_layers
=
parent_layers
)
wrapper
=
wrap_name_default
(
name_prefix
=
'recurrent_group'
)
__init__
=
wrapper
(
__init__
)
def
to_proto_impl
(
self
,
**
kwargs
):
def
in_args_converter
(
in_args
):
def
in_args_converter
(
*
in_args
):
if
not
isinstance
(
in_args
,
collections
.
Sequence
):
in_args
=
[
in_args
]
return
[
LayerOutputV2
(
input
)
for
input
in
in_args
]
...
...
python/paddle/v2/tests/CMakeLists.txt
浏览文件 @
632ad5c9
add_test
(
NAME test_v2_layer
COMMAND
${
PROJ_ROOT
}
/paddle/.set_python_path.sh -d
${
PROJ_ROOT
}
/python/
${
PYTHON_EXECUTABLE
}
${
PROJ_ROOT
}
/python/paddle/v2/tests/test_layer.py
${
PYTHON_EXECUTABLE
}
${
PROJ_ROOT
}
/python/paddle/v2/tests/test_layer.py
)
add_test
(
NAME test_v2_rnn_layer
COMMAND
${
PROJ_ROOT
}
/paddle/.set_python_path.sh -d
${
PROJ_ROOT
}
/python/
${
PYTHON_EXECUTABLE
}
${
PROJ_ROOT
}
/python/paddle/v2/tests/test_rnn_layer.py
)
python/paddle/v2/tests/test_layer.py
浏览文件 @
632ad5c9
...
...
@@ -11,16 +11,12 @@
# 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.
import
difflib
import
unittest
import
paddle.trainer_config_helpers
as
conf_helps
import
paddle.v2.activation
as
activation
import
paddle.v2.attr
as
attr
import
paddle.v2.data_type
as
data_type
import
paddle.v2.layer
as
layer
from
paddle.trainer_config_helpers.config_parser_utils
import
\
parse_network_config
as
parse_network
pixel
=
layer
.
data
(
name
=
'pixel'
,
type
=
data_type
.
dense_vector
(
784
))
label
=
layer
.
data
(
name
=
'label'
,
type
=
data_type
.
integer_value
(
10
))
...
...
@@ -57,51 +53,5 @@ class CostLayerTest(unittest.TestCase):
print
layer
.
parse_network
(
cost7
,
cost8
,
cost9
,
cost10
,
cost11
)
class
RNNTest
(
unittest
.
TestCase
):
def
test_simple_rnn
(
self
):
dict_dim
=
10
word_dim
=
8
hidden_dim
=
8
def
parse_old_rnn
():
def
step
(
y
):
mem
=
conf_helps
.
memory
(
name
=
"rnn_state"
,
size
=
hidden_dim
)
out
=
conf_helps
.
fc_layer
(
input
=
[
y
,
mem
],
size
=
hidden_dim
,
act
=
activation
.
Tanh
(),
bias_attr
=
True
,
name
=
"rnn_state"
)
return
out
def
test
():
data1
=
conf_helps
.
data_layer
(
name
=
"word"
,
size
=
dict_dim
)
embd
=
conf_helps
.
embedding_layer
(
input
=
data1
,
size
=
word_dim
)
conf_helps
.
recurrent_group
(
name
=
"rnn"
,
step
=
step
,
input
=
embd
)
return
str
(
parse_network
(
test
))
def
parse_new_rnn
():
def
new_step
(
y
):
mem
=
layer
.
memory
(
name
=
"rnn_state"
,
size
=
hidden_dim
)
out
=
layer
.
fc
(
input
=
[
y
,
mem
],
size
=
hidden_dim
,
act
=
activation
.
Tanh
(),
bias_attr
=
True
,
name
=
"rnn_state"
)
return
out
data1
=
layer
.
data
(
name
=
"word"
,
type
=
data_type
.
integer_value
(
dict_dim
))
embd
=
layer
.
embedding
(
input
=
data1
,
size
=
word_dim
)
rnn_layer
=
layer
.
recurrent_group
(
name
=
"rnn"
,
step
=
new_step
,
input
=
embd
)
return
str
(
layer
.
parse_network
(
rnn_layer
))
diff
=
difflib
.
unified_diff
(
parse_old_rnn
().
splitlines
(
1
),
parse_new_rnn
().
splitlines
(
1
))
print
''
.
join
(
diff
)
if
__name__
==
'__main__'
:
unittest
.
main
()
python/paddle/v2/tests/test_rnn_layer.py
0 → 100644
浏览文件 @
632ad5c9
# Copyright PaddlePaddle contributors. 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.
import
difflib
import
unittest
import
paddle.trainer_config_helpers
as
conf_helps
import
paddle.v2.activation
as
activation
import
paddle.v2.data_type
as
data_type
import
paddle.v2.layer
as
layer
from
paddle.trainer_config_helpers.config_parser_utils
import
\
parse_network_config
as
parse_network
class
RNNTest
(
unittest
.
TestCase
):
def
test_simple_rnn
(
self
):
dict_dim
=
10
word_dim
=
8
hidden_dim
=
8
def
parse_old_rnn
():
def
step
(
y
):
mem
=
conf_helps
.
memory
(
name
=
"rnn_state"
,
size
=
hidden_dim
)
out
=
conf_helps
.
fc_layer
(
input
=
[
y
,
mem
],
size
=
hidden_dim
,
act
=
activation
.
Tanh
(),
bias_attr
=
True
,
name
=
"rnn_state"
)
return
out
def
test
():
data
=
conf_helps
.
data_layer
(
name
=
"word"
,
size
=
dict_dim
)
embd
=
conf_helps
.
embedding_layer
(
input
=
data
,
size
=
word_dim
)
conf_helps
.
recurrent_group
(
name
=
"rnn"
,
step
=
step
,
input
=
embd
)
return
str
(
parse_network
(
test
))
def
parse_new_rnn
():
def
new_step
(
y
):
mem
=
layer
.
memory
(
name
=
"rnn_state"
,
size
=
hidden_dim
)
out
=
layer
.
fc
(
input
=
[
y
,
mem
],
size
=
hidden_dim
,
act
=
activation
.
Tanh
(),
bias_attr
=
True
,
name
=
"rnn_state"
)
return
out
data
=
layer
.
data
(
name
=
"word"
,
type
=
data_type
.
integer_value
(
dict_dim
))
embd
=
layer
.
embedding
(
input
=
data
,
size
=
word_dim
)
rnn_layer
=
layer
.
recurrent_group
(
name
=
"rnn"
,
step
=
new_step
,
input
=
embd
)
return
str
(
layer
.
parse_network
(
rnn_layer
))
diff
=
difflib
.
unified_diff
(
parse_old_rnn
().
splitlines
(
1
),
parse_new_rnn
().
splitlines
(
1
))
print
''
.
join
(
diff
)
def
test_sequence_rnn_multi_input
(
self
):
dict_dim
=
10
word_dim
=
8
hidden_dim
=
8
label_dim
=
3
def
parse_old_rnn
():
def
step
(
y
,
wid
):
z
=
conf_helps
.
embedding_layer
(
input
=
wid
,
size
=
word_dim
)
mem
=
conf_helps
.
memory
(
name
=
"rnn_state"
,
size
=
hidden_dim
)
out
=
conf_helps
.
fc_layer
(
input
=
[
y
,
z
,
mem
],
size
=
hidden_dim
,
act
=
conf_helps
.
TanhActivation
(),
bias_attr
=
True
,
name
=
"rnn_state"
)
return
out
def
test
():
data
=
conf_helps
.
data_layer
(
name
=
"word"
,
size
=
dict_dim
)
label
=
conf_helps
.
data_layer
(
name
=
"label"
,
size
=
label_dim
)
emb
=
conf_helps
.
embedding_layer
(
input
=
data
,
size
=
word_dim
)
out
=
conf_helps
.
recurrent_group
(
name
=
"rnn"
,
step
=
step
,
input
=
[
emb
,
data
])
rep
=
conf_helps
.
last_seq
(
input
=
out
)
prob
=
conf_helps
.
fc_layer
(
size
=
label_dim
,
input
=
rep
,
act
=
conf_helps
.
SoftmaxActivation
(),
bias_attr
=
True
)
conf_helps
.
outputs
(
conf_helps
.
classification_cost
(
input
=
prob
,
label
=
label
))
return
str
(
parse_network
(
test
))
def
parse_new_rnn
():
def
step
(
y
,
wid
):
z
=
layer
.
embedding
(
input
=
wid
,
size
=
word_dim
)
mem
=
layer
.
memory
(
name
=
"rnn_state"
,
size
=
hidden_dim
)
out
=
layer
.
fc
(
input
=
[
y
,
z
,
mem
],
size
=
hidden_dim
,
act
=
activation
.
Tanh
(),
bias_attr
=
True
,
name
=
"rnn_state"
)
return
out
data
=
layer
.
data
(
name
=
"word"
,
type
=
data_type
.
dense_vector
(
dict_dim
))
label
=
layer
.
data
(
name
=
"label"
,
type
=
data_type
.
dense_vector
(
label_dim
))
emb
=
layer
.
embedding
(
input
=
data
,
size
=
word_dim
)
out
=
layer
.
recurrent_group
(
name
=
"rnn"
,
step
=
step
,
input
=
[
emb
,
data
])
rep
=
layer
.
last_seq
(
input
=
out
)
prob
=
layer
.
fc
(
size
=
label_dim
,
input
=
rep
,
act
=
activation
.
Softmax
(),
bias_attr
=
True
)
cost
=
layer
.
classification_cost
(
input
=
prob
,
label
=
label
)
return
str
(
layer
.
parse_network
(
cost
))
diff
=
difflib
.
unified_diff
(
parse_old_rnn
().
splitlines
(
1
),
parse_new_rnn
().
splitlines
(
1
))
print
''
.
join
(
diff
)
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录