class_docstr: Adds utilities for computing interCTC loss from https://arxiv.org/abs/2102.03216.
  
  To use, make sure encoder accesses ``interctc['capture_layers']``
  property in the AccessMixin and registers ``interctc/layer_output_X`` and
  ``interctc/layer_length_X`` for all layers that we want to get loss from.
  Additionally, specify the following config parameters to set up loss::
  
      interctc:
          # can use different values
          loss_weights: [0.3]
          apply_at_layers: [8]
  
  Then call
  
      * ``self.setup_interctc(ctc_decoder_name, ctc_loss_name, ctc_wer_name)``
        in the init method
      * ``self.add_interctc_losses`` after computing regular loss.
      * ``self.finalize_interctc_metrics(metrics, outputs, prefix="val_")``
        in the `multi_validation_epoch_end` method.
      * ``self.finalize_interctc_metrics(metrics, outputs, prefix="test_")``
        in the `multi_test_epoch_end` method.
class_docstr_tok: ["Adds","utilities","for","computing","interCTC","loss","from","https",":","\/\/arxiv.org\/abs\/2102.03216",".","To","use",",","make","sure","encoder","accesses","``","interctc","[","'capture_layers","'","]","``","property","in","the","AccessMixin","and","registers","``","interctc\/layer_output_X","``","and","``","interctc\/layer_length_X","``","for","all","layers","that","we","want","to","get","loss","from",".","Additionally",",","specify","the","following","config","parameters","to","set","up","loss",":",":","interctc",":","#","can","use","different","values","loss_weights",":","[","0.3","]","apply_at_layers",":","[","8","]","Then","call","*","``","self.setup_interctc","(","ctc_decoder_name",",","ctc_loss_name",",","ctc_wer_name",")","``","in","the","init","method","*","``","self.add_interctc_losses","``","after","computing","regular","loss",".","*","``","self.finalize_interctc_metrics","(","metrics",",","outputs",",","prefix=","''","val_","''",")","``","in","the","`","multi_validation_epoch_end","`","method",".","*","``","self.finalize_interctc_metrics","(","metrics",",","outputs",",","prefix=","''","test_","''",")","``","in","the","`","multi_test_epoch_end","`","method","."]
docstr: Finalizes InterCTC WER and loss metrics for logging purposes.
  
  Should be called inside ``multi_validation_epoch_end`` (with ``prefix="val_"``) or
  ``multi_test_epoch_end`` (with ``prefix="test_"``).
  
  Note that ``metrics`` dictionary is going to be updated in-place.
docstr_tok: ["Finalizes","InterCTC","WER","and","loss","metrics","for","logging","purposes",".","Should","be","called","inside","``","multi_validation_epoch_end","``","(","with","``","prefix=","''","val_","''","``",")","or","``","multi_test_epoch_end","``","(","with","``","prefix=","''","test_","''","``",")",".","Note","that","``","metrics","``","dictionary","is","going","to","be","updated","in-place","."]
code: def finalize_interctc_metrics(
      self, metrics: Dict, outputs: List[Dict], prefix: str
  ):
      """Finalizes InterCTC WER and loss metrics for logging purposes.
  
      Should be called inside ``multi_validation_epoch_end`` (with ``prefix="val_"``) or
      ``multi_test_epoch_end`` (with ``prefix="test_"``).
  
      Note that ``metrics`` dictionary is going to be updated in-place.
      """
      if self.is_interctc_enabled():
          for layer_idx in self.get_interctc_param("apply_at_layers"):
              # assuming that if the first batch logged the metrics, then all batches did
              if f"{prefix}inter_ctc_loss_l{layer_idx}" in outputs[0]:
                  loss = torch.stack(
                      [
                          x[f"{prefix}inter_ctc_loss_l{layer_idx}"]
                          for x in outputs
                      ]
                  ).mean()
                  metrics["log"][
                      f"{prefix}inter_ctc_loss_l{layer_idx}"
                  ] = loss
  
              if f"{prefix}inter_wer_num_l{layer_idx}" in outputs[0]:
                  wer_num = torch.stack(
                      [
                          x[f"{prefix}inter_wer_num_l{layer_idx}"]
                          for x in outputs
                      ]
                  ).sum()
                  wer_denom = torch.stack(
                      [
                          x[f"{prefix}inter_wer_denom_l{layer_idx}"]
                          for x in outputs
                      ]
                  ).sum()
                  metrics["log"][f"{prefix}inter_wer_l{layer_idx}"] = (
                      wer_num / wer_denom
                  )
  
          if f"{prefix}final_loss" in outputs[0]:
              metrics["log"][f"{prefix}final_loss"] = torch.stack(
                  [x[f"{prefix}final_loss"] for x in outputs]
              ).mean()
  
file_path: NeMo/nemo/collections/asr/parts/mixins/interctc_mixin.py
imports: from typing import Dict, List, Optional, Tuple
  import torch
  from nemo.core.classes.mixins import AccessMixin
label_desc: Use image node_id 8 for calling the InterCTCMixin obj's underlying member method code with example usage: obj.finalize_interctc_metrics(metrics, outputs, prefix) without return types