Skip to content

Commit 1171c2c

Browse files
authored
Merge pull request #11457 from JiayiFeng/dev_add_doc
Add API reference
2 parents ea03a22 + f6daab4 commit 1171c2c

File tree

7 files changed

+280
-97
lines changed

7 files changed

+280
-97
lines changed

paddle/fluid/operators/activation_op.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ class SwishOpMaker : public framework::OpProtoAndCheckerMaker {
443443
AddComment(R"DOC(
444444
Swish Activation Operator.
445445
446-
$$out = \frac{x}{1 + e^{- \beta x}}$$
446+
$$out = \\frac{x}{1 + e^{- \beta x}}$$
447447
448448
)DOC");
449449
}

paddle/fluid/operators/pool_op.cc

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,6 @@ void Pool2dOpMaker::Make() {
204204
// TODO(dzhwinter): need to registered layout transform function
205205

206206
AddComment(R"DOC(
207-
Pool2d Operator.
208-
209207
The pooling2d operation calculates the output based on
210208
the input, pooling_type and ksize, strides, paddings parameters.
211209
Input(X) and output(Out) are in NCHW format, where N is batch size, C is the
@@ -215,19 +213,28 @@ These two elements represent height and width, respectively.
215213
The input(X) size and output(Out) size may be different.
216214
217215
Example:
216+
218217
Input:
218+
219219
X shape: $(N, C, H_{in}, W_{in})$
220+
220221
Output:
222+
221223
Out shape: $(N, C, H_{out}, W_{out})$
224+
222225
For ceil_mode = false:
223226
$$
224-
H_{out} = \frac{(H_{in} - ksize[0] + 2 * paddings[0])}{strides[0]} + 1 \\
225-
W_{out} = \frac{(W_{in} - ksize[1] + 2 * paddings[1])}{strides[1]} + 1
227+
H_{out} = \\frac{(H_{in} - ksize[0] + 2 * paddings[0])}{strides[0]} + 1
228+
$$
229+
$$
230+
W_{out} = \\frac{(W_{in} - ksize[1] + 2 * paddings[1])}{strides[1]} + 1
226231
$$
227232
For ceil_mode = true:
228233
$$
229-
H_{out} = \frac{(H_{in} - ksize[0] + 2 * paddings[0] + strides[0] - 1)}{strides[0]} + 1 \\
230-
W_{out} = \frac{(W_{in} - ksize[1] + 2 * paddings[1] + strides[1] - 1)}{strides[1]} + 1
234+
H_{out} = \\frac{(H_{in} - ksize[0] + 2 * paddings[0] + strides[0] - 1)}{strides[0]} + 1
235+
$$
236+
$$
237+
W_{out} = \\frac{(W_{in} - ksize[1] + 2 * paddings[1] + strides[1] - 1)}{strides[1]} + 1
231238
$$
232239
233240
)DOC");

python/paddle/fluid/layers/control_flow.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -819,17 +819,25 @@ def max_sequence_len(rank_table):
819819

820820

821821
def lod_tensor_to_array(x, table):
822-
""" Convert a LOD_TENSOR to an LOD_TENSOR_ARRAY.
822+
"""
823+
Convert a LoDTensor to a LoDTensorArray.
824+
825+
This function split a LoDTesnor to a LoDTensorArray according to its LoD
826+
information. LoDTensorArray is an alias of C++ std::vector<LoDTensor> in
827+
PaddlePaddle. The generated LoDTensorArray of this function can be further read
828+
or written by `read_from_array()` and `write_to_array()` operators. However,
829+
this function is generally an internal component of PaddlePaddle `DynamicRNN`.
830+
Users should not use it directly.
823831
824832
Args:
825-
x (Variable|list): The LOD tensor to be converted to a LOD tensor array.
833+
x (Variable|list): The LoDTensor to be converted to a LoDTensorArray.
826834
table (ParamAttr|list): The variable that stores the level of lod
827835
which is ordered by sequence length in
828-
descending order.
836+
descending order. It is generally generated
837+
by `layers.lod_rank_table()` API.
829838
830839
Returns:
831-
Variable: The variable of type array that has been converted from a
832-
tensor.
840+
Variable: The LoDTensorArray that has been converted from the input tensor.
833841
834842
Examples:
835843
.. code-block:: python
@@ -1141,6 +1149,13 @@ def array_length(array):
11411149

11421150

11431151
class ConditionalBlockGuard(BlockGuard):
1152+
"""
1153+
ConditionalBlockGuard is derived from BlockGuard. It is dedicated for
1154+
holding a ConditionalBlock, and helping users entering and exiting the
1155+
ConditionalBlock via Python's 'with' keyword. However, ConditionalBlockGuard
1156+
is generally an internal component of IfElse, users should not use it directly.
1157+
"""
1158+
11441159
def __init__(self, block):
11451160
if not isinstance(block, ConditionalBlock):
11461161
raise TypeError("block should be conditional block")
@@ -1825,26 +1840,26 @@ def reorder_lod_tensor_by_rank(x, rank_table):
18251840

18261841
def is_empty(x, cond=None, **ignored):
18271842
"""
1828-
**Is Empty**
1829-
1830-
This layer returns the truth value of whether the variable is empty.
1843+
Test whether a Variable is empty.
18311844
18321845
Args:
1833-
x(Variable): Operand of *is_empty*
1834-
cond(Variable|None): Optional output variable to store the result
1835-
of *is_empty*
1846+
x (Variable): The Variable to be tested.
1847+
cond (Variable|None): Output parameter. Returns the test result
1848+
of given 'x'. Default: None
18361849
18371850
Returns:
1838-
Variable: The tensor variable storing the output of *is_empty*.
1851+
Variable: A bool scalar. True if 'x' is an empty Variable.
18391852
18401853
Raises:
18411854
TypeError: If input cond is not a variable, or cond's dtype is
1842-
not bool
1855+
not bool.
18431856
18441857
Examples:
18451858
.. code-block:: python
18461859
1847-
less = fluid.layers.is_empty(x=input)
1860+
res = fluid.layers.is_empty(x=input)
1861+
# or:
1862+
fluid.layers.is_empty(x=input, cond=res)
18481863
"""
18491864
helper = LayerHelper("is_empty", **locals())
18501865
if cond is None:

python/paddle/fluid/layers/io.py

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,41 @@ def shuffle(reader, buffer_size):
544544

545545

546546
def batch(reader, batch_size):
547+
"""
548+
This layer is a reader decorator. It takes a reader and adds
549+
'batching' decoration on it. When reading with the result
550+
decorated reader, output data will be automatically organized
551+
to the form of batches.
552+
553+
Args:
554+
reader(Variable): The reader to be decorated with 'batching'.
555+
batch_size(int): The batch size.
556+
557+
Returns:
558+
Variable: The reader which has been decorated with 'batching'.
559+
560+
Examples:
561+
.. code-block:: python
562+
563+
raw_reader = fluid.layers.io.open_files(filenames=['./data1.recordio',
564+
'./data2.recordio'],
565+
shapes=[(3,224,224), (1)],
566+
lod_levels=[0, 0],
567+
dtypes=['float32', 'int64'],
568+
thread_num=2,
569+
buffer_size=2)
570+
batch_reader = fluid.layers.batch(reader=raw_reader, batch_size=5)
571+
572+
# If we read data with the raw_reader:
573+
# data = fluid.layers.read_file(raw_reader)
574+
# We can only get data instance by instance.
575+
#
576+
# However, if we read data with the batch_reader:
577+
# data = fluid.layers.read_file(batch_reader)
578+
# Each 5 adjacent instances will be automatically combined together
579+
# to become a batch. So what we get('data') is a batch data instead
580+
# of an instance.
581+
"""
547582
return __create_unshared_decorated_reader__(
548583
'create_batch_reader', reader, {'batch_size': int(batch_size)})
549584

@@ -589,15 +624,41 @@ def parallel(reader):
589624
{})
590625

591626

592-
def read_file(file_obj):
627+
def read_file(reader):
628+
"""
629+
Execute the given reader and get data via it.
630+
631+
A reader is also a Variable. It can be a raw reader generated by
632+
`fluid.layers.open_files()` or a decorated one generated by
633+
`fluid.layers.double_buffer()` and so on.
634+
635+
Args:
636+
637+
reader(Variable): The reader to execute.
638+
639+
Returns:
640+
Tuple[Variable]: Data read via the given reader.
641+
642+
Examples:
643+
.. code-block:: python
644+
645+
data_file = fluid.layers.open_files(
646+
filenames=['mnist.recordio'],
647+
shapes=[(-1, 748), (-1, 1)],
648+
lod_levels=[0, 0],
649+
dtypes=["float32", "int64"])
650+
data_file = fluid.layers.double_buffer(
651+
fluid.layers.batch(data_file, batch_size=64))
652+
input, label = fluid.layers.read_file(data_file)
653+
"""
593654
helper = LayerHelper('read_file')
594655
out = [
595656
helper.create_tmp_variable(
596657
stop_gradient=True, dtype='float32')
597-
for _ in range(len(file_obj.desc.shapes()))
658+
for _ in range(len(reader.desc.shapes()))
598659
]
599660
helper.append_op(
600-
type='read', inputs={'Reader': [file_obj]}, outputs={'Out': out})
661+
type='read', inputs={'Reader': [reader]}, outputs={'Out': out})
601662
if len(out) == 1:
602663
return out[0]
603664
else:

python/paddle/fluid/layers/learning_rate_scheduler.py

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,40 @@ def noam_decay(d_model, warmup_steps):
7171

7272

7373
def exponential_decay(learning_rate, decay_steps, decay_rate, staircase=False):
74-
"""Applies exponential decay to the learning rate.
74+
"""
75+
Applies exponential decay to the learning rate.
76+
77+
When training a model, it is often recommended to lower the learning rate as the
78+
training progresses. By using this function, the learning rate will be decayed by
79+
'decay_rate' every 'decay_steps' steps.
80+
81+
>>> if staircase == True:
82+
>>> decayed_learning_rate = learning_rate * decay_rate ^ floor(global_step / decay_steps)
83+
>>> else:
84+
>>> decayed_learning_rate = learning_rate * decay_rate ^ (global_step / decay_steps)
7585
76-
```python
77-
decayed_learning_rate = learning_rate *
78-
decay_rate ^ (global_step / decay_steps)
79-
```
8086
Args:
81-
learning_rate: A scalar float32 value or a Variable. This
82-
will be the initial learning rate during training
83-
decay_steps: A Python `int32` number.
84-
decay_rate: A Python `float` number.
85-
staircase: Boolean. If set true, decay the learning rate every decay_steps.
87+
learning_rate(Variable|float): The initial learning rate.
88+
decay_steps(int): See the decay computation above.
89+
decay_rate(float): The decay rate. See the decay computation above.
90+
staircase(Boolean): If True, decay the learning rate at discrete intervals.
91+
Default: False
8692
8793
Returns:
88-
The decayed learning rate
94+
Variable: The decayed learning rate
95+
96+
Examples:
97+
.. code-block:: python
98+
99+
base_lr = 0.1
100+
sgd_optimizer = fluid.optimizer.SGD(
101+
learning_rate=fluid.layers.exponential_decay(
102+
learning_rate=base_lr,
103+
decay_steps=10000,
104+
decay_rate=0.5,
105+
staircase=True))
106+
sgd_optimizer.minimize(avg_cost)
107+
89108
"""
90109
global_step = _decay_step_counter()
91110

@@ -129,22 +148,39 @@ def natural_exp_decay(learning_rate, decay_steps, decay_rate, staircase=False):
129148

130149

131150
def inverse_time_decay(learning_rate, decay_steps, decay_rate, staircase=False):
132-
"""Applies inverse time decay to the initial learning rate.
151+
"""
152+
Applies inverse time decay to the initial learning rate.
153+
154+
When training a model, it is often recommended to lower the learning rate as the
155+
training progresses. By using this function, an inverse decay function will be
156+
applied to the initial learning rate.
133157
134-
>>> if staircase:
158+
>>> if staircase == True:
135159
>>> decayed_learning_rate = learning_rate / (1 + decay_rate * floor(global_step / decay_step))
136160
>>> else:
137161
>>> decayed_learning_rate = learning_rate / (1 + decay_rate * global_step / decay_step)
138162
139163
Args:
140-
learning_rate: A scalar float32 value or a Variable. This
141-
will be the initial learning rate during training.
142-
decay_steps: A Python `int32` number.
143-
decay_rate: A Python `float` number.
144-
staircase: Boolean. If set true, decay the learning rate every decay_steps.
164+
learning_rate(Variable|float): The initial learning rate.
165+
decay_steps(int): See the decay computation above.
166+
decay_rate(float): The decay rate. See the decay computation above.
167+
staircase(Boolean): If True, decay the learning rate at discrete intervals.
168+
Default: False
145169
146170
Returns:
147-
The decayed learning rate
171+
Variable: The decayed learning rate
172+
173+
Examples:
174+
.. code-block:: python
175+
176+
base_lr = 0.1
177+
sgd_optimizer = fluid.optimizer.SGD(
178+
learning_rate=fluid.layers.inverse_time_decay(
179+
learning_rate=base_lr,
180+
decay_steps=10000,
181+
decay_rate=0.5,
182+
staircase=True))
183+
sgd_optimizer.minimize(avg_cost)
148184
"""
149185
global_step = _decay_step_counter()
150186

0 commit comments

Comments
 (0)