Skip to content

create_dl_layer_loss_ctcCreateDlLayerLossCtcCreateDlLayerLossCtccreate_dl_layer_loss_ctcT_create_dl_layer_loss_ctcπŸ”—

Short descriptionπŸ”—

create_dl_layer_loss_ctcCreateDlLayerLossCtcCreateDlLayerLossCtccreate_dl_layer_loss_ctcT_create_dl_layer_loss_ctc β€” Create a CTC loss layer.

SignatureπŸ”—

create_dl_layer_loss_ctc( dl_layer DLLayerInput, dl_layer DLLayerInputLengths, dl_layer DLLayerTarget, dl_layer DLLayerTargetLengths, string LayerName, attribute.name GenParamName, attribute.value GenParamValue, out dl_layer DLLayerLossCTC )void CreateDlLayerLossCtc( const HTuple& DLLayerInput, const HTuple& DLLayerInputLengths, const HTuple& DLLayerTarget, const HTuple& DLLayerTargetLengths, const HTuple& LayerName, const HTuple& GenParamName, const HTuple& GenParamValue, HTuple* DLLayerLossCTC )static void HOperatorSet.CreateDlLayerLossCtc( HTuple DLLayerInput, HTuple DLLayerInputLengths, HTuple DLLayerTarget, HTuple DLLayerTargetLengths, HTuple layerName, HTuple genParamName, HTuple genParamValue, out HTuple DLLayerLossCTC )def create_dl_layer_loss_ctc( dllayer_input: HHandle, dllayer_input_lengths: HHandle, dllayer_target: HHandle, dllayer_target_lengths: HHandle, layer_name: str, gen_param_name: MaybeSequence[str], gen_param_value: MaybeSequence[Union[int, float, str]] ) -> HHandle

Herror T_create_dl_layer_loss_ctc( const Htuple DLLayerInput, const Htuple DLLayerInputLengths, const Htuple DLLayerTarget, const Htuple DLLayerTargetLengths, const Htuple LayerName, const Htuple GenParamName, const Htuple GenParamValue, Htuple* DLLayerLossCTC )

HDlLayer HDlLayer::CreateDlLayerLossCtc( const HDlLayer& DLLayerInputLengths, const HDlLayer& DLLayerTarget, const HDlLayer& DLLayerTargetLengths, const HString& LayerName, const HTuple& GenParamName, const HTuple& GenParamValue ) const

HDlLayer HDlLayer::CreateDlLayerLossCtc( const HDlLayer& DLLayerInputLengths, const HDlLayer& DLLayerTarget, const HDlLayer& DLLayerTargetLengths, const HString& LayerName, const HString& GenParamName, const HString& GenParamValue ) const

HDlLayer HDlLayer::CreateDlLayerLossCtc( const HDlLayer& DLLayerInputLengths, const HDlLayer& DLLayerTarget, const HDlLayer& DLLayerTargetLengths, const char* LayerName, const char* GenParamName, const char* GenParamValue ) const

HDlLayer HDlLayer::CreateDlLayerLossCtc( const HDlLayer& DLLayerInputLengths, const HDlLayer& DLLayerTarget, const HDlLayer& DLLayerTargetLengths, const wchar_t* LayerName, const wchar_t* GenParamName, const wchar_t* GenParamValue ) const (Windows only)

HDlLayer HDlLayer.CreateDlLayerLossCtc( HDlLayer DLLayerInputLengths, HDlLayer DLLayerTarget, HDlLayer DLLayerTargetLengths, string layerName, HTuple genParamName, HTuple genParamValue )

HDlLayer HDlLayer.CreateDlLayerLossCtc( HDlLayer DLLayerInputLengths, HDlLayer DLLayerTarget, HDlLayer DLLayerTargetLengths, string layerName, string genParamName, string genParamValue )

DescriptionπŸ”—

The operator create_dl_layer_loss_ctcCreateDlLayerLossCtc creates a Connectionist Temporal Classification (CTC) loss layer whose handle is returned in DLLayerLossCTCDLLayerLossCTCdllayer_loss_ctc. See the reference cited below for information about the CTC loss.

With this loss layer it is possible to train sequence to sequence models (Seq2Seq). E.g., it can be used to train a model that is able to read text in an image. In order to do so, the sequences are compared, thus the determined network prediction DLLayerInputDLLayerInputdllayer_input with sequence length DLLayerInputLengthsDLLayerInputLengthsdllayer_input_lengths to the given DLLayerTargetDLLayerTargetdllayer_target with sequence length DLLayerTargetLengthsDLLayerTargetLengthsdllayer_target_lengths.

The following variables are important to understand the input shapes:

This layer expects multiple layers as input:

The parameter LayerNamelayerNamelayer_name sets an individual layer name. Note that if creating a model using create_dl_modelCreateDlModel each layer of the created network must have a unique name.

The CTC loss is typically applied in a CNN as follows. The input sequence is expected to be encoded in some CNN layer with the output shape [width: T, height: 1, depth: C]. Typically the end of a large fully convolutional classifier is pooled in height down to 1 with an average pooling layer. It is important that the last layer is wide enough to hold enough information. In order to obtain the sequence prediction in the output depth a 1x1 convolutional layer is added after the pooling with the number of kernels set to C. In this use case the CTC loss obtains this convolutional layer as input layer DLLayerInputDLLayerInputdllayer_input. The width of the input layer determines the maximum output sequence of the model.

The CTC loss can be applied to a batch of input items with differing input and target sequence lengths. T and S are the maximum lengths. In DLLayerInputLengthsDLLayerInputLengthsdllayer_input_lengths and DLLayerTargetLengthsDLLayerTargetLengthsdllayer_target_lengths the individual length of each item in a batch needs to be specified. Note that each individual loss within the batch is first normalized by dividing it by its corresponding target length. The final output loss is then computed as the mean of these normalized values across the entire batch.

The following generic parameters GenParamNamegenParamNamegen_param_name and the corresponding values GenParamValuegenParamValuegen_param_value are supported:

  • 'is_inference_output'"is_inference_output": Determines whether apply_dl_modelApplyDlModel will include the output of this layer in the dictionary DLResultBatchDLResultBatchdlresult_batch even without specifying this layer in Outputsoutputsoutputs ('true'"true") or not ('false'"false").

    Default: 'false'"false"

  • 'zero_infinity'"zero_infinity": Determines whether infinite loss values are set to zero in order to stabilize the training. If set to 'true'"true", the loss values and gradients of samples that would yield an infinite loss are set to zero.

    List of values: 'true'"true", 'false'"false"

    Default: 'false'"false"

  • 'loss_weight'"loss_weight": Determines the scalar weight factor with which the loss, calculated in this layer, is multiplied. This parameter can be used to specify the contribution of the CTC loss to the overall network loss in case multiple loss layers are used.

    Default: 1.01.0

Certain parameters of layers created using this operator create_dl_layer_loss_ctcCreateDlLayerLossCtc can be set and retrieved using further operators. The following tables give an overview, which parameters can be set using set_dl_model_layer_paramSetDlModelLayerParam and which ones can be retrieved using get_dl_model_layer_paramGetDlModelLayerParam or get_dl_layer_paramGetDlLayerParam. Note, the operators set_dl_model_layer_paramSetDlModelLayerParam and get_dl_model_layer_paramGetDlModelLayerParam require a model created by create_dl_modelCreateDlModel.

Layer Parameters set get
'input_layer'"input_layer" (DLLayerInputDLLayerInputdllayer_input, DLLayerInputLengthsDLLayerInputLengthsdllayer_input_lengths, DLLayerTargetDLLayerTargetdllayer_target, and/or DLLayerTargetLengthsDLLayerTargetLengthsdllayer_target_lengths) x
'name'"name" (LayerNamelayerNamelayer_name) x x
'output_layer'"output_layer" (DLLayerLossCTCDLLayerLossCTCdllayer_loss_ctc) x
'shape'"shape" x
'type'"type" x
'zero_infinity'"zero_infinity" x x
'loss_weight'"loss_weight" x x
Generic Layer Parameters set get
'is_inference_output'"is_inference_output" x x
'num_trainable_params'"num_trainable_params" x

Execution informationπŸ”—

Execution information
  • Multithreading type: reentrant (runs in parallel with non-exclusive operators).

  • Multithreading scope: global (may be called from any thread).

  • Processed without parallelization.

ParametersπŸ”—

DLLayerInputDLLayerInputdllayer_input (input_control) dl_layer β†’ (handle)HTuple (HHandle)HDlLayer, HTuple (IntPtr)HHandleHtuple (handle)

Input layer with network predictions.

DLLayerInputLengthsDLLayerInputLengthsdllayer_input_lengths (input_control) dl_layer β†’ (handle)HTuple (HHandle)HDlLayer, HTuple (IntPtr)HHandleHtuple (handle)

Input layer which specifies the input sequence length of each item in the batch.

DLLayerTargetDLLayerTargetdllayer_target (input_control) dl_layer β†’ (handle)HTuple (HHandle)HDlLayer, HTuple (IntPtr)HHandleHtuple (handle)

Input layer which specifies the target sequences. If the input dimensions of the CNN are changed the width of this layer is automatically resized to the same width as the DLLayerInputDLLayerInputdllayer_input layer.

DLLayerTargetLengthsDLLayerTargetLengthsdllayer_target_lengths (input_control) dl_layer β†’ (handle)HTuple (HHandle)HDlLayer, HTuple (IntPtr)HHandleHtuple (handle)

Input layer which specifies the target sequence length of each item in the batch.

LayerNamelayerNamelayer_name (input_control) string β†’ (string)HTuple (HString)HTuple (string)strHtuple (char*)

Name of the output layer.

GenParamNamegenParamNamegen_param_name (input_control) attribute.name(-array) β†’ (string)HTuple (HString)HTuple (string)MaybeSequence[str]Htuple (char*)

Generic input parameter names.

Default: [][]
List of values: 'is_inference_output', 'loss_weight', 'zero_infinity'"is_inference_output", "loss_weight", "zero_infinity"

GenParamValuegenParamValuegen_param_value (input_control) attribute.value(-array) β†’ (string / integer / real)HTuple (HString / Hlong / double)HTuple (string / int / long / double)MaybeSequence[Union[int, float, str]]Htuple (char* / Hlong / double)

Generic input parameter values.

Default: [][]
Suggested values: 'true', 'false'"true", "false"

DLLayerLossCTCDLLayerLossCTCdllayer_loss_ctc (output_control) dl_layer β†’ (handle)HTuple (HHandle)HDlLayer, HTuple (IntPtr)HHandleHtuple (handle)

CTC loss layer.

ExampleπŸ”—

(HDevelop)


* Create a simple Seq2Seq model which overfits to a single output sequence.

* Input sequence length
T := 6
* Number of classes including blank (blank is always class_id 0)
C := 3
* Batch Size
N := 1
* Maximum length of target sequences
S := 3

* Model creation
create_dl_layer_input ('input', [T,1,1], [], [], Input)
create_dl_layer_dense (Input, 'dense', T*C, [], [], DLLayerDense)
create_dl_layer_reshape (DLLayerDense, 'dense_reshape', [T,1,C], [], [],\
                         ConvFinal)

* Training part
create_dl_layer_input ('ctc_input_lengths', [1,1,1], [], [],\
                       DLLayerInputLengths)
create_dl_layer_input ('ctc_target', [S,1,1], ['allow_smaller_tuple'], ['true'], DLLayerTarget)
create_dl_layer_input ('ctc_target_lengths', [1,1,1], [], [],\
                       DLLayerTargetLengths)
* Create the loss layer
create_dl_layer_loss_ctc (ConvFinal, DLLayerInputLengths, DLLayerTarget,\
                          DLLayerTargetLengths, 'ctc_loss', [], [],\
                          DLLayerLossCTC)

* Inference part
create_dl_layer_softmax (ConvFinal, 'softmax', [], [], DLLayerSoftMax)
create_dl_layer_depth_max (DLLayerSoftMax, 'prediction', 'argmax', [], [],\
                           DLLayerDepthMaxArg, _)

* Setting a seed because the weights of the network are randomly initialized
set_system ('seed_rand', 35)

create_dl_model ([DLLayerLossCTC,DLLayerDepthMaxArg], DLModel)

set_dl_model_param (DLModel, 'batch_size', N)
set_dl_model_param (DLModel, 'runtime', 'gpu')
set_dl_model_param (DLModel, 'learning_rate', 1)

* Create input sample for training
InputSequence := [0,1,2,3,4,5]
TargetSequence := [1,2,1]
create_dict (InputSample)
InputSample.input := InputSequence
InputSample.ctc_input_lengths := |InputSequence|
InputSample.ctc_target := TargetSequence
InputSample.ctc_target_lengths := |TargetSequence|

PredictedSequence := []
dev_inspect_ctrl ([InputSequence, TargetSequence, CTCLoss, PredictedValues,\
                  PredictedSequence])
Eps := 0.01
MaxIterations := 15
for I := 0 to MaxIterations by 1
    apply_dl_model (DLModel, InputSample, ['prediction','softmax'], \
                    DLResultBatch)
    get_grayval (DLResultBatch.prediction, rep(0,T), [0:T-1], PredictedValues)
   train_dl_model_batch (DLModel, InputSample, DLTrainResult)
    CTCLoss := DLTrainResult.ctc_loss
    if (CTCLoss < Eps)
        break
    endif
    stop()
endfor

* Greedy decoding of predicted sequence
PredictedSequence := uniq(int(PredictedValues))
PredictedSequence := select_mask(PredictedSequence, PredictedSequence [#] 0)

ReferencesπŸ”—

Graves Alex et al., β€œConnectionist temporal classification: labelling unsegmented sequence data with recurrent neural networks.” Proceedings of the 23rd international conference on Machine learning. 2006.

ModuleπŸ”—

Deep Learning Professional