def _statistics_to_ordered_tuple(statistics, key_order):
  """Turn inputs into an ordered tuple.

  Note: facilitates single, multi-output or named multi-output Keras model API.

  Args:
    statistics: instance, tuple or dict of (named) statistics.
    key_order: in case of dict args, implied order.

  Returns:
    ordered tuple of statistics.
  """
  if statistics is None:
    ordered_stats = ()
  elif isinstance(statistics, dict):
    missing_keys = set(statistics.keys()) - set(key_order)
    if missing_keys:
      raise ValueError("Missing output(s) in model: %s" %
                       " ".join(missing_keys))
    ordered_stats = tuple(statistics.get(key, None) for key in key_order)
  elif isinstance(statistics, (tuple, list)) and len(statistics) and isinstance(
      statistics[0], (tuple, list)):
    ordered_stats = statistics
  else:
    ordered_stats = (statistics,)
  return ordered_stats