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
)
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__()
Start the scope block.
__exit__
View source
__exit__(
type_arg, value_arg, traceback_arg
)
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 2023-10-06 UTC.
[null,null,["Last updated 2023-10-06 UTC."],[],[],null,["# tf.name_scope\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.14.0/tensorflow/python/framework/ops.py#L5616-L5706) |\n\nA context manager for use when defining a Python op. \n\n tf.name_scope(\n name\n )\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.14.0/tensorflow/python/framework/ops.py#L5663-L5695) \n\n __enter__()\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.14.0/tensorflow/python/framework/ops.py#L5697-L5699) \n\n __exit__(\n type_arg, value_arg, traceback_arg\n )"]]