class_docstr: Mixer-TTS and Mixer-TTS-X models (https://arxiv.org/abs/2110.03584) that is used to generate mel spectrogram from text.
class_docstr_tok: ["Mixer-TTS","and","Mixer-TTS-X","models","(","https",":","\/\/arxiv.org\/abs\/2110.03584",")","that","is","used","to","generate","mel","spectrogram","from","text","."]
code: def _metrics(
      self,
      true_durs,
      true_text_len,
      pred_durs,
      true_pitch,
      pred_pitch,
      true_spect=None,
      pred_spect=None,
      true_spect_len=None,
      attn_logprob=None,
      attn_soft=None,
      attn_hard=None,
      attn_hard_dur=None,
  ):
      text_mask = get_mask_from_lengths(true_text_len)
      mel_mask = get_mask_from_lengths(true_spect_len)
      loss = 0.0
  
      # Dur loss and metrics
      durs_loss = F.mse_loss(
          pred_durs, (true_durs + 1).float().log(), reduction="none"
      )
      durs_loss = durs_loss * text_mask.float()
      durs_loss = durs_loss.sum() / text_mask.sum()
  
      durs_pred = pred_durs.exp() - 1
      durs_pred = torch.clamp_min(durs_pred, min=0)
      durs_pred = durs_pred.round().long()
  
      acc = (
          ((true_durs == durs_pred) * text_mask).sum().float()
          / text_mask.sum()
          * 100
      )
      acc_dist_1 = (
          (((true_durs - durs_pred).abs() <= 1) * text_mask)
          .sum()
          .float()
          / text_mask.sum()
          * 100
      )
      acc_dist_3 = (
          (((true_durs - durs_pred).abs() <= 3) * text_mask)
          .sum()
          .float()
          / text_mask.sum()
          * 100
      )
  
      pred_spect = pred_spect.transpose(1, 2)
  
      # Mel loss
      mel_loss = F.mse_loss(
          pred_spect, true_spect, reduction="none"
      ).mean(dim=-2)
      mel_loss = mel_loss * mel_mask.float()
      mel_loss = mel_loss.sum() / mel_mask.sum()
  
      loss = (
          loss
          + self.durs_loss_scale * durs_loss
          + self.mel_loss_scale * mel_loss
      )
  
      # Aligner loss
      bin_loss, ctc_loss = None, None
      ctc_loss = self.forward_sum_loss(
          attn_logprob=attn_logprob,
          in_lens=true_text_len,
          out_lens=true_spect_len,
      )
      loss = loss + ctc_loss
      if self.add_bin_loss:
          bin_loss = self.bin_loss(
              hard_attention=attn_hard, soft_attention=attn_soft
          )
          loss = loss + self.bin_loss_scale * bin_loss
      true_avg_pitch = average_features(
          true_pitch.unsqueeze(1), attn_hard_dur
      ).squeeze(1)
  
      # Pitch loss
      pitch_loss = F.mse_loss(
          pred_pitch, true_avg_pitch, reduction="none"
      )  # noqa
      pitch_loss = (pitch_loss * text_mask).sum() / text_mask.sum()
  
      loss = loss + self.pitch_loss_scale * pitch_loss
  
      return (
          loss,
          durs_loss,
          acc,
          acc_dist_1,
          acc_dist_3,
          pitch_loss,
          mel_loss,
          ctc_loss,
          bin_loss,
      )
  
imports: import contextlib
  from typing import List, Optional
  import numpy
  import omegaconf
  import torch
  import transformers
  import wandb
  from hydra.utils import instantiate
  from omegaconf import DictConfig
  from pytorch_lightning import Trainer
  from pytorch_lightning.loggers import WandbLogger
  from torch import nn
  from torch.nn import functional
  from transformers import AlbertTokenizer
  from nemo.collections.common.tokenizers.text_to_speech.tts_tokenizers import EnglishCharsTokenizer, EnglishPhonemesTokenizer
  from nemo.collections.tts.losses.aligner_loss import BinLoss, ForwardSumLoss
  from nemo.collections.tts.models.base import SpectrogramGenerator
  from nemo.collections.tts.modules.fastpitch import average_features, regulate_len
  from nemo.collections.tts.parts.utils.helpers import binarize_attention_parallel, g2p_backward_compatible_support, get_mask_from_lengths, plot_pitch_to_numpy, plot_spectrogram_to_numpy
  from nemo.core import Exportable
  from nemo.core.classes.common import PretrainedModelInfo, typecheck
  from nemo.core.neural_types.elements import LengthsType, LogprobsType, MelSpectrogramType, ProbsType, RegressionValuesType, TokenDurationType, TokenIndex, TokenLogDurationType
  from nemo.core.neural_types.neural_type import NeuralType
  from nemo.utils import logging, model_utils
label_desc: Use image node_id 7 for calling the MixerTTSModel obj's underlying member method code with example usage: obj._metrics(true_durs, true_text_len, pred_durs, true_pitch, pred_pitch, true_spect, pred_spect, true_spect_len, attn_logprob, attn_soft, attn_hard, attn_hard_dur) and returns: loss,  durs_loss,  acc,  acc_dist_1,  acc_dist_3,  pitch_loss,  mel_loss,  ctc_loss,  bin_loss