class_docstr: This class implements the beam-search algorithm for the transducer model.
  
  Parameters
  ----------
  decode_network_lst : list
      List of prediction network (PN) layers.
  tjoint: transducer_joint module
      This module perform the joint between TN and PN.
  classifier_network : list
      List of output layers (after performing joint between TN and PN)
      exp: (TN,PN) => joint => classifier_network_list [DNN bloc, Linear..] => chars prob
  blank_id : int
      The blank symbol/index.
  beam : int
      The width of beam. Greedy Search is used when beam = 1.
  nbest : int
      Number of hypotheses to keep.
  lm_module : torch.nn.ModuleList
      Neural networks modules for LM.
  lm_weight : float
      The weight of LM when performing beam search (λ).
      log P(y|x) + λ log P_LM(y). (default: 0.3)
  state_beam : float
      The threshold coefficient in log space to decide if hyps in A (process_hyps)
      is likely to compete with hyps in B (beam_hyps), if not, end the while loop.
      Reference: https://arxiv.org/pdf/1911.01629.pdf
  expand_beam : float
      The threshold coefficient to limit the number of expanded hypotheses
      that are added in A (process_hyp).
      Reference: https://arxiv.org/pdf/1911.01629.pdf
      Reference: https://github.com/kaldi-asr/kaldi/blob/master/src/decoder/simple-decoder.cc (See PruneToks)
  
  Example
  -------
  searcher = TransducerBeamSearcher(
      decode_network_lst=[hparams["emb"], hparams["dec"]],
      tjoint=hparams["Tjoint"],
      classifier_network=[hparams["transducer_lin"]],
      blank_id=0,
      beam_size=hparams["beam_size"],
      nbest=hparams["nbest"],
      lm_module=hparams["lm_model"],
      lm_weight=hparams["lm_weight"],
      state_beam=2.3,
      expand_beam=2.3,
  )
  >>> from speechbrain.nnet.transducer.transducer_joint import Transducer_joint
  >>> import speechbrain as sb
  >>> emb = sb.nnet.embedding.Embedding(
  ...     num_embeddings=35,
  ...     embedding_dim=3,
  ...     consider_as_one_hot=True,
  ...     blank_id=0
  ... )
  >>> dec = sb.nnet.RNN.GRU(
  ...     hidden_size=10, input_shape=(1, 40, 34), bidirectional=False
  ... )
  >>> lin = sb.nnet.linear.Linear(input_shape=(1, 40, 10), n_neurons=35)
  >>> joint_network= sb.nnet.linear.Linear(input_shape=(1, 1, 40, 35), n_neurons=35)
  >>> tjoint = Transducer_joint(joint_network, joint="sum")
  >>> searcher = TransducerBeamSearcher(
  ...     decode_network_lst=[emb, dec],
  ...     tjoint=tjoint,
  ...     classifier_network=[lin],
  ...     blank_id=0,
  ...     beam_size=1,
  ...     nbest=1,
  ...     lm_module=None,
  ...     lm_weight=0.0,
  ... )
  >>> enc = torch.rand([1, 20, 10])
  >>> hyps, _, _, _ = searcher(enc)
class_docstr_tok: ["This","class","implements","the","beam-search","algorithm","for","the","transducer","model",".","Parameters","--","--","--","--","--","decode_network_lst",":","list","List","of","prediction","network","(","PN",")","layers",".","tjoint",":","transducer_joint","module","This","module","perform","the","joint","between","TN","and","PN",".","classifier_network",":","list","List","of","output","layers","(","after","performing","joint","between","TN","and","PN",")","exp",":","(","TN",",","PN",")","=",">","joint","=",">","classifier_network_list","[","DNN","bloc",",","Linear","..","]","=",">","chars","prob","blank_id",":","int","The","blank","symbol\/index",".","beam",":","int","The","width","of","beam",".","Greedy","Search","is","used","when","beam","=","1.","nbest",":","int","Number","of","hypotheses","to","keep",".","lm_module",":","torch.nn.ModuleList","Neural","networks","modules","for","LM",".","lm_weight",":","float","The","weight","of","LM","when","performing","beam","search","(","\u03bb",")",".","log","P","(","y|x",")","+","\u03bb","log","P_LM","(","y",")",".","(","default",":","0.3",")","state_beam",":","float","The","threshold","coefficient","in","log","space","to","decide","if","hyps","in","A","(","process_hyps",")","is","likely","to","compete","with","hyps","in","B","(","beam_hyps",")",",","if","not",",","end","the","while","loop",".","Reference",":","https",":","\/\/arxiv.org\/pdf\/1911.01629.pdf","expand_beam",":","float","The","threshold","coefficient","to","limit","the","number","of","expanded","hypotheses","that","are","added","in","A","(","process_hyp",")",".","Reference",":","https",":","\/\/arxiv.org\/pdf\/1911.01629.pdf","Reference",":","https",":","\/\/github.com\/kaldi-asr\/kaldi\/blob\/master\/src\/decoder\/simple-decoder.cc","(","See","PruneToks",")","Example","--","--","--","-","searcher","=","TransducerBeamSearcher","(","decode_network_lst=","[","hparams","[","``","emb","''","]",",","hparams","[","``","dec","''","]","]",",","tjoint=hparams","[","``","Tjoint","''","]",",","classifier_network=","[","hparams","[","``","transducer_lin","''","]","]",",","blank_id=0",",","beam_size=hparams","[","``","beam_size","''","]",",","nbest=hparams","[","``","nbest","''","]",",","lm_module=hparams","[","``","lm_model","''","]",",","lm_weight=hparams","[","``","lm_weight","''","]",",","state_beam=2.3",",","expand_beam=2.3",",",")",">",">",">","from","speechbrain.nnet.transducer.transducer_joint","import","Transducer_joint",">",">",">","import","speechbrain","as","sb",">",">",">","emb","=","sb.nnet.embedding.Embedding","(","...","num_embeddings=35",",","...","embedding_dim=3",",","...","consider_as_one_hot=True",",","...","blank_id=0","...",")",">",">",">","dec","=","sb.nnet.RNN.GRU","(","...","hidden_size=10",",","input_shape=","(","1",",","40",",","34",")",",","bidirectional=False","...",")",">",">",">","lin","=","sb.nnet.linear.Linear","(","input_shape=","(","1",",","40",",","10",")",",","n_neurons=35",")",">",">",">","joint_network=","sb.nnet.linear.Linear","(","input_shape=","(","1",",","1",",","40",",","35",")",",","n_neurons=35",")",">",">",">","tjoint","=","Transducer_joint","(","joint_network",",","joint=","''","sum","''",")",">",">",">","searcher","=","TransducerBeamSearcher","(","...","decode_network_lst=","[","emb",",","dec","]",",","...","tjoint=tjoint",",","...","classifier_network=","[","lin","]",",","...","blank_id=0",",","...","beam_size=1",",","...","nbest=1",",","...","lm_module=None",",","...","lm_weight=0.0",",","...",")",">",">",">","enc","=","torch.rand","(","[","1",",","20",",","10","]",")",">",">",">","hyps",",","_",",","_",",","_","=","searcher","(","enc",")"]
docstr: Transducer greedy decoder is a greedy decoder over batch which apply Transducer rules:
      1- for each time step in the Transcription Network (TN) output:
          -> Update the ith utterance only if
              the previous target != the new one (we save the hiddens and the target)
          -> otherwise:
          ---> keep the previous target prediction from the decoder
  
  Arguments
  ----------
  tn_output : torch.tensor
      Output from transcription network with shape
      [batch, time_len, hiddens].
  
  hidden_state : (torch.Tensor, torch.Tensor)
      Hidden state to initially feed the decode network with. This is
      useful in conjunction with `return_hidden` to be able to perform
      beam search in a streaming context, so that you can reuse the last
      hidden state as an initial state across calls.
  
  return_hidden : bool
      Whether the return tuple should contain an extra 5th element with
      the hidden state at of the last step. See `hidden_state`.
  
  Returns
  -------
  Tuple of 4 or 5 elements (if `return_hidden`).
  
  First element: List[List[int]]
      List of decoded tokens
  
  Second element: torch.Tensor
      Outputs a logits tensor [B,T,1,Output_Dim]; padding
      has not been removed.
  
  Third element: None
      nbest; irrelevant for greedy decode
  
  Fourth element: None
      nbest scores; irrelevant for greedy decode
  
  Fifth element: Present if `return_hidden`, (torch.Tensor, torch.Tensor)
      Tuple representing the hidden state required to call
      `transducer_greedy_decode` where you left off in a streaming
      context.
docstr_tok: ["Transducer","greedy","decoder","is","a","greedy","decoder","over","batch","which","apply","Transducer","rules",":","1-","for","each","time","step","in","the","Transcription","Network","(","TN",")","output",":","-",">","Update","the","ith","utterance","only","if","the","previous","target","!","=","the","new","one","(","we","save","the","hiddens","and","the","target",")","-",">","otherwise",":","--","-",">","keep","the","previous","target","prediction","from","the","decoder","Arguments","--","--","--","--","--","tn_output",":","torch.tensor","Output","from","transcription","network","with","shape","[","batch",",","time_len",",","hiddens","]",".","hidden_state",":","(","torch.Tensor",",","torch.Tensor",")","Hidden","state","to","initially","feed","the","decode","network","with",".","This","is","useful","in","conjunction","with","`","return_hidden","`","to","be","able","to","perform","beam","search","in","a","streaming","context",",","so","that","you","can","reuse","the","last","hidden","state","as","an","initial","state","across","calls",".","return_hidden",":","bool","Whether","the","return","tuple","should","contain","an","extra","5th","element","with","the","hidden","state","at","of","the","last","step",".","See","`","hidden_state","`",".","Returns","--","--","--","-","Tuple","of","4","or","5","elements","(","if","`","return_hidden","`",")",".","First","element",":","List","[","List","[","int","]","]","List","of","decoded","tokens","Second","element",":","torch.Tensor","Outputs","a","logits","tensor","[","B",",","T,1",",","Output_Dim","]",";","padding","has","not","been","removed",".","Third","element",":","None","nbest",";","irrelevant","for","greedy","decode","Fourth","element",":","None","nbest","scores",";","irrelevant","for","greedy","decode","Fifth","element",":","Present","if","`","return_hidden","`",",","(","torch.Tensor",",","torch.Tensor",")","Tuple","representing","the","hidden","state","required","to","call","`","transducer_greedy_decode","`","where","you","left","off","in","a","streaming","context","."]
code: def transducer_greedy_decode(
      self, tn_output, hidden_state=None, return_hidden=False
  ):
      """Transducer greedy decoder is a greedy decoder over batch which apply Transducer rules:
          1- for each time step in the Transcription Network (TN) output:
              -> Update the ith utterance only if
                  the previous target != the new one (we save the hiddens and the target)
              -> otherwise:
              ---> keep the previous target prediction from the decoder
  
      Arguments
      ----------
      tn_output : torch.tensor
          Output from transcription network with shape
          [batch, time_len, hiddens].
  
      hidden_state : (torch.Tensor, torch.Tensor)
          Hidden state to initially feed the decode network with. This is
          useful in conjunction with `return_hidden` to be able to perform
          beam search in a streaming context, so that you can reuse the last
          hidden state as an initial state across calls.
  
      return_hidden : bool
          Whether the return tuple should contain an extra 5th element with
          the hidden state at of the last step. See `hidden_state`.
  
      Returns
      -------
      Tuple of 4 or 5 elements (if `return_hidden`).
  
      First element: List[List[int]]
          List of decoded tokens
  
      Second element: torch.Tensor
          Outputs a logits tensor [B,T,1,Output_Dim]; padding
          has not been removed.
  
      Third element: None
          nbest; irrelevant for greedy decode
  
      Fourth element: None
          nbest scores; irrelevant for greedy decode
  
      Fifth element: Present if `return_hidden`, (torch.Tensor, torch.Tensor)
          Tuple representing the hidden state required to call
          `transducer_greedy_decode` where you left off in a streaming
          context.
      """
      hyp = {
          "prediction": [[] for _ in range(tn_output.size(0))],
          "logp_scores": [0.0 for _ in range(tn_output.size(0))],
      }
      # prepare BOS = Blank for the Prediction Network (PN)
      input_PN = (
          torch.ones(
              (tn_output.size(0), 1),
              device=tn_output.device,
              dtype=torch.int32,
          )
          * self.blank_id
      )
  
      if hidden_state is None:
          # First forward-pass on PN
          out_PN, hidden = self._forward_PN(
              input_PN, self.decode_network_lst
          )
      else:
          out_PN, hidden = hidden_state
  
      # For each time step
      for t_step in range(tn_output.size(1)):
          # do unsqueeze over since tjoint must be have a 4 dim [B,T,U,Hidden]
          log_probs = self._joint_forward_step(
              tn_output[:, t_step, :].unsqueeze(1).unsqueeze(1),
              out_PN.unsqueeze(1),
          )
          # Sort outputs at time
          logp_targets, positions = torch.max(
              self.softmax(log_probs).squeeze(1).squeeze(1), dim=1
          )
          # Batch hidden update
          have_update_hyp = []
          for i in range(positions.size(0)):
              # Update hiddens only if
              # 1- current prediction is non blank
              if positions[i].item() != self.blank_id:
                  hyp["prediction"][i].append(positions[i].item())
                  hyp["logp_scores"][i] += logp_targets[i]
                  input_PN[i][0] = positions[i]
                  have_update_hyp.append(i)
          if len(have_update_hyp) > 0:
              # Select sentence to update
              # And do a forward steps + generated hidden
              (
                  selected_input_PN,
                  selected_hidden,
              ) = self._get_sentence_to_update(
                  have_update_hyp, input_PN, hidden
              )
              selected_out_PN, selected_hidden = self._forward_PN(
                  selected_input_PN,
                  self.decode_network_lst,
                  selected_hidden,
              )
              # update hiddens and out_PN
              out_PN[have_update_hyp] = selected_out_PN
              hidden = self._update_hiddens(
                  have_update_hyp, selected_hidden, hidden
              )
  
      ret = (
          hyp["prediction"],
          torch.Tensor(hyp["logp_scores"]).exp().mean(),
          None,
          None,
      )
  
      if return_hidden:
          # append the `(out_PN, hidden)` tuple to ret
          ret += (
              (
                  out_PN,
                  hidden,
              ),
          )
  
      return ret
  
file_path: speechbrain/speechbrain/decoders/transducer.py
imports: import torch
  from functools import partial
label_desc: Use image node_id 3 for calling the TransducerBeamSearcher obj's underlying member method code with example usage: obj.transducer_greedy_decode(tn_output, hidden_state, return_hidden) and returns: ret