tf.init_scope
bookmark_borderbookmark
Stay organized with collections
Save and categorize content based on your preferences.
A context manager that lifts ops out of control-flow scopes and function-building graphs.
View aliases
Compat aliases for migration
See
Migration guide for
more details.
tf.compat.v1.init_scope
@tf_contextlib.contextmanager
tf.init_scope()
There is often a need to lift variable initialization ops out of control-flow
scopes, function-building graphs, and gradient tapes. Entering an
init_scope
is a mechanism for satisfying these desiderata. In particular,
entering an init_scope
has three effects:
(1) All control dependencies are cleared the moment the scope is entered;
this is equivalent to entering the context manager returned from
control_dependencies(None)
, which has the side-effect of exiting
control-flow scopes like tf.cond
and tf.while_loop
.
(2) All operations that are created while the scope is active are lifted
into the lowest context on the context_stack
that is not building a
graph function. Here, a context is defined as either a graph or an eager
context. Every context switch, i.e., every installation of a graph as
the default graph and every switch into eager mode, is logged in a
thread-local stack called context_switches
; the log entry for a
context switch is popped from the stack when the context is exited.
Entering an init_scope
is equivalent to crawling up
context_switches
, finding the first context that is not building a
graph function, and entering it. A caveat is that if graph mode is
enabled but the default graph stack is empty, then entering an
init_scope
will simply install a fresh graph as the default one.
(3) The gradient tape is paused while the scope is active.
When eager execution is enabled, code inside an init_scope block runs with
eager execution enabled even when tracing a tf.function
. For example:
tf.compat.v1.enable_eager_execution()
@tf.function
def func():
# A function constructs TensorFlow graphs,
# it does not execute eagerly.
assert not tf.executing_eagerly()
with tf.init_scope():
# Initialization runs with eager execution enabled
assert tf.executing_eagerly()
Raises |
RuntimeError
|
if graph state is incompatible with this initialization.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2022-11-04 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2022-11-04 UTC."],[],[],null,["# tf.init_scope\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.7.4/tensorflow/python/framework/ops.py#L5817-L5919) |\n\nA context manager that lifts ops out of control-flow scopes and function-building graphs.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.init_scope`](https://www.tensorflow.org/api_docs/python/tf/init_scope)\n\n\u003cbr /\u003e\n\n @tf_contextlib.contextmanager\n tf.init_scope()\n\nThere is often a need to lift variable initialization ops out of control-flow\nscopes, function-building graphs, and gradient tapes. Entering an\n`init_scope` is a mechanism for satisfying these desiderata. In particular,\nentering an `init_scope` has three effects:\n\n(1) All control dependencies are cleared the moment the scope is entered;\nthis is equivalent to entering the context manager returned from\n`control_dependencies(None)`, which has the side-effect of exiting\ncontrol-flow scopes like [`tf.cond`](../tf/cond) and [`tf.while_loop`](../tf/while_loop).\n\n(2) All operations that are created while the scope is active are lifted\ninto the lowest context on the `context_stack` that is not building a\ngraph function. Here, a context is defined as either a graph or an eager\ncontext. Every context switch, i.e., every installation of a graph as\nthe default graph and every switch into eager mode, is logged in a\nthread-local stack called `context_switches`; the log entry for a\ncontext switch is popped from the stack when the context is exited.\nEntering an `init_scope` is equivalent to crawling up\n`context_switches`, finding the first context that is not building a\ngraph function, and entering it. A caveat is that if graph mode is\nenabled but the default graph stack is empty, then entering an\n`init_scope` will simply install a fresh graph as the default one.\n\n(3) The gradient tape is paused while the scope is active.\n\nWhen eager execution is enabled, code inside an init_scope block runs with\neager execution enabled even when tracing a [`tf.function`](../tf/function). For example: \n\n tf.compat.v1.enable_eager_execution()\n\n @tf.function\n def func():\n # A function constructs TensorFlow graphs,\n # it does not execute eagerly.\n assert not tf.executing_eagerly()\n with tf.init_scope():\n # Initialization runs with eager execution enabled\n assert tf.executing_eagerly()\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|----------------|----------------------------------------------------------|\n| `RuntimeError` | if graph state is incompatible with this initialization. |\n\n\u003cbr /\u003e"]]