Skip to content

Commit e6654c1

Browse files
authored
Merge pull request #11489 from wanghaoshuang/whs_doc1
Fix doc of warpctc, array_read, edit_distance and sequence_reshape.
2 parents c2e8f40 + 24766a1 commit e6654c1

File tree

2 files changed

+137
-65
lines changed

2 files changed

+137
-65
lines changed

python/paddle/fluid/layers/control_flow.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,19 +1055,38 @@ def equal(x, y, cond=None, **ignored):
10551055

10561056

10571057
def array_read(array, i):
1058-
"""This function performs the operation to read the data in as an
1058+
"""
1059+
This function performs the operation to read the data in as an
10591060
LOD_TENSOR_ARRAY.
1061+
1062+
.. code-block:: text
1063+
1064+
Given:
1065+
1066+
array = [0.6, 0.1, 0.3, 0.1]
1067+
1068+
And:
1069+
1070+
i = 2
1071+
1072+
Then:
1073+
1074+
output = 0.3
1075+
10601076
Args:
1061-
array (Variable|list): The input tensor that will be written to an array.
1062-
i (Variable|list): The subscript index in tensor array, that points the
1063-
place where data will be written to.
1077+
array (Variable|list): The input tensor that store data to be read.
1078+
i (Variable|list): The index of the data to be read from input array.
1079+
10641080
Returns:
10651081
Variable: The tensor type variable that has the data written to it.
1082+
10661083
Examples:
1067-
.. code-block::python
1068-
tmp = fluid.layers.zeros(shape=[10], dtype='int32')
1069-
i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=10)
1070-
arr = layers.array_read(tmp, i=i)
1084+
.. code-block:: python
1085+
1086+
tmp = fluid.layers.zeros(shape=[10], dtype='int32')
1087+
i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=10)
1088+
arr = fluid.layers.array_read(tmp, i=i)
1089+
10711090
"""
10721091
helper = LayerHelper('array_read', **locals())
10731092
if not isinstance(

python/paddle/fluid/layers/nn.py

Lines changed: 110 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,74 @@
2525
import random
2626

2727
__all__ = [
28-
'fc', 'embedding', 'dynamic_lstm', 'dynamic_lstmp', 'dynamic_gru',
29-
'gru_unit', 'linear_chain_crf', 'crf_decoding', 'cos_sim', 'cross_entropy',
30-
'square_error_cost', 'chunk_eval', 'sequence_conv', 'conv2d', 'conv3d',
31-
'sequence_pool', 'sequence_softmax', 'softmax', 'pool2d', 'pool3d',
32-
'batch_norm', 'beam_search_decode', 'conv2d_transpose', 'conv3d_transpose',
33-
'sequence_expand', 'lstm_unit', 'reduce_sum', 'reduce_mean', 'reduce_max',
34-
'reduce_min', 'reduce_prod', 'sequence_first_step', 'sequence_last_step',
35-
'dropout', 'split', 'ctc_greedy_decoder', 'edit_distance', 'l2_normalize',
36-
'matmul', 'topk', 'warpctc', 'sequence_reshape', 'transpose', 'im2sequence',
37-
'nce', 'beam_search', 'row_conv', 'multiplex', 'layer_norm',
38-
'softmax_with_cross_entropy', 'smooth_l1', 'one_hot',
39-
'autoincreased_step_counter', 'reshape', 'lod_reset', 'lrn', 'pad',
40-
'label_smooth', 'roi_pool', 'dice_loss', 'image_resize',
41-
'image_resize_short', 'resize_bilinear', 'gather', 'random_crop',
42-
'mean_iou', 'relu', 'log'
28+
'fc',
29+
'embedding',
30+
'dynamic_lstm',
31+
'dynamic_lstmp',
32+
'dynamic_gru',
33+
'gru_unit',
34+
'linear_chain_crf',
35+
'crf_decoding',
36+
'cos_sim',
37+
'cross_entropy',
38+
'square_error_cost',
39+
'chunk_eval',
40+
'sequence_conv',
41+
'conv2d',
42+
'conv3d',
43+
'sequence_pool',
44+
'sequence_softmax',
45+
'softmax',
46+
'pool2d',
47+
'pool3d',
48+
'batch_norm',
49+
'beam_search_decode',
50+
'conv2d_transpose',
51+
'conv3d_transpose',
52+
'sequence_expand',
53+
'lstm_unit',
54+
'reduce_sum',
55+
'reduce_mean',
56+
'reduce_max',
57+
'reduce_min',
58+
'reduce_prod',
59+
'sequence_first_step',
60+
'sequence_last_step',
61+
'dropout',
62+
'split',
63+
'ctc_greedy_decoder',
64+
'edit_distance',
65+
'l2_normalize',
66+
'matmul',
67+
'topk',
68+
'warpctc',
69+
'sequence_reshape',
70+
'transpose',
71+
'im2sequence',
72+
'nce',
73+
'beam_search',
74+
'row_conv',
75+
'multiplex',
76+
'layer_norm',
77+
'softmax_with_cross_entropy',
78+
'smooth_l1',
79+
'one_hot',
80+
'autoincreased_step_counter',
81+
'reshape',
82+
'lod_reset',
83+
'lrn',
84+
'pad',
85+
'label_smooth',
86+
'roi_pool',
87+
'dice_loss',
88+
'image_resize',
89+
'image_resize_short',
90+
'resize_bilinear',
91+
'gather',
92+
'random_crop',
93+
'mean_iou',
94+
'relu',
95+
'log',
4396
]
4497

4598

@@ -3257,8 +3310,7 @@ def topk(input, k, name=None):
32573310
return values, indices
32583311

32593312

3260-
def edit_distance(input, label, normalized=True, ignored_tokens=None,
3261-
name=None):
3313+
def edit_distance(input, label, normalized=True, ignored_tokens=None):
32623314
"""
32633315
EditDistance operator computes the edit distances between a batch of
32643316
hypothesis strings and their references. Edit distance, also called
@@ -3272,21 +3324,21 @@ def edit_distance(input, label, normalized=True, ignored_tokens=None,
32723324
32733325
"kitten" -> "sitten" -> "sittin" -> "sitting"
32743326
3275-
Input(Hyps) is a LoDTensor consisting of all the hypothesis strings with
3327+
The input is a LoDTensor consisting of all the hypothesis strings with
32763328
the total number denoted by `batch_size`, and the separation is specified
32773329
by the LoD information. And the `batch_size` reference strings are arranged
3278-
in order in the same way in the LoDTensor Input(Refs).
3330+
in order in the same way in the input LoDTensor.
32793331
3280-
Output(Out) contains the `batch_size` results and each stands for the edit
3332+
The output contains the `batch_size` results and each stands for the edit
32813333
distance for a pair of strings respectively. If Attr(normalized) is true,
32823334
the edit distance will be divided by the length of reference string.
32833335
32843336
Args:
32853337
input(Variable): The indices for hypothesis strings.
32863338
label(Variable): The indices for reference strings.
3287-
normalized(bool): Indicated whether to normalize the edit distance by
3339+
normalized(bool, default True): Indicated whether to normalize the edit distance by
32883340
the length of reference string.
3289-
ignored_tokens(list of int): Tokens that should be removed before
3341+
ignored_tokens(list<int>, default None): Tokens that should be removed before
32903342
calculating edit distance.
32913343
name (str): The name of this layer. It is optional.
32923344
@@ -3298,7 +3350,6 @@ def edit_distance(input, label, normalized=True, ignored_tokens=None,
32983350
32993351
x = fluid.layers.data(name='x', shape=[8], dtype='float32')
33003352
y = fluid.layers.data(name='y', shape=[7], dtype='float32')
3301-
33023353
cost = fluid.layers.edit_distance(input=x,label=y)
33033354
"""
33043355
helper = LayerHelper("edit_distance", **locals())
@@ -3418,35 +3469,33 @@ def warpctc(input, label, blank=0, norm_by_times=False):
34183469
input tensor.
34193470
34203471
Args:
3421-
input(Variable): (LodTensor, default: LoDTensor<float>),
3422-
the unscaled probabilities of variable-length sequences,
3423-
which is a 2-D Tensor with LoD information.
3424-
It's shape is [Lp, num_classes + 1], where Lp is the sum of all input
3425-
sequences' length and num_classes is the true number of classes.
3426-
(not including the blank label).
3427-
label(Variable): (LodTensor, default: LoDTensor<int>), the ground truth
3428-
of variable-length sequence, which is a 2-D Tensor with LoD
3429-
information. It is of the shape [Lg, 1], where Lg is th sum of
3430-
all labels' length.
3431-
blank (int): default 0, the blank label index of Connectionist
3432-
Temporal Classification (CTC) loss, which is in the
3433-
half-opened interval [0, num_classes + 1).
3434-
norm_by_times (bool): default false, whether to normalize
3435-
the gradients by the number of time-step, which is also the
3436-
sequence's length. There is no need to normalize the gradients
3437-
if warpctc layer was follewed by a mean_op.
3472+
input (Variable): The unscaled probabilities of variable-length sequences,
3473+
which is a 2-D Tensor with LoD information.
3474+
It's shape is [Lp, num_classes + 1], where Lp is the sum of all input
3475+
sequences' length and num_classes is the true number of classes.
3476+
(not including the blank label).
3477+
label (Variable): The ground truth of variable-length sequence,
3478+
which is a 2-D Tensor with LoD information. It is of the shape [Lg, 1],
3479+
where Lg is th sum of all labels' length.
3480+
blank (int, default 0): The blank label index of Connectionist
3481+
Temporal Classification (CTC) loss, which is in the
3482+
half-opened interval [0, num_classes + 1).
3483+
norm_by_times(bool, default false): Whether to normalize the gradients
3484+
by the number of time-step, which is also the sequence's length.
3485+
There is no need to normalize the gradients if warpctc layer was
3486+
follewed by a mean_op.
34383487
34393488
Returns:
34403489
Variable: The Connectionist Temporal Classification (CTC) loss,
34413490
which is a 2-D Tensor of the shape [batch_size, 1].
34423491
34433492
Examples:
3493+
34443494
.. code-block:: python
3445-
y = layers.data(
3446-
name='y', shape=[11, 8], dtype='float32', lod_level=1)
3447-
y_predict = layers.data(
3448-
name='y_predict', shape=[11, 1], dtype='float32')
3449-
cost = layers.warpctc(input=y_predict, label=y)
3495+
3496+
label = fluid.layers.data(shape=[11, 8], dtype='float32', lod_level=1)
3497+
predict = fluid.layers.data(shape=[11, 1], dtype='float32')
3498+
cost = fluid.layers.warpctc(input=predict, label=label)
34503499
34513500
"""
34523501
helper = LayerHelper('warpctc', **locals())
@@ -3475,37 +3524,41 @@ def sequence_reshape(input, new_dim):
34753524
.. code-block:: text
34763525
34773526
x is a LoDTensor:
3478-
x.lod = [[2, 4]]
3479-
x.data = [[1, 2], [3, 4],
3480-
[5, 6], [7, 8], [9, 10], [11, 12]]
3527+
x.lod = [[0, 2, 6]]
3528+
x.data = [[1, 2], [3, 4],
3529+
[5, 6], [7, 8],
3530+
[9, 10], [11, 12]]
34813531
x.dims = [6, 2]
34823532
34833533
set new_dim = 4
34843534
34853535
then out is a LoDTensor:
3486-
out.lod = [[1, 2]]
3487-
out.data = [[1, 2, 3, 4],
3488-
[5, 6, 7, 8], [9, 10, 11, 12]]
3536+
3537+
out.lod = [[0, 1, 3]]
3538+
3539+
out.data = [[1, 2, 3, 4],
3540+
[5, 6, 7, 8],
3541+
[9, 10, 11, 12]]
34893542
out.dims = [3, 4]
34903543
34913544
Currently, only 1-level LoDTensor is supported and please make sure
34923545
(original length * original dimension) can be divided by new dimension with
34933546
no remainder for each sequence.
34943547
34953548
Args:
3496-
input (Variable): (LodTensor, default: LoDTensor<float>), a 2-D LoDTensor
3497-
with shape being [N, M] where M for dimension.
3498-
new_dim (int): New dimension which the input LoDTensor is reshaped to.
3549+
3550+
input (Variable): A 2-D LoDTensor with shape being [N, M] where M for dimension.
3551+
new_dim (int): New dimension that the input LoDTensor is reshaped to.
34993552
35003553
Returns:
3554+
35013555
Variable: Reshaped LoDTensor according to new dimension.
35023556
35033557
Examples:
35043558
.. code-block:: python
35053559
3506-
x = fluid.layers.data(name='x', shape=[5, 20],
3507-
dtype='float32', lod_level=1)
3508-
x_reshaped = layers.sequence_reshape(input=x, new_dim=10)
3560+
x = fluid.layers.data(shape=[5, 20], dtype='float32', lod_level=1)
3561+
x_reshaped = fluid.layers.sequence_reshape(input=x, new_dim=10)
35093562
"""
35103563
helper = LayerHelper('sequence_reshape', **locals())
35113564
out = helper.create_tmp_variable(helper.input_dtype())

0 commit comments

Comments
 (0)