Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
urhero
Paddle
提交
350b268b
P
Paddle
项目概览
urhero
/
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,发现更多精彩内容 >>
提交
350b268b
编写于
5月 30, 2017
作者:
X
xuwei06
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Adding simple operators for v2 API
上级
0ef86cbd
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
174 addition
and
3 deletion
+174
-3
python/paddle/v2/__init__.py
python/paddle/v2/__init__.py
+1
-0
python/paddle/v2/config_base.py
python/paddle/v2/config_base.py
+2
-2
python/paddle/v2/op.py
python/paddle/v2/op.py
+120
-0
python/paddle/v2/tests/CMakeLists.txt
python/paddle/v2/tests/CMakeLists.txt
+1
-1
python/paddle/v2/tests/test_op.py
python/paddle/v2/tests/test_op.py
+50
-0
未找到文件。
python/paddle/v2/__init__.py
浏览文件 @
350b268b
...
...
@@ -27,6 +27,7 @@ from . import dataset
from
.
import
reader
from
.
import
plot
import
attr
import
op
import
pooling
import
inference
import
networks
...
...
python/paddle/v2/config_base.py
浏览文件 @
350b268b
...
...
@@ -36,9 +36,9 @@ def __map_docstr__(doc, name):
# xxx_layer to xxx
doc
=
re
.
sub
(
r
"(?P<name>[a-z]+)_layer"
,
r
"\g<name>"
,
doc
)
# XxxxActivation to paddle.v2.
A
ctivation.Xxxx
# XxxxActivation to paddle.v2.
a
ctivation.Xxxx
doc
=
re
.
sub
(
r
"(?P<name>[A-Z][a-zA-Z]+)Activation"
,
r
"paddle.v2.
A
ctivation.\g<name>"
,
doc
)
r
"paddle.v2.
a
ctivation.\g<name>"
,
doc
)
# xxx_evaluator to paddle.v2.evaluator.xxx
doc
=
re
.
sub
(
r
"(?P<name>[a-z]+)_evaluator"
,
r
"evaluator.\g<name>"
,
doc
)
...
...
python/paddle/v2/op.py
0 → 100644
浏览文件 @
350b268b
# Copyright (c) 2016 PaddlePaddle Authors. 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
layer
import
activation
as
act
from
config_base
import
Layer
from
paddle.trainer_config_helpers.attrs
import
is_compatible_with
from
paddle.trainer_config_helpers.default_decorators
import
wrap_name_default
__all__
=
[]
def
__register_unary_math_op__
(
op_name
,
act
):
def
op
(
input
,
name
=
None
):
return
layer
.
mixed
(
input
=
[
layer
.
identity_projection
(
input
=
input
)],
name
=
name
,
act
=
act
)
op
=
wrap_name_default
(
op_name
)(
op
)
op
.
__doc__
=
type
(
act
).
__doc__
globals
()[
op_name
]
=
op
__all__
.
append
(
op_name
)
__register_unary_math_op__
(
'exp'
,
act
.
Exp
())
__register_unary_math_op__
(
'log'
,
act
.
Log
())
__register_unary_math_op__
(
'abs'
,
act
.
Abs
())
__register_unary_math_op__
(
'sigmoid'
,
act
.
Sigmoid
())
__register_unary_math_op__
(
'tanh'
,
act
.
Tanh
())
__register_unary_math_op__
(
'square'
,
act
.
Square
())
__register_unary_math_op__
(
'relu'
,
act
.
Relu
())
__register_unary_math_op__
(
'sqrt'
,
act
.
Sqrt
())
__register_unary_math_op__
(
'reciprocal'
,
act
.
Reciprocal
())
__register_unary_math_op__
(
'softmax'
,
act
.
Softmax
())
def
__add__
(
layeroutput
,
other
):
if
is_compatible_with
(
other
,
float
):
return
layer
.
slope_intercept
(
input
=
layeroutput
,
intercept
=
other
)
if
not
isinstance
(
other
,
Layer
):
raise
TypeError
(
"Layer can only be added with"
" another Layer or a number"
)
if
layeroutput
.
size
==
other
.
size
:
return
layer
.
mixed
(
input
=
[
layer
.
identity_projection
(
input
=
layeroutput
),
layer
.
identity_projection
(
input
=
other
)
])
if
other
.
size
!=
1
and
layeroutput
.
size
!=
1
:
raise
TypeError
(
"Two Layer can be added only if they have equal size"
" or one of their sizes is 1. sizes are %s and %s"
%
(
layeroutput
.
size
,
other
.
size
))
elif
layeroutput
.
size
==
1
:
tmp
=
layeroutput
layeroutput
=
other
other
=
tmp
other
=
layer
.
repeat
(
other
,
layeroutput
.
size
)
return
layer
.
mixed
(
input
=
[
layer
.
identity_projection
(
input
=
layeroutput
),
layer
.
identity_projection
(
input
=
other
)
])
Layer
.
__radd__
=
__add__
Layer
.
__add__
=
__add__
def
__neg__
(
layeroutput
):
return
layer
.
slope_intercept
(
input
=
layeroutput
,
slope
=-
1.0
)
Layer
.
__neg__
=
__neg__
def
__sub__
(
layeroutput
,
other
):
if
is_compatible_with
(
other
,
float
):
return
layer
.
slope_intercept
(
input
=
layeroutput
,
intercept
=
other
)
if
not
isinstance
(
other
,
Layer
):
raise
TypeError
(
"Layer can only be subtracted with"
" another Layeroutput or a number"
)
return
__add__
(
layeroutput
,
-
other
)
Layer
.
__sub__
=
__sub__
def
__rsub__
(
layeroutput
,
other
):
neg
=
layer
.
slope_intercept
(
input
=
layeroutput
,
slope
=-
1.0
)
return
__add__
(
neg
,
other
)
Layer
.
__rsub__
=
__rsub__
def
__mul__
(
layeroutput
,
other
):
if
is_compatible_with
(
other
,
float
):
return
layer
.
slope_intercept
(
input
=
layeroutput
,
slope
=
other
)
if
not
isinstance
(
other
,
Layer
):
raise
TypeError
(
"Layer can only be multiplied with"
" another Layer or a number"
)
elif
layeroutput
.
size
==
1
:
return
layer
.
scaling
(
input
=
other
,
weight
=
layeroutput
)
elif
other
.
size
==
1
:
return
layer
.
scaling
(
input
=
layeroutput
,
weight
=
other
)
else
:
raise
TypeError
(
"At least one of the operand of '*' must be a number"
" or a Layer with size=1"
)
Layer
.
__mul__
=
__mul__
Layer
.
__rmul__
=
__mul__
python/paddle/v2/tests/CMakeLists.txt
浏览文件 @
350b268b
add_python_test
(
test_v2_api test_data_feeder.py test_parameters.py
add_python_test
(
test_v2_api test_data_feeder.py test_
op.py test_
parameters.py
test_layer.py test_rnn_layer.py test_topology.py test_image.py
)
python/paddle/v2/tests/test_op.py
0 → 100644
浏览文件 @
350b268b
# 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
unittest
import
paddle.v2.data_type
as
data_type
import
paddle.v2.layer
as
layer
import
paddle.v2.op
as
op
class
OpTest
(
unittest
.
TestCase
):
def
test_op
(
self
):
x
=
layer
.
data
(
name
=
'data'
,
type
=
data_type
.
dense_vector
(
128
))
x
=
op
.
exp
(
x
)
x
=
op
.
sqrt
(
x
)
x
=
op
.
reciprocal
(
x
)
x
=
op
.
log
(
x
)
x
=
op
.
abs
(
x
)
x
=
op
.
sigmoid
(
x
)
x
=
op
.
tanh
(
x
)
x
=
op
.
square
(
x
)
x
=
op
.
relu
(
x
)
y
=
1
+
x
y
=
y
+
1
y
=
x
+
y
y
=
y
-
x
y
=
y
-
2
y
=
2
-
y
y
=
2
*
y
y
=
y
*
3
z
=
layer
.
data
(
name
=
'data_2'
,
type
=
data_type
.
dense_vector
(
1
))
y
=
y
*
z
y
=
z
*
y
y
=
y
+
z
y
=
z
+
y
print
layer
.
parse_network
(
y
)
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录