Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
21baa94b
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2305
Star
20932
Fork
5423
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
21baa94b
编写于
7月 19, 2017
作者:
Y
Yu Yang
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'feature/expose_net_op' into feature/middle_level_net_api
上级
d5291340
0380bfb3
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
81 addition
and
23 deletion
+81
-23
paddle/framework/net.cc
paddle/framework/net.cc
+4
-1
paddle/pybind/pybind.cc
paddle/pybind/pybind.cc
+49
-22
python/paddle/v2/framework/tests/test_net.py
python/paddle/v2/framework/tests/test_net.py
+28
-0
未找到文件。
paddle/framework/net.cc
浏览文件 @
21baa94b
...
...
@@ -61,7 +61,10 @@ std::string PlainNet::DebugString() const {
std
::
ostringstream
os
;
os
<<
this
->
type_
<<
":"
<<
std
::
endl
;
for
(
auto
&
op
:
ops_
)
{
os
<<
"
\t
"
<<
op
->
DebugString
()
<<
std
::
endl
;
std
::
istringstream
is
(
op
->
DebugString
());
for
(
std
::
string
line
;
std
::
getline
(
is
,
line
);)
{
os
<<
" "
<<
line
<<
std
::
endl
;
}
}
return
os
.
str
();
}
...
...
paddle/pybind/pybind.cc
浏览文件 @
21baa94b
...
...
@@ -13,15 +13,16 @@ See the License for the specific language governing permissions and
limitations under the License. */
#include <Python.h>
#include <paddle/framework/op_registry.h>
#include <paddle/framework/operator.h>
#include <paddle/framework/scope.h>
#include <paddle/pybind/tensor_bind.h>
#include <pybind11/numpy.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <fstream>
#include <vector>
#include "paddle/framework/net.h"
#include "paddle/framework/op_registry.h"
#include "paddle/framework/operator.h"
#include "paddle/framework/scope.h"
#include "paddle/pybind/tensor_bind.h"
#include "pybind11/numpy.h"
#include "pybind11/pybind11.h"
#include "pybind11/stl.h"
namespace
py
=
pybind11
;
namespace
pd
=
paddle
::
framework
;
...
...
@@ -29,6 +30,17 @@ namespace pd = paddle::framework;
USE_OP
(
add_two
);
USE_OP_WITHOUT_KERNEL
(
fc
);
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 Paddle Paddle"
);
...
...
@@ -107,21 +119,36 @@ All parameter, weight, gradient are variables in Paddle.
return
new
paddle
::
platform
::
CPUDeviceContext
();
});
py
::
class_
<
pd
::
OperatorBase
,
pd
::
OperatorPtr
>
(
m
,
"Operator"
)
.
def
(
"__str__"
,
&
pd
::
OperatorBase
::
DebugString
)
.
def_static
(
"create"
,
[](
py
::
bytes
protobin
)
{
pd
::
OpDesc
desc
;
PADDLE_ENFORCE
(
desc
.
ParsePartialFromString
(
protobin
),
"Cannot parse user input to OpDesc"
);
PADDLE_ENFORCE
(
desc
.
IsInitialized
(),
"User OpDesc is not initialized, reason %s"
,
desc
.
InitializationErrorString
());
return
pd
::
OpRegistry
::
CreateOp
(
desc
);
})
.
def
(
"infer_shape"
,
&
pd
::
OperatorBase
::
InferShape
)
.
def
(
"run"
,
&
pd
::
OperatorBase
::
Run
)
.
def
(
"outputs"
,
[](
const
pd
::
OperatorPtr
&
op
)
{
return
op
->
outputs_
;
});
py
::
class_
<
pd
::
OperatorBase
,
pd
::
OperatorPtr
>
operator_base
(
m
,
"Operator"
);
operator_base
.
def_static
(
"create"
,
[](
py
::
bytes
protobin
)
->
pd
::
OperatorPtr
{
pd
::
OpDesc
desc
;
PADDLE_ENFORCE
(
desc
.
ParsePartialFromString
(
protobin
),
"Cannot parse user input to OpDesc"
);
PADDLE_ENFORCE
(
desc
.
IsInitialized
(),
"User OpDesc is not initialized, reason %s"
,
desc
.
InitializationErrorString
());
return
pd
::
OpRegistry
::
CreateOp
(
desc
);
});
ExposeOperator
(
operator_base
);
using
PlainNetPtr
=
std
::
shared_ptr
<
pd
::
PlainNet
>
;
py
::
class_
<
pd
::
PlainNet
,
PlainNetPtr
>
net
(
m
,
"Net"
);
net
.
def_static
(
"create"
,
[]()
->
std
::
shared_ptr
<
pd
::
PlainNet
>
{
auto
retv
=
std
::
make_shared
<
pd
::
PlainNet
>
();
retv
->
type_
=
"naive_net"
;
return
retv
;
})
.
def
(
"add_op"
,
&
pd
::
PlainNet
::
AddOp
)
.
def
(
"add_op"
,
[](
PlainNetPtr
&
self
,
const
PlainNetPtr
&
net
)
->
void
{
self
->
AddOp
(
std
::
static_pointer_cast
<
pd
::
OperatorBase
>
(
net
));
})
.
def
(
"complete_add_op"
,
&
pd
::
PlainNet
::
CompleteAddOp
)
.
def
(
"complete_add_op"
,
[](
PlainNetPtr
&
self
)
{
self
->
CompleteAddOp
();
});
ExposeOperator
(
net
);
return
m
.
ptr
();
}
python/paddle/v2/framework/tests/test_net.py
0 → 100644
浏览文件 @
21baa94b
import
paddle.v2.framework.core
as
core
from
paddle.v2.framework.create_op_creation_methods
import
op_creations
import
unittest
class
TestNet
(
unittest
.
TestCase
):
def
test_net_all
(
self
):
net
=
core
.
Net
.
create
()
op1
=
op_creations
.
add_two
(
X
=
"X"
,
Y
=
"Y"
,
Out
=
"Out"
)
net
.
add_op
(
op1
)
net2
=
core
.
Net
.
create
()
net2
.
add_op
(
op_creations
.
fc
(
X
=
"X"
,
W
=
"w"
,
Y
=
"fc.out"
))
net2
.
complete_add_op
(
True
)
net
.
add_op
(
net2
)
net
.
complete_add_op
(
True
)
expected
=
'''naive_net:
Op(add_two), inputs:(X, Y), outputs:(Out).
naive_net:
fc:
Op(mul), inputs:(X, w), outputs:(@TEMP@fc@0).
Op(sigmoid), inputs:(@TEMP@fc@0), outputs:(fc.out).
'''
self
.
assertEqual
(
expected
,
str
(
net
))
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录