Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
恒星小白
Paddle
提交
0bd75a57
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0bd75a57
编写于
2月 14, 2017
作者:
Q
qiaolongfei
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
change layers to layer
上级
281250f5
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
58 addition
and
37 deletion
+58
-37
demo/mnist/api_train.py
demo/mnist/api_train.py
+11
-10
python/paddle/v2/__init__.py
python/paddle/v2/__init__.py
+2
-2
python/paddle/v2/layer.py
python/paddle/v2/layer.py
+45
-25
未找到文件。
demo/mnist/api_train.py
浏览文件 @
0bd75a57
...
@@ -10,7 +10,7 @@ import random
...
@@ -10,7 +10,7 @@ import random
import
numpy
as
np
import
numpy
as
np
import
paddle.trainer.PyDataProvider2
as
dp
import
paddle.trainer.PyDataProvider2
as
dp
import
paddle.v2
import
paddle.v2
as
paddle_v2
import
py_paddle.swig_paddle
as
api
import
py_paddle.swig_paddle
as
api
from
paddle.trainer_config_helpers
import
*
from
paddle.trainer_config_helpers
import
*
from
py_paddle
import
DataProviderConverter
from
py_paddle
import
DataProviderConverter
...
@@ -58,7 +58,7 @@ def input_order_converter(generator):
...
@@ -58,7 +58,7 @@ def input_order_converter(generator):
def
main
():
def
main
():
api
.
initPaddle
(
"-use_gpu=false"
,
"-trainer_count=4"
)
# use 4 cpu cores
api
.
initPaddle
(
"-use_gpu=false"
,
"-trainer_count=4"
)
# use 4 cpu cores
optimizer
=
paddle
.
v2
.
optimizer
.
Adam
(
optimizer
=
paddle
_
v2
.
optimizer
.
Adam
(
learning_rate
=
1e-4
,
learning_rate
=
1e-4
,
batch_size
=
1000
,
batch_size
=
1000
,
model_average
=
ModelAverage
(
average_window
=
0.5
),
model_average
=
ModelAverage
(
average_window
=
0.5
),
...
@@ -71,16 +71,17 @@ def main():
...
@@ -71,16 +71,17 @@ def main():
assert
isinstance
(
updater
,
api
.
ParameterUpdater
)
assert
isinstance
(
updater
,
api
.
ParameterUpdater
)
# define network
# define network
images
=
paddle
.
v2
.
layers
.
data_layer
(
name
=
'pixel'
,
size
=
784
)
images
=
paddle_v2
.
layer
.
data
(
name
=
'pixel'
,
size
=
784
)
label
=
paddle
.
v2
.
layers
.
data_layer
(
name
=
'label'
,
size
=
10
)
label
=
paddle_v2
.
layer
.
data
(
name
=
'label'
,
size
=
10
)
hidden1
=
paddle
.
v2
.
layers
.
fc_layer
(
input
=
images
,
size
=
200
)
hidden1
=
paddle_v2
.
layer
.
fc
(
input
=
images
,
size
=
200
)
hidden2
=
paddle
.
v2
.
layers
.
fc_layer
(
input
=
hidden1
,
size
=
200
)
hidden2
=
paddle_v2
.
layer
.
fc
(
input
=
hidden1
,
size
=
200
)
inference
=
paddle
.
v2
.
layers
.
fc_layer
(
inference
=
paddle_v2
.
layer
.
fc
(
input
=
hidden2
,
input
=
hidden2
,
size
=
10
,
act
=
SoftmaxActivation
())
size
=
10
,
cost
=
paddle
.
v2
.
layers
.
classification_cost
(
input
=
inference
,
label
=
label
)
act
=
SoftmaxActivation
())
cost
=
paddle_v2
.
layer
.
classification_cost
(
input
=
inference
,
label
=
label
)
# Create Simple Gradient Machine.
# Create Simple Gradient Machine.
model_config
=
paddle
.
v2
.
layers
.
parse_network
(
cost
)
model_config
=
paddle
_v2
.
layer
.
parse_network
(
cost
)
m
=
api
.
GradientMachine
.
createFromConfigProto
(
model_config
,
m
=
api
.
GradientMachine
.
createFromConfigProto
(
model_config
,
api
.
CREATE_MODE_NORMAL
,
api
.
CREATE_MODE_NORMAL
,
optimizer
.
enable_types
())
optimizer
.
enable_types
())
...
...
python/paddle/v2/__init__.py
浏览文件 @
0bd75a57
...
@@ -13,6 +13,6 @@
...
@@ -13,6 +13,6 @@
# limitations under the License.
# limitations under the License.
import
optimizer
import
optimizer
import
layer
s
import
layer
__all__
=
[
'optimizer'
,
'layer
s
'
]
__all__
=
[
'optimizer'
,
'layer'
]
python/paddle/v2/layer
s
.py
→
python/paddle/v2/layer.py
浏览文件 @
0bd75a57
...
@@ -19,6 +19,21 @@ from paddle.trainer_config_helpers.default_decorators import wrap_name_default
...
@@ -19,6 +19,21 @@ from paddle.trainer_config_helpers.default_decorators import wrap_name_default
import
collections
import
collections
def
parse_network
(
*
outputs
):
"""
parse all output layers and then generate a model config proto.
:param outputs:
:return:
"""
def
__real_func__
():
context
=
dict
()
real_output
=
[
each
.
to_proto
(
context
=
context
)
for
each
in
outputs
]
conf_helps
.
outputs
(
real_output
)
return
__parse__
(
__real_func__
)
class
Layer
(
object
):
class
Layer
(
object
):
def
__init__
(
self
,
name
,
parent_layer
):
def
__init__
(
self
,
name
,
parent_layer
):
assert
isinstance
(
parent_layer
,
dict
)
assert
isinstance
(
parent_layer
,
dict
)
...
@@ -49,22 +64,13 @@ class Layer(object):
...
@@ -49,22 +64,13 @@ class Layer(object):
raise
NotImplementedError
()
raise
NotImplementedError
()
def
parse_network
(
*
outputs
):
def
__convert_to_v2__
(
method_name
,
name_prefix
,
parent_names
):
def
__real_func__
():
context
=
dict
()
real_output
=
[
each
.
to_proto
(
context
=
context
)
for
each
in
outputs
]
conf_helps
.
outputs
(
real_output
)
return
__parse__
(
__real_func__
)
def
__convert__
(
method_name
,
name_prefix
,
parent_names
):
if
name_prefix
is
not
None
:
if
name_prefix
is
not
None
:
wrapper
=
wrap_name_default
(
name_prefix
=
name_prefix
)
wrapper
=
wrap_name_default
(
name_prefix
=
name_prefix
)
else
:
else
:
wrapper
=
None
wrapper
=
None
class
__Impl__
(
Layer
):
class
V2LayerImpl
(
Layer
):
def
__init__
(
self
,
name
=
None
,
**
kwargs
):
def
__init__
(
self
,
name
=
None
,
**
kwargs
):
parent_layers
=
dict
()
parent_layers
=
dict
()
other_kwargs
=
dict
()
other_kwargs
=
dict
()
...
@@ -75,7 +81,7 @@ def __convert__(method_name, name_prefix, parent_names):
...
@@ -75,7 +81,7 @@ def __convert__(method_name, name_prefix, parent_names):
if
key
not
in
parent_names
:
if
key
not
in
parent_names
:
other_kwargs
[
key
]
=
kwargs
[
key
]
other_kwargs
[
key
]
=
kwargs
[
key
]
super
(
__Impl__
,
self
).
__init__
(
name
,
parent_layers
)
super
(
V2LayerImpl
,
self
).
__init__
(
name
,
parent_layers
)
self
.
__other_kwargs__
=
other_kwargs
self
.
__other_kwargs__
=
other_kwargs
if
wrapper
is
not
None
:
if
wrapper
is
not
None
:
...
@@ -89,24 +95,38 @@ def __convert__(method_name, name_prefix, parent_names):
...
@@ -89,24 +95,38 @@ def __convert__(method_name, name_prefix, parent_names):
args
[
each
]
=
self
.
__other_kwargs__
[
each
]
args
[
each
]
=
self
.
__other_kwargs__
[
each
]
return
getattr
(
conf_helps
,
method_name
)(
name
=
self
.
name
,
**
args
)
return
getattr
(
conf_helps
,
method_name
)(
name
=
self
.
name
,
**
args
)
return
__Impl__
return
V2LayerImpl
data_layer
=
__convert__
(
'data_layer'
,
None
,
[])
data
=
__convert_to_v2__
(
'data_layer'
,
None
,
[])
fc_layer
=
__convert__
(
'fc_layer'
,
name_prefix
=
'fc'
,
parent_names
=
[
'input'
])
fc
=
__convert_to_v2__
(
'fc_layer'
,
name_prefix
=
'fc'
,
parent_names
=
[
'input'
])
classification_cost
=
__convert__
(
max_id
=
__convert_to_v2__
(
'maxid_layer'
,
name_prefix
=
'maxid_layer'
,
parent_names
=
[
'input'
])
classification_cost
=
__convert_to_v2__
(
'classification_cost'
,
'classification_cost'
,
name_prefix
=
'classification_cost'
,
name_prefix
=
'classification_cost'
,
parent_names
=
[
'input'
,
'label'
])
parent_names
=
[
'input'
,
'label'
])
cross_entropy_cost
=
__convert_to_v2__
(
'cross_entropy'
,
name_prefix
=
'cross_entropy'
,
parent_names
=
[
'input'
,
'label'
])
__all__
=
[
'data_layer'
,
'fc_layer'
,
'classification_cost'
,
'parse_network'
]
__all__
=
[
'parse_network'
,
'data'
,
'fc'
,
'max_id'
,
'classification_cost'
,
'cross_entropy_cost'
]
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
data
=
data_layer
(
name
=
'pixel'
,
size
=
784
)
pixel
=
data
(
name
=
'pixel'
,
size
=
784
)
hidden
=
fc_layer
(
input
=
data
,
size
=
100
,
act
=
conf_helps
.
SigmoidActivation
())
label
=
data
(
name
=
'label'
,
size
=
10
)
predict
=
fc_layer
(
hidden
=
fc
(
input
=
pixel
,
size
=
100
,
act
=
conf_helps
.
SigmoidActivation
())
input
=
[
hidden
,
data
],
size
=
10
,
act
=
conf_helps
.
SoftmaxActivation
())
inference
=
fc
(
input
=
hidden
,
size
=
10
,
act
=
conf_helps
.
SoftmaxActivation
())
cost
=
classification_cost
(
maxid
=
max_id
(
input
=
inference
)
input
=
predict
,
label
=
data_layer
(
cost1
=
classification_cost
(
input
=
inference
,
label
=
label
)
name
=
'label'
,
size
=
10
))
cost2
=
cross_entropy_cost
(
input
=
inference
,
label
=
label
)
print
parse_network
(
cost
)
print
parse_network
(
cost1
)
print
parse_network
(
cost2
)
print
parse_network
(
cost1
,
cost2
)
print
parse_network
(
cost2
)
print
parse_network
(
inference
,
maxid
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录