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 __init__(
      self,
      max_decoder_step,
      input_size,
      hidden_size,  # decoder config
      word_emb,
      vocab: Vocab,  # word embedding & vocabulary TODO: add our vocabulary when building pipeline
      rnn_type="lstm",
      graph_pooling_strategy=None,  # RNN config
      use_attention=True,
      attention_type="uniform",
      rnn_emb_input_size=None,  # attention config
      attention_function="mlp",
      node_type_num=None,
      fuse_strategy="average",
      use_copy=False,
      use_coverage=False,
      coverage_strategy="sum",
      tgt_emb_as_output_layer=False,  # share label projection with word embedding
      dropout=0.3,
  ):
      super(StdRNNDecoder, self).__init__(
          use_attention=use_attention,
          use_copy=use_copy,
          use_coverage=use_coverage,
          attention_type=attention_type,
          fuse_strategy=fuse_strategy,
      )
      self.max_decoder_step = max_decoder_step
      self.word_emb_size = word_emb.embedding_dim
      self.decoder_input_size = input_size
      self.graph_pooling_strategy = graph_pooling_strategy
      self.dropout = nn.Dropout(p=dropout)
      self.tgt_emb = word_emb
      self.tgt_emb_as_output_layer = tgt_emb_as_output_layer
      if self.tgt_emb_as_output_layer:
          self.input_feed_size = self.word_emb_size
      else:
          self.input_feed_size = hidden_size
  
      self.rnn = self._build_rnn(
          rnn_type=rnn_type,
          input_size=self.word_emb_size + self.input_feed_size,
          hidden_size=hidden_size,
      )
      self.decoder_hidden_size = hidden_size
      self.rnn_type = rnn_type
      self.num_layers = 1
      self.out_logits_size = self.decoder_hidden_size
  
      # attention builder
      if self.use_attention:
          if self.rnn_type == "lstm":
              query_size = 2 * self.decoder_hidden_size
          elif self.rnn_type == "gru":
              query_size = self.decoder_hidden_size
          else:
              raise NotImplementedError()
          if attention_type == "uniform":
              self.enc_attention = Attention(
                  hidden_size=self.decoder_hidden_size,
                  query_size=query_size,
                  memory_size=input_size,
                  has_bias=True,
                  attention_funtion=attention_function,
              )
              self.out_logits_size += self.decoder_input_size
          elif attention_type == "sep_diff_encoder_type":
              assert isinstance(rnn_emb_input_size, int)
              self.rnn_emb_input_size = rnn_emb_input_size
              self.enc_attention = Attention(
                  hidden_size=self.decoder_hidden_size,
                  query_size=query_size,
                  memory_size=input_size,
                  has_bias=True,
                  attention_funtion=attention_function,
              )
              self.out_logits_size += self.decoder_input_size
              self.rnn_attention = Attention(
                  hidden_size=self.decoder_hidden_size,
                  query_size=query_size,
                  memory_size=rnn_emb_input_size,
                  has_bias=True,
                  attention_funtion=attention_function,
              )
              if self.fuse_strategy == "concatenate":
                  self.out_logits_size += self.rnn_emb_input_size
              else:
                  if rnn_emb_input_size != input_size:
                      raise ValueError(
                          "input RNN embedding size is not equal to graph embedding size"
                      )
          elif attention_type == "sep_diff_node_type":
              assert node_type_num >= 1
              attn_modules = [
                  Attention(
                      hidden_size=self.decoder_hidden_size,
                      query_size=query_size,
                      memory_size=input_size,
                      has_bias=True,
                      attention_funtion=attention_function,
                  )
                  for _ in range(node_type_num)
              ]
              self.node_type_num = node_type_num
              self.attn_modules = nn.ModuleList(attn_modules)
              if self.fuse_strategy == "concatenate":
                  self.out_logits_size += (
                      self.decoder_input_size * self.node_type_num
                  )
              elif self.fuse_strategy == "average":
                  self.out_logits_size += self.decoder_input_size
              else:
                  raise NotImplementedError()
          else:
              raise NotImplementedError()
  
      self.attention_type = attention_type
  
      if self.rnn_type == "lstm":
          self.encoder_decoder_adapter = nn.ModuleList(
              [
                  nn.Linear(
                      self.decoder_input_size, self.decoder_hidden_size
                  )
                  for _ in range(2)
              ]
          )
      elif self.rnn_type == "gru":
          self.encoder_decoder_adapter = nn.Linear(
              self.decoder_input_size, self.decoder_hidden_size
          )
      else:
          raise NotImplementedError()
  
      # input_feed
  
      self.memory_to_feed = nn.Sequential(
          nn.Linear(self.out_logits_size, self.input_feed_size),
          nn.ReLU(),
          nn.Dropout(dropout),
      )
  
      # project logits to labels
      # self.tgt_emb_as_output_layer = tgt_emb_as_output_layer
      if self.tgt_emb_as_output_layer:  # use pre_out layer
          self.out_embed_size = self.word_emb_size
          self.pre_out = nn.Linear(
              self.input_feed_size, self.out_embed_size
          )
          size_before_output = self.out_embed_size
      else:  # don't use pre_out layer
          size_before_output = self.input_feed_size
  
      # size_before_output = self.input_feed_size
      self.vocab = vocab
      vocab_size = len(vocab)
      self.vocab_size = vocab_size
      self.out_project = nn.Linear(
          size_before_output, vocab_size, bias=False
      )
      if self.tgt_emb_as_output_layer:
          # self.out_project.weight = self.tgt_emb.word_emb_layer.weight
          self.out_project.weight = self.tgt_emb.weight
  
      # coverage strategy
      if self.use_coverage:
          if not self.use_attention:
              raise ValueError(
                  "You should use attention when you use coverage strategy."
              )
  
          self.coverage_strategy = coverage_strategy
  
          self.coverage_weight = torch.Tensor(
              1, 1, self.decoder_hidden_size
          )
          self.coverage_weight = nn.Parameter(
              nn.init.xavier_uniform_(self.coverage_weight)
          )
  
      # copy: pointer network
      if self.use_copy:
          ptr_size = self.input_feed_size + self.word_emb_size
          if self.rnn_type == "lstm":
              ptr_size += self.decoder_hidden_size * 2
          elif self.rnn_type == "gru":
              ptr_size += self.decoder_hidden_size
          else:
              raise NotImplementedError()
          if self.use_attention:
              if self.attention_type == "uniform":
                  ptr_size += input_size
              elif self.attention_type == "sep_diff_encoder_type":
                  ptr_size += input_size + rnn_emb_input_size
              elif self.attention_type == "sep_diff_node_type":
                  ptr_size += input_size * node_type_num
          self.ptr = nn.Linear(ptr_size, 1)
  
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 1 to create a new StdRNNDecoder object from inherited base classes: RNNDecoderBase with example: obj = StdRNNDecoder(max_decoder_step, input_size, hidden_size, word_emb, vocab, rnn_type, graph_pooling_strategy, use_attention, attention_type, rnn_emb_input_size, attention_function, node_type_num, fuse_strategy, use_copy, use_coverage, coverage_strategy, tgt_emb_as_output_layer, dropout)