class_docstr: This ``SimpleSeq2Seq`` class is a :class:`Model` which takes a sequence, encodes it, and then
  uses the encoded representations to decode another sequence.  You can use this as the basis for
  a neural machine translation system, an abstractive summarization system, or any other common
  seq2seq problem.  The model here is simple, but should be a decent starting place for
  implementing recent models for these tasks.
  
  This ``SimpleSeq2Seq`` model takes an encoder (:class:`Seq2SeqEncoder`) as an input, and
  implements the functionality of the decoder.  In this implementation, the decoder uses the
  encoder's outputs in two ways. The hidden state of the decoder is initialized with the output
  from the final time-step of the encoder, and when using attention, a weighted average of the
  outputs from the encoder is concatenated to the inputs of the decoder at every timestep.
  
  Parameters
  ----------
  vocab : ``Vocabulary``, required
      Vocabulary containing source and target vocabularies. They may be under the same namespace
      (``tokens``) or the target tokens can have a different namespace, in which case it needs to
      be specified as ``target_namespace``.
  source_embedder : ``TextFieldEmbedder``, required
      Embedder for source side sequences
  encoder : ``Seq2SeqEncoder``, required
      The encoder of the "encoder/decoder" model
  max_decoding_steps : int, required
      Length of decoded sequences
  target_namespace : str, optional (default = 'tokens')
      If the target side vocabulary is different from the source side's, you need to specify the
      target's namespace here. If not, we'll assume it is "tokens", which is also the default
      choice for the source side, and this might cause them to share vocabularies.
  target_embedding_dim : int, optional (default = source_embedding_dim)
      You can specify an embedding dimensionality for the target side. If not, we'll use the same
      value as the source embedder's.
  attention_function: ``SimilarityFunction``, optional (default = None)
      If you want to use attention to get a dynamic summary of the encoder outputs at each step
      of decoding, this is the function used to compute similarity between the decoder hidden
      state and encoder outputs.
  scheduled_sampling_ratio: float, optional (default = 0.0)
      At each timestep during training, we sample a random number between 0 and 1, and if it is
      not less than this value, we use the ground truth labels for the whole batch. Else, we use
      the predictions from the previous time step for the whole batch. If this value is 0.0
      (default), this corresponds to teacher forcing, and if it is 1.0, it corresponds to not
      using target side ground truth labels.  See the following paper for more information:
      Scheduled Sampling for Sequence Prediction with Recurrent Neural Networks. Bengio et al.,
      2015.
class_docstr_tok: ["This","``","SimpleSeq2Seq","``","class","is","a",":","class",":","`","Model","`","which","takes","a","sequence",",","encodes","it",",","and","then","uses","the","encoded","representations","to","decode","another","sequence",".","You","can","use","this","as","the","basis","for","a","neural","machine","translation","system",",","an","abstractive","summarization","system",",","or","any","other","common","seq2seq","problem",".","The","model","here","is","simple",",","but","should","be","a","decent","starting","place","for","implementing","recent","models","for","these","tasks",".","This","``","SimpleSeq2Seq","``","model","takes","an","encoder","(",":","class",":","`","Seq2SeqEncoder","`",")","as","an","input",",","and","implements","the","functionality","of","the","decoder",".","In","this","implementation",",","the","decoder","uses","the","encoder","'s","outputs","in","two","ways",".","The","hidden","state","of","the","decoder","is","initialized","with","the","output","from","the","final","time-step","of","the","encoder",",","and","when","using","attention",",","a","weighted","average","of","the","outputs","from","the","encoder","is","concatenated","to","the","inputs","of","the","decoder","at","every","timestep",".","Parameters","--","--","--","--","--","vocab",":","``","Vocabulary","``",",","required","Vocabulary","containing","source","and","target","vocabularies",".","They","may","be","under","the","same","namespace","(","``","tokens","``",")","or","the","target","tokens","can","have","a","different","namespace",",","in","which","case","it","needs","to","be","specified","as","``","target_namespace","``",".","source_embedder",":","``","TextFieldEmbedder","``",",","required","Embedder","for","source","side","sequences","encoder",":","``","Seq2SeqEncoder","``",",","required","The","encoder","of","the","``","encoder\/decoder","''","model","max_decoding_steps",":","int",",","required","Length","of","decoded","sequences","target_namespace",":","str",",","optional","(","default","=","'tokens","'",")","If","the","target","side","vocabulary","is","different","from","the","source","side","'s",",","you","need","to","specify","the","target","'s","namespace","here",".","If","not",",","we","'ll","assume","it","is","``","tokens","''",",","which","is","also","the","default","choice","for","the","source","side",",","and","this","might","cause","them","to","share","vocabularies",".","target_embedding_dim",":","int",",","optional","(","default","=","source_embedding_dim",")","You","can","specify","an","embedding","dimensionality","for","the","target","side",".","If","not",",","we","'ll","use","the","same","value","as","the","source","embedder","'s",".","attention_function",":","``","SimilarityFunction","``",",","optional","(","default","=","None",")","If","you","want","to","use","attention","to","get","a","dynamic","summary","of","the","encoder","outputs","at","each","step","of","decoding",",","this","is","the","function","used","to","compute","similarity","between","the","decoder","hidden","state","and","encoder","outputs",".","scheduled_sampling_ratio",":","float",",","optional","(","default","=","0.0",")","At","each","timestep","during","training",",","we","sample","a","random","number","between","0","and","1",",","and","if","it","is","not","less","than","this","value",",","we","use","the","ground","truth","labels","for","the","whole","batch",".","Else",",","we","use","the","predictions","from","the","previous","time","step","for","the","whole","batch",".","If","this","value","is","0.0","(","default",")",",","this","corresponds","to","teacher","forcing",",","and","if","it","is","1.0",",","it","corresponds","to","not","using","target","side","ground","truth","labels",".","See","the","following","paper","for","more","information",":","Scheduled","Sampling","for","Sequence","Prediction","with","Recurrent","Neural","Networks",".","Bengio","et","al.",",","2015","."]
docstr: Decoder logic for producing the entire target sequence.
  
  Parameters
  ----------
  source_tokens : Dict[str, torch.LongTensor]
     The output of ``TextField.as_array()`` applied on the source ``TextField``. This will be
     passed through a ``TextFieldEmbedder`` and then through an encoder.
  target_tokens  =  None)
     Output of ``Textfield.as_array()`` applied on target ``TextField``. We assume that the
     target tokens are also represented as a ``TextField``.
docstr_tok: ["Decoder","logic","for","producing","the","entire","target","sequence",".","Parameters","--","--","--","--","--","source_tokens",":","Dict","[","str",",","torch.LongTensor","]","The","output","of","``","TextField.as_array","(",")","``","applied","on","the","source","``","TextField","``",".","This","will","be","passed","through","a","``","TextFieldEmbedder","``","and","then","through","an","encoder",".","target_tokens","=","None",")","Output","of","``","Textfield.as_array","(",")","``","applied","on","target","``","TextField","``",".","We","assume","that","the","target","tokens","are","also","represented","as","a","``","TextField","``","."]
code: def forward(self, source_tokens, target_tokens=None):  # type: ignore
      # pylint: disable=arguments-differ
      """
      Decoder logic for producing the entire target sequence.
  
      Parameters
      ----------
      source_tokens : Dict[str, torch.LongTensor]
         The output of ``TextField.as_array()`` applied on the source ``TextField``. This will be
         passed through a ``TextFieldEmbedder`` and then through an encoder.
      target_tokens  =  None)
         Output of ``Textfield.as_array()`` applied on target ``TextField``. We assume that the
         target tokens are also represented as a ``TextField``.
      """
      # (batch_size, input_sequence_length, encoder_output_dim)
      embedded_input = self._source_embedder(source_tokens)
      batch_size, _, _ = embedded_input.size()
      source_mask = get_text_field_mask(source_tokens)
      encoder_outputs = self._encoder(embedded_input, source_mask)
      final_encoder_output = encoder_outputs[
          :, -1
      ]  # (batch_size, encoder_output_dim)
      if target_tokens:
          targets = target_tokens["tokens"]
          target_sequence_length = targets.size()[1]
          # The last input from the target is either padding or the end symbol. Either way, we
          # don't have to process it.
          num_decoding_steps = target_sequence_length - 1
      else:
          num_decoding_steps = self._max_decoding_steps
      decoder_hidden = final_encoder_output
      decoder_context = encoder_outputs.new_zeros(
          batch_size, self._decoder_output_dim
      )
      last_predictions = None
      step_logits = []
      step_probabilities = []
      step_predictions = []
      for timestep in range(num_decoding_steps):
          if (
              self.training
              and torch.rand(1).item() >= self._scheduled_sampling_ratio
          ):
              input_choices = targets[:, timestep]
          else:
              if timestep == 0:
                  # For the first timestep, when we do not have targets, we input start symbols.
                  # (batch_size,)
                  input_choices = source_mask.new_full(
                      (batch_size,), fill_value=self._start_index
                  )
              else:
                  input_choices = last_predictions
          decoder_input = self._prepare_decode_step_input(
              input_choices,
              decoder_hidden,
              encoder_outputs,
              source_mask,
          )
          decoder_hidden, decoder_context = self._decoder_cell(
              decoder_input, (decoder_hidden, decoder_context)
          )
          # (batch_size, num_classes)
          output_projections = self._output_projection_layer(
              decoder_hidden
          )
          # list of (batch_size, 1, num_classes)
          step_logits.append(output_projections.unsqueeze(1))
          class_probabilities = F.softmax(output_projections, dim=-1)
          _, predicted_classes = torch.max(class_probabilities, 1)
          step_probabilities.append(class_probabilities.unsqueeze(1))
          last_predictions = predicted_classes
          # (batch_size, 1)
          step_predictions.append(last_predictions.unsqueeze(1))
      # step_logits is a list containing tensors of shape (batch_size, 1, num_classes)
      # This is (batch_size, num_decoding_steps, num_classes)
      logits = torch.cat(step_logits, 1)
      class_probabilities = torch.cat(step_probabilities, 1)
      all_predictions = torch.cat(step_predictions, 1)
      output_dict = {
          "logits": logits,
          "class_probabilities": class_probabilities,
          "predictions": all_predictions,
      }
      if target_tokens:
          target_mask = get_text_field_mask(target_tokens)
          loss = self._get_loss(logits, targets, target_mask)
          output_dict["loss"] = loss
          # TODO: Define metrics
      return output_dict
  
imports: from __future__ import absolute_import
  import numpy
  import torch
  from torch.nn.modules.rnn import LSTMCell
  from torch.nn.modules.linear import Linear
  import torch.nn.functional
  from allennlp.common.util import START_SYMBOL, END_SYMBOL
  from allennlp.data.vocabulary import Vocabulary
  from allennlp.modules import TextFieldEmbedder, Seq2SeqEncoder
  from allennlp.modules.attention import LegacyAttention
  from allennlp.modules.similarity_functions import SimilarityFunction
  from allennlp.modules.token_embedders import Embedding
  from allennlp.models.model import Model
  from allennlp.nn.util import get_text_field_mask, sequence_cross_entropy_with_logits, weighted_sum
label_desc: Use image node_id 2 for calling the SimpleSeq2Seq obj's underlying member method code with example usage: obj.forward(source_tokens, target_tokens) and returns: output_dict