warpctc

  • paddle.fluid.layers.warpctc(input, label, blank=0, norm_by_times=False, input_length=None, label_length=None)[源代码]

该OP用于计算 CTC loss 。该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。
    • label (Variable) - 可以是3-D Tensor或2-D LoDTensor,需要跟 input 保持一致。当输入类型为3-D Tensor时,表示输入是经过 padding 的定长序列,其 shape 为 [batch_size, label_length] ,其中, label_length 是最长的 label 序列的长度。当输入类型是2-D LoDTensor时,则表示输入为变长序列,其shape必须为 [Lp, 1] , 其中 Lp 是所有 label 序列的长度和。 label 中的数值为字符ID。数据类型支持int32。
    • 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。
    • label_length (Variable) - 必须是1-D Tensor。仅在label为定长序列时使用,表示 label 中每个序列的长度,shape为 [batch_size] 。数据类型支持int64。默认为None。

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

返回类型:Variable

代码示例

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