Skip to content

Commit

Permalink
Correct documentation for CTCLoss after #3 review
Browse files Browse the repository at this point in the history
  • Loading branch information
rkazants committed Jul 14, 2020
1 parent 946c6fd commit 5693dac
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions docs/ops/sequence/CTCLoss_4.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,36 @@

*CTCLoss* operation is presented in [Connectionist Temporal Classification - Labeling Unsegmented Sequence Data with Recurrent Neural Networks: Graves et al., 2016](http://www.cs.toronto.edu/~graves/icml_2006.pdf)

*CTCLoss* estimates a chance that a target can occur (or is real) for given input sequence of logits.
Briefly, *CTCLoss* operation finds all sequences aligned with a target sequence `labels[i,:]`, computes log-probabilities of these aligned sequences using `inputs[:,i,:]` of logits
*CTCLoss* estimates likelyhood that a target `labels[i,:]` can occur (or is real) for given input sequence of logits `logits[:,i,:]`.
Briefly, *CTCLoss* operation finds all sequences aligned with a target `labels[i,:]`, computes log-probabilities of the aligned sequences using `logits[:,i,:]`
and computes a negative sum of these log-probabilies.

Input sequences of logits from `inputs` can have different length. The length of each sequence `inputs[:,i,:]` equals `input_length[i]`.
A length of target sequence from `labels[i,:]` is determined by a pad `-1`. The length must not be greater than a lenght of corresponding input sequence `inputs[:,i,:]`.
Input sequences of logits `logits` can have different length. The length of each sequence `logits[:,i,:]` equals `logit_length[i]`.
A length of target sequence `labels[i,:]` equals `label_length[i]`. The length of the target sequence must not be greater than the length of corresponding input sequence `logits[:,i,:]`.
Otherwise, the operation behaviour is undefined.

*CTCLoss* calculation scheme:

1. Compute probability of `j`-th character at time step `t` for `i`-th input sequence from `inputs` using softmax formula:
1. Compute probability of `j`-th character at time step `t` for `i`-th input sequence from `logits` using softmax formula:
\f[
p_{t,i,j} = \frac{\exp(inputs[t,i,j])}{\sum^{K}_{k=0}{\exp(inputs[t,i,k])}}
p_{t,i,j} = \frac{\exp(logits[t,i,j])}{\sum^{K}_{k=0}{\exp(logits[t,i,k])}}
\f]

2. For a given `i`-th target from `labels[i,:]` find all aligned paths.
A path `S = (c1,c2,...,cT)` is aligned with a target `G=(g1,g2,...,gT)` if both chains are equal after decoding.
The decoding removes pads `-1` from a target `G` and merges repeated characters in `G` in case *preprocess_collapse_repeated* equal to True.
The decoding extracts substring of length `label_length[i]` from a target `G`, merges repeated characters in `G` in case *preprocess_collapse_repeated* equal to True and
finds unique elements in the order of character occurence in case *unique* equal to True.
The decoding merges repeated characters in `S` in case *ctc_merge_repeated* equal to True and removes blank characters represented by `blank_index`.
By default, `blank_index` is equal to `C-1`, where `C` is a number of classes including the blank.
For example, in case default *ctc_merge_repeated*, *preprocess_collapse_repeated*, *unique*, and `blank_index` a target sequence `(0,3,2,2,-1,-1,-1,-1,-1)` is processed to `(0,3,2,2)` and
a path `(0,0,4,3,2,2,4,2,4)` is also processed to `(0,3,2,2)`, where `C=5`. There exist other paths that are also aligned, for instance, `0,4,3,3,2,4,2,2,2`.
Paths checked for alignment with a target `label[:,i]` must be of length `input_length[i] = L_i`.
For example, in case default *ctc_merge_repeated*, *preprocess_collapse_repeated*, *unique*, and `blank_index` a target sequence `G=(0,3,2,2,2,2,2,4,3)` of a length `label_length[i]=4` is processed
to `(0,3,2,2)` and a path `S=(0,0,4,3,2,2,4,2,4)` of a length `logit_length[i]=9` is also processed to `(0,3,2,2)`, where `C=5`.
There exist other paths that are also aligned with `G`, for instance, `0,4,3,3,2,4,2,2,2`. Paths checked for alignment with a target `label[:,i]` must be of length `logit_length[i] = L_i`.
Compute probabilities of these aligned paths (alignments) as follows:
\f[
p(S) = \prod_{t=1}^{L_i} p_{t,i,ct}
\f]

3. Finally, compute negative sum of log-probabilities of all alignments:
3. Finally, compute negative sum of log-probabilities of all found alignments:
\f[
CTCLoss = \minus \sum_{S} \ln p(S)
\f]
Expand Down Expand Up @@ -72,13 +73,15 @@ CTCLoss = \minus \sum_{S} \ln p(S)

**Inputs**

* **1**: `inputs` - Input tensor with a batch of sequences. Type of elements is *T_F*. Shape of the tensor is `[T, N, C]`, where `T` is the maximum sequence length, `N` is the batch size and `C` is the number of classes including the blank. Required.
* **1**: `logits` - Input tensor with a batch of sequences of logits. Type of elements is *T_F*. Shape of the tensor is `[T, N, C]`, where `T` is the maximum sequence length, `N` is the batch size and `C` is the number of classes including the blank. Required.

* **2**: `input_length` - 1D input tensor of type *T_IND* and of a shape `[N]`. The tensor must consist of values not greater than `T`. Lengths of input sequences `inputs[:,i,:]`. Required.
* **2**: `logit_length` - 1D input tensor of type *T_IND* and of a shape `[N]`. The tensor must consist of non-negative values not greater than `T`. Lengths of input sequences of logits `logits[:,i,:]`. Required.

* **3**: `labels` - 2D tensor with shape `[N, T]` of type *T_IND*. A sequence can be shorter than the size `T` of the tensor, all elements that do not code sequence classes are filled with -1. Required.
* **3**: `labels` - 2D tensor with shape `[N, T]` of type *T_IND*. A length of a target sequence `labels[i,:]` is equal to `label_length[i]` and must contain of integers from a range `[0; C-1]` except `blank_index`. Required.

* **4**: `blank_index` - Scalar of type *T_IND*. Set the class index to use for the blank label. Default value is `C-1`. Optional.
* **4**: `label_length` - 1D tensor of type *T_IND* and of a shape `[N]`. The tensor must consist of non-negative values not greater than `T` and `label_length[i] <= logit_length[i]` for all possible `i`. Required.

* **5**: `blank_index` - Scalar of type *T_IND*. Set the class index to use for the blank label. Default value is `C-1`. Optional.

**Output**

Expand All @@ -101,14 +104,16 @@ CTCLoss = \minus \sum_{S} \ln p(S)
<dim>128</dim>
</port>
<port id="1">
<dim>20</dim>
<dim>8</dim>
</port>
<port id="2">
<dim>8</dim>
<dim>20</dim>
</port>
<port id="3">
<dim>8</dim>
</port>
<port id="4"> <!-- blan_index value is: 120 -->
</input>
<output>
<port id="0">
Expand Down

0 comments on commit 5693dac

Please sign in to comment.