(
    alpha,
    sigma,
    params_init,
    params_final,
    objective_fn,
    num_samples_per_iteration,
    loss_threshold_condition,
    normalize_error,
)
return (
      loss_threshold_condition(mean_perturbed_loss),
      mean_perturbed_loss,
      weighted_squared_distance_norm,
  )
Interpolate `params_init` and `params_final`, perturb, then evaluate.

  Args:
    alpha: The linear interpolation coefficient.
    sigma: The standard deviation of the Gaussian perturbations.
    params_init: The initial parameter settings, activated when `alpha` is zero.
    params_final: The final parameter settings, activated when `alpha` is one.
    objective_fn: A function that returns the objective value when passed an
      (interpolated) parameter value.
    num_samples_per_iteration: Number of perturbations to sample each iteration.
    loss_threshold_condition: A function that takes in a reference objective
      value and a candidate objective value and produces a thresholding
      decision.
    normalize_error: Whether to normalize the error that is minimized over in
      the definition of criticality by the Frobenius norm of the distance
      between initial and final parameters.

  Returns:
    The average loss of the interpolation across perturbations.
Interpolate `params_init` and `params_final`, perturb, then evaluate.
Interpolate params_init and params_final perturb then evaluate .
def _interpolate_and_perturb(
    alpha,
    sigma,
    params_init,
    params_final,
    objective_fn,
    num_samples_per_iteration,
    loss_threshold_condition,
    normalize_error,
):
  """Interpolate `params_init` and `params_final`, perturb, then evaluate.

  Args:
    alpha: The linear interpolation coefficient.
    sigma: The standard deviation of the Gaussian perturbations.
    params_init: The initial parameter settings, activated when `alpha` is zero.
    params_final: The final parameter settings, activated when `alpha` is one.
    objective_fn: A function that returns the objective value when passed an
      (interpolated) parameter value.
    num_samples_per_iteration: Number of perturbations to sample each iteration.
    loss_threshold_condition: A function that takes in a reference objective
      value and a candidate objective value and produces a thresholding
      decision.
    normalize_error: Whether to normalize the error that is minimized over in
      the definition of criticality by the Frobenius norm of the distance
      between initial and final parameters.

  Returns:
    The average loss of the interpolation across perturbations.
  """

  # Linearly interpolate.
  params = ((1 - alpha) * param_init + alpha * param_final
            for param_init, param_final in zip(params_init, params_final))

  # Resample perturbations and evaluate the objective.
  perturbed_losses = []
  for _ in range(num_samples_per_iteration):
    perturbed_losses += [
        objective_fn(
            (param + tf.random.normal(tf.shape(param), mean=0.0, stddev=sigma)
             for param in params))
    ]

  # Compute the Frobenius norm between the final and initial parameter values.
  squared_distance_norm = tf.reduce_sum(
      tuple(
          _squared_frobenius_norm(param_final - param_init)
          for param_init, param_final in zip(params_init, params_final)))

  # Estimate the expected loss over perturbations.
  mean_perturbed_loss = tf.reduce_mean(perturbed_losses)

  # Note: This error normalization was not included in the final paper, but
  # was discussed as a variant that accounts for large differences in norms
  # between different parameter tensors.
  if normalize_error:
    # Normalize by the Frobenius norm of the parameter distance.
    mean_perturbed_loss /= tf.math.sqrt(squared_distance_norm)

  # Compute the weighted norm in the definition of criticality.
  weighted_squared_distance_norm = ((alpha / sigma)**2 * squared_distance_norm)

  return (
      loss_threshold_condition(mean_perturbed_loss),
      mean_perturbed_loss,
      weighted_squared_distance_norm,
  )
def _interpolate_and_perturb ( alpha , sigma , params_init , params_final , objective_fn , num_samples_per_iteration , loss_threshold_condition , normalize_error , ) : # Linearly interpolate. params = ( ( 1 - alpha ) * param_init + alpha * param_final for param_init , param_final in zip ( params_init , params_final ) ) # Resample perturbations and evaluate the objective. perturbed_losses = [ ] for _ in range ( num_samples_per_iteration ) : perturbed_losses += [ objective_fn ( ( param + tf . random . normal ( tf . shape ( param ) , mean = 0.0 , stddev = sigma ) for param in params ) ) ] # Compute the Frobenius norm between the final and initial parameter values. squared_distance_norm = tf . reduce_sum ( tuple ( _squared_frobenius_norm ( param_final - param_init ) for param_init , param_final in zip ( params_init , params_final ) ) ) # Estimate the expected loss over perturbations. mean_perturbed_loss = tf . reduce_mean ( perturbed_losses ) # Note: This error normalization was not included in the final paper, but # was discussed as a variant that accounts for large differences in norms # between different parameter tensors. if normalize_error : # Normalize by the Frobenius norm of the parameter distance. mean_perturbed_loss /= tf . math . sqrt ( squared_distance_norm ) # Compute the weighted norm in the definition of criticality. weighted_squared_distance_norm = ( ( alpha / sigma ) ** 2 * squared_distance_norm ) return ( loss_threshold_condition ( mean_perturbed_loss ) , mean_perturbed_loss , weighted_squared_distance_norm , )