Returns a TensorFlow Session for use in executing tests. Note that this will set this session and the graph as global defaults. Use the `use_gpu` and `force_gpu` options to control where ops are run. If `force_gpu` is True, all ops are pinned to `/device:GPU:0`. Otherwise, if `use_
(self, graph=None, config=None, use_gpu=False, force_gpu=False)
| 2035 | # pylint: disable=g-doc-return-or-yield |
| 2036 | @contextlib.contextmanager |
| 2037 | def session(self, graph=None, config=None, use_gpu=False, force_gpu=False): |
| 2038 | """Returns a TensorFlow Session for use in executing tests. |
| 2039 | |
| 2040 | Note that this will set this session and the graph as global defaults. |
| 2041 | |
| 2042 | Use the `use_gpu` and `force_gpu` options to control where ops are run. If |
| 2043 | `force_gpu` is True, all ops are pinned to `/device:GPU:0`. Otherwise, if |
| 2044 | `use_gpu` is True, TensorFlow tries to run as many ops on the GPU as |
| 2045 | possible. If both `force_gpu and `use_gpu` are False, all ops are pinned to |
| 2046 | the CPU. |
| 2047 | |
| 2048 | Example: |
| 2049 | ```python |
| 2050 | class MyOperatorTest(test_util.TensorFlowTestCase): |
| 2051 | def testMyOperator(self): |
| 2052 | with self.session(use_gpu=True): |
| 2053 | valid_input = [1.0, 2.0, 3.0, 4.0, 5.0] |
| 2054 | result = MyOperator(valid_input).eval() |
| 2055 | self.assertEqual(result, [1.0, 2.0, 3.0, 5.0, 8.0] |
| 2056 | invalid_input = [-1.0, 2.0, 7.0] |
| 2057 | with self.assertRaisesOpError("negative input not supported"): |
| 2058 | MyOperator(invalid_input).eval() |
| 2059 | ``` |
| 2060 | |
| 2061 | Args: |
| 2062 | graph: Optional graph to use during the returned session. |
| 2063 | config: An optional config_pb2.ConfigProto to use to configure the |
| 2064 | session. |
| 2065 | use_gpu: If True, attempt to run as many ops as possible on GPU. |
| 2066 | force_gpu: If True, pin all ops to `/device:GPU:0`. |
| 2067 | |
| 2068 | Yields: |
| 2069 | A Session object that should be used as a context manager to surround |
| 2070 | the graph building and execution code in a test case. |
| 2071 | """ |
| 2072 | if context.executing_eagerly(): |
| 2073 | yield EagerSessionWarner() |
| 2074 | else: |
| 2075 | with self._create_session(graph, config, force_gpu) as sess: |
| 2076 | with self._constrain_devices_and_set_default(sess, use_gpu, force_gpu): |
| 2077 | yield sess |
| 2078 | |
| 2079 | @contextlib.contextmanager |
| 2080 | def cached_session(self, |
no test coverage detected