CTCLoss<T>
Models & Types · AiDotNet.LossFunctions
Implements the Connectionist Temporal Classification (CTC) loss function for sequence-to-sequence learning.
Inheritance: LossFunctionBase<T> → CTCLoss<T>
Implements: ISequenceLossFunction<T>
For Beginners
Connectionist Temporal Classification (CTC) is a loss function designed for sequence-to-sequence learning problems where the alignment between input and output sequences is unknown.
For example, in speech recognition, we have:
- Input: An audio waveform (long sequence of sound samples)
- Output: Text transcript (shorter sequence of characters)
The key challenge is that we don’t know exactly which parts of the audio correspond to each character. CTC solves this by considering all possible alignments between the input and output sequences.
CTC introduces a special “blank” token to handle:
- Repetitions of characters (e.g., “hello” vs “hheellloo”)
- Silence or transitions between sounds
This loss function is commonly used in:
- Speech recognition
- Handwriting recognition
- Any task where input and output sequences have different lengths and unknown alignment
Example
using AiDotNet.LossFunctions;
using AiDotNet.Tensors.LinearAlgebra;
var loss = new CTCLoss<float>();
var predicted = new Vector<float>(new[] { 0.9f, 0.2f, 0.7f });
var actual = new Vector<float>(new[] { 1.0f, 0.0f, 1.0f });
float value = loss.CalculateLoss(predicted, actual);
Console.WriteLine($"CTCLoss = {value:F4}");
Constructors
| Constructor | Summary |
|---|---|
CTCLoss(int numClasses, int blankIndex, bool inputsAreLogProbs) |
Initializes a new instance of the CTCLoss class. |
Properties
| Property | Summary |
|---|---|
bool InputsAreLogProbabilities { get; } |
Gets whether predictions supplied to this loss must already be log probabilities. |
int BlankIndex { get; } |
Gets the blank symbol index used by this CTC loss. |
int NumClasses { get; } |
Gets the vocabulary cardinality expected on the final prediction axis. |
Methods
| Method | Summary |
|---|---|
T CalculateLoss(Tensor<T> logProbs, int[][] targets, int[] inputLengths, int[] targetLengths) |
Calculates the CTC loss for a batch of sequences. |
T CalculateLoss(Vector<T> predicted, Vector<T> actual) |
Calculates CTC loss from flattened predicted log-probs and encoded target labels. |
Tensor<T> CalculateGradient(Tensor<T> logProbs, int[][] targets, int[] inputLengths, int[] targetLengths) |
Calculates the gradient of the CTC loss with respect to the inputs. |
Tensor<T> ComputeTapeLoss(Tensor<T> predicted, Tensor<T> target) |