-
Notifications
You must be signed in to change notification settings - Fork 74k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
variable_scope
use auxiliary_name_scope
to control whether to create new name scope
#14390
Merged
yifeif
merged 17 commits into
tensorflow:master
from
facaiy:ENH/variable_scope_support_auxiliary_name_scope
Dec 19, 2017
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f0cd568
TST: add test case
facaiy dedbfc3
ENH: variable_scope supports auxiliary_name_scope
facaiy 2d36ebd
DOC: add document
facaiy e3d00cd
ENH: support to reuse name scope given by user
facaiy a1b96e1
TST: add test for name scope
facaiy bedbe8e
DOC: revise docment for name scope
facaiy d473494
CLN: revise TypeError for None
facaiy ad62043
TST: add test for reentering variable_scope
facaiy 1c210bb
BUG: root scope reuse name scope
facaiy 6d73bdf
TST: add test case for root scope
facaiy 13ec8f5
TST: remove name scope tests
facaiy f760155
ENH: remove name scope support
facaiy e9e3c14
DOC: remove name scope support
facaiy a9a42a0
TST: split and add more test cases
facaiy c59e89a
BUG: root name scope is empty string
facaiy a1faeb5
CLN: remove comment
facaiy e236290
Merge remote-tracking branch 'upstream/master' into ENH/variable_scop…
facaiy File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1578,6 +1578,10 @@ def __enter__(self): | |
else self._name_or_scope) | ||
self._reuse = (self._reuse | ||
or self._old.reuse) # Re-using is inherited by sub-scopes. | ||
if self._old_name_scope is None: | ||
name_scope = self._name_or_scope | ||
else: | ||
name_scope = self._old_name_scope | ||
variable_scope_object = VariableScope( | ||
self._reuse, | ||
name=self._new_name, | ||
|
@@ -1588,7 +1592,7 @@ def __enter__(self): | |
dtype=self._old.dtype, | ||
use_resource=self._old.use_resource, | ||
custom_getter=self._old.custom_getter, | ||
name_scope=self._old_name_scope or self._name_or_scope, | ||
name_scope=name_scope, | ||
constraint=self._constraint) | ||
if self._initializer is not None: | ||
variable_scope_object.set_initializer(self._initializer) | ||
|
@@ -1757,7 +1761,8 @@ def __init__(self, | |
reuse=None, | ||
dtype=None, | ||
use_resource=None, | ||
constraint=None): | ||
constraint=None, | ||
auxiliary_name_scope=True): | ||
"""Initialize the context manager. | ||
|
||
Args: | ||
|
@@ -1789,6 +1794,8 @@ def __init__(self, | |
variable and return the Tensor for the projected value | ||
(which must have the same shape). Constraints are not safe to | ||
use when doing asynchronous distributed training. | ||
auxiliary_name_scope: If `True`, we create an auxiliary name scope with | ||
the scope. If `False`, we don't touch name scope. | ||
|
||
Returns: | ||
A scope that can be captured and reused. | ||
|
@@ -1826,6 +1833,10 @@ def __init__(self, | |
self._graph = ops._get_graph_from_inputs(self._values) # pylint: disable=protected-access | ||
self._cached_pure_variable_scope = None | ||
self._current_name_scope = None | ||
if not isinstance(auxiliary_name_scope, bool): | ||
raise TypeError("The auxiliary_name_scope must be `True` or `False`, " | ||
"while get {}".format(auxiliary_name_scope)) | ||
self._auxiliary_name_scope = auxiliary_name_scope | ||
|
||
def __enter__(self): | ||
# If the default graph is building a function, then we should not replace it | ||
|
@@ -1844,6 +1855,21 @@ def __enter__(self): | |
if self._current_name_scope is not None: | ||
self._current_name_scope.__enter__() | ||
return self._cached_pure_variable_scope.__enter__() | ||
|
||
if self._auxiliary_name_scope: | ||
# Create a new name scope later | ||
current_name_scope = None | ||
else: | ||
# Reenter the current name scope | ||
name_scope = ops.get_name_scope() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just worry that |
||
if name_scope: | ||
# Hack to reenter | ||
name_scope = name_scope + "/" | ||
current_name_scope = ops.name_scope(name_scope) | ||
else: | ||
# Root scope | ||
current_name_scope = ops.name_scope(name_scope) | ||
|
||
if self._name_or_scope is not None: | ||
if not isinstance(self._name_or_scope, | ||
(VariableScope,) + six.string_types): | ||
|
@@ -1853,8 +1879,8 @@ def __enter__(self): | |
name_scope = self._name_or_scope | ||
else: | ||
name_scope = self._name_or_scope.name.split("/")[-1] | ||
if name_scope: | ||
self._current_name_scope = ops.name_scope(name_scope) | ||
if name_scope or current_name_scope: | ||
self._current_name_scope = current_name_scope or ops.name_scope(name_scope) | ||
current_name_scope_name = self._current_name_scope.__enter__() | ||
if isinstance(self._name_or_scope, six.string_types): | ||
old_name_scope = current_name_scope_name | ||
|
@@ -1892,7 +1918,7 @@ def __enter__(self): | |
else: # Here name_or_scope is None. Using default name, but made unique. | ||
if self._reuse: | ||
raise ValueError("reuse=True cannot be used without a name_or_scope") | ||
self._current_name_scope = ops.name_scope(self._default_name) | ||
self._current_name_scope = current_name_scope or ops.name_scope(self._default_name) | ||
current_name_scope_name = self._current_name_scope.__enter__() | ||
unique_default_name = _get_unique_variable_scope(self._default_name) | ||
self._cached_pure_variable_scope = _pure_variable_scope( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the
original_name_scope
is reasonable (but counterintuitive?) behavior. We almost never createvariable_scope
like this case. I'm afraid that user may misuse the argument and get lost in name scope.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps it's better to restrict
auxiliary_name_scope
for reentering case. Namely,auxiliary_name_scope
only takes effect when VariableScope is passed.