class_docstr: Tacotron2 model from *Natural TTS Synthesis by Conditioning WaveNet on Mel Spectrogram Predictions*
  :cite:`shen2018natural` based on the implementation from
  `Nvidia Deep Learning Examples <https://github.com/NVIDIA/DeepLearningExamples/>`_.
  
  See Also:
      * :class:`torchaudio.pipelines.Tacotron2TTSBundle`: TTS pipeline with pretrained model.
  
  Args:
      mask_padding (bool, optional): Use mask padding (Default: ``False``).
      n_mels (int, optional): Number of mel bins (Default: ``80``).
      n_symbol (int, optional): Number of symbols for the input text (Default: ``148``).
      n_frames_per_step (int, optional): Number of frames processed per step, only 1 is supported (Default: ``1``).
      symbol_embedding_dim (int, optional): Input embedding dimension (Default: ``512``).
      encoder_n_convolution (int, optional): Number of encoder convolutions (Default: ``3``).
      encoder_kernel_size (int, optional): Encoder kernel size (Default: ``5``).
      encoder_embedding_dim (int, optional): Encoder embedding dimension (Default: ``512``).
      decoder_rnn_dim (int, optional): Number of units in decoder LSTM (Default: ``1024``).
      decoder_max_step (int, optional): Maximum number of output mel spectrograms (Default: ``2000``).
      decoder_dropout (float, optional): Dropout probability for decoder LSTM (Default: ``0.1``).
      decoder_early_stopping (bool, optional): Continue decoding after all samples are finished (Default: ``True``).
      attention_rnn_dim (int, optional): Number of units in attention LSTM (Default: ``1024``).
      attention_hidden_dim (int, optional): Dimension of attention hidden representation (Default: ``128``).
      attention_location_n_filter (int, optional): Number of filters for attention model (Default: ``32``).
      attention_location_kernel_size (int, optional): Kernel size for attention model (Default: ``31``).
      attention_dropout (float, optional): Dropout probability for attention LSTM (Default: ``0.1``).
      prenet_dim (int, optional): Number of ReLU units in prenet layers (Default: ``256``).
      postnet_n_convolution (int, optional): Number of postnet convolutions (Default: ``5``).
      postnet_kernel_size (int, optional): Postnet kernel size (Default: ``5``).
      postnet_embedding_dim (int, optional): Postnet embedding dimension (Default: ``512``).
      gate_threshold (float, optional): Probability threshold for stop token (Default: ``0.5``).
class_docstr_tok: ["Tacotron2","model","from","*","Natural","TTS","Synthesis","by","Conditioning","WaveNet","on","Mel","Spectrogram","Predictions","*",":","cite",":","`","shen2018natural","`","based","on","the","implementation","from","`","Nvidia","Deep","Learning","Examples","<","https",":","\/\/github.com\/NVIDIA\/DeepLearningExamples\/",">","`","_",".","See","Also",":","*",":","class",":","`","torchaudio.pipelines.Tacotron2TTSBundle","`",":","TTS","pipeline","with","pretrained","model",".","Args",":","mask_padding","(","bool",",","optional",")",":","Use","mask","padding","(","Default",":","``","False","``",")",".","n_mels","(","int",",","optional",")",":","Number","of","mel","bins","(","Default",":","``","80","``",")",".","n_symbol","(","int",",","optional",")",":","Number","of","symbols","for","the","input","text","(","Default",":","``","148","``",")",".","n_frames_per_step","(","int",",","optional",")",":","Number","of","frames","processed","per","step",",","only","1","is","supported","(","Default",":","``","1","``",")",".","symbol_embedding_dim","(","int",",","optional",")",":","Input","embedding","dimension","(","Default",":","``","512","``",")",".","encoder_n_convolution","(","int",",","optional",")",":","Number","of","encoder","convolutions","(","Default",":","``","3","``",")",".","encoder_kernel_size","(","int",",","optional",")",":","Encoder","kernel","size","(","Default",":","``","5","``",")",".","encoder_embedding_dim","(","int",",","optional",")",":","Encoder","embedding","dimension","(","Default",":","``","512","``",")",".","decoder_rnn_dim","(","int",",","optional",")",":","Number","of","units","in","decoder","LSTM","(","Default",":","``","1024","``",")",".","decoder_max_step","(","int",",","optional",")",":","Maximum","number","of","output","mel","spectrograms","(","Default",":","``","2000","``",")",".","decoder_dropout","(","float",",","optional",")",":","Dropout","probability","for","decoder","LSTM","(","Default",":","``","0.1","``",")",".","decoder_early_stopping","(","bool",",","optional",")",":","Continue","decoding","after","all","samples","are","finished","(","Default",":","``","True","``",")",".","attention_rnn_dim","(","int",",","optional",")",":","Number","of","units","in","attention","LSTM","(","Default",":","``","1024","``",")",".","attention_hidden_dim","(","int",",","optional",")",":","Dimension","of","attention","hidden","representation","(","Default",":","``","128","``",")",".","attention_location_n_filter","(","int",",","optional",")",":","Number","of","filters","for","attention","model","(","Default",":","``","32","``",")",".","attention_location_kernel_size","(","int",",","optional",")",":","Kernel","size","for","attention","model","(","Default",":","``","31","``",")",".","attention_dropout","(","float",",","optional",")",":","Dropout","probability","for","attention","LSTM","(","Default",":","``","0.1","``",")",".","prenet_dim","(","int",",","optional",")",":","Number","of","ReLU","units","in","prenet","layers","(","Default",":","``","256","``",")",".","postnet_n_convolution","(","int",",","optional",")",":","Number","of","postnet","convolutions","(","Default",":","``","5","``",")",".","postnet_kernel_size","(","int",",","optional",")",":","Postnet","kernel","size","(","Default",":","``","5","``",")",".","postnet_embedding_dim","(","int",",","optional",")",":","Postnet","embedding","dimension","(","Default",":","``","512","``",")",".","gate_threshold","(","float",",","optional",")",":","Probability","threshold","for","stop","token","(","Default",":","``","0.5","``",")","."]
docstr: Using Tacotron2 for inference. The input is a batch of encoded
  sentences (``tokens``) and its corresponding lengths (``lengths``). The
  output is the generated mel spectrograms, its corresponding lengths, and
  the attention weights from the decoder.
  
  The input `tokens` should be padded with zeros to length max of ``lengths``.
  
  Args:
      tokens (Tensor): The input tokens to Tacotron2 with shape `(n_batch, max of lengths)`.
      lengths (Tensor or None, optional):
          The valid length of each sample in ``tokens`` with shape `(n_batch, )`.
          If ``None``, it is assumed that the all the tokens are valid. Default: ``None``
  
  Returns:
      (Tensor, Tensor, Tensor):
          Tensor
              The predicted mel spectrogram with shape `(n_batch, n_mels, max of mel_specgram_lengths)`.
          Tensor
              The length of the predicted mel spectrogram with shape `(n_batch, )`.
          Tensor
              Sequence of attention weights from the decoder with shape
              `(n_batch, max of mel_specgram_lengths, max of lengths)`.
docstr_tok: ["Using","Tacotron2","for","inference",".","The","input","is","a","batch","of","encoded","sentences","(","``","tokens","``",")","and","its","corresponding","lengths","(","``","lengths","``",")",".","The","output","is","the","generated","mel","spectrograms",",","its","corresponding","lengths",",","and","the","attention","weights","from","the","decoder",".","The","input","`","tokens","`","should","be","padded","with","zeros","to","length","max","of","``","lengths","``",".","Args",":","tokens","(","Tensor",")",":","The","input","tokens","to","Tacotron2","with","shape","`","(","n_batch",",","max","of","lengths",")","`",".","lengths","(","Tensor","or","None",",","optional",")",":","The","valid","length","of","each","sample","in","``","tokens","``","with","shape","`","(","n_batch",",",")","`",".","If","``","None","``",",","it","is","assumed","that","the","all","the","tokens","are","valid",".","Default",":","``","None","``","Returns",":","(","Tensor",",","Tensor",",","Tensor",")",":","Tensor","The","predicted","mel","spectrogram","with","shape","`","(","n_batch",",","n_mels",",","max","of","mel_specgram_lengths",")","`",".","Tensor","The","length","of","the","predicted","mel","spectrogram","with","shape","`","(","n_batch",",",")","`",".","Tensor","Sequence","of","attention","weights","from","the","decoder","with","shape","`","(","n_batch",",","max","of","mel_specgram_lengths",",","max","of","lengths",")","`","."]
code: def infer(
      self, tokens: Tensor, lengths: Optional[Tensor] = None
  ) -> Tuple[Tensor, Tensor, Tensor]:
      r"""Using Tacotron2 for inference. The input is a batch of encoded
      sentences (``tokens``) and its corresponding lengths (``lengths``). The
      output is the generated mel spectrograms, its corresponding lengths, and
      the attention weights from the decoder.
  
      The input `tokens` should be padded with zeros to length max of ``lengths``.
  
      Args:
          tokens (Tensor): The input tokens to Tacotron2 with shape `(n_batch, max of lengths)`.
          lengths (Tensor or None, optional):
              The valid length of each sample in ``tokens`` with shape `(n_batch, )`.
              If ``None``, it is assumed that the all the tokens are valid. Default: ``None``
  
      Returns:
          (Tensor, Tensor, Tensor):
              Tensor
                  The predicted mel spectrogram with shape `(n_batch, n_mels, max of mel_specgram_lengths)`.
              Tensor
                  The length of the predicted mel spectrogram with shape `(n_batch, )`.
              Tensor
                  Sequence of attention weights from the decoder with shape
                  `(n_batch, max of mel_specgram_lengths, max of lengths)`.
      """
      n_batch, max_length = tokens.shape
      if lengths is None:
          lengths = (
              torch.tensor([max_length])
              .expand(n_batch)
              .to(tokens.device, tokens.dtype)
          )
  
      assert lengths is not None  # For TorchScript compiler
      embedded_inputs = self.embedding(tokens).transpose(1, 2)
      encoder_outputs = self.encoder(embedded_inputs, lengths)
      (
          mel_specgram,
          mel_specgram_lengths,
          _,
          alignments,
      ) = self.decoder.infer(encoder_outputs, lengths)
  
      mel_outputs_postnet = self.postnet(mel_specgram)
      mel_outputs_postnet = mel_specgram + mel_outputs_postnet
  
      alignments = alignments.unfold(1, n_batch, n_batch).transpose(
          0, 2
      )
  
      return mel_outputs_postnet, mel_specgram_lengths, alignments
  
imports: import warnings
  from typing import List, Optional, Tuple, Union
  import torch
  from torch import nn, Tensor
  from torch.nn import functional
label_desc: Use image node_id 3 for calling the Tacotron2 obj's underlying member method code with example usage: obj.infer(tokens, lengths) and returns: mel_outputs_postnet,  mel_specgram_lengths,  alignments