code: def sequence_loss(logits, ground_truth, seq_length=None):
      def sequence_mask(length):
          max_length = logits.size(1)
          batch_size = logits.size(0)
          range_tensor = torch.arange(
              0,
              max_length,
              dtype=seq_length.dtype,
              device=seq_length.device,
          )
          range_tensor = torch.stack([range_tensor] * batch_size, 0)
  
          expanded_length = torch.stack([length] * max_length, -1)
          mask = (range_tensor < expanded_length).float()
          return mask
  
      loss = nn.CrossEntropyLoss(reduction="none")(
          logits.permute((0, 2, 1)), ground_truth
      )
  
      if seq_length is None:
          loss = loss.mean()
      else:
          mask = sequence_mask(seq_length)
          loss = (loss * mask).sum(-1) / seq_length.float()
          loss = loss.mean()
      return loss
  
file_path: dgl/examples/pytorch/ggnn/ggsnn.py
imports: import torch
  import torch.nn.functional
  from dgl.nn.pytorch import GatedGraphConv, GlobalAttentionPooling
  from torch import nn
label_desc: Use image node_id 1 for calling a global function with example usage: sequence_loss(logits, ground_truth, seq_length) and returns: loss, mask