25
25
import random
26
26
27
27
__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' ,
43
96
]
44
97
45
98
@@ -3257,8 +3310,7 @@ def topk(input, k, name=None):
3257
3310
return values , indices
3258
3311
3259
3312
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 ):
3262
3314
"""
3263
3315
EditDistance operator computes the edit distances between a batch of
3264
3316
hypothesis strings and their references. Edit distance, also called
@@ -3272,21 +3324,21 @@ def edit_distance(input, label, normalized=True, ignored_tokens=None,
3272
3324
3273
3325
"kitten" -> "sitten" -> "sittin" -> "sitting"
3274
3326
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
3276
3328
the total number denoted by `batch_size`, and the separation is specified
3277
3329
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 .
3279
3331
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
3281
3333
distance for a pair of strings respectively. If Attr(normalized) is true,
3282
3334
the edit distance will be divided by the length of reference string.
3283
3335
3284
3336
Args:
3285
3337
input(Variable): The indices for hypothesis strings.
3286
3338
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
3288
3340
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
3290
3342
calculating edit distance.
3291
3343
name (str): The name of this layer. It is optional.
3292
3344
@@ -3298,7 +3350,6 @@ def edit_distance(input, label, normalized=True, ignored_tokens=None,
3298
3350
3299
3351
x = fluid.layers.data(name='x', shape=[8], dtype='float32')
3300
3352
y = fluid.layers.data(name='y', shape=[7], dtype='float32')
3301
-
3302
3353
cost = fluid.layers.edit_distance(input=x,label=y)
3303
3354
"""
3304
3355
helper = LayerHelper ("edit_distance" , ** locals ())
@@ -3418,35 +3469,33 @@ def warpctc(input, label, blank=0, norm_by_times=False):
3418
3469
input tensor.
3419
3470
3420
3471
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.
3438
3487
3439
3488
Returns:
3440
3489
Variable: The Connectionist Temporal Classification (CTC) loss,
3441
3490
which is a 2-D Tensor of the shape [batch_size, 1].
3442
3491
3443
3492
Examples:
3493
+
3444
3494
.. 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)
3450
3499
3451
3500
"""
3452
3501
helper = LayerHelper ('warpctc' , ** locals ())
@@ -3475,37 +3524,41 @@ def sequence_reshape(input, new_dim):
3475
3524
.. code-block:: text
3476
3525
3477
3526
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]]
3481
3531
x.dims = [6, 2]
3482
3532
3483
3533
set new_dim = 4
3484
3534
3485
3535
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]]
3489
3542
out.dims = [3, 4]
3490
3543
3491
3544
Currently, only 1-level LoDTensor is supported and please make sure
3492
3545
(original length * original dimension) can be divided by new dimension with
3493
3546
no remainder for each sequence.
3494
3547
3495
3548
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.
3499
3552
3500
3553
Returns:
3554
+
3501
3555
Variable: Reshaped LoDTensor according to new dimension.
3502
3556
3503
3557
Examples:
3504
3558
.. code-block:: python
3505
3559
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)
3509
3562
"""
3510
3563
helper = LayerHelper ('sequence_reshape' , ** locals ())
3511
3564
out = helper .create_tmp_variable (helper .input_dtype ())
0 commit comments