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: Takes logits (unnormalized outputs from the decoder) of size (batch_size,
  num_decoding_steps, num_classes), target indices of size (batch_size, num_decoding_steps+1)
  and corresponding masks of size (batch_size, num_decoding_steps+1) steps and computes cross
  entropy loss while taking the mask into account.
  
  The length of ``targets`` is expected to be greater than that of ``logits`` because the
  decoder does not need to compute the output corresponding to the last timestep of
  ``targets``. This method aligns the inputs appropriately to compute the loss.
  
  During training, we want the logit corresponding to timestep i to be similar to the target
  token from timestep i + 1. That is, the targets should be shifted by one timestep for
  appropriate comparison.  Consider a single example where the target has 3 words, and
  padding is to 7 tokens.
     The complete sequence would correspond to <S> w1  w2  w3  <E> <P> <P>
     and the mask would be                     1   1   1   1   1   0   0
     and let the logits be                     l1  l2  l3  l4  l5  l6
  We actually need to compare:
     the sequence           w1  w2  w3  <E> <P> <P>
     with masks             1   1   1   1   0   0
     against                l1  l2  l3  l4  l5  l6
     (where the input was)  <S> w1  w2  w3  <E> <P>
docstr_tok: ["Takes","logits","(","unnormalized","outputs","from","the","decoder",")","of","size","(","batch_size",",","num_decoding_steps",",","num_classes",")",",","target","indices","of","size","(","batch_size",",","num_decoding_steps+1",")","and","corresponding","masks","of","size","(","batch_size",",","num_decoding_steps+1",")","steps","and","computes","cross","entropy","loss","while","taking","the","mask","into","account",".","The","length","of","``","targets","``","is","expected","to","be","greater","than","that","of","``","logits","``","because","the","decoder","does","not","need","to","compute","the","output","corresponding","to","the","last","timestep","of","``","targets","``",".","This","method","aligns","the","inputs","appropriately","to","compute","the","loss",".","During","training",",","we","want","the","logit","corresponding","to","timestep","i","to","be","similar","to","the","target","token","from","timestep","i","+","1",".","That","is",",","the","targets","should","be","shifted","by","one","timestep","for","appropriate","comparison",".","Consider","a","single","example","where","the","target","has","3","words",",","and","padding","is","to","7","tokens",".","The","complete","sequence","would","correspond","to","<","S",">","w1","w2","w3","<","E",">","<","P",">","<","P",">","and","the","mask","would","be","1","1","1","1","1","0","0","and","let","the","logits","be","l1","l2","l3","l4","l5","l6","We","actually","need","to","compare",":","the","sequence","w1","w2","w3","<","E",">","<","P",">","<","P",">","with","masks","1","1","1","1","0","0","against","l1","l2","l3","l4","l5","l6","(","where","the","input","was",")","<","S",">","w1","w2","w3","<","E",">","<","P",">"]
code: def _get_loss(logits, targets, target_mask):
      """
      Takes logits (unnormalized outputs from the decoder) of size (batch_size,
      num_decoding_steps, num_classes), target indices of size (batch_size, num_decoding_steps+1)
      and corresponding masks of size (batch_size, num_decoding_steps+1) steps and computes cross
      entropy loss while taking the mask into account.
  
      The length of ``targets`` is expected to be greater than that of ``logits`` because the
      decoder does not need to compute the output corresponding to the last timestep of
      ``targets``. This method aligns the inputs appropriately to compute the loss.
  
      During training, we want the logit corresponding to timestep i to be similar to the target
      token from timestep i + 1. That is, the targets should be shifted by one timestep for
      appropriate comparison.  Consider a single example where the target has 3 words, and
      padding is to 7 tokens.
         The complete sequence would correspond to <S> w1  w2  w3  <E> <P> <P>
         and the mask would be                     1   1   1   1   1   0   0
         and let the logits be                     l1  l2  l3  l4  l5  l6
      We actually need to compare:
         the sequence           w1  w2  w3  <E> <P> <P>
         with masks             1   1   1   1   0   0
         against                l1  l2  l3  l4  l5  l6
         (where the input was)  <S> w1  w2  w3  <E> <P>
      """
      relevant_targets = targets[
          :, 1:
      ].contiguous()  # (batch_size, num_decoding_steps)
      relevant_mask = target_mask[
          :, 1:
      ].contiguous()  # (batch_size, num_decoding_steps)
      loss = sequence_cross_entropy_with_logits(
          logits, relevant_targets, relevant_mask
      )
      return loss
  
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 4 for calling the SimpleSeq2Seq obj's underlying member method code with example usage: obj._get_loss(logits, targets, target_mask) and returns: loss