code: def _get_loss_function(
      loss_fn: LossFunction,
      reward_ignore_threshold: Optional[float],
      weighted_by_inverse_propensity: bool,
  ):
      reduction_type = "none"
  
      if loss_fn == LossFunction.MSE:
          torch_fn = torch.nn.MSELoss(reduction=reduction_type)
      elif loss_fn == LossFunction.SmoothL1Loss:
          torch_fn = torch.nn.SmoothL1Loss(reduction=reduction_type)
      elif loss_fn == LossFunction.L1Loss:
          torch_fn = torch.nn.L1Loss(reduction=reduction_type)
      elif loss_fn == LossFunction.BCELoss:
          torch_fn = torch.nn.BCELoss(reduction=reduction_type)
  
      def wrapper_loss_fn(pred, target, weight, batch):
          if loss_fn == LossFunction.BCELoss:
              valid_step = batch.valid_step
              assert valid_step is not None
              pred = pred / valid_step
              target = target / valid_step
  
          loss = torch_fn(pred, target)
  
          if weighted_by_inverse_propensity:
              assert weight.shape == loss.shape
              loss = loss * weight
  
          # ignore abnormal reward only during training
          if pred.requires_grad and reward_ignore_threshold is not None:
              loss = loss[target <= reward_ignore_threshold]
              assert len(loss) > 0, (
                  f"reward ignore threshold set too small. target={target}, "
                  f"threshold={reward_ignore_threshold}"
              )
  
          return torch.mean(loss)
  
      return wrapper_loss_fn
  
imports: import logging
  from enum import Enum
  from typing import Optional
  import numpy
  import reagent.core.types
  import torch
  from reagent.core.dataclasses import field
  from reagent.models.base import ModelBase
  from reagent.optimizer.union import Optimizer__Union
  from reagent.training.reagent_lightning_module import ReAgentLightningModule
label_desc: Use image node_id 1 for calling a global function with example usage: _get_loss_function(loss_fn, reward_ignore_threshold, weighted_by_inverse_propensity) and returns: wrapper_loss_fn, torch