megengine.functional.nn.ctc_loss

ctc_loss(pred, pred_lengths, label, label_lengths, blank=0, reduction='mean')[source]

The Connectionist Temporal Classification loss.

Parameters
  • pred (Tensor) – The probabilities of the output, shape is (T, N, C) , where T=input length, N=batch size, and C=number of classes (including blank).

  • pred_lengths (Tensor) – number of time steps for each sequence in pred, shape is (N, )

  • label (Tensor) – groundtruth labels, containing the indices of groundtruth symbols for each sequence at each output time step, and the blank symbol should not be included. shape is (N, S) or (sum(label_lengths)).

  • label_lengths (Tensor) – number of time steps for each sequence in the groundtruth, shape is (N, )

  • blank (int) – the blank symbol number, default 0

  • reduction (str) – the reduction to apply to the output: ‘none’ | ‘mean’ | ‘sum’. Default: ‘mean’

Return type

Tensor

Returns

loss value.

Examples

>>> pred = Tensor([[[0.0614, 0.9386],[0.8812, 0.1188]],[[0.699, 0.301 ],[0.2572, 0.7428]]])
>>> pred_lengths = Tensor([2, 2])
>>> label = Tensor([1, 1])
>>> label_lengths = Tensor([1, 1])
>>> F.nn.ctc_loss(pred, pred_lengths, label, label_lengths)
Tensor(0.1504417, device=xpux:0)