code: def sequence_loss(
      flow_preds, flow_gt, valid_flow_mask, gamma=0.8, max_flow=400
  ):
      """Loss function defined over sequence of flow predictions"""
  
      if gamma > 1:
          raise ValueError(f"Gamma should be < 1, got {gamma}.")
  
      # exclude invalid pixels and extremely large diplacements
      flow_norm = torch.sum(flow_gt**2, dim=1).sqrt()
      valid_flow_mask = valid_flow_mask & (flow_norm < max_flow)
  
      valid_flow_mask = valid_flow_mask[:, None, :, :]
  
      flow_preds = torch.stack(
          flow_preds
      )  # shape = (num_flow_updates, batch_size, 2, H, W)
  
      abs_diff = (flow_preds - flow_gt).abs()
      abs_diff = (abs_diff * valid_flow_mask).mean(axis=(1, 2, 3, 4))
  
      num_predictions = flow_preds.shape[0]
      weights = gamma ** torch.arange(num_predictions - 1, -1, -1).to(
          flow_gt.device
      )
      flow_loss = (abs_diff * weights).sum()
  
      return flow_loss
  
imports: import datetime
  import os
  import time
  from collections import defaultdict, deque
  import torch
  import torch.distributed
  import torch.nn.functional
label_desc: Use image node_id 2 for calling a global function with example usage: sequence_loss(flow_preds, flow_gt, valid_flow_mask, gamma, max_flow) and returns: flow_loss