未验证 提交 9328c3cf 编写于 作者: Y Yu Yang 提交者: GitHub

Merge pull request #11308 from reyoung/feature/polish_api_ref

Simplize API Reference Documentation
...@@ -54,18 +54,18 @@ class BatchSizeLikeOp : public framework::OperatorWithKernel { ...@@ -54,18 +54,18 @@ class BatchSizeLikeOp : public framework::OperatorWithKernel {
class BatchSizeLikeOpMaker : public framework::OpProtoAndCheckerMaker { class BatchSizeLikeOpMaker : public framework::OpProtoAndCheckerMaker {
public: public:
void Make() final { void Make() final {
AddInput("Input", AddInput(
"(Tensor) Tensor " "Input",
"whose input_dim_idx'th dimension specifies the batch_size"); "Tensor whose input_dim_idx'th dimension specifies the batch_size");
AddOutput("Out", AddOutput("Out",
"(Tensor) Tensor of specified shape will be filled " "Tensor of specified shape will be filled "
"with the specified value"); "with the specified value");
AddAttr<std::vector<int>>("shape", "(vector<int>) The shape of the output"); AddAttr<std::vector<int>>("shape", "The shape of the output");
AddAttr<int>("input_dim_idx", AddAttr<int>("input_dim_idx",
"(int, default 0) The index of input's batch size dimension") "default 0. The index of input's batch size dimension")
.SetDefault(0); .SetDefault(0);
AddAttr<int>("output_dim_idx", AddAttr<int>("output_dim_idx",
"(int, default 0) The index of output's batch size dimension") "default 0. The index of output's batch size dimension")
.SetDefault(0); .SetDefault(0);
Apply(); Apply();
} }
......
...@@ -56,17 +56,16 @@ class BilinearInterpOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -56,17 +56,16 @@ class BilinearInterpOpMaker : public framework::OpProtoAndCheckerMaker {
public: public:
void Make() override { void Make() override {
AddInput("X", AddInput("X",
"(Tensor) The input tensor of bilinear interpolation, " "The input tensor of bilinear interpolation, "
"This is a 4-D tensor with shape of (N x C x h x w)"); "This is a 4-D tensor with shape of (N x C x h x w)");
AddInput("OutSize", AddInput("OutSize",
"(Tensor) This is a 1-D tensor with two number. " "This is a 1-D tensor with two number. "
"The first number is height and the second number is width.") "The first number is height and the second number is width.")
.AsDispensable(); .AsDispensable();
AddOutput("Out", AddOutput("Out", "The dimension of output is (N x C x out_h x out_w)");
"(Tensor) The dimension of output is (N x C x out_h x out_w]");
AddAttr<int>("out_h", "(int) output height of bilinear interpolation op."); AddAttr<int>("out_h", "output height of bilinear interpolation op.");
AddAttr<int>("out_w", "(int) output width of bilinear interpolation op."); AddAttr<int>("out_w", "output width of bilinear interpolation op.");
AddComment(R"DOC( AddComment(R"DOC(
Bilinear interpolation is an extension of linear interpolation for Bilinear interpolation is an extension of linear interpolation for
interpolating functions of two variables (e.g. H-direction and interpolating functions of two variables (e.g. H-direction and
......
...@@ -32,16 +32,16 @@ class FillConstantBatchSizeLikeOp : public BatchSizeLikeOp { ...@@ -32,16 +32,16 @@ class FillConstantBatchSizeLikeOp : public BatchSizeLikeOp {
class FillConstantBatchSizeLikeOpMaker : public BatchSizeLikeOpMaker { class FillConstantBatchSizeLikeOpMaker : public BatchSizeLikeOpMaker {
protected: protected:
void Apply() override { void Apply() override {
AddAttr<int>("dtype", AddAttr<int>(
"(int, default 5 (FP32)) " "dtype",
"Output data type") "It could be numpy.dtype. Output data type. Default is float32")
.SetDefault(framework::proto::VarType::FP32); .SetDefault(framework::proto::VarType::FP32);
AddAttr<float>("value", "(float, default 0) The value to be filled") AddAttr<float>("value", "default 0. The value to be filled")
.SetDefault(0.0f); .SetDefault(0.0f);
AddComment(R"DOC( AddComment(R"DOC(
FillConstantBatchSizeLike Operator. This function creates a tensor of specified *shape*, *dtype* and batch size,
and initializes this with a constant supplied in *value*. The batch size is
Fill up a variable with specified constant value. obtained from the `input` tensor.
)DOC"); )DOC");
} }
......
...@@ -67,8 +67,6 @@ class LinearChainCRFOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -67,8 +67,6 @@ class LinearChainCRFOpMaker : public framework::OpProtoAndCheckerMaker {
"mini-batch. Note: S is equal to the sequence number in a mini-batch. " "mini-batch. Note: S is equal to the sequence number in a mini-batch. "
"The output is no longer a LoDTensor."); "The output is no longer a LoDTensor.");
AddComment(R"DOC( AddComment(R"DOC(
LinearChainCRF Operator.
Conditional Random Field defines an undirected probabilistic graph with nodes Conditional Random Field defines an undirected probabilistic graph with nodes
denoting random variables and edges denoting dependencies between these denoting random variables and edges denoting dependencies between these
variables. CRF learns the conditional probability $P(Y|X)$, where variables. CRF learns the conditional probability $P(Y|X)$, where
......
...@@ -74,25 +74,18 @@ class LoadOp : public framework::OperatorBase { ...@@ -74,25 +74,18 @@ class LoadOp : public framework::OperatorBase {
class LoadOpProtoMaker : public framework::OpProtoAndCheckerMaker { class LoadOpProtoMaker : public framework::OpProtoAndCheckerMaker {
public: public:
void Make() override { void Make() override {
AddOutput("Out", "(Tensor) The tensor need to be loaded"); AddOutput("Out", "The tensor need to be loaded");
AddAttr<bool>( AddAttr<bool>(
"load_as_fp16", "load_as_fp16",
"(boolean, default false)"
"If true, the tensor will be first loaded and then " "If true, the tensor will be first loaded and then "
"converted to float16 data type. Otherwise, the tensor will be " "converted to float16 data type. Otherwise, the tensor will be "
"directly loaded without data type conversion.") "directly loaded without data type conversion. Default is false.")
.SetDefault(false); .SetDefault(false);
AddAttr<std::string>("file_path", AddAttr<std::string>("file_path",
"(string) " R"(Variable will be loaded from "file_path")")
"Variable will be loaded from \"file_path\".")
.AddCustomChecker( .AddCustomChecker(
[](const std::string &path) { return !path.empty(); }); [](const std::string &path) { return !path.empty(); });
AddComment(R"DOC( AddComment("Load operator will load a tensor variable from disk file.");
Load Operator.
Load operator will load a tensor variable from disk file.
)DOC");
} }
}; };
} // namespace operators } // namespace operators
......
...@@ -42,10 +42,15 @@ class MaxSeqenceLenOp : public framework::OperatorBase { ...@@ -42,10 +42,15 @@ class MaxSeqenceLenOp : public framework::OperatorBase {
class MaxSeqenceLenOpProtoMaker : public framework::OpProtoAndCheckerMaker { class MaxSeqenceLenOpProtoMaker : public framework::OpProtoAndCheckerMaker {
public: public:
void Make() override { void Make() override {
AddInput("RankTable", "The lod_rank_table."); AddInput("RankTable", "Input variable which is a LoDRankTable object");
AddOutput("Out", "The max sequence length."); AddOutput("Out", "The max sequence length");
AddComment( AddComment(R"DOC(
R"DOC(Calculate the max sequence length through lod_rank_table.)DOC"); Given a LoDRankTable object, this layer returns the max length of
a batch of sequences. In fact, a LoDRankTable object contains a list of
tuples(<sequence index, sequence length>) and the list is already sorted by
sequence length in descending order, so the operator just returns the
sequence length of the first tuple element
)DOC");
} }
}; };
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
# limitations under the License. # limitations under the License.
import contextlib import contextlib
from layer_function_generator import autodoc from layer_function_generator import autodoc, templatedoc
from tensor import assign, fill_constant from tensor import assign, fill_constant
from .. import core from .. import core
from ..framework import Program, Variable, Operator from ..framework import Program, Variable, Operator
...@@ -721,26 +721,22 @@ def lod_rank_table(x, level=0): ...@@ -721,26 +721,22 @@ def lod_rank_table(x, level=0):
return table return table
@templatedoc()
def max_sequence_len(rank_table): def max_sequence_len(rank_table):
"""Max Sequence Len Operator. Given a LoDRankTable object, this layer """
returns the max length of a batch of sequences. In fact, a LoDRankTable ${comment}
object contains a list of tuples(<sequence index, sequence length>) and
the list is already sorted by sequence length in descending order, so the >>> import paddle.fluid as fluid
operator just returns the sequence length of the first tuple element. >>> x = fluid.layers.data(name='x', shape=[10], dtype='float32',
>>> lod_level=1)
>>> rank_table = layers.lod_rank_table(x=x, level=0)
>>> max_seq_len = layers.max_sequence_len(rank_table)
Args: Args:
rank_table (Variable): Input variable which is a LoDRankTable object. rank_table(${rank_table_type}): ${rank_table_comment}.
Returns: Returns:
Variable: The max length of sequence. ${out_comment}.
Examples:
.. code-block:: python
x = fluid.layers.data(name='x', shape=[10],
dtype='float32', lod_level=1)
rank_table = layers.lod_rank_table(x=x, level=0)
max_seq_len = layers.max_sequence_len(rank_table)
""" """
helper = LayerHelper("max_seqence_len", **locals()) helper = LayerHelper("max_seqence_len", **locals())
res = helper.create_tmp_variable(dtype="int64") res = helper.create_tmp_variable(dtype="int64")
......
...@@ -19,11 +19,12 @@ from ..unique_name import generate as unique_name ...@@ -19,11 +19,12 @@ from ..unique_name import generate as unique_name
from control_flow import BlockGuard from control_flow import BlockGuard
from ..layer_helper import LayerHelper from ..layer_helper import LayerHelper
from ..executor import global_scope from ..executor import global_scope
from layer_function_generator import generate_layer_fn, templatedoc
__all__ = [ __all__ = [
'data', 'BlockGuardServ', 'ListenAndServ', 'Send', 'open_recordio_file', 'data', 'BlockGuardServ', 'ListenAndServ', 'Send', 'open_recordio_file',
'open_files', 'read_file', 'shuffle', 'batch', 'double_buffer', 'open_files', 'read_file', 'shuffle', 'batch', 'double_buffer',
'random_data_generator', 'Preprocessor' 'random_data_generator', 'Preprocessor', 'load'
] ]
...@@ -662,3 +663,29 @@ class Preprocessor(object): ...@@ -662,3 +663,29 @@ class Preprocessor(object):
"sink_var_names": self.sink_var_names "sink_var_names": self.sink_var_names
}) })
return monkey_patch_reader_methods(self.reader) return monkey_patch_reader_methods(self.reader)
@templatedoc()
def load(out, file_path, load_as_fp16=None):
"""
${comment}
>>> import paddle.fluid as fluid
>>> tmp_tensor = fluid.layers.create_tensor(dtype='float32')
>>> fluid.layers.load(tmp_tensor, "./tmp_tensor.bin")
Args:
out(${out_type}): ${out_comment}.
file_path(${file_path_type}): ${file_path_comment}.
load_as_fp16(${load_as_fp16_type}): ${load_as_fp16_comment}.
Returns:
None
"""
helper = LayerHelper("load", **locals())
attrs = {"file_path": file_path}
if load_as_fp16 is not None:
attrs['load_as_fp16'] = load_as_fp16
helper.append_op(type="load", inputs={}, output={"Out": out}, args=attrs)
...@@ -224,7 +224,10 @@ def autodoc(comment=""): ...@@ -224,7 +224,10 @@ def autodoc(comment=""):
return __impl__ return __impl__
def templatedoc(): _inline_math_single_dollar = re.compile(r"\$([^\$]+)\$")
def templatedoc(op_type=None):
""" """
Decorator of layer function. It will use the docstring from the layer Decorator of layer function. It will use the docstring from the layer
function as the template. The template arguments are: function as the template. The template arguments are:
...@@ -238,32 +241,47 @@ def templatedoc(): ...@@ -238,32 +241,47 @@ def templatedoc():
Decorated function. Decorated function.
""" """
def trim_ending_dot(msg):
return msg.rstrip('.')
def escape_inline_math(msg):
return _inline_math_single_dollar.sub(repl=r':math:`\1`', string=msg)
def __impl__(func): def __impl__(func):
op_proto = OpProtoHolder.instance().get_op_proto(func.__name__) if op_type is None:
op_type_name = func.__name__
else:
op_type_name = op_type
op_proto = OpProtoHolder.instance().get_op_proto(op_type_name)
tmpl = string.Template(func.__doc__) tmpl = string.Template(func.__doc__)
comment_lines = op_proto.comment.split("\n") comment_lines = op_proto.comment.split("\n")
comment = "" comment = ""
for line in comment_lines: for line in comment_lines:
line = line.lstrip() line = line.strip()
comment += line if len(line) != 0:
comment += "\n" comment += escape_inline_math(line)
comment += " "
args = {"comment": comment} elif len(comment) != 0:
comment += "\n \n "
args = {"comment": trim_ending_dot(comment)}
for each_input in op_proto.inputs: for each_input in op_proto.inputs:
input_name = _convert_(each_input.name) input_name = _convert_(each_input.name)
args["{0}_comment".format(input_name)] = each_input.comment args["{0}_comment".format(input_name)] = trim_ending_dot(
each_input.comment)
args["{0}_type".format(input_name)] = "Variable" args["{0}_type".format(input_name)] = "Variable"
for each_attr in op_proto.attrs: for each_attr in op_proto.attrs:
input_name = _convert_(each_attr.name) input_name = _convert_(each_attr.name)
args["{0}_comment".format(input_name)] = each_attr.comment args["{0}_comment".format(input_name)] = trim_ending_dot(
each_attr.comment)
args["{0}_type".format(input_name)] = _type_to_str_(each_attr.type) args["{0}_type".format(input_name)] = _type_to_str_(each_attr.type)
for each_opt in op_proto.outputs: for each_opt in op_proto.outputs:
output_name = _convert_(each_opt.name) output_name = _convert_(each_opt.name)
args["{0}_comment".format(output_name)] = each_opt.comment args["{0}_comment".format(output_name)] = trim_ending_dot(
each_opt.comment)
args["{0}_type".format(output_name)] = "Variable" args["{0}_type".format(output_name)] = "Variable"
func.__doc__ = tmpl.substitute(args) func.__doc__ = tmpl.substitute(args)
return func return func
......
...@@ -11,6 +11,14 @@ ...@@ -11,6 +11,14 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
"""
When training a model, it's often useful to decay the
learning rate during training process, this is called
learning_rate_decay. There are many strategies to do
this, this module will provide some classical method.
User can also implement their own learning_rate_decay
strategy according to this module.
"""
import control_flow import control_flow
import nn import nn
...@@ -22,14 +30,6 @@ __all__ = [ ...@@ -22,14 +30,6 @@ __all__ = [
'exponential_decay', 'natural_exp_decay', 'inverse_time_decay', 'exponential_decay', 'natural_exp_decay', 'inverse_time_decay',
'polynomial_decay', 'piecewise_decay', 'noam_decay' 'polynomial_decay', 'piecewise_decay', 'noam_decay'
] ]
"""
When training a model, it's often useful to decay the
learning rate during training process, this is called
learning_rate_decay. There are many strategies to do
this, this module will provide some classical method.
User can also implement their own learning_rate_decay
strategy according to this module.
"""
def _decay_step_counter(begin=0): def _decay_step_counter(begin=0):
...@@ -41,18 +41,20 @@ def _decay_step_counter(begin=0): ...@@ -41,18 +41,20 @@ def _decay_step_counter(begin=0):
def noam_decay(d_model, warmup_steps): def noam_decay(d_model, warmup_steps):
"""Apply decay to learning rate. """
```python Noam decay method. The numpy implementation of noam decay as follows.
lr_value = np.power(d_model, -0.5) * np.min([
np.power(current_steps, -0.5), >>> import numpy as np
np.power(warmup_steps, -1.5) * current_steps >>> lr_value = np.power(d_model, -0.5) * np.min([
]) >>> np.power(current_steps, -0.5),
``` >>> np.power(warmup_steps, -1.5) * current_steps])
Please reference `attention is all you need
<https://arxiv.org/pdf/1706.03762.pdf>`_.
Args: Args:
d_model(Variable): The dimensionality of input and output of model. d_model(Variable): The dimensionality of input and output of model.
Reference: attention is all you need
https://arxiv.org/pdf/1706.03762.pdf
warmup_steps(Variable): A super parameter. warmup_steps(Variable): A super parameter.
Returns: Returns:
......
...@@ -4037,18 +4037,25 @@ def image_resize(input, ...@@ -4037,18 +4037,25 @@ def image_resize(input,
return out return out
@templatedoc(op_type="bilinear_interp")
def resize_bilinear(input, out_shape=None, scale=None, name=None): def resize_bilinear(input, out_shape=None, scale=None, name=None):
""" """
This is an alias of layer 'image_resize' with bilinear interpolation. ${comment}
Args:
input(${x_type}): ${x_comment}.
out_shape(${out_size_type}): ${out_size_comment}.
The mathematical meaning of resize bilinear layer is scale(float|None): The multiplier for the input height or width. At
Bilinear interpolation. least one of out_shape or scale must be set. And out_shape has
Bilinear interpolation is an extension of linear interpolation for a higher priority than scale. Default: None.
interpolating functions of two variables (e.g. H-direction and
W-direction in this layer) on a rectilinear 2D grid. name(str|None): The output variable name.
Returns:
For details, please refer to Wikipedia: ${out_comment}.
https://en.wikipedia.org/wiki/Bilinear_interpolation
""" """
return image_resize(input, out_shape, scale, name, 'BILINEAR') return image_resize(input, out_shape, scale, name, 'BILINEAR')
......
...@@ -18,6 +18,7 @@ from ..framework import convert_np_dtype_to_dtype_ ...@@ -18,6 +18,7 @@ from ..framework import convert_np_dtype_to_dtype_
from ..framework import Variable from ..framework import Variable
from ..initializer import Constant, force_init_on_cpu from ..initializer import Constant, force_init_on_cpu
from ..core import VarDesc from ..core import VarDesc
from layer_function_generator import templatedoc
import numpy import numpy
__all__ = [ __all__ = [
...@@ -266,6 +267,7 @@ def fill_constant(shape, dtype, value, force_cpu=False, out=None): ...@@ -266,6 +267,7 @@ def fill_constant(shape, dtype, value, force_cpu=False, out=None):
return out return out
@templatedoc()
def fill_constant_batch_size_like(input, def fill_constant_batch_size_like(input,
shape, shape,
dtype, dtype,
...@@ -273,30 +275,28 @@ def fill_constant_batch_size_like(input, ...@@ -273,30 +275,28 @@ def fill_constant_batch_size_like(input,
input_dim_idx=0, input_dim_idx=0,
output_dim_idx=0): output_dim_idx=0):
""" """
**fill_constant_batch_size_like** ${comment}
This function creates a tensor of specified *shape*, *dtype* and batch size,
and initializes this with a constant supplied in *value*. The batch size is
obtained from the `input` tensor.
It also sets *stop_gradient* to True. It also sets *stop_gradient* to True.
>>> data = fluid.layers.fill_constant_batch_size_like(
>>> input=like, shape=[1], value=0, dtype='int64')
Args: Args:
input(Variable): Tensor whose dimensions will be used to get batch size input(${input_type}): ${input_comment}.
shape(tuple|list|None): Shape of output tensor
dtype(np.dtype|core.VarDesc.VarType|str): Data type of output tensor
value(float): Constant value to initialize the output tensor
input_dim_idx(int): Index of input's batch size dimension
output_dim_idx(int): Index of output's batch size dimension
Returns: shape(${shape_type}): ${shape_comment}.
Variable: The tensor variable storing the output
Examples: dtype(${dtype_type}): ${dtype_comment}.
.. code-block:: python
value(${value_type}): ${value_comment}.
data = fluid.layers.fill_constant_batch_size_like( input_dim_idx(${input_dim_idx_type}): ${input_dim_idx_comment}.
input=like, shape=[1], value=0, dtype='int64')
output_dim_idx(${output_dim_idx_type}): ${output_dim_idx_comment}.
Returns:
${out_comment}.
""" """
helper = LayerHelper("fill_constant_batch_size_like", **locals()) helper = LayerHelper("fill_constant_batch_size_like", **locals())
out = helper.create_tmp_variable(dtype=dtype) out = helper.create_tmp_variable(dtype=dtype)
...@@ -437,22 +437,6 @@ def save_combine(x, file_path, overwrite=True): ...@@ -437,22 +437,6 @@ def save_combine(x, file_path, overwrite=True):
"overwrite": overwrite}) "overwrite": overwrite})
def load(out, file_path):
"""
Loads a variable from a given file.
Args:
out(variable): The variable to be read from the disk file.
file_path(str): The path of the disk file.
"""
helper = LayerHelper("load", **locals())
helper.append_op(
type="load",
inputs={},
output={"Out": out},
args={"file_path": file_path})
def load_combine(out, file_path): def load_combine(out, file_path):
""" """
Loads a list of vairables from a single file. Loads a list of vairables from a single file.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册