def _map_captured_resources_to_created_resources(
    original_captures, resource_map):
  """Maps eager resources captured by a function to Graph resources for export.

  Args:
    original_captures: A dictionary mapping from resource tensors captured by
      the function to interior placeholders for those resources (inside the
      function body).
    resource_map: A dictionary mapping from resource tensors owned by the eager
      context to resource tensors in the exported graph.

  Returns:
    A dictionary mapping from interior placeholders in the function body to
    exterior stand-in resource tensors which belong to the exported graph.

  Raises:
    AssertionError: If the function references a resource which is not part of
      `resource_map`.
  """
  export_captures = {}
  for exterior, interior in original_captures.items():
    mapped_resource = resource_map.get(exterior, None)
    if mapped_resource is None:
      raise AssertionError(
          ("Tried to export a function which references untracked stateful "
           "object {}. Stateful TensorFlow objects (e.g. tf.Variable) must "
           "be tracked by the main object. Objects may be tracked by "
           "assigning them to an attribute of another tracked object, or to "
           "an attribute of the main object directly.")
          .format(interior))
    export_captures[interior] = mapped_resource
  return export_captures