A context manager for use when defining a Python op. This context manager validates that the given `values` are from the same graph, makes that graph the default graph, and pushes a name scope in that graph (see `tf.Graph.name_scope` for more details on that). For example, to define a
| 6339 | # some object creation overhead. |
| 6340 | @tf_export(v1=["name_scope"]) |
| 6341 | class name_scope(object): # pylint: disable=invalid-name |
| 6342 | """A context manager for use when defining a Python op. |
| 6343 | |
| 6344 | This context manager validates that the given `values` are from the |
| 6345 | same graph, makes that graph the default graph, and pushes a |
| 6346 | name scope in that graph (see |
| 6347 | `tf.Graph.name_scope` |
| 6348 | for more details on that). |
| 6349 | |
| 6350 | For example, to define a new Python op called `my_op`: |
| 6351 | |
| 6352 | ```python |
| 6353 | def my_op(a, b, c, name=None): |
| 6354 | with tf.name_scope(name, "MyOp", [a, b, c]) as scope: |
| 6355 | a = tf.convert_to_tensor(a, name="a") |
| 6356 | b = tf.convert_to_tensor(b, name="b") |
| 6357 | c = tf.convert_to_tensor(c, name="c") |
| 6358 | # Define some computation that uses `a`, `b`, and `c`. |
| 6359 | return foo_op(..., name=scope) |
| 6360 | ``` |
| 6361 | """ |
| 6362 | |
| 6363 | @property |
| 6364 | def name(self): |
| 6365 | return self._name |
| 6366 | |
| 6367 | def __init__(self, name, default_name=None, values=None): |
| 6368 | """Initialize the context manager. |
| 6369 | |
| 6370 | Args: |
| 6371 | name: The name argument that is passed to the op function. |
| 6372 | default_name: The default name to use if the `name` argument is `None`. |
| 6373 | values: The list of `Tensor` arguments that are passed to the op function. |
| 6374 | |
| 6375 | Raises: |
| 6376 | TypeError: if `default_name` is passed in but not a string. |
| 6377 | """ |
| 6378 | if not (default_name is None or isinstance(default_name, six.string_types)): |
| 6379 | raise TypeError( |
| 6380 | "`default_name` type (%s) is not a string type. You likely meant to " |
| 6381 | "pass this into the `values` kwarg." % type(default_name)) |
| 6382 | self._name = default_name if name is None else name |
| 6383 | self._default_name = default_name |
| 6384 | self._values = values |
| 6385 | self._ctx = context.context() |
| 6386 | self._in_eager_mode = self._ctx.executing_eagerly() |
| 6387 | self._has_symbolic_input_in_eager = False |
| 6388 | if self._values and self._in_eager_mode: |
| 6389 | # The presence of a graph tensor in `self._values` overrides the context. |
| 6390 | for value in self._values: |
| 6391 | if hasattr(value, "graph"): |
| 6392 | self._has_symbolic_input_in_eager = True |
| 6393 | self._name_scope = value.graph.name_scope(self._name) |
| 6394 | |
| 6395 | def __enter__(self): |
| 6396 | """Start the scope block. |
| 6397 | |
| 6398 | Returns: |
no outgoing calls
no test coverage detected