code: def masked_softmax(vector, mask):
      """
      ``torch.nn.functional.softmax(vector)`` does not work if some elements of ``vector`` should be
      masked.  This performs a softmax on just the non-masked portions of ``vector``.  Passing
      ``None`` in for the mask is also acceptable; you'll just get a regular 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, this function returns an array
      of ``0.0``. This behavior may cause ``NaN`` if this is used as the last layer of a model
      that uses categorical cross-entropy loss.
      """
      if mask is None:
          result = torch.nn.functional.softmax(vector, dim=-1)
      else:
          # To limit numerical errors from large vector elements outside the mask, we zero these out.
          result = torch.nn.functional.softmax(vector * mask, dim=-1)
          result = result * mask
          result = result / (result.sum(dim=1, keepdim=True) + 1e-13)
      return result
  
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 7 for calling a global function with example usage: masked_softmax(vector, mask) and returns: result