code: def _get_loss_for_volatility_based_calibration(
      *,
      prices,
      strikes,
      expiries,
      forwards,
      is_call_options,
      volatility_type,
      approximation_type,
      dtype,
      optimizer_arg_handler,
  ):
      """Creates a loss function to be used in volatility-based calibration."""
      if volatility_type == SabrImpliedVolatilityType.LOGNORMAL:
          underlying_distribution = UnderlyingDistribution.LOG_NORMAL
      elif volatility_type == SabrImpliedVolatilityType.NORMAL:
          underlying_distribution = UnderlyingDistribution.NORMAL
      else:
          raise ValueError("Unsupported `volatility_type`!")
      target_implied_vol = black_scholes.implied_vol(
          prices=prices,
          strikes=strikes,
          expiries=expiries,
          forwards=forwards,
          is_call_options=is_call_options,
          underlying_distribution=underlying_distribution,
      )
  
      @make_val_and_grad_fn
      def loss_function(x):
          """Loss function for vol-based optimization."""
          candidate_alpha = optimizer_arg_handler.get_alpha(x)
          candidate_volvol = optimizer_arg_handler.get_volvol(x)
          candidate_rho = optimizer_arg_handler.get_rho(x)
          candidate_beta = optimizer_arg_handler.get_beta(x)
  
          implied_vol = approximations.implied_volatility(
              strikes=strikes,
              expiries=expiries,
              forwards=forwards,
              alpha=tf.expand_dims(candidate_alpha, axis=-1),
              beta=tf.expand_dims(candidate_beta, axis=-1),
              volvol=tf.expand_dims(candidate_volvol, axis=-1),
              rho=tf.expand_dims(candidate_rho, axis=-1),
              volatility_type=volatility_type,
              approximation_type=approximation_type,
              dtype=dtype,
          )
          # shape `batch_shape`
          return tf.math.reduce_mean(
              (target_implied_vol - implied_vol) ** 2, axis=-1
          )
  
      return loss_function
  
imports: from typing import Callable, Tuple
  import tensorflow.compat.v2
  from tf_quant_finance import black_scholes
  from tf_quant_finance import types
  from tf_quant_finance import utils
  from tf_quant_finance.black_scholes.implied_vol_utils import UnderlyingDistribution
  from tf_quant_finance.math import make_val_and_grad_fn
  from tf_quant_finance.math import optimizer
  from tf_quant_finance.models.sabr import approximations
  from tf_quant_finance.models.sabr.approximations.implied_volatility import SabrApproximationType
  from tf_quant_finance.models.sabr.approximations.implied_volatility import SabrImpliedVolatilityType
label_desc: Use image node_id 2 for calling a global function with example usage: _get_loss_for_volatility_based_calibration() and returns: loss_function, tf