warpctc

    该OP用于计算 。该OP的底层调用了第三方 baidu-research::warp-ctc 的实现。

    参数:

    • input (Variable) - 可以是3-D Tensor或2-D LoDTensor。当输入类型是3-D Tensor时,则表示输入是经过padding的定长序列,其 shape 必须是 [seq_length, batch_size, num_classes + 1] 。当输入类型是2-D LoDTensor时,则表示输入为变长序列,其shape必须为 [Lp,num_classes+1]Lp 是所有输入序列长度之和。以上 shape 中的 num_classes 是实际类别数,不包括空白标签。该输入不需要经过 softmax 操作,因为该OP的内部对 input 做了 softmax 操作。数据类型仅支持float32。

    • blank (int,可选) - 空格标记的ID,其取值范围为 [0,num_classes+1) 。数据类型支持int32。缺省值为0。

    • norm_by_times (bool,可选) - 是否根据序列长度对梯度进行正则化。数据类型支持 bool 。缺省值为False。

    • input_length (Variable) - 必须是1-D Tensor。仅在输入为定长序列时使用,表示输入数据中每个序列的长度,shape为 [batch_size] 。数据类型支持int64。默认为None。

    返回:Shape为[batch_size,1]的2-D Tensor,表示每一个序列的CTC loss。数据类型与 input 一致。

    返回类型:Variable

    代码示例

    1. # using Tensor
    2. import numpy as np
    3. # length of the longest logit sequence
    4. max_seq_length = 5
    5. # length of the longest label sequence
    6. # number of logit sequences
    7. batch_size = 16
    8. # class num
    9. class_num = 5
    10. logits = fluid.data(name='logits',
    11. shape=[max_seq_length, batch_size, class_num+1],
    12. dtype='float32')
    13. logits_length = fluid.data(name='logits_length', shape=[None],
    14. dtype='int64')
    15. label = fluid.data(name='label', shape=[batch_size, max_label_length],
    16. label_length = fluid.data(name='labels_length', shape=[None],
    17. dtype='int64')
    18. input_length=logits_length,
    19. label_length=label_length)
    20. place = fluid.CPUPlace()
    21. x = np.random.rand(max_seq_length, batch_size, class_num+1).astype("float32")
    22. y = np.random.randint(0, class_num, [batch_size, max_label_length]).astype("int32")
    23. exe = fluid.Executor(place)
    24. output= exe.run(fluid.default_main_program(),
    25. feed={"logits": x,
    26. "label": y,
    27. "logits_length": np.array([max_seq_length]*batch_size).astype("int64"),
    28. "labels_length": np.array([max_label_length]*batch_size).astype("int64")},
    29. fetch_list=[cost.name])
    30. print(output)