Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
8deff48d
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看板
未验证
提交
8deff48d
编写于
6月 08, 2018
作者:
Y
Yu Yang
提交者:
GitHub
6月 08, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #11081 from reyoung/feature/python_doc
Add document to random crop operator
上级
e3cb0dc3
59d75bda
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
106 addition
and
13 deletion
+106
-13
paddle/fluid/operators/random_crop_op.cc
paddle/fluid/operators/random_crop_op.cc
+3
-3
python/paddle/fluid/layers/layer_function_generator.py
python/paddle/fluid/layers/layer_function_generator.py
+52
-8
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+51
-2
未找到文件。
paddle/fluid/operators/random_crop_op.cc
浏览文件 @
8deff48d
...
@@ -36,11 +36,11 @@ class RandomCropOpMaker : public framework::OpProtoAndCheckerMaker {
...
@@ -36,11 +36,11 @@ class RandomCropOpMaker : public framework::OpProtoAndCheckerMaker {
AddInput
(
"Seed"
,
"The random seed."
);
AddInput
(
"Seed"
,
"The random seed."
);
AddOutput
(
"Out"
,
"The cropped instance batch."
);
AddOutput
(
"Out"
,
"The cropped instance batch."
);
AddOutput
(
"SeedOut"
,
"The random seed after random cropping."
)
AddOutput
(
"SeedOut"
,
"The random seed after random cropping."
)
.
As
Dispensabl
e
();
.
As
Intermediat
e
();
AddAttr
<
std
::
vector
<
int
>>
(
"shape"
,
"The shape of a cropped instance."
);
AddAttr
<
std
::
vector
<
int
>>
(
"shape"
,
"The shape of a cropped instance."
);
AddComment
(
R"DOC(
AddComment
(
R"DOC(
This operator takes a batch of instance, and do random cropping on each instance.
This operator takes a batch of instance, and do random cropping on each instance.
It means that cropping positions differs on each instance, which is determined
It means that cropping positions differs on each instance, which is determined
by an uniform random generator. All cropped instances have the same shape, which
by an uniform random generator. All cropped instances have the same shape, which
is determined by the operator's attribute 'shape'.
is determined by the operator's attribute 'shape'.
)DOC"
);
)DOC"
);
...
...
python/paddle/fluid/layers/layer_function_generator.py
浏览文件 @
8deff48d
...
@@ -15,16 +15,13 @@ import re
...
@@ -15,16 +15,13 @@ import re
import
cStringIO
import
cStringIO
import
functools
import
functools
import
warnings
import
warnings
import
string
from
..proto
import
framework_pb2
from
..proto
import
framework_pb2
from
..framework
import
OpProtoHolder
,
Variable
from
..framework
import
OpProtoHolder
,
Variable
from
..layer_helper
import
LayerHelper
from
..layer_helper
import
LayerHelper
__all__
=
[
__all__
=
[
'deprecated'
,
'generate_layer_fn'
,
'autodoc'
,
'templatedoc'
]
'deprecated'
,
'generate_layer_fn'
,
'autodoc'
,
]
def
_convert_
(
name
):
def
_convert_
(
name
):
...
@@ -43,6 +40,10 @@ def _convert_(name):
...
@@ -43,6 +40,10 @@ def _convert_(name):
return
re
.
sub
(
'([a-z0-9])([A-Z])'
,
r
'\1_\2'
,
s1
).
lower
()
return
re
.
sub
(
'([a-z0-9])([A-Z])'
,
r
'\1_\2'
,
s1
).
lower
()
def
_type_to_str_
(
tp
):
return
framework_pb2
.
AttrType
.
Name
(
tp
)
def
_generate_doc_string_
(
op_proto
):
def
_generate_doc_string_
(
op_proto
):
"""
"""
Generate docstring by OpProto
Generate docstring by OpProto
...
@@ -54,9 +55,6 @@ def _generate_doc_string_(op_proto):
...
@@ -54,9 +55,6 @@ def _generate_doc_string_(op_proto):
str: the document string
str: the document string
"""
"""
def
_type_to_str_
(
tp
):
return
framework_pb2
.
AttrType
.
Name
(
tp
)
if
not
isinstance
(
op_proto
,
framework_pb2
.
OpProto
):
if
not
isinstance
(
op_proto
,
framework_pb2
.
OpProto
):
raise
TypeError
(
"OpProto should be `framework_pb2.OpProto`"
)
raise
TypeError
(
"OpProto should be `framework_pb2.OpProto`"
)
...
@@ -224,3 +222,49 @@ def autodoc(comment=""):
...
@@ -224,3 +222,49 @@ def autodoc(comment=""):
return
func
return
func
return
__impl__
return
__impl__
def
templatedoc
():
"""
Decorator of layer function. It will use the docstring from the layer
function as the template. The template arguments are:
* ${comment}: The operator comment written in CPP.
* ${{name}_comment}: The comment of ${name} written with AddAttr, AddOutput,
and AddInput. The ${name} is Python snake style. i.e., xxx_xxx.
* ${{name}_type}: The type of ${name}.
Returns:
Decorated function.
"""
def
__impl__
(
func
):
op_proto
=
OpProtoHolder
.
instance
().
get_op_proto
(
func
.
__name__
)
tmpl
=
string
.
Template
(
func
.
__doc__
)
comment_lines
=
op_proto
.
comment
.
split
(
"
\n
"
)
comment
=
""
for
line
in
comment_lines
:
line
=
line
.
lstrip
()
comment
+=
line
comment
+=
"
\n
"
args
=
{
"comment"
:
comment
}
for
each_input
in
op_proto
.
inputs
:
input_name
=
_convert_
(
each_input
.
name
)
args
[
"{0}_comment"
.
format
(
input_name
)]
=
each_input
.
comment
args
[
"{0}_type"
.
format
(
input_name
)]
=
"Variable"
for
each_attr
in
op_proto
.
attrs
:
input_name
=
_convert_
(
each_attr
.
name
)
args
[
"{0}_comment"
.
format
(
input_name
)]
=
each_attr
.
comment
args
[
"{0}_type"
.
format
(
input_name
)]
=
_type_to_str_
(
each_attr
.
type
)
for
each_opt
in
op_proto
.
outputs
:
output_name
=
_convert_
(
each_opt
.
name
)
args
[
"{0}_comment"
.
format
(
output_name
)]
=
each_opt
.
comment
args
[
"{0}_type"
.
format
(
output_name
)]
=
"Variable"
func
.
__doc__
=
tmpl
.
substitute
(
args
)
return
func
return
__impl__
python/paddle/fluid/layers/nn.py
浏览文件 @
8deff48d
...
@@ -19,9 +19,10 @@ from ..layer_helper import LayerHelper
...
@@ -19,9 +19,10 @@ from ..layer_helper import LayerHelper
from
..initializer
import
Normal
,
Constant
from
..initializer
import
Normal
,
Constant
from
..framework
import
Variable
from
..framework
import
Variable
from
..param_attr
import
ParamAttr
from
..param_attr
import
ParamAttr
from
layer_function_generator
import
autodoc
from
layer_function_generator
import
autodoc
,
templatedoc
from
tensor
import
concat
from
tensor
import
concat
import
utils
import
utils
import
random
__all__
=
[
__all__
=
[
'fc'
,
'fc'
,
...
@@ -801,7 +802,22 @@ def gru_unit(input,
...
@@ -801,7 +802,22 @@ def gru_unit(input,
return
updated_hidden
,
reset_hidden_pre
,
gate
return
updated_hidden
,
reset_hidden_pre
,
gate
@
templatedoc
()
def
linear_chain_crf
(
input
,
label
,
param_attr
=
None
):
def
linear_chain_crf
(
input
,
label
,
param_attr
=
None
):
"""
Linear Chain CRF.
${comment}
Args:
input(${emission_type}): ${emission_comment}
label(${label_type}): ${label_comment}
param_attr(ParamAttr): The attribute of the learnable parameter.
Returns:
${log_likelihood_comment}
"""
helper
=
LayerHelper
(
'linear_chain_crf'
,
**
locals
())
helper
=
LayerHelper
(
'linear_chain_crf'
,
**
locals
())
size
=
input
.
shape
[
1
]
size
=
input
.
shape
[
1
]
transition
=
helper
.
create_parameter
(
transition
=
helper
.
create_parameter
(
...
@@ -827,7 +843,19 @@ def linear_chain_crf(input, label, param_attr=None):
...
@@ -827,7 +843,19 @@ def linear_chain_crf(input, label, param_attr=None):
return
log_likelihood
return
log_likelihood
@
templatedoc
()
def
crf_decoding
(
input
,
param_attr
,
label
=
None
):
def
crf_decoding
(
input
,
param_attr
,
label
=
None
):
"""
${comment}
Args:
input(${emission_type}): ${emission_comment}
param_attr(ParamAttr): The parameter attribute for training.
label(${label_type}): ${label_comment}
Returns:
${viterbi_path_comment}
"""
helper
=
LayerHelper
(
'crf_decoding'
,
**
locals
())
helper
=
LayerHelper
(
'crf_decoding'
,
**
locals
())
transition
=
helper
.
get_parameter
(
param_attr
.
name
)
transition
=
helper
.
get_parameter
(
param_attr
.
name
)
viterbi_path
=
helper
.
create_tmp_variable
(
dtype
=
helper
.
input_dtype
())
viterbi_path
=
helper
.
create_tmp_variable
(
dtype
=
helper
.
input_dtype
())
...
@@ -4107,10 +4135,31 @@ def gather(input, index):
...
@@ -4107,10 +4135,31 @@ def gather(input, index):
return
out
return
out
def
random_crop
(
input
,
shape
,
seed
=
1
):
@
templatedoc
()
def
random_crop
(
x
,
shape
,
seed
=
None
):
"""
${comment}
Examples:
>>> img = fluid.layers.data("img", [3, 256, 256])
>>> cropped_img = fluid.layers.random_crop(img, shape=[3, 224, 224])
Args:
x(${x_type}): ${x_comment}
shape(${shape_type}): ${shape_comment}
seed(int|${seed_type}|None): ${seed_comment} By default, the seed will
get from `random.randint(-65536, 65535)`.
Returns:
${out_comment}
"""
helper
=
LayerHelper
(
"random_crop"
,
**
locals
())
helper
=
LayerHelper
(
"random_crop"
,
**
locals
())
dtype
=
helper
.
input_dtype
()
dtype
=
helper
.
input_dtype
()
out
=
helper
.
create_tmp_variable
(
dtype
)
out
=
helper
.
create_tmp_variable
(
dtype
)
if
seed
is
None
:
seed
=
random
.
randint
(
-
65536
,
65535
)
if
isinstance
(
seed
,
int
):
if
isinstance
(
seed
,
int
):
seed_value
=
seed
seed_value
=
seed
seed
=
helper
.
create_tmp_variable
(
dtype
=
"int64"
)
seed
=
helper
.
create_tmp_variable
(
dtype
=
"int64"
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录