Returns a context manager that specifies the resource container to use. Stateful operations, such as variables and queues, can maintain their states on devices so that they can be shared by multiple processes. A resource container is a string name under which these stateful operatio
(self, container_name)
| 4457 | # pylint: disable=g-doc-return-or-yield |
| 4458 | @tf_contextlib.contextmanager |
| 4459 | def container(self, container_name): |
| 4460 | """Returns a context manager that specifies the resource container to use. |
| 4461 | |
| 4462 | Stateful operations, such as variables and queues, can maintain their |
| 4463 | states on devices so that they can be shared by multiple processes. |
| 4464 | A resource container is a string name under which these stateful |
| 4465 | operations are tracked. These resources can be released or cleared |
| 4466 | with `tf.Session.reset()`. |
| 4467 | |
| 4468 | For example: |
| 4469 | |
| 4470 | ```python |
| 4471 | with g.container('experiment0'): |
| 4472 | # All stateful Operations constructed in this context will be placed |
| 4473 | # in resource container "experiment0". |
| 4474 | v1 = tf.Variable([1.0]) |
| 4475 | v2 = tf.Variable([2.0]) |
| 4476 | with g.container("experiment1"): |
| 4477 | # All stateful Operations constructed in this context will be |
| 4478 | # placed in resource container "experiment1". |
| 4479 | v3 = tf.Variable([3.0]) |
| 4480 | q1 = tf.queue.FIFOQueue(10, tf.float32) |
| 4481 | # All stateful Operations constructed in this context will be |
| 4482 | # be created in the "experiment0". |
| 4483 | v4 = tf.Variable([4.0]) |
| 4484 | q1 = tf.queue.FIFOQueue(20, tf.float32) |
| 4485 | with g.container(""): |
| 4486 | # All stateful Operations constructed in this context will be |
| 4487 | # be placed in the default resource container. |
| 4488 | v5 = tf.Variable([5.0]) |
| 4489 | q3 = tf.queue.FIFOQueue(30, tf.float32) |
| 4490 | |
| 4491 | # Resets container "experiment0", after which the state of v1, v2, v4, q1 |
| 4492 | # will become undefined (such as uninitialized). |
| 4493 | tf.Session.reset(target, ["experiment0"]) |
| 4494 | ``` |
| 4495 | |
| 4496 | Args: |
| 4497 | container_name: container name string. |
| 4498 | |
| 4499 | Returns: |
| 4500 | A context manager for defining resource containers for stateful ops, |
| 4501 | yields the container name. |
| 4502 | """ |
| 4503 | original_container = self._container |
| 4504 | self._container = container_name |
| 4505 | try: |
| 4506 | yield self._container |
| 4507 | finally: |
| 4508 | self._container = original_container |
| 4509 | |
| 4510 | # pylint: enable=g-doc-return-or-yield |
| 4511 |
no outgoing calls