class_docstr: Draw samples from the decoder by beam search.
  
  Parameters
  ----------
  beam_size : int
      The beam size.
  decoder
      Function of the one-step-ahead decoder, should inherit SequenceDecoder and
      have the form::
  
          outputs, new_states = decoder(step_input, states)
  
      The outputs, input should follow these rules:
  
      - step_input has shape (batch_size,),
      - outputs has shape (batch_size, V),
      - states and new_states have the same structure.
  eos_id
      Id of the EOS token. No other elements will be appended to the sample if it reaches eos_id,
      if eos_id == None, beam will not die
  scorer : BeamSearchScorer, default BeamSearchScorer(alpha=1.0, K=5)
      The score function used in beam search.
  max_length_a
      The `a` value in the formula `a * x + b`. Generate sequences of maximum length `a * x + b`,
      where `x` is the maximum source length.
  max_length_b
      The b value in the formula `a * x + b`. Generate sequences of maximum length `a * x + b`,
      where `x` is the maximum source length.
  min_length
      The minimum length of the generated sequences.
  temperature
      The temperature.
  stochastic
      Whether to use stochastic beam search,
      see [ICML2019] "Stochastic Beams and Where to Find Them" for detail.
      https://arxiv.org/abs/1903.06059
  sampling
      Whether to use multinomial sampler.
  sampling_topp
      Multinomial sampling with topp,
      see [ICLR2020] "The Curious Case of Neural Text Degeneration"
      https://arxiv.org/abs/1904.09751
  sampling_topk
      Multinomial sampling with topk,
      see [ACL2018] "Hierarchical Neural Story Generation"
      https://www.aclweb.org/anthology/P18-1082.pdf
  early_return
      Whether to return when all beams are dead.
      Without early_return, the sequences will be generated until the
      maximum length is reached.
class_docstr_tok: ["Draw","samples","from","the","decoder","by","beam","search",".","Parameters","--","--","--","--","--","beam_size",":","int","The","beam","size",".","decoder","Function","of","the","one-step-ahead","decoder",",","should","inherit","SequenceDecoder","and","have","the","form",":",":","outputs",",","new_states","=","decoder","(","step_input",",","states",")","The","outputs",",","input","should","follow","these","rules",":","-","step_input","has","shape","(","batch_size",",",")",",","-","outputs","has","shape","(","batch_size",",","V",")",",","-","states","and","new_states","have","the","same","structure",".","eos_id","Id","of","the","EOS","token",".","No","other","elements","will","be","appended","to","the","sample","if","it","reaches","eos_id",",","if","eos_id","==","None",",","beam","will","not","die","scorer",":","BeamSearchScorer",",","default","BeamSearchScorer","(","alpha=1.0",",","K=5",")","The","score","function","used","in","beam","search",".","max_length_a","The","`","a","`","value","in","the","formula","`","a","*","x","+","b","`",".","Generate","sequences","of","maximum","length","`","a","*","x","+","b","`",",","where","`","x","`","is","the","maximum","source","length",".","max_length_b","The","b","value","in","the","formula","`","a","*","x","+","b","`",".","Generate","sequences","of","maximum","length","`","a","*","x","+","b","`",",","where","`","x","`","is","the","maximum","source","length",".","min_length","The","minimum","length","of","the","generated","sequences",".","temperature","The","temperature",".","stochastic","Whether","to","use","stochastic","beam","search",",","see","[","ICML2019","]","``","Stochastic","Beams","and","Where","to","Find","Them","''","for","detail",".","https",":","\/\/arxiv.org\/abs\/1903.06059","sampling","Whether","to","use","multinomial","sampler",".","sampling_topp","Multinomial","sampling","with","topp",",","see","[","ICLR2020","]","``","The","Curious","Case","of","Neural","Text","Degeneration","''","https",":","\/\/arxiv.org\/abs\/1904.09751","sampling_topk","Multinomial","sampling","with","topk",",","see","[","ACL2018","]","``","Hierarchical","Neural","Story","Generation","''","https",":","\/\/www.aclweb.org\/anthology\/P18-1082.pdf","early_return","Whether","to","return","when","all","beams","are","dead",".","Without","early_return",",","the","sequences","will","be","generated","until","the","maximum","length","is","reached","."]
code: def __init__(
      self,
      beam_size: int,
      decoder: BaseStepDecoder,
      eos_id: Optional[int],
      vocab_size: int,
      scorer: Optional[Callable] = None,
      max_length_a: int = 0,
      max_length_b: int = 200,
      min_length: int = 1,
      temperature: float = 1.0,
      stochastic: bool = False,
      sampling: bool = False,
      sampling_topp: float = -1.0,
      sampling_topk: int = -1,
      early_return: bool = True,
  ):
      super().__init__()
      self._beam_size = beam_size
      self._vocab_size = vocab_size
      assert (
          beam_size > 0
      ), "beam_size must be larger than 0. Received beam_size={}".format(
          beam_size
      )
      self._decoder = decoder
      self._eos_id = eos_id
      assert (
          eos_id is None or eos_id >= 0
      ), "eos_id cannot be negative! Received eos_id={}".format(eos_id)
      self._max_length_a = max_length_a
      self._max_length_b = max_length_b
      self._min_length = min_length
      if scorer is None:
          if stochastic:
              scorer = BeamSearchScorer(
                  alpha=0.0, temperature=temperature
              )
          else:
              scorer = BeamSearchScorer(
                  alpha=1.0, K=5, temperature=temperature
              )
  
      self._scorer = scorer
      self._data_batch_axis = decoder.data_batch_axis
      self._state_batch_axis = decoder.state_batch_axis
      self._sampling = sampling
      self._sampling_topp = sampling_topp
      self._sampling_topk = sampling_topk
      self._early_return = early_return
      self._stochastic = stochastic
      if stochastic:
          self._updater = _BeamSearchStepUpdate(
              beam_size=beam_size,
              vocab_size=vocab_size,
              eos_id=eos_id,
              scorer=scorer,
              state_batch_axis=decoder.state_batch_axis,
              stochastic=True,
          )
      else:
          if sampling:
              self._updater = _MultinomialStepUpdate(
                  beam_size=beam_size,
                  vocab_size=vocab_size,
                  eos_id=eos_id,
                  state_batch_axis=decoder.state_batch_axis,
                  sampling_topp=sampling_topp,
                  sampling_topk=sampling_topk,
                  temperature=temperature,
              )
          else:
              self._updater = _BeamSearchStepUpdate(
                  beam_size=beam_size,
                  vocab_size=vocab_size,
                  eos_id=eos_id,
                  scorer=scorer,
                  state_batch_axis=decoder.state_batch_axis,
                  stochastic=False,
              )
  
      if not stochastic:
          self._updater.hybridize()
      else:
          if isinstance(scorer, BeamSearchScorer):
              if scorer._alpha != 0.0:
                  warnings.warn(
                      "To use stochastic beam search, we need to set the alpha as 0.0"
                  )
  
imports: import warnings
  import mxnet
  import abc
  from mxnet.gluon import HybridBlock
  from typing import Callable, Optional
  from .layers import get_activation
label_desc: Use image node_id 1 to create a new BeamSearchSampler object from inherited base classes: HybridBlock with example: obj = BeamSearchSampler(beam_size, decoder, eos_id, vocab_size, scorer, max_length_a, max_length_b, min_length, temperature, stochastic, sampling, sampling_topp, sampling_topk, early_return)