class_docstr: Based on the default log_uniform_candidate_sampler in tensorflow.
  
  !!! NOTE
      num_words DOES NOT include padding id.
  
  !!! NOTE
      In all cases except (tie_embeddings=True and use_character_inputs=False)
      the weights are dimensioned as num_words and do not include an entry for the padding (0) id.
      For the (tie_embeddings=True and use_character_inputs=False) case,
      then the embeddings DO include the extra 0 padding, to be consistent with the word embedding layer.
  
  # Parameters
  
  num_words, `int`, required
      The number of words in the vocabulary
  embedding_dim, `int`, required
      The dimension to softmax over
  num_samples, `int`, required
      During training take this many samples. Must be less than num_words.
  sparse, `bool`, optional (default = `False`)
      If this is true, we use a sparse embedding matrix.
  unk_id, `int`, optional (default = `None`)
      If provided, the id that represents unknown characters.
  use_character_inputs, `bool`, optional (default = `True`)
      Whether to use character inputs
  use_fast_sampler, `bool`, optional (default = `False`)
      Whether to use the fast cython sampler.
class_docstr_tok: ["Based","on","the","default","log_uniform_candidate_sampler","in","tensorflow",".","!","!","!","NOTE","num_words","DOES","NOT","include","padding","id",".","!","!","!","NOTE","In","all","cases","except","(","tie_embeddings=True","and","use_character_inputs=False",")","the","weights","are","dimensioned","as","num_words","and","do","not","include","an","entry","for","the","padding","(","0",")","id",".","For","the","(","tie_embeddings=True","and","use_character_inputs=False",")","case",",","then","the","embeddings","DO","include","the","extra","0","padding",",","to","be","consistent","with","the","word","embedding","layer",".","#","Parameters","num_words",",","`","int","`",",","required","The","number","of","words","in","the","vocabulary","embedding_dim",",","`","int","`",",","required","The","dimension","to","softmax","over","num_samples",",","`","int","`",",","required","During","training","take","this","many","samples",".","Must","be","less","than","num_words",".","sparse",",","`","bool","`",",","optional","(","default","=","`","False","`",")","If","this","is","true",",","we","use","a","sparse","embedding","matrix",".","unk_id",",","`","int","`",",","optional","(","default","=","`","None","`",")","If","provided",",","the","id","that","represents","unknown","characters",".","use_character_inputs",",","`","bool","`",",","optional","(","default","=","`","True","`",")","Whether","to","use","character","inputs","use_fast_sampler",",","`","bool","`",",","optional","(","default","=","`","False","`",")","Whether","to","use","the","fast","cython","sampler","."]
code: def _forward_train(
      self,
      embeddings: torch.Tensor,
      targets: torch.Tensor,
      target_token_embedding: torch.Tensor,
  ) -> torch.Tensor:
      # (target_token_embedding is only used in the tie_embeddings case,
      #  which is not implemented)
  
      # want to compute (n, n_samples + 1) array with the log
      # probabilities where the first index is the true target
      # and the remaining ones are the the negative samples.
      # then we can just select the first column
  
      # NOTE: targets input has padding removed (so 0 == the first id, NOT the padding id)
  
      (
          sampled_ids,
          target_expected_count,
          sampled_expected_count,
      ) = self.log_uniform_candidate_sampler(
          targets, choice_func=self.choice_func
      )
  
      long_targets = targets.long()
      long_targets.requires_grad_(False)
  
      # Get the softmax weights (so we can compute logits)
      # shape (batch_size * max_sequence_length + num_samples)
      all_ids = torch.cat([long_targets, sampled_ids], dim=0)
  
      if self.sparse:
          all_ids_1 = all_ids.unsqueeze(1)
          all_w = self.softmax_w(all_ids_1).squeeze(1)
          all_b = self.softmax_b(all_ids_1).squeeze(2).squeeze(1)
      else:
          all_w = torch.nn.functional.embedding(all_ids, self.softmax_w)
          # the unsqueeze / squeeze works around an issue with 1 dim
          # embeddings
          all_b = torch.nn.functional.embedding(
              all_ids, self.softmax_b.unsqueeze(1)
          ).squeeze(1)
  
      batch_size = long_targets.size(0)
      true_w = all_w[:batch_size, :]
      sampled_w = all_w[batch_size:, :]
      true_b = all_b[:batch_size]
      sampled_b = all_b[batch_size:]
  
      # compute the logits and remove log expected counts
      # [batch_size, ]
      true_logits = (
          (true_w * embeddings).sum(dim=1)
          + true_b
          - torch.log(
              target_expected_count
              + util.tiny_value_of_dtype(target_expected_count.dtype)
          )
      )
      # [batch_size, n_samples]
      sampled_logits = (
          torch.matmul(embeddings, sampled_w.t())
          + sampled_b
          - torch.log(
              sampled_expected_count
              + util.tiny_value_of_dtype(sampled_expected_count.dtype)
          )
      )
  
      # remove true labels -- we will take
      # softmax, so set the sampled logits of true values to a large
      # negative number
      # [batch_size, n_samples]
      true_in_sample_mask = sampled_ids == long_targets.unsqueeze(1)
      masked_sampled_logits = sampled_logits.masked_fill(
          true_in_sample_mask, -10000.0
      )
      # now concat the true logits as index 0
      # [batch_size, n_samples + 1]
      logits = torch.cat(
          [true_logits.unsqueeze(1), masked_sampled_logits], dim=1
      )
  
      # finally take log_softmax
      log_softmax = torch.nn.functional.log_softmax(logits, dim=1)
      # true log likelihood is index 0, loss = -1.0 * sum over batch
      # the likelihood loss can become very large if the corresponding
      # true logit is very small, so we apply a per-target cap here
      # so that a single logit for a very rare word won't dominate the batch.
      nll_loss = -1.0 * log_softmax[:, 0].sum()
      return nll_loss
  
file_path: allennlp/allennlp/modules/sampled_softmax_loss.py
imports: from typing import Set, Tuple
  import numpy
  import torch
  from allennlp.common.checks import ConfigurationError
  from allennlp.nn import util
label_desc: Use image node_id 4 for calling the SampledSoftmaxLoss obj's underlying member method code with example usage: obj._forward_train(embeddings, targets, target_token_embedding) and returns: nll_loss