class_docstr: Decoder layer.
  
  Args:
      eprojs (int): Dimension of input variables from encoder.
      odim (int): The output dimension.
      dtype (str): Decoder type.
      dlayers (int): Number of layers for decoder.
      dunits (int): Dimension of input vector of decoder.
      sos (int): Number to indicate the start of sequences.
      eos (int): Number to indicate the end of sequences.
      att (Module): Attention module defined at
          `espnet.espnet.nets.chainer_backend.attentions`.
      verbose (int): Verbosity level.
      char_list (List[str]): List of all characters.
      labeldist (numpy.array): Distributed array of counted transcript length.
      lsm_weight (float): Weight to use when calculating the training loss.
      sampling_probability (float): Threshold for scheduled sampling.
class_docstr_tok: ["Decoder","layer",".","Args",":","eprojs","(","int",")",":","Dimension","of","input","variables","from","encoder",".","odim","(","int",")",":","The","output","dimension",".","dtype","(","str",")",":","Decoder","type",".","dlayers","(","int",")",":","Number","of","layers","for","decoder",".","dunits","(","int",")",":","Dimension","of","input","vector","of","decoder",".","sos","(","int",")",":","Number","to","indicate","the","start","of","sequences",".","eos","(","int",")",":","Number","to","indicate","the","end","of","sequences",".","att","(","Module",")",":","Attention","module","defined","at","`","espnet.espnet.nets.chainer_backend.attentions","`",".","verbose","(","int",")",":","Verbosity","level",".","char_list","(","List","[","str","]",")",":","List","of","all","characters",".","labeldist","(","numpy.array",")",":","Distributed","array","of","counted","transcript","length",".","lsm_weight","(","float",")",":","Weight","to","use","when","calculating","the","training","loss",".","sampling_probability","(","float",")",":","Threshold","for","scheduled","sampling","."]
docstr: Beam search implementation.
  
  Args:
      h (chainer.Variable): One of the output from the encoder.
      lpz (chainer.Variable | None): Result of net propagation.
      recog_args (Namespace): The argument.
      char_list (List[str]): List of all characters.
      rnnlm (Module): RNNLM module. Defined at `espnet.lm.chainer_backend.lm`
  
  Returns:
      List[Dict[str,Any]]: Result of recognition.
docstr_tok: ["Beam","search","implementation",".","Args",":","h","(","chainer.Variable",")",":","One","of","the","output","from","the","encoder",".","lpz","(","chainer.Variable","|","None",")",":","Result","of","net","propagation",".","recog_args","(","Namespace",")",":","The","argument",".","char_list","(","List","[","str","]",")",":","List","of","all","characters",".","rnnlm","(","Module",")",":","RNNLM","module",".","Defined","at","`","espnet.lm.chainer_backend.lm","`","Returns",":","List","[","Dict","[","str",",","Any","]","]",":","Result","of","recognition","."]
code: def recognize_beam(self, h, lpz, recog_args, char_list, rnnlm=None):
      """Beam search implementation.
  
      Args:
          h (chainer.Variable): One of the output from the encoder.
          lpz (chainer.Variable | None): Result of net propagation.
          recog_args (Namespace): The argument.
          char_list (List[str]): List of all characters.
          rnnlm (Module): RNNLM module. Defined at `espnet.lm.chainer_backend.lm`
  
      Returns:
          List[Dict[str,Any]]: Result of recognition.
  
      """
      logging.info("input lengths: " + str(h.shape[0]))
      # initialization
      c_list = [None]  # list of cell state of each layer
      z_list = [None]  # list of hidden state of each layer
      for _ in range(1, self.dlayers):
          c_list.append(None)
          z_list.append(None)
      a = None
      self.att.reset()  # reset pre-computation of h
  
      # search parms
      beam = recog_args.beam_size
      penalty = recog_args.penalty
      ctc_weight = recog_args.ctc_weight
  
      # preprate sos
      y = self.xp.full(1, self.sos, "i")
      if recog_args.maxlenratio == 0:
          maxlen = h.shape[0]
      else:
          # maxlen >= 1
          maxlen = max(1, int(recog_args.maxlenratio * h.shape[0]))
      minlen = int(recog_args.minlenratio * h.shape[0])
      logging.info("max output length: " + str(maxlen))
      logging.info("min output length: " + str(minlen))
  
      # initialize hypothesis
      if rnnlm:
          hyp = {
              "score": 0.0,
              "yseq": [y],
              "c_prev": c_list,
              "z_prev": z_list,
              "a_prev": a,
              "rnnlm_prev": None,
          }
      else:
          hyp = {
              "score": 0.0,
              "yseq": [y],
              "c_prev": c_list,
              "z_prev": z_list,
              "a_prev": a,
          }
      if lpz is not None:
          ctc_prefix_score = CTCPrefixScore(lpz, 0, self.eos, self.xp)
          hyp["ctc_state_prev"] = ctc_prefix_score.initial_state()
          hyp["ctc_score_prev"] = 0.0
          if ctc_weight != 1.0:
              # pre-pruning based on attention scores
              ctc_beam = min(
                  lpz.shape[-1], int(beam * CTC_SCORING_RATIO)
              )
          else:
              ctc_beam = lpz.shape[-1]
      hyps = [hyp]
      ended_hyps = []
  
      for i in range(maxlen):
          logging.debug("position " + str(i))
  
          hyps_best_kept = []
          for hyp in hyps:
              ey = self.embed(hyp["yseq"][i])  # utt list (1) x zdim
              att_c, att_w = self.att(
                  [h], hyp["z_prev"][0], hyp["a_prev"]
              )
              ey = F.hstack((ey, att_c))  # utt(1) x (zdim + hdim)
  
              z_list, c_list = self.rnn_forward(
                  ey, z_list, c_list, hyp["z_prev"], hyp["c_prev"]
              )
  
              # get nbest local scores and their ids
              local_att_scores = F.log_softmax(
                  self.output(z_list[-1])
              ).data
              if rnnlm:
                  rnnlm_state, local_lm_scores = rnnlm.predict(
                      hyp["rnnlm_prev"], hyp["yseq"][i]
                  )
                  local_scores = (
                      local_att_scores
                      + recog_args.lm_weight * local_lm_scores
                  )
              else:
                  local_scores = local_att_scores
  
              if lpz is not None:
                  local_best_ids = self.xp.argsort(
                      local_scores, axis=1
                  )[0, ::-1][:ctc_beam]
                  ctc_scores, ctc_states = ctc_prefix_score(
                      hyp["yseq"], local_best_ids, hyp["ctc_state_prev"]
                  )
                  local_scores = (1.0 - ctc_weight) * local_att_scores[
                      :, local_best_ids
                  ] + ctc_weight * (ctc_scores - hyp["ctc_score_prev"])
                  if rnnlm:
                      local_scores += (
                          recog_args.lm_weight
                          * local_lm_scores[:, local_best_ids]
                      )
                  joint_best_ids = self.xp.argsort(
                      local_scores, axis=1
                  )[0, ::-1][:beam]
                  local_best_scores = local_scores[:, joint_best_ids]
                  local_best_ids = local_best_ids[joint_best_ids]
              else:
                  local_best_ids = self.xp.argsort(
                      local_scores, axis=1
                  )[0, ::-1][:beam]
                  local_best_scores = local_scores[:, local_best_ids]
  
              for j in range(beam):
                  new_hyp = {}
                  # do not copy {z,c}_list directly
                  new_hyp["z_prev"] = z_list[:]
                  new_hyp["c_prev"] = c_list[:]
                  new_hyp["a_prev"] = att_w
                  new_hyp["score"] = (
                      hyp["score"] + local_best_scores[0, j]
                  )
                  new_hyp["yseq"] = [0] * (1 + len(hyp["yseq"]))
                  new_hyp["yseq"][: len(hyp["yseq"])] = hyp["yseq"]
                  new_hyp["yseq"][len(hyp["yseq"])] = self.xp.full(
                      1, local_best_ids[j], "i"
                  )
                  if rnnlm:
                      new_hyp["rnnlm_prev"] = rnnlm_state
                  if lpz is not None:
                      new_hyp["ctc_state_prev"] = ctc_states[
                          joint_best_ids[j]
                      ]
                      new_hyp["ctc_score_prev"] = ctc_scores[
                          joint_best_ids[j]
                      ]
                  # will be (2 x beam) hyps at most
                  hyps_best_kept.append(new_hyp)
  
              hyps_best_kept = sorted(
                  hyps_best_kept, key=lambda x: x["score"], reverse=True
              )[:beam]
  
          # sort and get nbest
          hyps = hyps_best_kept
          logging.debug(
              "number of pruned hypotheses: " + str(len(hyps))
          )
          logging.debug(
              "best hypo: "
              + "".join(
                  [char_list[int(x)] for x in hyps[0]["yseq"][1:]]
              ).replace("<space>", " ")
          )
  
          # add eos in the final loop to avoid that there are no ended hyps
          if i == maxlen - 1:
              logging.info(
                  "adding <eos> in the last position in the loop"
              )
              for hyp in hyps:
                  hyp["yseq"].append(self.xp.full(1, self.eos, "i"))
  
          # add ended hypotheses to a final list,
          # and removed them from current hypotheses
          # (this will be a problem, number of hyps < beam)
          remained_hyps = []
          for hyp in hyps:
              if hyp["yseq"][-1] == self.eos:
                  # only store the sequence that has more than minlen outputs
                  # also add penalty
                  if len(hyp["yseq"]) > minlen:
                      hyp["score"] += (i + 1) * penalty
                      if (
                          rnnlm
                      ):  # Word LM needs to add final <eos> score
                          hyp[
                              "score"
                          ] += recog_args.lm_weight * rnnlm.final(
                              hyp["rnnlm_prev"]
                          )
                      ended_hyps.append(hyp)
              else:
                  remained_hyps.append(hyp)
  
          # end detection
          if (
              end_detect(ended_hyps, i)
              and recog_args.maxlenratio == 0.0
          ):
              logging.info("end detected at %d", i)
              break
  
          hyps = remained_hyps
          if len(hyps) > 0:
              logging.debug("remaining hypotheses: " + str(len(hyps)))
          else:
              logging.info("no hypothesis. Finish decoding.")
              break
  
          for hyp in hyps:
              logging.debug(
                  "hypo: "
                  + "".join(
                      [char_list[int(x)] for x in hyp["yseq"][1:]]
                  ).replace("<space>", " ")
              )
  
          logging.debug(
              "number of ended hypotheses: " + str(len(ended_hyps))
          )
  
      nbest_hyps = sorted(
          ended_hyps, key=lambda x: x["score"], reverse=True
      )[: min(len(ended_hyps), recog_args.nbest)]
  
      # check number of hypotheses
      if len(nbest_hyps) == 0:
          logging.warning(
              "there is no N-best results, "
              "perform recognition again with smaller minlenratio."
          )
          # should copy because Namespace will be overwritten globally
          recog_args = Namespace(**vars(recog_args))
          recog_args.minlenratio = max(
              0.0, recog_args.minlenratio - 0.1
          )
          return self.recognize_beam(
              h, lpz, recog_args, char_list, rnnlm
          )
  
      logging.info(
          "total log probability: " + str(nbest_hyps[0]["score"])
      )
      logging.info(
          "normalized log probability: "
          + str(nbest_hyps[0]["score"] / len(nbest_hyps[0]["yseq"]))
      )
  
      return nbest_hyps
  
file_path: espnet/espnet/nets/chainer_backend/rnn/decoders.py
imports: import logging
  import random
  from argparse import Namespace
  import chainer
  import chainer.functions
  import chainer.links
  import numpy
  import espnet.nets.chainer_backend.deterministic_embed_id
  from espnet.nets.ctc_prefix_score import CTCPrefixScore
  from espnet.nets.e2e_asr_common import end_detect
label_desc: Use image node_id 4 for calling the Decoder obj's underlying member method code with example usage: obj.recognize_beam(h, lpz, recog_args, char_list, rnnlm) and returns: nbest_hyps, self