Beispiel #1
0
    def __init__(self, *args, **kwargs):
        # Maps node id -> (node, revive setter function)
        # Nodes recreated from the config may generate other nodes. This list
        # records all nodes that were generated directly/indirectly from the config,
        # so that they do not get recreated multiple times.
        self._nodes_recreated_from_config = {}
        self._all_nodes_recreated_from_config = (
            object_identity.ObjectIdentityWeakSet())
        # Store all node ids that have already been traversed when tracking nodes
        # that were recreated from the config.
        self._traversed_nodes_from_config = []

        # Maps model id -> (blank model obj, list of child layer or their node ids)
        # This tracks all layers in functional and sequential models. These models
        # are only reconstructed after all of their child layers have been created.
        self.model_layer_dependencies = {}
        self._models_to_reconstruct = []

        super(KerasObjectLoader, self).__init__(*args, **kwargs)

        # Now that the node object has been fully loaded, and the checkpoint has
        # been restored, the object no longer needs to track objects added from
        # SerializedAttributes. (Note that saving a training checkpoint still
        # functions correctly, because layers and variables are tracked separately
        # by the Layer object.)
        # TODO(kathywu): Instead of outright deleting these nodes (which would
        # make restoring from a different checkpoint tricky), mark them as extra
        # dependencies that are OK to overwrite.
        for node in self._nodes:
            if not isinstance(node, base_layer.Layer):
                continue
            for name in PUBLIC_ATTRIBUTES:
                delete_tracking(node, name)
Beispiel #2
0
    def __init__(self, *args, **kwargs):
        # Maps node id -> (node, revive setter function)
        # Nodes recreated from the config may generate other nodes. This list
        # records all nodes that were generated directly/indirectly from the config,
        # so that they do not get recreated multiple times.
        self._nodes_recreated_from_config = {}
        self._all_nodes_recreated_from_config = (
            object_identity.ObjectIdentityWeakSet())
        # Store all node ids that have already been traversed when tracking nodes
        # that were recreated from the config.
        self._traversed_nodes_from_config = []

        # Maps model id -> (blank model obj, list of child layer or their node ids)
        # This tracks all layers in functional and sequential models. These models
        # are only reconstructed after all of their child layers have been created.
        self.model_layer_dependencies = {}
        self._models_to_reconstruct = []

        super(KerasObjectLoader, self).__init__(*args, **kwargs)
Beispiel #3
0
  def __init__(self, name, collections=None, capture_by_value=None):
    """Construct a new FuncGraph.

    The graph will inherit its graph key, collections, seed, and distribution
    strategy stack from the current context or graph.

    Args:
      name: the name of the function.
      collections: a dictionary of collections this FuncGraph should start
        with. If not specified (None), the FuncGraph will read (but not write
        to) the outer graph's collections that are not whitelisted, and both
        read and write to the outer graph's collections that are whitelisted.
        The current whitelisted collections are the global variables, the
        local variables, and the trainable variables.
        Defaults to None.
      capture_by_value: An optional boolean. If True, the func graph will
        capture Variables by value instead of reference. By default inherit
        from outer graphs, and failing that will default to False.
    """
    super(FuncGraph, self).__init__()

    self.name = name
    self.inputs = []
    self.outputs = []
    self.control_outputs = []
    self.control_captures = set()
    self.structured_input_signature = None
    self.structured_outputs = None
    self._weak_variables = []
    self._watched_variables = object_identity.ObjectIdentityWeakSet()
    self.outer_graph = ops.get_default_graph()
    self._captures = py_collections.OrderedDict()
    # If not None, records the names of output args of this function. Used to
    # preserve the output names in the signature of a serialized+deserialized
    # function. Private at the moment mostly because it's often out of date.
    self._output_names = None
    # Maps arbitrary key -> (closure, nest of placeholders), where at function
    # call time the value of closure() will be used to feed the nest of
    # placeholders.
    self._deferred_captures = py_collections.OrderedDict()
    # Inherit capture-by-value from outer graph.
    if capture_by_value is not None:
      self.capture_by_value = capture_by_value
    elif self.outer_graph is not None and isinstance(
        self.outer_graph, FuncGraph):
      self.capture_by_value = self.outer_graph.capture_by_value
    else:
      self.capture_by_value = False

    self._building_function = True
    # Map from resource tensor name to last op (in program order) which uses
    # this tensor. Used to enforce that execution order matches program order
    # for resource tensors.
    self._last_op_using_resource_tensor = {}

    graph = self.outer_graph

    if context.executing_eagerly():
      self.seed = context.global_seed()
      # [for tf-data user migration from TF1.0 to 2.0] seed_used keep track of
      # any None op_seed for random_op in the function, in which case we end up
      # using function seed, which could be unintended behavior for the op.
      self._seed_used = False
    else:
      self.seed = graph.seed
      self._seed_used = False
      # TODO(allenl): Figure out if we can remove colocation stack
      # specialization (currently used in cond_v2), here and in the cache key.
      self._colocation_stack = graph._colocation_stack.copy()  # pylint: disable=protected-access

    if collections is None:
      for collection_name in graph.get_all_collection_keys():
        if collection_name not in WHITELIST_COLLECTIONS:
          self._collections[collection_name] = graph.get_collection(
              collection_name)
      for collection_name in WHITELIST_COLLECTIONS:
        self._collections[collection_name] = graph.get_collection_ref(
            collection_name)
    else:
      self._collections = collections

    # Keep track of whether this FuncGraph is exportable to SavedModel. Use
    # `graph.mark_as_unsaveable(reason)` to mark this FuncGraph and any
    # dependent functions as unsaveable.
    self._saveable = True
    self._saving_errors = set()