tf.name_scope
Stay organized with collections
Save and categorize content based on your preferences.
A context manager for use when defining a Python op.
tf.name_scope(
name
) -> None
Used in the notebooks
Used in the guide |
Used in the tutorials |
|
|
This context manager pushes a name scope, which will make the name of all
operations added within it have a prefix.
For example, to define a new Python op called my_op
:
def my_op(a, b, c, name=None):
with tf.name_scope("MyOp") as scope:
a = tf.convert_to_tensor(a, name="a")
b = tf.convert_to_tensor(b, name="b")
c = tf.convert_to_tensor(c, name="c")
# Define some computation that uses `a`, `b`, and `c`.
return foo_op(..., name=scope)
When executed, the Tensors a
, b
, c
, will have names MyOp/a
, MyOp/b
,
and MyOp/c
.
Inside a tf.function
, if the scope name already exists, the name will be
made unique by appending _n
. For example, calling my_op
the second time
will generate MyOp_1/a
, etc.
Args |
name
|
The prefix to use on all names created within the name scope.
|
Raises |
ValueError
|
If name is not a string.
|
Methods
__enter__
View source
__enter__() -> str
Start the scope block.
__exit__
View source
__exit__(
type_arg: None, value_arg: None, traceback_arg: None
) -> bool
Raise any exception triggered within the runtime context.
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 2024-04-26 UTC.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.name_scope\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/framework/ops.py#L5701-L5793) |\n\nA context manager for use when defining a Python op. \n\n tf.name_scope(\n name\n ) -\u003e None\n\n### Used in the notebooks\n\n| Used in the guide | Used in the tutorials |\n|-------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Migrating model checkpoints](https://www.tensorflow.org/guide/migrate/migrating_checkpoints) | - [Displaying text data in TensorBoard](https://www.tensorflow.org/tensorboard/text_summaries) - [Graph-based Neural Structured Learning in TFX](https://www.tensorflow.org/tfx/tutorials/tfx/neural_structured_learning) |\n\nThis context manager pushes a name scope, which will make the name of all\noperations added within it have a prefix.\n\nFor example, to define a new Python op called `my_op`: \n\n def my_op(a, b, c, name=None):\n with tf.name_scope(\"MyOp\") as scope:\n a = tf.convert_to_tensor(a, name=\"a\")\n b = tf.convert_to_tensor(b, name=\"b\")\n c = tf.convert_to_tensor(c, name=\"c\")\n # Define some computation that uses `a`, `b`, and `c`.\n return foo_op(..., name=scope)\n\nWhen executed, the Tensors `a`, `b`, `c`, will have names `MyOp/a`, `MyOp/b`,\nand `MyOp/c`.\n\nInside a [`tf.function`](../tf/function), if the scope name already exists, the name will be\nmade unique by appending `_n`. For example, calling `my_op` the second time\nwill generate `MyOp_1/a`, etc.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------|---------------------------------------------------------------|\n| `name` | The prefix to use on all names created within the name scope. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|--------------------------|\n| `ValueError` | If name is not a string. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Attributes ---------- ||\n|--------|---------------|\n| `name` | \u003cbr /\u003e \u003cbr /\u003e |\n\n\u003cbr /\u003e\n\nMethods\n-------\n\n### `__enter__`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/framework/ops.py#L5748-L5780) \n\n __enter__() -\u003e str\n\nStart the scope block.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ||\n|---|---|\n| The scope name. ||\n\n\u003cbr /\u003e\n\n### `__exit__`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/framework/ops.py#L5782-L5786) \n\n __exit__(\n type_arg: None, value_arg: None, traceback_arg: None\n ) -\u003e bool\n\nRaise any exception triggered within the runtime context."]]