class_docstr:     The standard rnn for sequence decoder.
  Parameters
  ----------
  max_decoder_step: int
      The maximal decoding step.
  input_size: int
      The dimension for standard rnn decoder's input.
  hidden_size: int
      The dimension for standard rnn decoder's hidden representation during calculation.
  word_emb: torch.nn.Embedding
      The target's embedding matrix.
  vocab: Any
      The target's vocabulary
  rnn_type: str, option=["lstm", "gru"], default="lstm"
      The rnn's type. We support ``lstm`` and ``gru`` here.
  use_attention: bool, default=True
      Whether use attention during decoding.
  attention_type: str, option=["uniform", "sep_diff_encoder_type", sep_diff_node_type], default="uniform" # noqa
      The attention strategy choice.
      "``uniform``": uniform attention. We will attend on the nodes uniformly.
      "``sep_diff_encoder_type``": separate attention.
          We will attend on graph encoder and rnn encoder's results separately.
      "``sep_diff_node_type``": separate attention.
          We will attend on different node type separately.
  
  attention_function: str, option=["general", "mlp"], default="mlp"
      Different attention function.
  node_type_num: int, default=None
      When we choose "``sep_diff_node_type``", we must set this parameter.
      This parameter indicate the the amount of node type.
  fuse_strategy: str, option=["average", "concatenate"], default=average
      The strategy to fuse attention results generated by separate attention.
      "``average``": We will take an average on all results.
      "``concatenate``": We will concatenate all results to one.
  use_copy: bool, default=False
      Whether use ``copy`` mechanism. See pointer network. Note that you must use attention first.
  use_coverage: bool, default=False
      Whether use ``coverage`` mechanism. Note that you must use attention first.
  coverage_strategy: str, option=["sum", "max"], default="sum"
      The coverage strategy when calculating the coverage vector.
  tgt_emb_as_output_layer: bool, default=False
      When this option is set ``True``, the output projection layer(It is used to project RNN encoded # noqa
      representation to target sequence)'s weight will be shared with the target vocabulary's embedding. # noqa
  dropout: float, default=0.3
class_docstr_tok: ["The","standard","rnn","for","sequence","decoder",".","Parameters","--","--","--","--","--","max_decoder_step",":","int","The","maximal","decoding","step",".","input_size",":","int","The","dimension","for","standard","rnn","decoder","'s","input",".","hidden_size",":","int","The","dimension","for","standard","rnn","decoder","'s","hidden","representation","during","calculation",".","word_emb",":","torch.nn.Embedding","The","target","'s","embedding","matrix",".","vocab",":","Any","The","target","'s","vocabulary","rnn_type",":","str",",","option=","[","``","lstm","''",",","``","gru","''","]",",","default=","''","lstm","''","The","rnn","'s","type",".","We","support","``","lstm","``","and","``","gru","``","here",".","use_attention",":","bool",",","default=True","Whether","use","attention","during","decoding",".","attention_type",":","str",",","option=","[","``","uniform","''",",","``","sep_diff_encoder_type","''",",","sep_diff_node_type","]",",","default=","''","uniform","''","#","noqa","The","attention","strategy","choice",".","``","``","uniform","``","``",":","uniform","attention",".","We","will","attend","on","the","nodes","uniformly",".","``","``","sep_diff_encoder_type","``","``",":","separate","attention",".","We","will","attend","on","graph","encoder","and","rnn","encoder","'s","results","separately",".","``","``","sep_diff_node_type","``","``",":","separate","attention",".","We","will","attend","on","different","node","type","separately",".","attention_function",":","str",",","option=","[","``","general","''",",","``","mlp","''","]",",","default=","''","mlp","''","Different","attention","function",".","node_type_num",":","int",",","default=None","When","we","choose","``","``","sep_diff_node_type","``","``",",","we","must","set","this","parameter",".","This","parameter","indicate","the","the","amount","of","node","type",".","fuse_strategy",":","str",",","option=","[","``","average","''",",","``","concatenate","''","]",",","default=average","The","strategy","to","fuse","attention","results","generated","by","separate","attention",".","``","``","average","``","``",":","We","will","take","an","average","on","all","results",".","``","``","concatenate","``","``",":","We","will","concatenate","all","results","to","one",".","use_copy",":","bool",",","default=False","Whether","use","``","copy","``","mechanism",".","See","pointer","network",".","Note","that","you","must","use","attention","first",".","use_coverage",":","bool",",","default=False","Whether","use","``","coverage","``","mechanism",".","Note","that","you","must","use","attention","first",".","coverage_strategy",":","str",",","option=","[","``","sum","''",",","``","max","''","]",",","default=","''","sum","''","The","coverage","strategy","when","calculating","the","coverage","vector",".","tgt_emb_as_output_layer",":","bool",",","default=False","When","this","option","is","set","``","True","``",",","the","output","projection","layer","(","It","is","used","to","project","RNN","encoded","#","noqa","representation","to","target","sequence",")","'s","weight","will","be","shared","with","the","target","vocabulary","'s","embedding",".","#","noqa","dropout",":","float",",","default=0.3"]
code: def decode_step(
      self,
      decoder_input,
      input_feed,
      rnn_state,
      encoder_out,
      dec_input_mask,
      rnn_emb=None,
      enc_attn_weights_average=None,
      src_seq=None,
      oov_dict=None,
  ):
      batch_size = decoder_input.shape[0]
      dec_emb = self.tgt_emb(decoder_input)
      dec_emb = torch.cat((dec_emb, input_feed), dim=1)
      dec_emb = self.dropout(dec_emb)
      if self.use_coverage and enc_attn_weights_average:
          coverage_vec = self.coverage_function(
              enc_attn_weights_average
          )
      else:
          coverage_vec = None
  
      dec_out, rnn_state = self.rnn(dec_emb.unsqueeze(0), rnn_state)
      dec_out = dec_out.squeeze(0)
  
      if self.rnn_type == "lstm":
          rnn_state = tuple([self.dropout(x) for x in rnn_state])
          hidden = torch.cat(rnn_state, -1).squeeze(0)
      elif self.rnn_type == "gru":
          rnn_state = self.dropout(rnn_state)
          hidden = rnn_state.squeeze(0)
      else:
          raise NotImplementedError()
      attn_collect = []
      score_collect = []
  
      if self.use_attention:
          if self.use_coverage and coverage_vec is not None:
              coverage_repr = coverage_vec
          else:
              coverage_repr = None
          if coverage_repr is not None:
              coverage_repr = (
                  coverage_repr.unsqueeze(-1) * self.coverage_weight
              )
          if (
              self.attention_type == "uniform"
              or self.attention_type == "sep_diff_encoder_type"
          ):
              enc_mask = extract_mask(dec_input_mask, token=-1)
              enc_mask = 1 - enc_mask
  
              attn_res, scores = self.enc_attention(
                  query=hidden,
                  memory=encoder_out,
                  memory_mask=enc_mask,
                  coverage=coverage_repr,
              )
              attn_collect.append(attn_res)
              score_collect.append(scores)
              if self.attention_type == "sep_diff_encoder_type":
                  rnn_attn_res, rnn_scores = self.rnn_attention(
                      query=hidden,
                      memory=rnn_emb,
                      memory_mask=enc_mask,
                      coverage=coverage_repr,
                  )
                  score_collect.append(rnn_scores)
                  attn_collect.append(rnn_attn_res)
          elif self.attention_type == "sep_diff_node_type":
              for i in range(self.node_type_num):
                  node_mask = extract_mask(dec_input_mask, token=i)
                  attn, scores = self.attn_modules[i](
                      query=hidden,
                      memory=encoder_out,
                      memory_mask=node_mask,
                      coverage=coverage_repr,
                  )
                  attn_collect.append(attn)
                  score_collect.append(scores)
  
      if self.use_attention:
          if self.attention_type == "uniform":
              assert len(attn_collect) == 1
              assert len(score_collect) == 1
              attn_total = attn_collect[0]
          elif (
              self.attention_type == "sep_diff_encoder_type"
              or self.attention_type == "sep_diff_node_type"
          ):
              if self.fuse_strategy == "average":
                  attn_total = reduce(
                      lambda x, y: x + y, attn_collect
                  ) / len(attn_collect)
              elif self.fuse_strategy == "concatenate":
                  attn_total = torch.cat(attn_collect, dim=-1)
              else:
                  raise NotImplementedError()
          else:
              raise NotImplementedError()
  
          decoder_output = torch.cat((dec_out, attn_total), dim=-1)
          dec_attn_scores = reduce(
              lambda x, y: x + y, score_collect
          ) / len(score_collect)
      else:
          decoder_output = dec_out
  
      out = self.memory_to_feed(decoder_output)
  
      # project
      if self.tgt_emb_as_output_layer:
          out_embed = torch.tanh(self.pre_out(out))
          # out_embed = torch.tanh(out)
          out_embed = self.dropout(out_embed)
      else:
          out_embed = out
  
      decoder_output = self.out_project(out_embed)  # [B, S, Vocab]
  
      if self.use_copy:
          assert src_seq is not None
          assert oov_dict is not None
          # output = torch.zeros(batch_size, oov_dict.get_vocab_size()).to(decoder_input.device)
          attn_ptr = torch.cat(attn_collect, dim=-1)
          pgen_collect = [dec_emb, hidden, attn_ptr]
  
          prob_ptr = torch.sigmoid(
              self.ptr(torch.cat(pgen_collect, -1))
          )
          prob_gen = 1 - prob_ptr
          gen_output = torch.softmax(decoder_output, dim=-1)
  
          ret = prob_gen * gen_output
          need_pad_length = (
              oov_dict.get_vocab_size() - self.vocab.get_vocab_size()
          )
          output = torch.cat(
              (ret, ret.new_zeros((batch_size, need_pad_length))), dim=1
          )
          # output[:, :self.vocab.get_vocab_size()] = ret
  
          ptr_output = dec_attn_scores
  
          output.scatter_add_(1, src_seq, prob_ptr * ptr_output)
          decoder_output = output
      else:
          decoder_output = torch.softmax(decoder_output, dim=-1)
  
      return (
          decoder_output,
          out,
          rnn_state,
          dec_attn_scores,
          coverage_vec,
      )
  
file_path: graph4nlp/graph4nlp/pytorch/modules/prediction/generation/StdRNNDecoder.py
imports: import random
  from functools import reduce
  import torch
  import torch.nn
  from graph4nlp.pytorch.modules.prediction.generation.attention import Attention
  from graph4nlp.pytorch.modules.prediction.generation.base import RNNDecoderBase
  from graph4nlp.pytorch.modules.utils.vocab_utils import Vocab
label_desc: Use image node_id 5 for calling the StdRNNDecoder obj's underlying member method code with example usage: obj.decode_step(decoder_input, input_feed, rnn_state, encoder_out, dec_input_mask, rnn_emb, enc_attn_weights_average, src_seq, oov_dict) and returns: decoder_output,  out,  rnn_state,  dec_attn_scores,  coverage_vec