def get_total_kl_cost(name="total_kl_cost", filter_by_name_substring=None):
  """Get the total cost for all (or a subset of) the stochastic variables.

  Args:
    name: A name for the tensor representing the total kl cost.
    filter_by_name_substring: A string used to filter which variables count
      toward the total KL cost. By default, this argument is `None`, and all
      variables trained using Bayes by Backprop are included. If this argument
      is provided, the variables whose KL costs are summed will be all those
      whose name contains `filter_by_name_substring`. An example use of this
      would be to select all variables within a particular scope.

  Returns:
    A tensor representing the total KL cost in the ELBO loss.
  """
  all_variable_metadata = get_variable_metadata(filter_by_name_substring)
  if not all_variable_metadata:
    tf.logging.warning("No Bayes by Backprop variables found!")
    return tf.constant(0.0, shape=())
  return tf.add_n([md.kl_cost for md in all_variable_metadata], name=name)