Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
0ab678e9
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看板
提交
0ab678e9
编写于
7月 24, 2017
作者:
Y
Yu Yang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add unittest for network
上级
c14f3e8f
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
62 addition
and
15 deletion
+62
-15
paddle/pybind/pybind.cc
paddle/pybind/pybind.cc
+0
-11
python/paddle/v2/framework/network.py
python/paddle/v2/framework/network.py
+37
-3
python/paddle/v2/framework/tests/CMakeLists.txt
python/paddle/v2/framework/tests/CMakeLists.txt
+2
-1
python/paddle/v2/framework/tests/test_network.py
python/paddle/v2/framework/tests/test_network.py
+23
-0
未找到文件。
paddle/pybind/pybind.cc
浏览文件 @
0ab678e9
...
...
@@ -56,17 +56,6 @@ void ExposeOperator(ClassType& m) {
.
def
(
"__str__"
,
&
ClassType
::
type
::
DebugString
);
}
template
<
typename
ClassType
>
void
ExposeOperator
(
ClassType
&
m
)
{
m
.
def
(
"infer_shape"
,
&
ClassType
::
type
::
InferShape
)
.
def
(
"run"
,
&
ClassType
::
type
::
Run
)
.
def
(
"outputs"
,
[](
const
typename
ClassType
::
type
&
op
)
->
std
::
vector
<
std
::
string
>
{
return
op
.
outputs_
;
})
.
def
(
"__str__"
,
&
ClassType
::
type
::
DebugString
);
}
PYBIND11_PLUGIN
(
core
)
{
py
::
module
m
(
"core"
,
"C++ core of PaddlePaddle"
);
...
...
python/paddle/v2/framework/network.py
浏览文件 @
0ab678e9
...
...
@@ -2,13 +2,28 @@ import paddle.v2.framework.core as core
from
paddle.v2.framework.create_op_creation_methods
import
op_creations
from
default_scope_funcs
import
create_var
,
get_var
,
get_cur_scope
__all__
=
[
'Network'
]
# Only expose Network
class
NetworkFunctor
(
object
):
"""
Network Op Creation Function. Used internally in this module.
It convert string input to Variable. If it is not created before, just
create in scope.
It is a functor object. means the instances are callable.
:param func: The op creation function which generated in Python.
:param net: The Network instance.
"""
def
__init__
(
self
,
func
,
net
):
self
.
func
=
func
self
.
net
=
net
def
__call__
(
self
,
**
kwargs
):
def
__call__
(
self
,
*
args
,
**
kwargs
):
if
len
(
args
)
!=
0
:
raise
ValueError
(
"Paddle must use keyword argument"
)
inputs
=
self
.
func
.
all_input_args
for
ipt
in
inputs
:
if
ipt
in
kwargs
:
...
...
@@ -58,6 +73,22 @@ class NetworkFunctor(object):
class
Network
(
object
):
"""
The network concept. It avoid user to manually create operator, create
variable, and combine them into a Net. Just use Network.xxx can create the
operator, create variables in default scope, and add them into `self.net`.
For example:
.. code-block: python
net = Network()
out = net.add_two(X="a", Y="b")
fc_out = net.fc(X="out", W="fc.w")
net.run(...)
"""
def
__init__
(
self
):
self
.
net
=
core
.
Net
.
create
()
funcs
=
(
func_name
for
func_name
in
dir
(
op_creations
)
...
...
@@ -65,6 +96,9 @@ class Network(object):
self
.
generate_idx
=
0
self
.
var_name_map
=
dict
()
# TODO(yuyang18): This code can work, but do not generate a good
# docstring, try to give a better way generate function in runtime
# later.
for
func_name
in
funcs
:
func
=
getattr
(
op_creations
,
func_name
)
impl
=
NetworkFunctor
(
func
,
self
)
...
...
@@ -92,5 +126,5 @@ if __name__ == '__main__':
net
=
Network
()
out
=
net
.
add_two
(
X
=
"a"
,
Y
=
"b"
)
fc_out
=
net
.
fc
(
X
=
out
,
W
=
"fc.w"
,
b
=
"fc.b"
,
activation
=
"softmax"
)
print
str
(
net
)
net
.
complete_add_op
()
print
net
python/paddle/v2/framework/tests/CMakeLists.txt
浏览文件 @
0ab678e9
...
...
@@ -12,4 +12,5 @@ add_python_test(test_framework
test_mul_op.py
test_sigmoid_op.py
test_softmax_op.py
test_rowwise_add_op.py
)
test_rowwise_add_op.py
test_network.py
)
python/paddle/v2/framework/tests/test_network.py
0 → 100644
浏览文件 @
0ab678e9
from
paddle.v2.framework.network
import
Network
import
paddle.v2.framework.core
as
core
import
unittest
class
TestNet
(
unittest
.
TestCase
):
def
test_net_all
(
self
):
net
=
Network
()
out
=
net
.
add_two
(
X
=
"X"
,
Y
=
"Y"
)
fc_out
=
net
.
fc
(
X
=
out
,
W
=
"w"
)
net
.
complete_add_op
()
self
.
assertTrue
(
isinstance
(
fc_out
,
core
.
Variable
))
self
.
assertEqual
(
'''Op(naive_net), inputs:(@EMPTY@, X, Y, w), outputs:(@TEMP@fc@0, add_two@OUT@0, fc@OUT@1).
Op(add_two), inputs:(X, Y), outputs:(add_two@OUT@0).
Op(fc), inputs:(add_two@OUT@0, w, @EMPTY@), outputs:(fc@OUT@1, @TEMP@fc@0).
Op(mul), inputs:(add_two@OUT@0, w), outputs:(@TEMP@fc@0).
Op(sigmoid), inputs:(@TEMP@fc@0), outputs:(fc@OUT@1).
'''
,
str
(
net
))
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录