CTCLoss¶
-
class
CTCLoss(layout='NTC', label_layout='NT', weight=None, **kwargs)¶ Connectionist Temporal Classification Loss.
Parameters: - layout : str, default ‘NTC’
Layout of prediction tensor. ‘N’, ‘T’, ‘C’ stands for batch size, sequence length, and alphabet_size respectively.
- label_layout : str, default ‘NT’
Layout of the labels. ‘N’, ‘T’ stands for batch size, and sequence length respectively.
- weight : float or None
Global scalar weight for loss.
- Inputs:
- pred: unnormalized prediction tensor (before softmax). Its shape depends on layout. If layout is ‘TNC’, pred should have shape (sequence_length, batch_size, alphabet_size). Note that in the last dimension, index alphabet_size-1 is reserved for internal use as blank label. So alphabet_size is one plus the actual alphabet size.
- label: zero-based label tensor. Its shape depends on label_layout. If label_layout is ‘TN’, label should have shape (label_sequence_length, batch_size).
- pred_lengths: optional (default None), used for specifying the length of each entry when different pred entries in the same batch have different lengths. pred_lengths should have shape (batch_size,).
- label_lengths: optional (default None), used for specifying the length of each entry when different label entries in the same batch have different lengths. label_lengths should have shape (batch_size,).
- Outputs:
- loss: output loss has shape (batch_size,).
- **Example: suppose the vocabulary is [a, b, c], and in one batch we**
- have three sequences ‘ba’, ‘cbb’, and ‘abac’. We can index the labels as
- `{‘a’: 0, ‘b’: 1, ‘c’: 2, blank: 3}`. Then `alphabet_size` should be 4,
- where label 3 is reserved for internal use by `CTCLoss`. We then need to
- pad each sequence with `-1` to make a rectangular `label` tensor::
- [[1, 0, -1, -1],
[2, 1, 1, -1], [0, 1, 0, 2]]