Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
40427979
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看板
提交
40427979
编写于
2月 27, 2017
作者:
Q
qiaolongfei
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refine code
上级
c49644a4
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
41 addition
and
44 deletion
+41
-44
demo/mnist/api_train_v2.py
demo/mnist/api_train_v2.py
+4
-2
python/paddle/v2/data_feeder.py
python/paddle/v2/data_feeder.py
+1
-1
python/paddle/v2/data_type.py
python/paddle/v2/data_type.py
+3
-3
python/paddle/v2/layer.py
python/paddle/v2/layer.py
+1
-0
python/paddle/v2/parameters.py
python/paddle/v2/parameters.py
+2
-2
python/paddle/v2/tests/CMakeLists.txt
python/paddle/v2/tests/CMakeLists.txt
+1
-1
python/paddle/v2/tests/test_topology.py
python/paddle/v2/tests/test_topology.py
+13
-8
python/paddle/v2/topology.py
python/paddle/v2/topology.py
+12
-17
python/paddle/v2/trainer.py
python/paddle/v2/trainer.py
+4
-10
未找到文件。
demo/mnist/api_train_v2.py
浏览文件 @
40427979
...
@@ -41,10 +41,12 @@ def main():
...
@@ -41,10 +41,12 @@ def main():
trainer
.
train
(
trainer
.
train
(
train_data_reader
=
train_reader
,
train_data_reader
=
train_reader
,
topology
=
[
cost
]
,
topology
=
cost
,
parameters
=
parameters
,
parameters
=
parameters
,
event_handler
=
event_handler
,
event_handler
=
event_handler
,
batch_size
=
32
)
# batch size should be refactor in Data reader
batch_size
=
32
,
# batch size should be refactor in Data reader
reader_dict
=
{
images
.
name
:
0
,
label
.
name
:
1
})
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
...
...
python/paddle/v2/data_feeder.py
浏览文件 @
40427979
...
@@ -23,7 +23,7 @@ class DataFeeder(DataProviderConverter):
...
@@ -23,7 +23,7 @@ class DataFeeder(DataProviderConverter):
"""
"""
DataFeeder converts the data returned by paddle.reader into a data structure
DataFeeder converts the data returned by paddle.reader into a data structure
of Arguments which is defined in the API. The paddle.reader usually returns
of Arguments which is defined in the API. The paddle.reader usually returns
a list of mini-batch data entries. Each data entry in the list is one sampe.
a list of mini-batch data entries. Each data entry in the list is one samp
l
e.
Each sample is a list or a tuple with one feature or multiple features.
Each sample is a list or a tuple with one feature or multiple features.
DataFeeder converts this mini-batch data entries into Arguments in order
DataFeeder converts this mini-batch data entries into Arguments in order
to feed it to C++ interface.
to feed it to C++ interface.
...
...
python/paddle/v2/data_type.py
浏览文件 @
40427979
...
@@ -13,10 +13,10 @@
...
@@ -13,10 +13,10 @@
# limitations under the License.
# limitations under the License.
from
paddle.trainer.PyDataProvider2
import
\
from
paddle.trainer.PyDataProvider2
import
\
InputType
,
dense_vector
,
sparse_binary_vector
,
\
InputType
,
DataType
,
dense_vector
,
sparse_binary_vector
,
\
sparse_vector
,
integer_value
,
integer_value_sequence
sparse_vector
,
integer_value
,
integer_value_sequence
__all__
=
[
__all__
=
[
'InputType'
,
'
dense_vector'
,
'sparse_binary_vector'
,
'sparse
_vector'
,
'InputType'
,
'
DataType'
,
'dense_vector'
,
'sparse_binary
_vector'
,
'integer_value'
,
'integer_value_sequence'
'
sparse_vector'
,
'
integer_value'
,
'integer_value_sequence'
]
]
python/paddle/v2/layer.py
浏览文件 @
40427979
...
@@ -284,6 +284,7 @@ def mixed(size=0,
...
@@ -284,6 +284,7 @@ def mixed(size=0,
return
MixedLayerV2
(
size
,
input
,
name
,
act
,
bias_attr
,
layer_attr
)
return
MixedLayerV2
(
size
,
input
,
name
,
act
,
bias_attr
,
layer_attr
)
LayerV2
=
Layer
data
=
DataLayerV2
data
=
DataLayerV2
AggregateLevel
=
conf_helps
.
layers
.
AggregateLevel
AggregateLevel
=
conf_helps
.
layers
.
AggregateLevel
ExpandLevel
=
conf_helps
.
layers
.
ExpandLevel
ExpandLevel
=
conf_helps
.
layers
.
ExpandLevel
...
...
python/paddle/v2/parameters.py
浏览文件 @
40427979
...
@@ -2,7 +2,7 @@ import numpy as np
...
@@ -2,7 +2,7 @@ import numpy as np
import
py_paddle.swig_paddle
as
api
import
py_paddle.swig_paddle
as
api
from
paddle.proto.ParameterConfig_pb2
import
ParameterConfig
from
paddle.proto.ParameterConfig_pb2
import
ParameterConfig
import
topology
as
v2_t
opology
from
topology
import
T
opology
__all__
=
[
'Parameters'
,
'create'
]
__all__
=
[
'Parameters'
,
'create'
]
...
@@ -13,7 +13,7 @@ def create(layers):
...
@@ -13,7 +13,7 @@ def create(layers):
:param layers:
:param layers:
:return:
:return:
"""
"""
topology
=
v2_topology
.
Topology
(
layers
)
topology
=
Topology
(
layers
)
pool
=
Parameters
()
pool
=
Parameters
()
for
param
in
topology
.
proto
().
parameters
:
for
param
in
topology
.
proto
().
parameters
:
pool
.
__append_config__
(
param
)
pool
.
__append_config__
(
param
)
...
...
python/paddle/v2/tests/CMakeLists.txt
浏览文件 @
40427979
...
@@ -8,5 +8,5 @@ add_test(NAME test_v2_api
...
@@ -8,5 +8,5 @@ add_test(NAME test_v2_api
add_test
(
NAME topology_test
add_test
(
NAME topology_test
COMMAND
${
PROJ_ROOT
}
/paddle/.set_python_path.sh -d
${
PROJ_ROOT
}
/python/
COMMAND
${
PROJ_ROOT
}
/paddle/.set_python_path.sh -d
${
PROJ_ROOT
}
/python/
${
PYTHON_EXECUTABLE
}
${
PROJ_ROOT
}
/python/paddle/v2/tests/t
opology_test
.py
${
PYTHON_EXECUTABLE
}
${
PROJ_ROOT
}
/python/paddle/v2/tests/t
est_topology
.py
WORKING_DIRECTORY
${
PROJ_ROOT
}
/python/paddle
)
WORKING_DIRECTORY
${
PROJ_ROOT
}
/python/paddle
)
python/paddle/v2/tests/t
opology_test
.py
→
python/paddle/v2/tests/t
est_topology
.py
浏览文件 @
40427979
...
@@ -30,14 +30,19 @@ class TestTopology(unittest.TestCase):
...
@@ -30,14 +30,19 @@ class TestTopology(unittest.TestCase):
act
=
conf_helps
.
SoftmaxActivation
())
act
=
conf_helps
.
SoftmaxActivation
())
cost
=
layer
.
classification_cost
(
input
=
inference
,
label
=
label
)
cost
=
layer
.
classification_cost
(
input
=
inference
,
label
=
label
)
topo
=
topology
.
Topology
(
cost
)
topo
=
topology
.
Topology
(
cost
)
type
=
topo
.
data_type
()
data_types
=
topo
.
data_type
()
self
.
assertEqual
(
len
(
type
),
2
)
self
.
assertEqual
(
len
(
data_types
),
2
)
self
.
assertEqual
(
type
[
0
][
0
],
"pixel"
)
pixel_data_type
=
filter
(
lambda
type
:
type
[
0
]
==
"pixel"
,
data_types
)
self
.
assertEqual
(
type
[
0
][
1
].
type
,
data_type
.
DataType
.
Dense
)
self
.
assertEqual
(
len
(
pixel_data_type
),
1
)
self
.
assertEqual
(
type
[
0
][
1
].
dim
,
784
)
pixel_data_type
=
pixel_data_type
[
0
]
self
.
assertEqual
(
type
[
1
][
0
],
"label"
)
self
.
assertEqual
(
pixel_data_type
[
1
].
type
,
data_type
.
DataType
.
Dense
)
self
.
assertEqual
(
type
[
1
][
1
].
type
,
data_type
.
DataType
.
Index
)
self
.
assertEqual
(
pixel_data_type
[
1
].
dim
,
784
)
self
.
assertEqual
(
type
[
1
][
1
].
dim
,
10
)
label_data_type
=
filter
(
lambda
type
:
type
[
0
]
==
"label"
,
data_types
)
self
.
assertEqual
(
len
(
label_data_type
),
1
)
label_data_type
=
label_data_type
[
0
]
self
.
assertEqual
(
label_data_type
[
1
].
type
,
data_type
.
DataType
.
Index
)
self
.
assertEqual
(
label_data_type
[
1
].
dim
,
10
)
def
test_get_layer
(
self
):
def
test_get_layer
(
self
):
pixel
=
layer
.
data
(
name
=
'pixel'
,
type
=
data_type
.
dense_vector
(
784
))
pixel
=
layer
.
data
(
name
=
'pixel'
,
type
=
data_type
.
dense_vector
(
784
))
...
...
python/paddle/v2/topology.py
浏览文件 @
40427979
...
@@ -49,30 +49,30 @@ class Topology(object):
...
@@ -49,30 +49,30 @@ class Topology(object):
result_layer
=
[]
result_layer
=
[]
def
find_layer_by_name
(
layer
,
layer_name
):
def
find_layer_by_name
(
layer
,
layer_name
):
if
layer
.
name
==
layer_name
and
len
(
result_layer
)
==
0
:
if
len
(
result_layer
)
==
1
:
return
elif
layer
.
name
==
layer_name
:
result_layer
.
append
(
layer
)
result_layer
.
append
(
layer
)
for
parent_layer
in
layer
.
__parent_layers__
.
values
():
else
:
find_layer_by_name
(
parent_layer
,
layer_name
)
for
parent_layer
in
layer
.
__parent_layers__
.
values
():
find_layer_by_name
(
parent_layer
,
layer_name
)
for
layer
in
self
.
layers
:
for
layer
in
self
.
layers
:
find_layer_by_name
(
layer
,
name
)
find_layer_by_name
(
layer
,
name
)
assert
len
(
result_layer
)
==
1
return
result_layer
[
0
]
return
result_layer
[
0
]
def
data_layer
(
self
):
def
data_layer
s
(
self
):
"""
"""
get all data layer
get all data layer
:return:
:return:
"""
"""
data_layers
=
[]
data_layers
=
set
()
def
find_data_layer
(
layer
):
def
find_data_layer
(
layer
):
assert
isinstance
(
layer
,
layer
.
LayerV2
)
if
isinstance
(
layer
,
v2_layer
.
DataLayerV2
):
if
isinstance
(
layer
,
v2_layer
.
DataLayerV2
):
if
len
(
data_layers
.
add
(
layer
)
filter
(
lambda
data_layer
:
data_layer
.
name
==
layer
.
name
,
data_layers
))
==
0
:
data_layers
.
append
(
layer
)
for
parent_layer
in
layer
.
__parent_layers__
.
values
():
for
parent_layer
in
layer
.
__parent_layers__
.
values
():
find_data_layer
(
parent_layer
)
find_data_layer
(
parent_layer
)
...
@@ -85,14 +85,9 @@ class Topology(object):
...
@@ -85,14 +85,9 @@ class Topology(object):
"""
"""
get data_type from proto, such as:
get data_type from proto, such as:
[('image', dense_vector(768)), ('label', integer_value(10))]
[('image', dense_vector(768)), ('label', integer_value(10))]
the order is the same with __model_config__.input_layer_names
"""
"""
data_types_lists
=
[]
return
[(
data_layer
.
name
,
data_layer
.
type
)
for
layer_name
in
self
.
__model_config__
.
input_layer_names
:
for
data_layer
in
self
.
data_layers
()]
data_types_lists
.
append
(
(
layer_name
,
self
.
get_layer
(
layer_name
).
type
))
return
data_types_lists
def
__check_layer_type__
(
layer
):
def
__check_layer_type__
(
layer
):
...
...
python/paddle/v2/trainer.py
浏览文件 @
40427979
import
collections
import
collections
import
py_paddle.swig_paddle
as
api
import
py_paddle.swig_paddle
as
api
from
py_paddle
import
DataProviderConverter
from
data_feeder
import
DataFeeder
from
data_feeder
import
DataFeeder
from
topology
import
Topology
from
.
import
event
as
v2_event
from
.
import
event
as
v2_event
from
.
import
optimizer
as
v2_optimizer
from
.
import
optimizer
as
v2_optimizer
from
.
import
parameters
as
v2_parameters
from
.
import
parameters
as
v2_parameters
from
.
import
topology
as
v2_topology
__all__
=
[
'ITrainer'
,
'SGD'
]
__all__
=
[
'ITrainer'
,
'SGD'
]
...
@@ -69,7 +68,6 @@ class SGD(ITrainer):
...
@@ -69,7 +68,6 @@ class SGD(ITrainer):
test_data_reader
=
None
,
test_data_reader
=
None
,
event_handler
=
None
,
event_handler
=
None
,
batch_size
=
32
,
batch_size
=
32
,
data_types
=
None
,
reader_dict
=
None
):
reader_dict
=
None
):
"""
"""
Training method. Will train num_passes of input data.
Training method. Will train num_passes of input data.
...
@@ -83,13 +81,12 @@ class SGD(ITrainer):
...
@@ -83,13 +81,12 @@ class SGD(ITrainer):
occurred.
occurred.
:type event_handler: (BaseEvent) => None
:type event_handler: (BaseEvent) => None
:param batch_size: Not important, will be removed after data refactor.
:param batch_size: Not important, will be removed after data refactor.
:param data_types: Not important, will be removed after data refactor.
:return:
:return:
"""
"""
if
event_handler
is
None
:
if
event_handler
is
None
:
event_handler
=
default_event_handler
event_handler
=
default_event_handler
topology
=
v2_topology
.
Topology
(
topology
)
topology
=
Topology
(
topology
)
__check_train_args__
(
**
locals
())
__check_train_args__
(
**
locals
())
...
@@ -109,10 +106,7 @@ class SGD(ITrainer):
...
@@ -109,10 +106,7 @@ class SGD(ITrainer):
assert
isinstance
(
pass_evaluator
,
api
.
Evaluator
)
assert
isinstance
(
pass_evaluator
,
api
.
Evaluator
)
out_args
=
api
.
Arguments
.
createArguments
(
0
)
out_args
=
api
.
Arguments
.
createArguments
(
0
)
data_types_lists
=
[
data_type
[
1
]
for
data_type
in
topology
.
data_type
()]
feeder
=
DataFeeder
(
topology
.
data_type
(),
reader_dict
)
converter
=
DataProviderConverter
(
input_types
=
data_types_lists
)
feeder
=
DataFeeder
(
data_types
,
reader_dict
)
for
pass_id
in
xrange
(
num_passes
):
for
pass_id
in
xrange
(
num_passes
):
event_handler
(
v2_event
.
BeginPass
(
pass_id
))
event_handler
(
v2_event
.
BeginPass
(
pass_id
))
...
@@ -195,7 +189,7 @@ def __check_train_args__(train_data_reader, topology, parameters,
...
@@ -195,7 +189,7 @@ def __check_train_args__(train_data_reader, topology, parameters,
raise
ValueError
(
'test_data_reader should be a function, which can '
raise
ValueError
(
'test_data_reader should be a function, which can '
'return a iterator'
)
'return a iterator'
)
if
not
isinstance
(
topology
,
v2_topology
.
Topology
):
if
not
isinstance
(
topology
,
Topology
):
raise
ValueError
(
'topology should be a model config'
)
raise
ValueError
(
'topology should be a model config'
)
if
not
isinstance
(
parameters
,
v2_parameters
.
Parameters
):
if
not
isinstance
(
parameters
,
v2_parameters
.
Parameters
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录