def as_default(self): try: if not self._used_once: # If an outer eager VariableStore was explicitly created and set by # the first time this template store was used (even if not at # constructor time) then pick up the outer variable store. default = variable_scope._get_default_variable_store() # pylint: disable=protected-access if default._store_eager_variables: # pylint: disable=protected-access self._eager_variable_store._store = default # pylint: disable=protected-access self._used_once = True with self._eager_variable_store.as_default(): # pylint: disable=protected-access yield finally: # Each _EagerTemplateVariableStore object lives underneath a variable # scope (see EagerTemplate.__call__). This variable scope's subscopes are # closed when the EagerTemplate object returns from __call__. For # top-level _EagerTemplateVariableStore objects, the variable store to # which the variable scope is attached is different from the # EagerVariableStore; as such it is necessary to close its subscopes # here as well. if self._variable_scope_name is None: raise RuntimeError("A variable scope must be set before an " "_EagerTemplateVariableStore object exits.") variable_scope.get_variable_scope_store().close_variable_subscopes( self._variable_scope_name)
def as_default(self): try: with self._eager_variable_store.as_default(): yield finally: # Each _EagerTemplateVariableStore object lives underneath a variable # scope (see EagerTemplate.__call__). This variable scope's subscopes are # closed when the EagerTemplate object returns from __call__. For # top-level _EagerTemplateVariableStore objects, the variable store to # which the variable scope is attached is different from the # EagerVariableStore; as such it is necessary to close its subscopes # here as well. if self._variable_scope_name is None: raise RuntimeError("A variable scope must be set before an " "_EagerTemplateVariableStore object exits.") variable_scope.get_variable_scope_store().close_variable_subscopes( self._variable_scope_name)
def as_default(self): try: with self._eager_variable_store.as_default(): yield finally: # Each _EagerTemplateVariableStore object lives underneath a variable # scope (see EagerTemplate.__call__). This variable scope's subscopes are # closed when the EagerTemplate object returns from __call__. For # top-level _EagerTemplateVariableStore objects, the variable store to # which the variable scope is attached is different from the # EagerVariableStore; as such it is necessary to close its subscopes # here as well. if self._variable_scope_name is None: raise RuntimeError("A variable scope must be set before an " "_EagerTemplateVariableStore object exits.") variable_scope.get_variable_scope_store().close_variable_subscopes( self._variable_scope_name)
def __init__(self, name=None): """Configure the `Network`. Args: name: The name to use for this `Network`. If specified, it must be unique in the context where this `Network` is first (1) added to another `Network` (in which case it must not share a name with other `Layers` added to that `Network`), or (2) built/called (in which case no other 'top-level' `Network`s may share this name). If unspecified or None, the `Network` will be named using its class name, with a number appended if necessary for uniqueness (e.g. MyNetwork -> 'my_network_1'). Raises: ValueError: If `name` is not valid. Note that some naming errors will instead be raised when the `Network` is called. """ if context.executing_eagerly(): logging.warning(( "** tfe.Network is deprecated and will be removed in a future " "version.\n\n%s") % _NETWORK_DEPRECATION_MESSAGE) if isinstance(name, variable_scope.VariableScope): raise ValueError("VariableScopes are not valid Network names.") if name is not None and "/" in name: raise ValueError( "Forward slashes ('/') are not allowed in Network names.") super(Network, self).__init__(name=name) self._layers = [] self._sub_layer_name_uids = collections.defaultdict(int) # Initially None, but set to False for networks which are first built as # top-level. self._first_parent = None # A weak reference to our first parent. self._non_network_sublayers = [] self._owned_layers = {} # The scope to use if we end up without a parent. self._default_parent_variable_scope = variable_scope.get_variable_scope( ) # Hold on to the variable scope counts from init to check whether a scope # with the name we want was ever created in our parent scope. Without this # check we might have name collisions if the parent scope on init gets # closed before build is called. self._variable_scope_counts_on_init = ( variable_scope.get_variable_scope_store().variable_scopes_count)
def __init__(self, name=None): """Configure the `Network`. Args: name: The name to use for this `Network`. If specified, it must be unique in the context where this `Network` is first (1) added to another `Network` (in which case it must not share a name with other `Layers` added to that `Network`), or (2) built/called (in which case no other 'top-level' `Network`s may share this name). If unspecified or None, the `Network` will be named using its class name, with a number appended if necessary for uniqueness (e.g. MyNetwork -> 'my_network_1'). Raises: ValueError: If `name` is not valid. Note that some naming errors will instead be raised when the `Network` is called. """ if context.executing_eagerly(): logging.warning( ("** tfe.Network is deprecated and will be removed in a future " "version.\n\n%s") % _NETWORK_DEPRECATION_MESSAGE) if isinstance(name, variable_scope.VariableScope): raise ValueError("VariableScopes are not valid Network names.") if name is not None and "/" in name: raise ValueError( "Forward slashes ('/') are not allowed in Network names.") super(Network, self).__init__(name=name) self._layers = [] self._sub_layer_name_uids = collections.defaultdict(int) # Initially None, but set to False for networks which are first built as # top-level. self._first_parent = None # A weak reference to our first parent. self._non_network_sublayers = [] self._owned_layers = {} # The scope to use if we end up without a parent. self._default_parent_variable_scope = variable_scope.get_variable_scope() # Hold on to the variable scope counts from init to check whether a scope # with the name we want was ever created in our parent scope. Without this # check we might have name collisions if the parent scope on init gets # closed before build is called. self._variable_scope_counts_on_init = ( variable_scope.get_variable_scope_store().variable_scopes_count)