code: def masked_log_softmax(vector, mask):
      """
      ``torch.nn.functional.log_softmax(vector)`` does not work if some elements of ``vector`` should be
      masked.  This performs a log_softmax on just the non-masked portions of ``vector``.  Passing
      ``None`` in for the mask is also acceptable; you'll just get a regular log_softmax.
  
      We assume that both ``vector`` and ``mask`` (if given) have shape ``(batch_size, vector_dim)``.
  
      In the case that the input vector is completely masked, the return value of this function is
      arbitrary, but not ``nan``.  You should be masking the result of whatever computation comes out
      of this in that case, anyway, so the specific values returned shouldn't matter.  Also, the way
      that we deal with this case relies on having single-precision floats; mixing half-precision
      floats with fully-masked vectors will likely give you ``nans``.
  
      If your logits are all extremely negative (i.e., the max value in your logit vector is -50 or
      lower), the way we handle masking here could mess you up.  But if you've got logit values that
      extreme, you've got bigger problems than this.
      """
      if mask is not None:
          # vector + mask.log() is an easy way to zero out masked elements in logspace, but it
          # results in nans when the whole vector is masked.  We need a very small value instead of a
          # zero in the mask for these cases.  log(1 + 1e-45) is still basically 0, so we can safely
          # just add 1e-45 before calling mask.log().  We use 1e-45 because 1e-46 is so small it
          # becomes 0 - this is just the smallest value we can actually use.
          vector = vector + (mask + 1e-45).log()
      return torch.nn.functional.log_softmax(vector, dim=1)
  
imports: from __future__ import division
  from __future__ import absolute_import
  from collections import defaultdict
  import logging
  import math
  import torch
  from allennlp.common.checks import ConfigurationError
label_desc: Use image node_id 8 for calling a global function with example usage: masked_log_softmax(vector, mask) and returns: torch