diff options
author | 2018-09-26 11:54:30 +0800 | |
---|---|---|
committer | 2018-09-26 11:54:30 +0800 | |
commit | 35174f46b973c66a2e6894a12b3018d60e8414ec (patch) | |
tree | 5bdae0172159bc02ec3a470722bf959b14dd47ba /tensorflow/python/kernel_tests | |
parent | f0886f7269de900d226455d4831722f6fc94a71b (diff) | |
parent | 6666516f390f125ed70ddbd4e6f89b83d953c408 (diff) |
Merge remote-tracking branch 'origin'
Diffstat (limited to 'tensorflow/python/kernel_tests')
150 files changed, 5520 insertions, 2762 deletions
diff --git a/tensorflow/python/kernel_tests/BUILD b/tensorflow/python/kernel_tests/BUILD index 0403211d92..5183e4d30c 100644 --- a/tensorflow/python/kernel_tests/BUILD +++ b/tensorflow/python/kernel_tests/BUILD @@ -286,7 +286,10 @@ tf_py_test( srcs = ["decode_csv_op_test.py"], additional_deps = [ "//third_party/py/numpy", + "//tensorflow/python/eager:context", "//tensorflow/python:client_testlib", + "//tensorflow/python:errors", + "//tensorflow/python:framework_test_lib", "//tensorflow/python:parsing_ops", ], ) @@ -535,6 +538,21 @@ tf_py_test( ) tf_py_test( + name = "logging_ops_logging_level_test", + size = "small", + srcs = ["logging_ops_logging_level_test.py"], + additional_deps = [ + "//tensorflow/python:client_testlib", + "//tensorflow/python:framework_for_generated_wrappers", + "//tensorflow/python:framework_test_lib", + "//tensorflow/python:logging_ops", + ], + tags = [ + "no_windows", + ], +) + +tf_py_test( name = "logging_ops_test", size = "small", srcs = ["logging_ops_test.py"], @@ -958,6 +976,19 @@ tf_py_test( ) tf_py_test( + name = "string_format_op_test", + size = "small", + srcs = ["string_format_op_test.py"], + additional_deps = [ + "//tensorflow/python:client_testlib", + "//tensorflow/python:framework_for_generated_wrappers", + "//tensorflow/python:framework_test_lib", + "//tensorflow/python:string_ops", + "//tensorflow/python:math_ops", + ], +) + +tf_py_test( name = "string_join_op_test", size = "small", srcs = ["string_join_op_test.py"], @@ -1011,6 +1042,7 @@ tf_py_test( size = "small", srcs = ["substr_op_test.py"], additional_deps = [ + "@absl_py//absl/testing:parameterized", "//third_party/py/numpy", "//tensorflow/python:client_testlib", "//tensorflow/python:errors", @@ -1631,6 +1663,18 @@ cuda_py_test( ) cuda_py_test( + name = "extract_volume_patches_op_test", + size = "small", + srcs = ["extract_volume_patches_op_test.py"], + additional_deps = [ + "//third_party/py/numpy", + "//tensorflow/python:array_ops", + "//tensorflow/python:client_testlib", + "//tensorflow/python:framework_for_generated_wrappers", + ], +) + +cuda_py_test( name = "functional_ops_test", size = "small", srcs = ["functional_ops_test.py"], @@ -2795,6 +2839,46 @@ cuda_py_test( ) cuda_py_test( + name = "cwise_ops_binary_test", + size = "medium", + srcs = ["cwise_ops_binary_test.py"], + additional_deps = [ + "//third_party/py/numpy", + "//tensorflow/python:array_ops", + "//tensorflow/python:client_testlib", + "//tensorflow/python:framework", + "//tensorflow/python:framework_for_generated_wrappers", + "//tensorflow/python:gradients", + "//tensorflow/python:math_ops", + "//tensorflow/python:math_ops_gen", + "//tensorflow/python:nn_grad", + "//tensorflow/python:platform", + "//tensorflow/python:variables", + ], + shard_count = 50, +) + +cuda_py_test( + name = "cwise_ops_unary_test", + size = "medium", + srcs = ["cwise_ops_unary_test.py"], + additional_deps = [ + "//third_party/py/numpy", + "//tensorflow/python:array_ops", + "//tensorflow/python:client_testlib", + "//tensorflow/python:framework", + "//tensorflow/python:framework_for_generated_wrappers", + "//tensorflow/python:gradients", + "//tensorflow/python:math_ops", + "//tensorflow/python:math_ops_gen", + "//tensorflow/python:nn_grad", + "//tensorflow/python:platform", + "//tensorflow/python:variables", + ], + shard_count = 50, +) + +cuda_py_test( name = "embedding_ops_test", size = "medium", srcs = ["embedding_ops_test.py"], @@ -3160,3 +3244,27 @@ tf_py_test( grpc_enabled = True, tags = ["no_gpu"], # TODO(b/111656070) ) + +# TODO(b/116053459): Replace with cuda_py_test. +tf_py_test( + name = "while_v2_test", + size = "medium", + srcs = ["while_v2_test.py"], + additional_deps = [ + "@absl_py//absl/testing:parameterized", + "//tensorflow/core:protos_all_py", + "//tensorflow/python:array_ops", + "//tensorflow/python:client_testlib", + "//tensorflow/python:constant_op", + "//tensorflow/python:control_flow_ops", + "//tensorflow/python:dtypes", + "//tensorflow/python:framework", + "//tensorflow/python:framework_ops", + "//tensorflow/python:gradients_impl", + "//tensorflow/python:list_ops", + "//tensorflow/python:tf_optimizer", + "//tensorflow/python:while_v2", + ], + grpc_enabled = True, + tags = ["no_gpu"], # TODO(b/116053459) +) diff --git a/tensorflow/python/kernel_tests/accumulate_n_test.py b/tensorflow/python/kernel_tests/accumulate_n_test.py index b793906fac..0bc5268f38 100644 --- a/tensorflow/python/kernel_tests/accumulate_n_test.py +++ b/tensorflow/python/kernel_tests/accumulate_n_test.py @@ -76,7 +76,7 @@ class AccumulateNV2Test(test_util.TensorFlowTestCase): # Putting them here so that everything that exercises AccumulateNV2 is in # one place and the default build runs all unit tests. def testSimple(self): - with self.test_session(): + with self.cached_session(): random_arrays = [ np.random.rand(16, 16, 16, 16).astype(np.float32) for _ in range(20) ] @@ -91,27 +91,27 @@ class AccumulateNV2Test(test_util.TensorFlowTestCase): self.assertAllClose(np_val, tf_val.eval()) def testZeroArgs(self): - with self.test_session(): + with self.cached_session(): with self.assertRaises(ValueError): tf_val = math_ops.accumulate_n([]) tf_val.eval() def testWrongShape(self): - with self.test_session(): + with self.cached_session(): with self.assertRaises(ValueError): a = variables.Variable(0.2) b = variables.Variable(0.1) math_ops.accumulate_n([a, b], shape=[2, 2]) # Should be shape=[] def testIncompatibleShapes(self): - with self.test_session(): + with self.cached_session(): with self.assertRaises(ValueError): a = variables.Variable(np.array([0.1, 0.2])) b = variables.Variable(np.array([[0.3], [0.4]])) math_ops.accumulate_n([a, b]) def testWrongType(self): - with self.test_session(): + with self.cached_session(): with self.assertRaises(TypeError): a = variables.Variable(0.2, dtype=np.float32) b = variables.Variable(0.1, dtype=np.float32) @@ -119,7 +119,7 @@ class AccumulateNV2Test(test_util.TensorFlowTestCase): def testWrongTypeOneInput(self): # Scenario that used to trigger a bug, even when testWrongType() worked - with self.test_session(): + with self.cached_session(): with self.assertRaises(TypeError): a = variables.Variable(0.2, dtype=np.float32) math_ops.accumulate_n([a], tensor_dtype=np.int32) diff --git a/tensorflow/python/kernel_tests/ackermann_test.py b/tensorflow/python/kernel_tests/ackermann_test.py index 5e0d87c783..d267e49752 100644 --- a/tensorflow/python/kernel_tests/ackermann_test.py +++ b/tensorflow/python/kernel_tests/ackermann_test.py @@ -34,7 +34,7 @@ class AckermannTest(test.TestCase): self.assertEqual(len(ackermann.OP_LIST.op), 1) self.assertEqual(ackermann.OP_LIST.op[0].name, 'Ackermann') - with self.test_session(): + with self.cached_session(): self.assertEqual(ackermann.ackermann().eval(), b'A(m, 0) == A(m-1, 1)') diff --git a/tensorflow/python/kernel_tests/argmax_op_test.py b/tensorflow/python/kernel_tests/argmax_op_test.py index 1202c463e8..127d14c250 100644 --- a/tensorflow/python/kernel_tests/argmax_op_test.py +++ b/tensorflow/python/kernel_tests/argmax_op_test.py @@ -104,20 +104,20 @@ class ArgMaxTest(test.TestCase): self._testDim(np.int64) def testEmpty(self): - with self.test_session(): + with self.cached_session(): for op in math_ops.argmin, math_ops.argmax: with self.assertRaisesOpError( r"Reduction axis 0 is empty in shape \[0\]"): op([], 0).eval() def testDefaultAxis(self): - with self.test_session(): + with self.cached_session(): for op in math_ops.argmin, math_ops.argmax: ans = op([1]).eval() self.assertAllEqual(ans, 0) def testOutputEmpty(self): - with self.test_session(): + with self.cached_session(): for op in math_ops.argmin, math_ops.argmax: ret = op(array_ops.zeros(shape=[1, 0, 2]), axis=-1).eval() self.assertEqual(ret.shape, (1, 0)) diff --git a/tensorflow/python/kernel_tests/array_ops_test.py b/tensorflow/python/kernel_tests/array_ops_test.py index a164682227..2fe85839d0 100644 --- a/tensorflow/python/kernel_tests/array_ops_test.py +++ b/tensorflow/python/kernel_tests/array_ops_test.py @@ -50,7 +50,7 @@ class BatchMatrixTransposeTest(test_util.TensorFlowTestCase): def testNonBatchMatrix(self): matrix = [[1, 2, 3], [4, 5, 6]] # Shape (2, 3) expected_transposed = [[1, 4], [2, 5], [3, 6]] # Shape (3, 2) - with self.test_session(): + with self.cached_session(): transposed = array_ops.matrix_transpose(matrix) self.assertEqual((3, 2), transposed.get_shape()) self.assertAllEqual(expected_transposed, transposed.eval()) @@ -58,7 +58,7 @@ class BatchMatrixTransposeTest(test_util.TensorFlowTestCase): def testConjugate(self): m = [[1 + 1j, 2 + 2j, 3 + 3j], [4 + 4j, 5 + 5j, 6 + 6j]] expected_transposed = [[1 - 1j, 4 - 4j], [2 - 2j, 5 - 5j], [3 - 3j, 6 - 6j]] - with self.test_session(): + with self.cached_session(): matrix = ops.convert_to_tensor(m) transposed = array_ops.matrix_transpose(matrix, conjugate=True) self.assertEqual((3, 2), transposed.get_shape()) @@ -71,7 +71,7 @@ class BatchMatrixTransposeTest(test_util.TensorFlowTestCase): matrix_1_t = [[11, 44], [22, 55], [33, 66]] batch_matrix = [matrix_0, matrix_1] # Shape (2, 2, 3) expected_transposed = [matrix_0_t, matrix_1_t] # Shape (2, 3, 2) - with self.test_session(): + with self.cached_session(): transposed = array_ops.matrix_transpose(batch_matrix) self.assertEqual((2, 3, 2), transposed.get_shape()) self.assertAllEqual(expected_transposed, transposed.eval()) @@ -79,7 +79,7 @@ class BatchMatrixTransposeTest(test_util.TensorFlowTestCase): def testNonBatchMatrixDynamicallyDefined(self): matrix = [[1, 2, 3], [4, 5, 6]] # Shape (2, 3) expected_transposed = [[1, 4], [2, 5], [3, 6]] # Shape (3, 2) - with self.test_session(): + with self.cached_session(): matrix_ph = array_ops.placeholder(dtypes.int32) transposed = array_ops.matrix_transpose(matrix_ph) self.assertAllEqual( @@ -94,7 +94,7 @@ class BatchMatrixTransposeTest(test_util.TensorFlowTestCase): matrix_1_t = [[11, 44], [22, 55], [33, 66]] batch_matrix = [matrix_0, matrix_1] # Shape (2, 2, 3) expected_transposed = [matrix_0_t, matrix_1_t] # Shape (2, 3, 2) - with self.test_session(): + with self.cached_session(): batch_matrix_ph = array_ops.placeholder(dtypes.int32) transposed = array_ops.matrix_transpose(batch_matrix_ph) self.assertAllEqual( @@ -105,7 +105,7 @@ class BatchMatrixTransposeTest(test_util.TensorFlowTestCase): def testTensorWithStaticRankLessThanTwoRaisesBecauseNotAMatrix(self): vector = [1, 2, 3] - with self.test_session(): + with self.cached_session(): with self.assertRaisesRegexp(ValueError, "should be a "): array_ops.matrix_transpose(vector) @@ -129,7 +129,7 @@ class BooleanMaskTest(test_util.TensorFlowTestCase): masked_arr = arr[:, mask] elif axis == 2: masked_arr = arr[:, :, mask] - with self.test_session(): + with self.cached_session(): masked_tensor = array_ops.boolean_mask(arr, mask, axis=axis) # Leading dimension size of masked_tensor is always unknown until runtime @@ -176,7 +176,7 @@ class BooleanMaskTest(test_util.TensorFlowTestCase): numpy_result = arr[mask] tf_result = array_ops.boolean_mask(arr, mask) self.assertAllEqual(numpy_result.shape[1:], tf_result.get_shape()[1:]) - with self.test_session(): + with self.cached_session(): self.assertAllClose(numpy_result, tf_result.eval()) def testEmptyInput1D(self): @@ -185,7 +185,7 @@ class BooleanMaskTest(test_util.TensorFlowTestCase): numpy_result = arr[mask] tf_result = array_ops.boolean_mask(arr, mask) self.assertAllEqual(numpy_result.shape[1:], tf_result.get_shape()[1:]) - with self.test_session(): + with self.cached_session(): self.assertAllClose(numpy_result, tf_result.eval()) def testEmptyOutput(self): @@ -199,7 +199,7 @@ class BooleanMaskTest(test_util.TensorFlowTestCase): def testWorksWithDimensionsEqualToNoneDuringGraphBuild(self): # The rank of the mask tensor must be specified. This is explained # in the docstring as well. - with self.test_session() as sess: + with self.cached_session() as sess: ph_tensor = array_ops.placeholder(dtypes.int32, shape=None) ph_mask = array_ops.placeholder(dtypes.bool, shape=[None]) @@ -217,7 +217,7 @@ class BooleanMaskTest(test_util.TensorFlowTestCase): def testMaskDimensionsSetToNoneRaises(self): # The rank of the mask tensor must be specified. This is explained # in the docstring as well. - with self.test_session(): + with self.cached_session(): tensor = array_ops.placeholder(dtypes.int32, shape=[None, 2]) mask = array_ops.placeholder(dtypes.bool, shape=None) with self.assertRaisesRegexp(ValueError, "dimensions must be specified"): @@ -226,21 +226,21 @@ class BooleanMaskTest(test_util.TensorFlowTestCase): def testMaskHasMoreDimsThanTensorRaises(self): mask = [[True, True], [False, False]] tensor = [1, 2, 3, 4] - with self.test_session(): + with self.cached_session(): with self.assertRaisesRegexp(ValueError, "incompatible"): array_ops.boolean_mask(tensor, mask).eval() def testMaskIsScalarRaises(self): mask = True tensor = 1 - with self.test_session(): + with self.cached_session(): with self.assertRaisesRegexp(ValueError, "mask.*scalar"): array_ops.boolean_mask(tensor, mask).eval() def testMaskShapeDifferentThanFirstPartOfTensorShapeRaises(self): mask = [True, True, True] tensor = [[1, 2], [3, 4]] - with self.test_session(): + with self.cached_session(): with self.assertRaisesRegexp(ValueError, "incompatible"): array_ops.boolean_mask(tensor, mask).eval() @@ -345,7 +345,7 @@ class ReverseV2Test(test_util.TensorFlowTestCase): def testInvalid(self): x_np = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32) axis = array_ops.placeholder(dtypes.int32) - with self.test_session(): + with self.cached_session(): with self.assertRaisesRegexp(errors_impl.InvalidArgumentError, "is out of valid range"): array_ops.reverse_v2(x_np, axis).eval(feed_dict={axis: [-30]}) @@ -954,7 +954,7 @@ class StridedSliceAssignChecker(object): class SliceAssignTest(test_util.TensorFlowTestCase): def testInvalidSlice(self): - with self.test_session() as sess: + with self.cached_session() as sess: foo = constant_op.constant([1, 2, 3]) with self.assertRaisesRegexp(ValueError, "Sliced assignment" " is only supported for variables"): @@ -1000,7 +1000,7 @@ class SliceAssignTest(test_util.TensorFlowTestCase): with self.assertRaisesRegexp( errors.FailedPreconditionError, "Attempting to use uninitialized value Variable"): - with self.test_session() as sess: + with self.cached_session() as sess: v = variables.Variable([1, 2]) sess.run(v[:].assign([1, 2])) @@ -1019,7 +1019,7 @@ class SliceAssignTest(test_util.TensorFlowTestCase): too_small_val = constant_op.constant([3, 4], dtype=dtypes.int8) too_large_val = constant_op.constant([3, 4], dtype=dtypes.int64) v = resource_variable_ops.ResourceVariable(init_val) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(v.initializer) with self.assertRaises(ValueError): sess.run(v[:].assign(too_large_val)) @@ -1066,12 +1066,12 @@ class ShapeSizeRankTest(test_util.TensorFlowTestCase): class SequenceMaskTest(test_util.TensorFlowTestCase): def testExceptions(self): - with self.test_session(): + with self.cached_session(): with self.assertRaisesRegexp(ValueError, "maxlen must be scalar"): array_ops.sequence_mask([10, 20], [10, 20]) def testOneDimensionalWithMaxlen(self): - with self.test_session(): + with self.cached_session(): res = array_ops.sequence_mask(constant_op.constant([1, 3, 2]), 5) self.assertAllEqual(res.get_shape(), [3, 5]) self.assertAllEqual( @@ -1081,7 +1081,7 @@ class SequenceMaskTest(test_util.TensorFlowTestCase): @test_util.enable_c_shapes def testOneDimensionalDtypeWithoutMaxlen(self): - with self.test_session(): + with self.cached_session(): # test dtype and default maxlen: res = array_ops.sequence_mask(constant_op.constant([0, 1, 4]), dtype=dtypes.float32) @@ -1092,7 +1092,7 @@ class SequenceMaskTest(test_util.TensorFlowTestCase): @test_util.enable_c_shapes def testOneDimensionalWithoutMaxlen(self): - with self.test_session(): + with self.cached_session(): res = array_ops.sequence_mask( constant_op.constant([0, 1, 4])) self.assertAllEqual(res.get_shape().as_list(), [3, 4]) @@ -1104,7 +1104,7 @@ class SequenceMaskTest(test_util.TensorFlowTestCase): @test_util.enable_c_shapes def testTwoDimensional(self): - with self.test_session(): + with self.cached_session(): res = array_ops.sequence_mask(constant_op.constant([[1, 3, 2]]), 5) self.assertAllEqual(res.get_shape(), [1, 3, 5]) self.assertAllEqual(res.eval(), [[[True, False, False, False, False], [ @@ -1137,7 +1137,7 @@ class SequenceMaskTest(test_util.TensorFlowTestCase): [[True, False, False, False, False], [True, True, True, False, False], [True, True, False, False, False]]) - with self.test_session(): + with self.cached_session(): check_dtypes(dtypes.int32, dtypes.int32) check_dtypes(dtypes.int32, dtypes.int64) check_dtypes(dtypes.int64, dtypes.int32) @@ -1216,7 +1216,7 @@ class UnravelIndexTest(test_util.TensorFlowTestCase): # TODO(b/73086570): Reenable test. @unittest.skip("Test does not pass internally.") def testUnravelIndex(self): - with self.test_session(): + with self.cached_session(): for dtype in [dtypes.int32, dtypes.int64]: indices_1 = constant_op.constant(1621, dtype=dtype) dims_1 = constant_op.constant([6, 7, 8, 9], dtype=dtype) @@ -1237,13 +1237,13 @@ class UnravelIndexTest(test_util.TensorFlowTestCase): class GuaranteeConstOpTest(test_util.TensorFlowTestCase): def testSimple(self): - with self.test_session(): + with self.cached_session(): a = array_ops.constant(10) guarantee_a = array_ops.guarantee_const(a) self.assertEqual(10, guarantee_a.eval()) def testVariables(self): - with self.test_session() as sess: + with self.cached_session() as sess: for use_resource in [False, True]: a = variable_scope.get_variable( "var_{}".format(use_resource), [], @@ -1254,7 +1254,7 @@ class GuaranteeConstOpTest(test_util.TensorFlowTestCase): self.assertEqual(10.0, guarantee_a.eval()) def testResourceRejection(self): - with self.test_session() as sess: + with self.cached_session() as sess: a = variable_scope.get_variable( "resource_var", [], initializer=init_ops.constant_initializer(10.0), @@ -1276,5 +1276,203 @@ class SnapshotOpTest(test_util.TensorFlowTestCase): self.assertAllEqual(y.eval(), [0, 1, 2, 3]) +@test_util.run_all_in_graph_and_eager_modes +class SortedSearchTest(test_util.TensorFlowTestCase): + + def testUpperBoundFloatHandCoded(self): + cdf = np.array([0, .2, .5, .6, .8, 1.], dtype=np.float32) + arr = np.array([.04, .99, .53, .58, .31, .01, .79, .8, .21], + dtype=np.float32) + result = np.searchsorted(cdf, arr, side="right") + tf_result = self.evaluate(array_ops.searchsorted(cdf, arr, side="right")) + self.assertAllEqual(result, tf_result) + + def testUpperBoundFloatRandomNd(self): + dim_size = 7 + for d in range(1, 5): + shape = [dim_size] * d + cdf = np.cumsum( + np.random.uniform(size=shape).astype(np.float32), axis=(d - 1)) + arr = np.random.uniform(size=shape).astype(np.float32) * dim_size + + tf_result = self.evaluate(array_ops.searchsorted(cdf, arr, side="right")) + + cdf = cdf.reshape([-1, dim_size]) + arr = arr.reshape([-1, dim_size]) + result = np.zeros(arr.shape, dtype=np.int32) + for i in range(dim_size**(d - 1)): + result[i, :] = np.searchsorted(cdf[i, :], arr[i, :], side="right") + + result = result.reshape(shape) + + self.assertAllEqual(result, tf_result) + + def testUpperBoundFloatUneven(self): + batch_size = 7 + size_search_array = 1000 + size_values = 47 + cdf = np.cumsum( + np.random.uniform(size=[batch_size, size_search_array]).astype( + np.float32), + axis=1) + arr = np.random.uniform(size=[batch_size, size_values]).astype( + np.float32) * size_search_array + + tf_result = self.evaluate(array_ops.searchsorted(cdf, arr, side="right")) + + result = np.zeros(arr.shape, dtype=np.int32) + for i in range(batch_size): + result[i, :] = np.searchsorted(cdf[i, :], arr[i, :], side="right") + + self.assertAllEqual(result, tf_result) + + def testLowerBoundFloatHandCoded(self): + cdf = np.array([0, .2, .5, .6, .8, 1.], dtype=np.float32) + arr = np.array([.04, .99, .53, .58, .31, .01, .79, .8, .21], + dtype=np.float32) + result = np.searchsorted(cdf, arr, side="left") + tf_result = self.evaluate(array_ops.searchsorted(cdf, arr, side="left")) + self.assertAllEqual(result, tf_result) + + def testLowerBoundFloatRandomNd(self): + dim_size = 7 + for d in range(1, 5): + shape = [dim_size] * d + cdf = np.cumsum( + np.random.uniform(size=shape).astype(np.float32), axis=(d - 1)) + arr = np.random.uniform(size=shape).astype(np.float32) * dim_size + + tf_result = self.evaluate(array_ops.searchsorted(cdf, arr, side="left")) + + cdf = cdf.reshape([-1, dim_size]) + arr = arr.reshape([-1, dim_size]) + result = np.zeros(arr.shape, dtype=np.int32) + for i in range(dim_size**(d - 1)): + result[i, :] = np.searchsorted(cdf[i, :], arr[i, :], side="left") + + result = result.reshape(shape) + + self.assertAllEqual(result, tf_result) + + def testLowerBoundFloatUneven(self): + batch_size = 7 + size_search_array = 1000 + size_values = 47 + cdf = np.cumsum( + np.random.uniform(size=[batch_size, size_search_array]).astype( + np.float32), + axis=1) + arr = np.random.uniform(size=[batch_size, size_values]).astype( + np.float32) * size_search_array + + tf_result = self.evaluate(array_ops.searchsorted(cdf, arr, side="left")) + + result = np.zeros(arr.shape, dtype=np.int32) + for i in range(batch_size): + result[i, :] = np.searchsorted(cdf[i, :], arr[i, :], side="left") + + self.assertAllEqual(result, tf_result) + + def testUpperBoundIntHandCoded(self): + cdf = np.array([0, 20, 50, 60, 80, 100], dtype=np.int64) + arr = np.array([4, 99, 53, 58, 31, 1, 79, 8, 21], dtype=np.int64) + result = np.searchsorted(cdf, arr, side="right") + tf_result = self.evaluate(array_ops.searchsorted(cdf, arr, side="right")) + self.assertAllEqual(result, tf_result) + + def testUpperBoundIntRandomNd(self): + dim_size = 7 + for d in range(1, 5): + shape = [dim_size] * d + cdf = np.cumsum( + np.random.randint(low=0, high=10, size=shape).astype(np.int64), + axis=(d - 1)) + arr = np.random.randint( + low=0, high=10 * dim_size, size=shape).astype(np.int64) + + tf_result = self.evaluate(array_ops.searchsorted(cdf, arr, side="right")) + + cdf = cdf.reshape([-1, dim_size]) + arr = arr.reshape([-1, dim_size]) + result = np.zeros(arr.shape, dtype=np.int32) + for i in range(dim_size**(d - 1)): + result[i, :] = np.searchsorted(cdf[i, :], arr[i, :], side="right") + + result = result.reshape(shape) + + self.assertAllEqual(result, tf_result) + + def testUpperBoundIntUneven(self): + batch_size = 7 + size_search_array = 1000 + size_values = 47 + cdf = np.cumsum( + np.random.randint(low=0, high=10, + size=[batch_size, + size_search_array]).astype(np.int64), + axis=1) + arr = np.random.randint( + low=0, high=10 * size_search_array, size=[batch_size, + size_values]).astype(np.int64) + + tf_result = self.evaluate(array_ops.searchsorted(cdf, arr, side="right")) + + result = np.zeros(arr.shape, dtype=np.int32) + for i in range(batch_size): + result[i, :] = np.searchsorted(cdf[i, :], arr[i, :], side="right") + + self.assertAllEqual(result, tf_result) + + def testLowerBoundIntHandCoded(self): + cdf = np.array([0, 20, 50, 60, 80, 100], dtype=np.int64) + arr = np.array([4, 99, 53, 58, 31, 1, 79, 8, 21], dtype=np.int64) + result = np.searchsorted(cdf, arr, side="left") + tf_result = self.evaluate(array_ops.searchsorted(cdf, arr, side="left")) + self.assertAllEqual(result, tf_result) + + def testLowerBoundIntRandomNd(self): + dim_size = 7 + for d in range(1, 5): + shape = [dim_size] * d + cdf = np.cumsum( + np.random.randint(low=0, high=10, size=shape).astype(np.int64), + axis=(d - 1)) + arr = np.random.randint( + low=0, high=10 * dim_size, size=shape).astype(np.int64) + + tf_result = self.evaluate(array_ops.searchsorted(cdf, arr, side="left")) + + cdf = cdf.reshape([-1, dim_size]) + arr = arr.reshape([-1, dim_size]) + result = np.zeros(arr.shape, dtype=np.int32) + for i in range(dim_size**(d - 1)): + result[i, :] = np.searchsorted(cdf[i, :], arr[i, :], side="left") + + result = result.reshape(shape) + + self.assertAllEqual(result, tf_result) + + def testLowerBoundIntUneven(self): + batch_size = 7 + size_search_array = 1000 + size_values = 47 + cdf = np.cumsum( + np.random.randint(low=0, high=10, + size=[batch_size, + size_search_array]).astype(np.int64), + axis=1) + arr = np.random.randint( + low=0, high=10 * size_search_array, size=[batch_size, + size_values]).astype(np.int64) + + tf_result = self.evaluate(array_ops.searchsorted(cdf, arr, side="left")) + + result = np.zeros(arr.shape, dtype=np.int32) + for i in range(batch_size): + result[i, :] = np.searchsorted(cdf[i, :], arr[i, :], side="left") + + self.assertAllEqual(result, tf_result) + + if __name__ == "__main__": test_lib.main() diff --git a/tensorflow/python/kernel_tests/as_string_op_test.py b/tensorflow/python/kernel_tests/as_string_op_test.py index 51aa17babe..dd4a90e5f6 100644 --- a/tensorflow/python/kernel_tests/as_string_op_test.py +++ b/tensorflow/python/kernel_tests/as_string_op_test.py @@ -32,7 +32,7 @@ class AsStringOpTest(test.TestCase): 0, 1, -1, 0.5, 0.25, 0.125, float("INF"), float("NAN"), float("-INF") ] - with self.test_session(): + with self.cached_session(): for dtype in (dtypes.float32, dtypes.float64): input_ = array_ops.placeholder(dtype) @@ -84,7 +84,7 @@ class AsStringOpTest(test.TestCase): int_inputs_ = [0, -1, 1, -128, 127, -101, 101, -0] s = lambda strs: [x.decode("ascii") for x in strs] - with self.test_session(): + with self.cached_session(): for dtype in (dtypes.int32, dtypes.int64, dtypes.int8): input_ = array_ops.placeholder(dtype) @@ -117,7 +117,7 @@ class AsStringOpTest(test.TestCase): # testing int8 s = lambda strs: [x.decode("ascii") for x in strs] - with self.test_session(): + with self.cached_session(): input_ = array_ops.placeholder(dtypes.int32) int_inputs_ = [np.iinfo(np.int32).min, np.iinfo(np.int32).max] output = string_ops.as_string(input_) @@ -133,7 +133,7 @@ class AsStringOpTest(test.TestCase): def testHalfInt(self): s = lambda strs: [x.decode("ascii") for x in strs] - with self.test_session(): + with self.cached_session(): input_ = array_ops.placeholder(dtypes.int16) int_inputs_ = [np.iinfo(np.int16).min, np.iinfo(np.int16).max] output = string_ops.as_string(input_) @@ -144,7 +144,7 @@ class AsStringOpTest(test.TestCase): bool_inputs_ = [False, True] s = lambda strs: [x.decode("ascii") for x in strs] - with self.test_session(): + with self.cached_session(): for dtype in (dtypes.bool,): input_ = array_ops.placeholder(dtype) @@ -159,7 +159,7 @@ class AsStringOpTest(test.TestCase): ] complex_inputs_ = [(x + (x + 1) * 1j) for x in float_inputs_] - with self.test_session(): + with self.cached_session(): for dtype in (dtypes.complex64, dtypes.complex128): input_ = array_ops.placeholder(dtype) diff --git a/tensorflow/python/kernel_tests/atrous_convolution_test.py b/tensorflow/python/kernel_tests/atrous_convolution_test.py index b98e5fd386..6b16fca29d 100644 --- a/tensorflow/python/kernel_tests/atrous_convolution_test.py +++ b/tensorflow/python/kernel_tests/atrous_convolution_test.py @@ -263,7 +263,7 @@ class AtrousConvolutionTest(test.TestCase): self.assertLess(err, err_tolerance) def testGradient(self): - with self.test_session(): + with self.cached_session(): for padding in ["SAME", "VALID"]: for rate_width in range(1, 3): for rate_height in range(1, 3): diff --git a/tensorflow/python/kernel_tests/attention_ops_test.py b/tensorflow/python/kernel_tests/attention_ops_test.py index fb74698660..1e09ba5b65 100644 --- a/tensorflow/python/kernel_tests/attention_ops_test.py +++ b/tensorflow/python/kernel_tests/attention_ops_test.py @@ -84,7 +84,7 @@ class ExtractGlimpseTest(test.TestCase): image_ops.extract_glimpse(t_cols_4d, t1, t2), [0, 2, 1, 3])) # Evaluate the TensorFlow Graph. - with self.test_session() as sess: + with self.cached_session() as sess: value_rows, value_cols = sess.run([glimpse_rows, glimpse_cols]) # Check dimensions of returned glimpse. @@ -118,7 +118,7 @@ class ExtractGlimpseTest(test.TestCase): def testEmptyTensor(self): empty_image = np.zeros((0, 4, 3, 0)) offsets = np.zeros((0, 2)) - with self.test_session(): + with self.cached_session(): result = image_ops.extract_glimpse(empty_image, [1, 1], offsets) self.assertAllEqual( np.zeros( diff --git a/tensorflow/python/kernel_tests/barrier_ops_test.py b/tensorflow/python/kernel_tests/barrier_ops_test.py index 7f49c63957..4d36b3a465 100644 --- a/tensorflow/python/kernel_tests/barrier_ops_test.py +++ b/tensorflow/python/kernel_tests/barrier_ops_test.py @@ -67,7 +67,7 @@ class BarrierTest(test.TestCase): """, b.barrier_ref.op.node_def) def testInsertMany(self): - with self.test_session(): + with self.cached_session(): b = data_flow_ops.Barrier( (dtypes.float32, dtypes.float32), shapes=((), ()), name="B") size_t = b.ready_size() @@ -83,7 +83,7 @@ class BarrierTest(test.TestCase): self.assertEquals(size_t.eval(), [3]) def testInsertManyEmptyTensor(self): - with self.test_session(): + with self.cached_session(): error_message = ("Empty tensors are not supported, but received shape " r"\'\(0,\)\' at index 1") with self.assertRaisesRegexp(ValueError, error_message): @@ -91,7 +91,7 @@ class BarrierTest(test.TestCase): (dtypes.float32, dtypes.float32), shapes=((1,), (0,)), name="B") def testInsertManyEmptyTensorUnknown(self): - with self.test_session(): + with self.cached_session(): b = data_flow_ops.Barrier((dtypes.float32, dtypes.float32), name="B") size_t = b.ready_size() self.assertEqual([], size_t.get_shape()) @@ -103,7 +103,7 @@ class BarrierTest(test.TestCase): insert_0_op.run() def testTakeMany(self): - with self.test_session() as sess: + with self.cached_session() as sess: b = data_flow_ops.Barrier( (dtypes.float32, dtypes.float32), shapes=((), ()), name="B") size_t = b.ready_size() @@ -128,7 +128,7 @@ class BarrierTest(test.TestCase): self.assertEqual(values_1_val[idx], v1) def testTakeManySmallBatch(self): - with self.test_session() as sess: + with self.cached_session() as sess: b = data_flow_ops.Barrier( (dtypes.float32, dtypes.float32), shapes=((), ()), name="B") size_t = b.ready_size() @@ -192,7 +192,7 @@ class BarrierTest(test.TestCase): insert_1_3_op.run() def testUseBarrierWithShape(self): - with self.test_session() as sess: + with self.cached_session() as sess: b = data_flow_ops.Barrier( (dtypes.float32, dtypes.float32), shapes=((2, 2), (8,)), name="B") size_t = b.ready_size() @@ -221,7 +221,7 @@ class BarrierTest(test.TestCase): self.assertAllEqual(values_1_val[idx], v1) def testParallelInsertMany(self): - with self.test_session() as sess: + with self.cached_session() as sess: b = data_flow_ops.Barrier(dtypes.float32, shapes=()) size_t = b.ready_size() keys = [str(x).encode("ascii") for x in range(10)] @@ -241,7 +241,7 @@ class BarrierTest(test.TestCase): self.assertEqual(values_val[idx], v) def testParallelTakeMany(self): - with self.test_session() as sess: + with self.cached_session() as sess: b = data_flow_ops.Barrier(dtypes.float32, shapes=()) size_t = b.ready_size() keys = [str(x).encode("ascii") for x in range(10)] @@ -275,7 +275,7 @@ class BarrierTest(test.TestCase): zip(keys, values), [(k[0], v[0]) for k, v in zip(key_vals, value_vals)]) def testBlockingTakeMany(self): - with self.test_session() as sess: + with self.cached_session() as sess: b = data_flow_ops.Barrier(dtypes.float32, shapes=()) keys = [str(x).encode("ascii") for x in range(10)] values = [float(x) for x in range(10)] @@ -297,7 +297,7 @@ class BarrierTest(test.TestCase): t.join() def testParallelInsertManyTakeMany(self): - with self.test_session() as sess: + with self.cached_session() as sess: b = data_flow_ops.Barrier( (dtypes.float32, dtypes.int64), shapes=((), (2,))) num_iterations = 100 @@ -376,7 +376,7 @@ class BarrierTest(test.TestCase): self.assertAllEqual(taken_i["values_1"], expected_values_1) def testClose(self): - with self.test_session() as sess: + with self.cached_session() as sess: b = data_flow_ops.Barrier( (dtypes.float32, dtypes.float32), shapes=((), ()), name="B") size_t = b.ready_size() @@ -434,7 +434,7 @@ class BarrierTest(test.TestCase): sess.run(take_t[0]) def testCancel(self): - with self.test_session() as sess: + with self.cached_session() as sess: b = data_flow_ops.Barrier( (dtypes.float32, dtypes.float32), shapes=((), ()), name="B") size_t = b.ready_size() @@ -487,7 +487,7 @@ class BarrierTest(test.TestCase): sess.run(take_t[0]) def _testClosedEmptyBarrierTakeManyAllowSmallBatchRaises(self, cancel): - with self.test_session() as sess: + with self.cached_session() as sess: b = data_flow_ops.Barrier( (dtypes.float32, dtypes.float32), shapes=((), ()), name="B") take_t = b.take_many(1, allow_small_batch=True) @@ -500,7 +500,7 @@ class BarrierTest(test.TestCase): self._testClosedEmptyBarrierTakeManyAllowSmallBatchRaises(cancel=True) def _testParallelInsertManyTakeManyCloseHalfwayThrough(self, cancel): - with self.test_session() as sess: + with self.cached_session() as sess: b = data_flow_ops.Barrier( (dtypes.float32, dtypes.int64), shapes=((), (2,))) num_iterations = 50 @@ -576,7 +576,7 @@ class BarrierTest(test.TestCase): self._testParallelInsertManyTakeManyCloseHalfwayThrough(cancel=True) def _testParallelPartialInsertManyTakeManyCloseHalfwayThrough(self, cancel): - with self.test_session() as sess: + with self.cached_session() as sess: b = data_flow_ops.Barrier( (dtypes.float32, dtypes.int64), shapes=((), (2,))) num_iterations = 100 @@ -676,7 +676,7 @@ class BarrierTest(test.TestCase): self._testParallelPartialInsertManyTakeManyCloseHalfwayThrough(cancel=True) def testIncompatibleSharedBarrierErrors(self): - with self.test_session(): + with self.cached_session(): # Do component types and shapes. b_a_1 = data_flow_ops.Barrier( (dtypes.float32,), shapes=(()), shared_name="b_a") diff --git a/tensorflow/python/kernel_tests/base64_ops_test.py b/tensorflow/python/kernel_tests/base64_ops_test.py index be96f45497..1b399942ef 100644 --- a/tensorflow/python/kernel_tests/base64_ops_test.py +++ b/tensorflow/python/kernel_tests/base64_ops_test.py @@ -48,7 +48,7 @@ class Base64OpsTest(test_util.TensorFlowTestCase): return base64_msg def _RunTest(self, msg, pad): - with self.test_session() as sess: + with self.cached_session() as sess: if pad: encoded, decoded = sess.run([self._encoded_t, self._decoded_t], feed_dict={self._msg: msg}) @@ -92,7 +92,7 @@ class Base64OpsTest(test_util.TensorFlowTestCase): encoded = string_ops.encode_base64(msg, pad=pad) decoded = string_ops.decode_base64(encoded) - with self.test_session() as sess: + with self.cached_session() as sess: encoded_value, decoded_value = sess.run([encoded, decoded]) self.assertEqual(encoded_value.shape, msg.shape) @@ -102,7 +102,7 @@ class Base64OpsTest(test_util.TensorFlowTestCase): def try_decode(enc): self._decoded_f.eval(feed_dict={self._encoded_f: enc}) - with self.test_session(): + with self.cached_session(): # Invalid length. msg = np.random.bytes(99) enc = base64.urlsafe_b64encode(msg) diff --git a/tensorflow/python/kernel_tests/basic_gpu_test.py b/tensorflow/python/kernel_tests/basic_gpu_test.py index 987a6ffcd4..67e8618198 100644 --- a/tensorflow/python/kernel_tests/basic_gpu_test.py +++ b/tensorflow/python/kernel_tests/basic_gpu_test.py @@ -174,7 +174,7 @@ class BroadcastSimpleTest(test.TestCase): numeric_gradient_type=None): z = np_func(x, y) zs = list(z.shape) - with self.test_session(): + with self.cached_session(): inx = ops.convert_to_tensor(x) iny = ops.convert_to_tensor(y) if x.dtype in (np.float32, np.float64): @@ -195,7 +195,7 @@ class BroadcastSimpleTest(test.TestCase): numeric_gradient_type=None): z = np_func(x, y) zs = list(z.shape) - with self.test_session(): + with self.cached_session(): inx = ops.convert_to_tensor(x) iny = ops.convert_to_tensor(y) if x.dtype in (np.float32, np.float64): @@ -260,7 +260,7 @@ class GpuMultiSessionMemoryTest(test_util.TensorFlowTestCase): threads = [] results = [] for _ in xrange(n_threads): - session = self.test_session(graph=ops.Graph(), use_gpu=True) + session = self.session(graph=ops.Graph(), use_gpu=True) results.append(set()) args = (session, results[-1]) threads.append(threading.Thread(target=self._run_session, args=args)) diff --git a/tensorflow/python/kernel_tests/batch_gather_op_test.py b/tensorflow/python/kernel_tests/batch_gather_op_test.py index 8e7ae89f9d..7dd347989a 100644 --- a/tensorflow/python/kernel_tests/batch_gather_op_test.py +++ b/tensorflow/python/kernel_tests/batch_gather_op_test.py @@ -86,7 +86,7 @@ class GatherTest(test.TestCase): def testString(self): params = np.array([[b"asdf", b"zxcv"], [b"qwer", b"uiop"]]) - with self.test_session(): + with self.cached_session(): indices_tf = constant_op.constant([1]) self.assertAllEqual([[b"qwer", b"uiop"]], array_ops.batch_gather(params, indices_tf).eval()) diff --git a/tensorflow/python/kernel_tests/batchtospace_op_test.py b/tensorflow/python/kernel_tests/batchtospace_op_test.py index 6143cd3baa..03f3f64353 100644 --- a/tensorflow/python/kernel_tests/batchtospace_op_test.py +++ b/tensorflow/python/kernel_tests/batchtospace_op_test.py @@ -60,7 +60,7 @@ class BatchToSpaceDepthToSpace(test.TestCase, PythonOpImpl): array_ops.depth_to_space( array_ops.transpose(x, [3, 1, 2, 0]), block_size=block_size), [3, 1, 2, 0]) - with self.test_session(): + with self.cached_session(): self.assertAllEqual(y1.eval(), y2.eval()) @@ -235,7 +235,7 @@ class BatchToSpaceGradientTest(test.TestCase, PythonOpImpl): # Check the gradients. def _checkGrad(self, x, crops, block_size): assert 4 == x.ndim - with self.test_session(): + with self.cached_session(): tf_x = ops.convert_to_tensor(x) tf_y = self.batch_to_space(tf_x, crops, block_size) epsilon = 1e-5 @@ -293,7 +293,7 @@ class BatchToSpaceNDGradientTest(test.TestCase): block_shape = np.array(block_shape) crops = constant_op.constant( np.array(crops).reshape((len(block_shape), 2)), crops_dtype) - with self.test_session(): + with self.cached_session(): tf_x = ops.convert_to_tensor(x) tf_y = array_ops.batch_to_space_nd(tf_x, block_shape, crops) epsilon = 1e-5 diff --git a/tensorflow/python/kernel_tests/bcast_ops_test.py b/tensorflow/python/kernel_tests/bcast_ops_test.py index 3305e55c05..3ec820aead 100644 --- a/tensorflow/python/kernel_tests/bcast_ops_test.py +++ b/tensorflow/python/kernel_tests/bcast_ops_test.py @@ -28,11 +28,11 @@ from tensorflow.python.platform import test class BcastOpsTest(test.TestCase): def _GetBroadcastShape(self, xs, ys): - with self.test_session() as sess: + with self.cached_session() as sess: return sess.run(broadcast_args(xs, ys)) def _GetGradientArgs(self, xs, ys): - with self.test_session() as sess: + with self.cached_session() as sess: return sess.run(broadcast_gradient_args(xs, ys)) def testBasic(self): diff --git a/tensorflow/python/kernel_tests/betainc_op_test.py b/tensorflow/python/kernel_tests/betainc_op_test.py index 16fdedac41..92d21462d5 100644 --- a/tensorflow/python/kernel_tests/betainc_op_test.py +++ b/tensorflow/python/kernel_tests/betainc_op_test.py @@ -47,7 +47,7 @@ class BetaincTest(test.TestCase): tf_b_s = constant_op.constant(b_s, dtype=dtype) tf_x_s = constant_op.constant(x_s, dtype=dtype) tf_out_t = math_ops.betainc(tf_a_s, tf_b_s, tf_x_s) - with self.test_session(): + with self.cached_session(): tf_out = tf_out_t.eval() scipy_out = special.betainc(a_s, b_s, x_s).astype(np_dt) @@ -60,13 +60,13 @@ class BetaincTest(test.TestCase): # Test out-of-range values (most should return nan output) combinations = list(itertools.product([-1, 0, 0.5, 1.0, 1.5], repeat=3)) a_comb, b_comb, x_comb = np.asarray(list(zip(*combinations)), dtype=np_dt) - with self.test_session(): + with self.cached_session(): tf_comb = math_ops.betainc(a_comb, b_comb, x_comb).eval() scipy_comb = special.betainc(a_comb, b_comb, x_comb).astype(np_dt) self.assertAllCloseAccordingToType(scipy_comb, tf_comb) # Test broadcasting between scalars and other shapes - with self.test_session(): + with self.cached_session(): self.assertAllCloseAccordingToType( special.betainc(0.1, b_s, x_s).astype(np_dt), math_ops.betainc(0.1, b_s, x_s).eval(), @@ -96,7 +96,7 @@ class BetaincTest(test.TestCase): with self.assertRaisesRegexp(ValueError, "must be equal"): math_ops.betainc(0.5, [0.5], [[0.5]]) - with self.test_session(): + with self.cached_session(): with self.assertRaisesOpError("Shapes of .* are inconsistent"): a_p = array_ops.placeholder(dtype) b_p = array_ops.placeholder(dtype) @@ -140,7 +140,7 @@ class BetaincTest(test.TestCase): self._testBetaInc(a_s, b_s, x_s, dtypes.float32) def testBetaIncFpropAndBpropAreNeverNAN(self): - with self.test_session() as sess: + with self.cached_session() as sess: space = np.logspace(-8, 5).tolist() space_x = np.linspace(1e-16, 1 - 1e-16).tolist() ga_s, gb_s, gx_s = zip(*list(itertools.product(space, space, space_x))) @@ -161,7 +161,7 @@ class BetaincTest(test.TestCase): def testBetaIncGrads(self): err_tolerance = 1e-3 - with self.test_session(): + with self.cached_session(): # Test gradient ga_s = np.abs(np.random.randn(2, 2) * 30) # in (0, infty) gb_s = np.abs(np.random.randn(2, 2) * 30) # in (0, infty) diff --git a/tensorflow/python/kernel_tests/bincount_op_test.py b/tensorflow/python/kernel_tests/bincount_op_test.py index 2767df127e..8a58b3f97e 100644 --- a/tensorflow/python/kernel_tests/bincount_op_test.py +++ b/tensorflow/python/kernel_tests/bincount_op_test.py @@ -93,7 +93,7 @@ class BincountTest(test_util.TensorFlowTestCase): def test_negative(self): # unsorted_segment_sum will only report InvalidArgumentError on CPU - with self.test_session(): + with self.cached_session(): with self.assertRaises(errors.InvalidArgumentError): math_ops.bincount([1, 2, 3, -1, 6, 8]).eval() diff --git a/tensorflow/python/kernel_tests/boosted_trees/BUILD b/tensorflow/python/kernel_tests/boosted_trees/BUILD index 4f92ab0795..20446781f0 100644 --- a/tensorflow/python/kernel_tests/boosted_trees/BUILD +++ b/tensorflow/python/kernel_tests/boosted_trees/BUILD @@ -74,3 +74,16 @@ tf_py_test( "//tensorflow/python:resources", ], ) + +tf_py_test( + name = "quantile_ops_test", + size = "small", + srcs = ["quantile_ops_test.py"], + additional_deps = [ + "//tensorflow/core/kernels/boosted_trees:boosted_trees_proto_py", + "//tensorflow/python:boosted_trees_ops", + "//tensorflow/python:constant_op", + "//tensorflow/python:framework_test_lib", + "//tensorflow/python:resources", + ], +) diff --git a/tensorflow/python/kernel_tests/boosted_trees/prediction_ops_test.py b/tensorflow/python/kernel_tests/boosted_trees/prediction_ops_test.py index 4e31b1ea2a..7cdc67f83f 100644 --- a/tensorflow/python/kernel_tests/boosted_trees/prediction_ops_test.py +++ b/tensorflow/python/kernel_tests/boosted_trees/prediction_ops_test.py @@ -30,7 +30,7 @@ class TrainingPredictionOpsTest(test_util.TensorFlowTestCase): def testCachedPredictionOnEmptyEnsemble(self): """Tests that prediction on a dummy ensemble does not fail.""" - with self.test_session() as session: + with self.cached_session() as session: # Create a dummy ensemble. tree_ensemble = boosted_trees_ops.TreeEnsemble( 'ensemble', serialized_proto='') @@ -63,7 +63,7 @@ class TrainingPredictionOpsTest(test_util.TensorFlowTestCase): def testNoCachedPredictionButTreeExists(self): """Tests that predictions are updated once trees are added.""" - with self.test_session() as session: + with self.cached_session() as session: tree_ensemble_config = boosted_trees_pb2.TreeEnsemble() text_format.Merge(""" trees { @@ -129,7 +129,7 @@ class TrainingPredictionOpsTest(test_util.TensorFlowTestCase): def testCachedPredictionIsCurrent(self): """Tests that prediction based on previous node in the tree works.""" - with self.test_session() as session: + with self.cached_session() as session: tree_ensemble_config = boosted_trees_pb2.TreeEnsemble() text_format.Merge(""" trees { @@ -201,7 +201,7 @@ class TrainingPredictionOpsTest(test_util.TensorFlowTestCase): def testCachedPredictionFromTheSameTree(self): """Tests that prediction based on previous node in the tree works.""" - with self.test_session() as session: + with self.cached_session() as session: tree_ensemble_config = boosted_trees_pb2.TreeEnsemble() text_format.Merge(""" trees { @@ -315,7 +315,7 @@ class TrainingPredictionOpsTest(test_util.TensorFlowTestCase): def testCachedPredictionFromPreviousTree(self): """Tests the predictions work when we have cache from previous trees.""" - with self.test_session() as session: + with self.cached_session() as session: tree_ensemble_config = boosted_trees_pb2.TreeEnsemble() text_format.Merge(""" trees { @@ -445,9 +445,81 @@ class TrainingPredictionOpsTest(test_util.TensorFlowTestCase): # change= 0.1(1.14+7.0-7.0) self.assertAllClose([[1], [0.114]], logits_updates) + def testCategoricalSplits(self): + """Tests the training prediction work for categorical splits.""" + with self.cached_session() as session: + tree_ensemble_config = boosted_trees_pb2.TreeEnsemble() + text_format.Merge( + """ + trees { + nodes { + categorical_split { + feature_id: 1 + value: 2 + left_id: 1 + right_id: 2 + } + } + nodes { + categorical_split { + feature_id: 0 + value: 13 + left_id: 3 + right_id: 4 + } + } + nodes { + leaf { + scalar: 7.0 + } + } + nodes { + leaf { + scalar: 5.0 + } + } + nodes { + leaf { + scalar: 6.0 + } + } + } + tree_weights: 1.0 + tree_metadata { + is_finalized: true + } + """, tree_ensemble_config) + + # Create existing ensemble with one root split + tree_ensemble = boosted_trees_ops.TreeEnsemble( + 'ensemble', serialized_proto=tree_ensemble_config.SerializeToString()) + tree_ensemble_handle = tree_ensemble.resource_handle + resources.initialize_resources(resources.shared_resources()).run() + + feature_0_values = [13, 1, 3] + feature_1_values = [2, 2, 1] + + # No previous cached values. + cached_tree_ids = [0, 0, 0] + cached_node_ids = [0, 0, 0] + + # Grow tree ensemble. + predict_op = boosted_trees_ops.training_predict( + tree_ensemble_handle, + cached_tree_ids=cached_tree_ids, + cached_node_ids=cached_node_ids, + bucketized_features=[feature_0_values, feature_1_values], + logits_dimension=1) + + logits_updates, new_tree_ids, new_node_ids = session.run(predict_op) + + self.assertAllClose([0, 0, 0], new_tree_ids) + self.assertAllClose([3, 4, 2], new_node_ids) + self.assertAllClose([[5.], [6.], [7.]], logits_updates) + def testCachedPredictionFromTheSameTreeWithPostPrunedNodes(self): """Tests that prediction based on previous node in the tree works.""" - with self.test_session() as session: + with self.cached_session() as session: tree_ensemble_config = boosted_trees_pb2.TreeEnsemble() text_format.Merge(""" trees { @@ -577,7 +649,7 @@ class TrainingPredictionOpsTest(test_util.TensorFlowTestCase): def testCachedPredictionFromThePreviousTreeWithPostPrunedNodes(self): """Tests that prediction based on previous node in the tree works.""" - with self.test_session() as session: + with self.cached_session() as session: tree_ensemble_config = boosted_trees_pb2.TreeEnsemble() text_format.Merge(""" trees { @@ -722,7 +794,7 @@ class TrainingPredictionOpsTest(test_util.TensorFlowTestCase): def testCachedPredictionTheWholeTreeWasPruned(self): """Tests that prediction based on previous node in the tree works.""" - with self.test_session() as session: + with self.cached_session() as session: tree_ensemble_config = boosted_trees_pb2.TreeEnsemble() text_format.Merge(""" trees { @@ -794,7 +866,7 @@ class PredictionOpsTest(test_util.TensorFlowTestCase): def testPredictionOnEmptyEnsemble(self): """Tests that prediction on a empty ensemble does not fail.""" - with self.test_session() as session: + with self.cached_session() as session: # Create an empty ensemble. tree_ensemble = boosted_trees_ops.TreeEnsemble( 'ensemble', serialized_proto='') @@ -816,7 +888,7 @@ class PredictionOpsTest(test_util.TensorFlowTestCase): def testPredictionMultipleTree(self): """Tests the predictions work when we have multiple trees.""" - with self.test_session() as session: + with self.cached_session() as session: tree_ensemble_config = boosted_trees_pb2.TreeEnsemble() text_format.Merge(""" trees { @@ -924,13 +996,232 @@ class PredictionOpsTest(test_util.TensorFlowTestCase): logits = session.run(predict_op) self.assertAllClose(expected_logits, logits) + def testCategoricalSplits(self): + """Tests the predictions work for categorical splits.""" + with self.cached_session() as session: + tree_ensemble_config = boosted_trees_pb2.TreeEnsemble() + text_format.Merge( + """ + trees { + nodes { + categorical_split { + feature_id: 1 + value: 2 + left_id: 1 + right_id: 2 + } + } + nodes { + categorical_split { + feature_id: 0 + value: 13 + left_id: 3 + right_id: 4 + } + } + nodes { + leaf { + scalar: 7.0 + } + } + nodes { + leaf { + scalar: 5.0 + } + } + nodes { + leaf { + scalar: 6.0 + } + } + } + tree_weights: 1.0 + """, tree_ensemble_config) + + # Create existing ensemble with one root split + tree_ensemble = boosted_trees_ops.TreeEnsemble( + 'ensemble', serialized_proto=tree_ensemble_config.SerializeToString()) + tree_ensemble_handle = tree_ensemble.resource_handle + resources.initialize_resources(resources.shared_resources()).run() + + feature_0_values = [13, 1, 3] + feature_1_values = [2, 2, 1] + + expected_logits = [[5.], [6.], [7.]] + + # Prediction should work fine. + predict_op = boosted_trees_ops.predict( + tree_ensemble_handle, + bucketized_features=[feature_0_values, feature_1_values], + logits_dimension=1) + + logits = session.run(predict_op) + self.assertAllClose(expected_logits, logits) + class FeatureContribsOpsTest(test_util.TensorFlowTestCase): """Tests feature contribs ops for model understanding.""" + def testContribsForOnlyABiasNode(self): + """Tests case when, after training, only left with a bias node. + + For example, this could happen if the final ensemble contains one tree that + got pruned up to the root. + """ + with self.cached_session() as session: + tree_ensemble_config = boosted_trees_pb2.TreeEnsemble() + text_format.Merge( + """ + trees { + nodes { + leaf { + scalar: 1.72 + } + } + } + tree_weights: 0.1 + tree_metadata: { + num_layers_grown: 0 + } + """, tree_ensemble_config) + + tree_ensemble = boosted_trees_ops.TreeEnsemble( + 'ensemble', serialized_proto=tree_ensemble_config.SerializeToString()) + tree_ensemble_handle = tree_ensemble.resource_handle + resources.initialize_resources(resources.shared_resources()).run() + + # All features are unused. + feature_0_values = [36, 32] + feature_1_values = [13, -29] + feature_2_values = [11, 27] + + # Expected logits are computed by traversing the logit path and + # subtracting child logits from parent logits. + bias = 1.72 * 0.1 # Root node of tree_0. + expected_feature_ids = ((), ()) + expected_logits_paths = ((bias,), (bias,)) + + bucketized_features = [ + feature_0_values, feature_1_values, feature_2_values + ] + + debug_op = boosted_trees_ops.example_debug_outputs( + tree_ensemble_handle, + bucketized_features=bucketized_features, + logits_dimension=1) + + serialized_examples_debug_outputs = session.run(debug_op) + feature_ids = [] + logits_paths = [] + for example in serialized_examples_debug_outputs: + example_debug_outputs = boosted_trees_pb2.DebugOutput() + example_debug_outputs.ParseFromString(example) + feature_ids.append(example_debug_outputs.feature_ids) + logits_paths.append(example_debug_outputs.logits_path) + + self.assertAllClose(feature_ids, expected_feature_ids) + self.assertAllClose(logits_paths, expected_logits_paths) + + def testContribsMultipleTreeWhenFirstTreeIsABiasNode(self): + """Tests case when, after training, first tree contains only a bias node.""" + with self.cached_session() as session: + tree_ensemble_config = boosted_trees_pb2.TreeEnsemble() + text_format.Merge( + """ + trees { + nodes { + leaf { + scalar: 1.72 + } + } + } + trees { + nodes { + bucketized_split { + feature_id: 2 + threshold: 26 + left_id: 1 + right_id: 2 + } + } + nodes { + bucketized_split { + feature_id: 0 + threshold: 50 + left_id: 3 + right_id: 4 + } + metadata { + original_leaf: {scalar: 5.5} + } + } + nodes { + leaf { + scalar: 7.0 + } + } + nodes { + leaf { + scalar: 5.0 + } + } + nodes { + leaf { + scalar: 6.0 + } + } + } + tree_weights: 1. + tree_weights: 0.1 + tree_metadata: { + num_layers_grown: 0 + } + tree_metadata: { + num_layers_grown: 1 + } + """, tree_ensemble_config) + + tree_ensemble = boosted_trees_ops.TreeEnsemble( + 'ensemble', serialized_proto=tree_ensemble_config.SerializeToString()) + tree_ensemble_handle = tree_ensemble.resource_handle + resources.initialize_resources(resources.shared_resources()).run() + + feature_0_values = [36, 32] + feature_1_values = [13, -29] # Unused feature. + feature_2_values = [11, 27] + + # Expected logits are computed by traversing the logit path and + # subtracting child logits from parent logits. + expected_feature_ids = ((2, 0), (2,)) + # bias = 1.72 * 1. # Root node of tree_0. + # example_0 : (bias, 0.1 * 5.5 + bias, 0.1 * 5. + bias) + # example_1 : (bias, 0.1 * 7. + bias ) + expected_logits_paths = ((1.72, 2.27, 2.22), (1.72, 2.42)) + + bucketized_features = [ + feature_0_values, feature_1_values, feature_2_values + ] + + debug_op = boosted_trees_ops.example_debug_outputs( + tree_ensemble_handle, + bucketized_features=bucketized_features, + logits_dimension=1) + + serialized_examples_debug_outputs = session.run(debug_op) + feature_ids = [] + logits_paths = [] + for example in serialized_examples_debug_outputs: + example_debug_outputs = boosted_trees_pb2.DebugOutput() + example_debug_outputs.ParseFromString(example) + feature_ids.append(example_debug_outputs.feature_ids) + logits_paths.append(example_debug_outputs.logits_path) + + self.assertAllClose(feature_ids, expected_feature_ids) + self.assertAllClose(logits_paths, expected_logits_paths) + def testContribsMultipleTree(self): """Tests that the contribs work when we have multiple trees.""" - with self.test_session() as session: + with self.cached_session() as session: tree_ensemble_config = boosted_trees_pb2.TreeEnsemble() text_format.Merge( """ @@ -1018,11 +1309,14 @@ class FeatureContribsOpsTest(test_util.TensorFlowTestCase): tree_weights: 0.2 tree_weights: 1.0 tree_metadata: { - num_layers_grown: 1} + num_layers_grown: 1 + } tree_metadata: { - num_layers_grown: 2} + num_layers_grown: 2 + } tree_metadata: { - num_layers_grown: 1} + num_layers_grown: 1 + } """, tree_ensemble_config) tree_ensemble = boosted_trees_ops.TreeEnsemble( diff --git a/tensorflow/python/kernel_tests/boosted_trees/quantile_ops_test.py b/tensorflow/python/kernel_tests/boosted_trees/quantile_ops_test.py new file mode 100644 index 0000000000..e0d46bae83 --- /dev/null +++ b/tensorflow/python/kernel_tests/boosted_trees/quantile_ops_test.py @@ -0,0 +1,140 @@ +# Copyright 2017 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== +"""Test for checking quantile related ops.""" + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +from tensorflow.python.framework import constant_op +from tensorflow.python.framework import dtypes +from tensorflow.python.framework import test_util +from tensorflow.python.ops import boosted_trees_ops +from tensorflow.python.ops import resources +from tensorflow.python.ops.gen_boosted_trees_ops import boosted_trees_quantile_stream_resource_handle_op as resource_handle_op +from tensorflow.python.ops.gen_boosted_trees_ops import is_boosted_trees_quantile_stream_resource_initialized as resource_initialized +from tensorflow.python.platform import googletest + + +class QuantileOpsTest(test_util.TensorFlowTestCase): + + def create_resource(self, name, eps, max_elements, num_streams=1): + quantile_accumulator_handle = resource_handle_op( + container="", shared_name=name, name=name) + create_op = boosted_trees_ops.create_quantile_stream_resource( + quantile_accumulator_handle, + epsilon=eps, + max_elements=max_elements, + num_streams=num_streams) + is_initialized_op = resource_initialized(quantile_accumulator_handle) + resources.register_resource(quantile_accumulator_handle, create_op, + is_initialized_op) + return quantile_accumulator_handle + + def setUp(self): + """Sets up the quantile ops test as follows. + + Create a batch of 6 examples having 2 features + The data looks like this + | Instance | instance weights | Feature 0 | Feature 1 + | 0 | 10 | 1.2 | 2.3 + | 1 | 1 | 12.1 | 1.2 + | 2 | 1 | 0.3 | 1.1 + | 3 | 1 | 0.5 | 2.6 + | 4 | 1 | 0.6 | 3.2 + | 5 | 1 | 2.2 | 0.8 + """ + + self._feature_0 = constant_op.constant( + [[1.2], [12.1], [0.3], [0.5], [0.6], [2.2]], dtype=dtypes.float32) + self._feature_1 = constant_op.constant( + [[2.3], [1.2], [1.1], [2.6], [3.2], [0.8]], dtype=dtypes.float32) + self._feature_0_boundaries = constant_op.constant( + [0.3, 0.6, 1.2, 12.1], dtype=dtypes.float32) + self._feature_1_boundaries = constant_op.constant( + [0.8, 1.2, 2.3, 3.2], dtype=dtypes.float32) + self._feature_0_quantiles = constant_op.constant( + [[2], [3], [0], [1], [1], [3]], dtype=dtypes.int32) + self._feature_1_quantiles = constant_op.constant( + [[2], [1], [1], [3], [3], [0]], dtype=dtypes.int32) + + self._example_weights = constant_op.constant( + [10, 1, 1, 1, 1, 1], dtype=dtypes.float32) + + self.eps = 0.01 + self.max_elements = 1 << 16 + self.num_quantiles = constant_op.constant(3, dtype=dtypes.int64) + + def testBasicQuantileBucketsSingleResource(self): + with self.cached_session() as sess: + quantile_accumulator_handle = self.create_resource("floats", self.eps, + self.max_elements, 2) + resources.initialize_resources(resources.shared_resources()).run() + summaries = boosted_trees_ops.make_quantile_summaries( + [self._feature_0, self._feature_1], self._example_weights, + epsilon=self.eps) + summary_op = boosted_trees_ops.quantile_add_summaries( + quantile_accumulator_handle, summaries) + flush_op = boosted_trees_ops.quantile_flush( + quantile_accumulator_handle, self.num_quantiles) + buckets = boosted_trees_ops.get_bucket_boundaries( + quantile_accumulator_handle, num_features=2) + quantiles = boosted_trees_ops.boosted_trees_bucketize( + [self._feature_0, self._feature_1], buckets) + sess.run(summary_op) + sess.run(flush_op) + self.assertAllClose(self._feature_0_boundaries, buckets[0].eval()) + self.assertAllClose(self._feature_1_boundaries, buckets[1].eval()) + + self.assertAllClose(self._feature_0_quantiles, quantiles[0].eval()) + self.assertAllClose(self._feature_1_quantiles, quantiles[1].eval()) + + def testBasicQuantileBucketsMultipleResources(self): + with self.cached_session() as sess: + quantile_accumulator_handle_0 = self.create_resource("float_0", self.eps, + self.max_elements) + quantile_accumulator_handle_1 = self.create_resource("float_1", self.eps, + self.max_elements) + resources.initialize_resources(resources.shared_resources()).run() + summaries = boosted_trees_ops.make_quantile_summaries( + [self._feature_0, self._feature_1], self._example_weights, + epsilon=self.eps) + summary_op_0 = boosted_trees_ops.quantile_add_summaries( + quantile_accumulator_handle_0, + [summaries[0]]) + summary_op_1 = boosted_trees_ops.quantile_add_summaries( + quantile_accumulator_handle_1, + [summaries[1]]) + flush_op_0 = boosted_trees_ops.quantile_flush( + quantile_accumulator_handle_0, self.num_quantiles) + flush_op_1 = boosted_trees_ops.quantile_flush( + quantile_accumulator_handle_1, self.num_quantiles) + bucket_0 = boosted_trees_ops.get_bucket_boundaries( + quantile_accumulator_handle_0, num_features=1) + bucket_1 = boosted_trees_ops.get_bucket_boundaries( + quantile_accumulator_handle_1, num_features=1) + quantiles = boosted_trees_ops.boosted_trees_bucketize( + [self._feature_0, self._feature_1], bucket_0 + bucket_1) + sess.run([summary_op_0, summary_op_1]) + sess.run([flush_op_0, flush_op_1]) + self.assertAllClose(self._feature_0_boundaries, bucket_0[0].eval()) + self.assertAllClose(self._feature_1_boundaries, bucket_1[0].eval()) + + self.assertAllClose(self._feature_0_quantiles, quantiles[0].eval()) + self.assertAllClose(self._feature_1_quantiles, quantiles[1].eval()) + + +if __name__ == "__main__": + googletest.main() diff --git a/tensorflow/python/kernel_tests/boosted_trees/resource_ops_test.py b/tensorflow/python/kernel_tests/boosted_trees/resource_ops_test.py index d5f0c22d6e..65bb9ab55f 100644 --- a/tensorflow/python/kernel_tests/boosted_trees/resource_ops_test.py +++ b/tensorflow/python/kernel_tests/boosted_trees/resource_ops_test.py @@ -31,7 +31,7 @@ class ResourceOpsTest(test_util.TensorFlowTestCase): """Tests resource_ops.""" def testCreate(self): - with self.test_session(): + with self.cached_session(): ensemble = boosted_trees_ops.TreeEnsemble('ensemble') resources.initialize_resources(resources.shared_resources()).run() stamp_token = ensemble.get_stamp_token() @@ -44,7 +44,7 @@ class ResourceOpsTest(test_util.TensorFlowTestCase): self.assertAllEqual([0, 1], nodes_range.eval()) def testCreateWithProto(self): - with self.test_session(): + with self.cached_session(): ensemble_proto = boosted_trees_pb2.TreeEnsemble() text_format.Merge( """ @@ -161,7 +161,7 @@ class ResourceOpsTest(test_util.TensorFlowTestCase): self.assertAllEqual([16, 19], nodes_range.eval()) def testSerializeDeserialize(self): - with self.test_session(): + with self.cached_session(): # Initialize. ensemble = boosted_trees_ops.TreeEnsemble('ensemble', stamp_token=5) resources.initialize_resources(resources.shared_resources()).run() diff --git a/tensorflow/python/kernel_tests/boosted_trees/stats_ops_test.py b/tensorflow/python/kernel_tests/boosted_trees/stats_ops_test.py index 568e695fd5..09e9cfa3af 100644 --- a/tensorflow/python/kernel_tests/boosted_trees/stats_ops_test.py +++ b/tensorflow/python/kernel_tests/boosted_trees/stats_ops_test.py @@ -30,7 +30,7 @@ class StatsOpsTest(test_util.TensorFlowTestCase): def testCalculateBestGainsWithoutRegularization(self): """Testing Gain calculation without any regularization.""" - with self.test_session() as sess: + with self.cached_session() as sess: max_splits = 7 node_id_range = [1, 3] # node 1 through 2 will be processed. stats_summary_list = [ @@ -78,7 +78,7 @@ class StatsOpsTest(test_util.TensorFlowTestCase): def testCalculateBestGainsWithL2(self): """Testing Gain calculation with L2.""" - with self.test_session() as sess: + with self.cached_session() as sess: max_splits = 7 node_id_range = [1, 3] # node 1 through 2 will be processed. stats_summary_list = [ @@ -126,7 +126,7 @@ class StatsOpsTest(test_util.TensorFlowTestCase): def testCalculateBestGainsWithL1(self): """Testing Gain calculation with L1.""" - with self.test_session() as sess: + with self.cached_session() as sess: max_splits = 7 node_id_range = [1, 3] # node 1 through 2 will be processed. stats_summary_list = [ @@ -177,7 +177,7 @@ class StatsOpsTest(test_util.TensorFlowTestCase): def testCalculateBestGainsWithTreeComplexity(self): """Testing Gain calculation with L2.""" - with self.test_session() as sess: + with self.cached_session() as sess: max_splits = 7 node_id_range = [1, 3] # node 1 through 2 will be processed. stats_summary_list = [ @@ -229,7 +229,7 @@ class StatsOpsTest(test_util.TensorFlowTestCase): def testCalculateBestGainsWithMinNodeWeight(self): """Testing Gain calculation without any regularization.""" - with self.test_session() as sess: + with self.cached_session() as sess: max_splits = 7 node_id_range = [1, 3] # node 1 through 2 will be processed. stats_summary_list = [ @@ -276,7 +276,7 @@ class StatsOpsTest(test_util.TensorFlowTestCase): def testCalculateBestGainsWithMinNodeWeightNoSplitOnFeturePossible(self): """Testing Gain calculation without any regularization.""" - with self.test_session() as sess: + with self.cached_session() as sess: max_splits = 7 node_id_range = [1, 3] # node 1 through 2 will be processed. stats_summary_list = [ @@ -329,7 +329,7 @@ class StatsOpsTest(test_util.TensorFlowTestCase): def testMakeStatsSummarySimple(self): """Simple test for MakeStatsSummary.""" - with self.test_session(): + with self.cached_session(): self.assertAllClose([[[[1., 5.], [2., 6.]], [[3., 7.], [4., 8.]]]], boosted_trees_ops.make_stats_summary( node_ids=[0, 0, 1, 1], @@ -341,7 +341,7 @@ class StatsOpsTest(test_util.TensorFlowTestCase): def testMakeStatsSummaryAccumulate(self): """Tests that Summary actually accumulates.""" - with self.test_session(): + with self.cached_session(): max_splits = 3 num_buckets = 4 node_ids = [1, 1, 2, 2, 1, 1, 2, 0] @@ -363,7 +363,7 @@ class StatsOpsTest(test_util.TensorFlowTestCase): def testMakeStatsSummaryMultipleFeatures(self): """Tests that MakeStatsSummary works for multiple features.""" - with self.test_session(): + with self.cached_session(): max_splits = 3 num_buckets = 4 node_ids = [1, 1, 2, 2, 1, 1, 2, 0] @@ -392,7 +392,7 @@ class StatsOpsTest(test_util.TensorFlowTestCase): result.eval()) def _verify_precision(self, length): - with self.test_session(): + with self.cached_session(): max_splits = 1 num_buckets = 1 node_ids = array_ops.fill([length], 0) diff --git a/tensorflow/python/kernel_tests/boosted_trees/training_ops_test.py b/tensorflow/python/kernel_tests/boosted_trees/training_ops_test.py index d55240297a..ea022820e4 100644 --- a/tensorflow/python/kernel_tests/boosted_trees/training_ops_test.py +++ b/tensorflow/python/kernel_tests/boosted_trees/training_ops_test.py @@ -32,7 +32,7 @@ class UpdateTreeEnsembleOpTest(test_util.TensorFlowTestCase): def testGrowWithEmptyEnsemble(self): """Test growing an empty ensemble.""" - with self.test_session() as session: + with self.cached_session() as session: # Create empty ensemble. tree_ensemble = boosted_trees_ops.TreeEnsemble('ensemble') tree_ensemble_handle = tree_ensemble.resource_handle @@ -141,7 +141,7 @@ class UpdateTreeEnsembleOpTest(test_util.TensorFlowTestCase): def testBiasCenteringOnEmptyEnsemble(self): """Test growing with bias centering on an empty ensemble.""" - with self.test_session() as session: + with self.cached_session() as session: # Create empty ensemble. tree_ensemble = boosted_trees_ops.TreeEnsemble('ensemble') tree_ensemble_handle = tree_ensemble.resource_handle @@ -184,7 +184,7 @@ class UpdateTreeEnsembleOpTest(test_util.TensorFlowTestCase): def testGrowExistingEnsembleTreeNotFinalized(self): """Test growing an existing ensemble with the last tree not finalized.""" - with self.test_session() as session: + with self.cached_session() as session: tree_ensemble_config = boosted_trees_pb2.TreeEnsemble() text_format.Merge(""" trees { @@ -368,7 +368,7 @@ class UpdateTreeEnsembleOpTest(test_util.TensorFlowTestCase): def testGrowExistingEnsembleTreeFinalized(self): """Test growing an existing ensemble with the last tree finalized.""" - with self.test_session() as session: + with self.cached_session() as session: tree_ensemble_config = boosted_trees_pb2.TreeEnsemble() text_format.Merge(""" trees { @@ -517,7 +517,7 @@ class UpdateTreeEnsembleOpTest(test_util.TensorFlowTestCase): def testPrePruning(self): """Test growing an existing ensemble with pre-pruning.""" - with self.test_session() as session: + with self.cached_session() as session: tree_ensemble_config = boosted_trees_pb2.TreeEnsemble() text_format.Merge(""" trees { @@ -673,7 +673,7 @@ class UpdateTreeEnsembleOpTest(test_util.TensorFlowTestCase): def testMetadataWhenCantSplitDueToEmptySplits(self): """Test that the metadata is updated even though we can't split.""" - with self.test_session() as session: + with self.cached_session() as session: tree_ensemble_config = boosted_trees_pb2.TreeEnsemble() text_format.Merge( """ @@ -784,7 +784,7 @@ class UpdateTreeEnsembleOpTest(test_util.TensorFlowTestCase): def testMetadataWhenCantSplitDuePrePruning(self): """Test metadata is updated correctly when no split due to prepruning.""" - with self.test_session() as session: + with self.cached_session() as session: tree_ensemble_config = boosted_trees_pb2.TreeEnsemble() text_format.Merge( """ @@ -919,7 +919,7 @@ class UpdateTreeEnsembleOpTest(test_util.TensorFlowTestCase): def testPostPruningOfSomeNodes(self): """Test growing an ensemble with post-pruning.""" - with self.test_session() as session: + with self.cached_session() as session: # Create empty ensemble. tree_ensemble_config = boosted_trees_pb2.TreeEnsemble() tree_ensemble = boosted_trees_ops.TreeEnsemble( @@ -1253,7 +1253,7 @@ class UpdateTreeEnsembleOpTest(test_util.TensorFlowTestCase): def testPostPruningOfAllNodes(self): """Test growing an ensemble with post-pruning, with all nodes are pruned.""" - with self.test_session() as session: + with self.cached_session() as session: # Create empty ensemble. # Create empty ensemble. tree_ensemble_config = boosted_trees_pb2.TreeEnsemble() @@ -1436,7 +1436,7 @@ class UpdateTreeEnsembleOpTest(test_util.TensorFlowTestCase): def testPostPruningChangesNothing(self): """Test growing an ensemble with post-pruning with all gains >0.""" - with self.test_session() as session: + with self.cached_session() as session: # Create empty ensemble. tree_ensemble_config = boosted_trees_pb2.TreeEnsemble() tree_ensemble = boosted_trees_ops.TreeEnsemble( diff --git a/tensorflow/python/kernel_tests/broadcast_to_ops_test.py b/tensorflow/python/kernel_tests/broadcast_to_ops_test.py index bd2339f31d..09c325f2bc 100644 --- a/tensorflow/python/kernel_tests/broadcast_to_ops_test.py +++ b/tensorflow/python/kernel_tests/broadcast_to_ops_test.py @@ -90,7 +90,7 @@ class BroadcastToTest(test_util.TensorFlowTestCase): x = constant_op.constant(1, dtype=dtypes.float32) v = array_ops.broadcast_to(x, [2, 4, 3]) out = 2 * v - with self.test_session(): + with self.cached_session(): err = gradient_checker.compute_gradient_error(x, x.get_shape(), out, out.get_shape()) self.assertLess(err, 1e-4) @@ -100,7 +100,7 @@ class BroadcastToTest(test_util.TensorFlowTestCase): dtype=dtypes.float32) v = array_ops.broadcast_to(x, [2, 5, 3]) out = 2 * v - with self.test_session(): + with self.cached_session(): err = gradient_checker.compute_gradient_error(x, x.get_shape(), out, out.get_shape()) self.assertLess(err, 1e-4) @@ -110,7 +110,7 @@ class BroadcastToTest(test_util.TensorFlowTestCase): dtype=dtypes.float32) v = array_ops.broadcast_to(x, [5, 2, 3]) out = 2 * v - with self.test_session(): + with self.cached_session(): err = gradient_checker.compute_gradient_error(x, x.get_shape(), out, out.get_shape()) self.assertLess(err, 1e-4) @@ -119,7 +119,7 @@ class BroadcastToTest(test_util.TensorFlowTestCase): x = constant_op.constant([[1, 2, 3], [4, 5, 6]], dtype=dtypes.float32) v = array_ops.broadcast_to(x, [5, 4, 6]) out = 2 * v - with self.test_session(): + with self.cached_session(): err = gradient_checker.compute_gradient_error(x, x.get_shape(), out, out.get_shape()) self.assertLess(err, 1e-4) diff --git a/tensorflow/python/kernel_tests/candidate_sampler_ops_test.py b/tensorflow/python/kernel_tests/candidate_sampler_ops_test.py index 28b3dc45e9..b19077db56 100644 --- a/tensorflow/python/kernel_tests/candidate_sampler_ops_test.py +++ b/tensorflow/python/kernel_tests/candidate_sampler_ops_test.py @@ -38,7 +38,7 @@ class RangeSamplerOpsTest(test.TestCase): TRUE_LABELS = [[1, 2], [0, 4], [3, 3]] def testTrueCandidates(self): - with self.test_session() as sess: + with self.cached_session() as sess: indices = constant_op.constant([0, 0, 1, 1, 2, 2]) true_candidates_vec = constant_op.constant([1, 2, 0, 4, 3, 3]) true_candidates_matrix = array_ops.reshape( @@ -50,7 +50,7 @@ class RangeSamplerOpsTest(test.TestCase): self.assertAllEqual(true_candidates_val, self.TRUE_LABELS) def testSampledCandidates(self): - with self.test_session(): + with self.cached_session(): true_classes = constant_op.constant( [[1, 2], [0, 4], [3, 3]], dtype=dtypes.int64) sampled_candidates, _, _ = candidate_sampling_ops.all_candidate_sampler( @@ -62,7 +62,7 @@ class RangeSamplerOpsTest(test.TestCase): self.assertEqual(sampled_candidates.get_shape(), [self.NUM_SAMPLED]) def testTrueLogExpectedCount(self): - with self.test_session(): + with self.cached_session(): true_classes = constant_op.constant( [[1, 2], [0, 4], [3, 3]], dtype=dtypes.int64) _, true_expected_count, _ = candidate_sampling_ops.all_candidate_sampler( @@ -77,7 +77,7 @@ class RangeSamplerOpsTest(test.TestCase): [self.BATCH_SIZE, self.NUM_TRUE]) def testSampledLogExpectedCount(self): - with self.test_session(): + with self.cached_session(): true_classes = constant_op.constant( [[1, 2], [0, 4], [3, 3]], dtype=dtypes.int64) _, _, sampled_expected_count = candidate_sampling_ops.all_candidate_sampler( # pylint: disable=line-too-long @@ -90,7 +90,7 @@ class RangeSamplerOpsTest(test.TestCase): self.assertEqual(sampled_log_expected_count.get_shape(), [self.NUM_SAMPLED]) def testAccidentalHits(self): - with self.test_session() as sess: + with self.cached_session() as sess: true_classes = constant_op.constant( [[1, 2], [0, 4], [3, 3]], dtype=dtypes.int64) sampled_candidates, _, _ = candidate_sampling_ops.all_candidate_sampler( @@ -109,7 +109,7 @@ class RangeSamplerOpsTest(test.TestCase): def testSeed(self): def draw(seed): - with self.test_session(): + with self.cached_session(): true_classes = constant_op.constant( [[1, 2], [0, 4], [3, 3]], dtype=dtypes.int64) sampled, _, _ = candidate_sampling_ops.log_uniform_candidate_sampler( diff --git a/tensorflow/python/kernel_tests/cast_op_test.py b/tensorflow/python/kernel_tests/cast_op_test.py index 214d5cb3c0..c90520e46d 100644 --- a/tensorflow/python/kernel_tests/cast_op_test.py +++ b/tensorflow/python/kernel_tests/cast_op_test.py @@ -174,7 +174,7 @@ class CastOpTest(test.TestCase): self.assertAllEqual(np.isnan(self._cast(np.nan, np.float64, True)), True) def _OpError(self, x, dtype, err): - with self.test_session(): + with self.cached_session(): with self.assertRaisesOpError(err): math_ops.cast(x, dtype).eval() @@ -182,7 +182,7 @@ class CastOpTest(test.TestCase): self._OpError(np.arange(0, 10), dtypes.string, "Cast.*int64.*string.*") def testCastToTypeOfVariable(self): - with self.test_session() as sess: + with self.cached_session() as sess: x = variables.Variable(5, dtype=dtypes.float32) y = variables.Variable(True, dtype=dtypes.bool) cast = math_ops.cast(y, x.dtype) @@ -193,7 +193,7 @@ class CastOpTest(test.TestCase): t = [dtypes.float32, dtypes.float64, dtypes.complex64, dtypes.complex128] for src_t in t: for dst_t in t: - with self.test_session(): + with self.cached_session(): x = constant_op.constant(1.0, src_t) z = array_ops.identity(x) y = math_ops.cast(z, dst_t) @@ -209,7 +209,7 @@ class SparseTensorCastTest(test.TestCase): shape = constant_op.constant([3], dtypes.int64) st = sparse_tensor.SparseTensor(indices, values, shape) st_cast = math_ops.cast(st, dtypes.float32) - with self.test_session(): + with self.cached_session(): self.assertAllEqual(st_cast.indices.eval(), [[0], [1], [2]]) self.assertAllEqual(st_cast.values.eval(), np.array([1, 2, 3], np.float32)) @@ -221,7 +221,7 @@ class SaturateCastTest(test.TestCase): def testSaturate(self): in_types = dtypes.float32, out_types = dtypes.int8, dtypes.uint8, dtypes.int16, dtypes.float32 - with self.test_session() as sess: + with self.cached_session() as sess: for in_type in in_types: for out_type in out_types: lo, hi = in_type.min, in_type.max diff --git a/tensorflow/python/kernel_tests/check_ops_test.py b/tensorflow/python/kernel_tests/check_ops_test.py index 27a674e223..bd4011d58e 100644 --- a/tensorflow/python/kernel_tests/check_ops_test.py +++ b/tensorflow/python/kernel_tests/check_ops_test.py @@ -785,7 +785,7 @@ class EnsureShapeTest(test.TestCase): derived = math_ops.divide(placeholder, 3, name="MyDivide") derived = check_ops.ensure_shape(derived, (3, 3, 3)) feed_val = [[1], [2]] - with self.test_session() as sess: + with self.cached_session() as sess: with self.assertRaisesWithPredicateMatch( errors.InvalidArgumentError, r"Shape of tensor MyDivide \[2,1\] is not compatible with " @@ -797,7 +797,7 @@ class EnsureShapeTest(test.TestCase): derived = placeholder / 3 derived = check_ops.ensure_shape(derived, (None, None, 3)) feed_val = [[1], [2]] - with self.test_session() as sess: + with self.cached_session() as sess: with self.assertRaisesWithPredicateMatch( errors.InvalidArgumentError, r"Shape of tensor [A-Za-z_]* \[2,1\] is not compatible with " @@ -809,7 +809,7 @@ class EnsureShapeTest(test.TestCase): derived = placeholder / 3 derived = check_ops.ensure_shape(derived, (2, 1)) feed_val = [[1], [2]] - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(derived, feed_dict={placeholder: feed_val}) def testEnsuresDynamicShape_WithUnknownDims(self): @@ -817,7 +817,7 @@ class EnsureShapeTest(test.TestCase): derived = placeholder / 3 derived = check_ops.ensure_shape(derived, (None, None)) feed_val = [[1], [2]] - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(derived, feed_dict={placeholder: feed_val}) def testGradient(self): @@ -826,7 +826,7 @@ class EnsureShapeTest(test.TestCase): gradient = gradients.gradients(derived, placeholder) feed_val = [[4.0], [-1.0]] - with self.test_session() as sess: + with self.cached_session() as sess: gradient_values, = sess.run(gradient, feed_dict={placeholder: feed_val}) expected = [[1.0], [1.0]] diff --git a/tensorflow/python/kernel_tests/checkpoint_ops_test.py b/tensorflow/python/kernel_tests/checkpoint_ops_test.py index 7f147ba53a..51611b75af 100644 --- a/tensorflow/python/kernel_tests/checkpoint_ops_test.py +++ b/tensorflow/python/kernel_tests/checkpoint_ops_test.py @@ -57,7 +57,7 @@ class GenerateVocabRemappingTest(test.TestCase): new_vocab_offset=0) expected_remapping = range(0, 3) expected_num_present = 3 - with self.test_session(): + with self.cached_session(): self.assertAllEqual(expected_remapping, remapping.eval()) self.assertAllEqual(expected_num_present, num_present.eval()) @@ -70,7 +70,7 @@ class GenerateVocabRemappingTest(test.TestCase): new_vocab_offset=0) expected_remapping = [2, 0, 1] expected_num_present = 3 - with self.test_session(): + with self.cached_session(): self.assertAllEqual(expected_remapping, remapping.eval()) self.assertAllEqual(expected_num_present, num_present.eval()) @@ -83,7 +83,7 @@ class GenerateVocabRemappingTest(test.TestCase): new_vocab_offset=1) expected_remapping = [0] expected_num_present = 1 - with self.test_session(): + with self.cached_session(): self.assertAllEqual(expected_remapping, remapping.eval()) self.assertAllEqual(expected_num_present, num_present.eval()) @@ -98,7 +98,7 @@ class GenerateVocabRemappingTest(test.TestCase): old_vocab_size=2) expected_remapping = [-1, 0, 1] expected_num_present = 2 - with self.test_session(): + with self.cached_session(): self.assertAllEqual(expected_remapping, remapping.eval()) self.assertAllEqual(expected_num_present, num_present.eval()) @@ -122,7 +122,7 @@ class LoadAndRemapMatrixTest(test.TestCase): self.old_tensor_name = 'some_scope/matrix' save = saver.Saver([matrix]) - with self.test_session() as sess: + with self.cached_session() as sess: variables.global_variables_initializer().run() self.bundle_file = os.path.join(test.get_temp_dir(), 'bundle_checkpoint') save.save(sess, self.bundle_file) @@ -140,7 +140,7 @@ class LoadAndRemapMatrixTest(test.TestCase): initializing_values=[], num_rows=2, num_cols=self.old_num_cols) - with self.test_session(): + with self.cached_session(): self.assertAllClose(self.matrix_value[row_remapping], remapped_matrix.eval()) @@ -155,7 +155,7 @@ class LoadAndRemapMatrixTest(test.TestCase): initializing_values=[], num_rows=len(row_remapping), num_cols=len(col_remapping)) - with self.test_session(): + with self.cached_session(): self.assertAllClose(self.matrix_value[row_remapping][:, col_remapping], remapped_matrix.eval()) @@ -170,7 +170,7 @@ class LoadAndRemapMatrixTest(test.TestCase): initializing_values=[], num_rows=len(row_remapping), num_cols=len(col_remapping)) - with self.test_session(): + with self.cached_session(): self.assertAllClose(self.matrix_value[row_remapping][:, col_remapping], remapped_matrix.eval()) @@ -189,7 +189,7 @@ class LoadAndRemapMatrixTest(test.TestCase): expected_remapped_matrix = np.reshape( [33, init_val, init_val, init_val, 1, init_val], [3, 2]) - with self.test_session(): + with self.cached_session(): self.assertAllClose(expected_remapped_matrix, remapped_matrix.eval()) def test_load_and_remap_all_missing_rows(self): @@ -204,7 +204,7 @@ class LoadAndRemapMatrixTest(test.TestCase): initializing_values=initializing_values, num_rows=num_rows, num_cols=self.old_num_cols) - with self.test_session(): + with self.cached_session(): self.assertAllClose( np.reshape(initializing_values, (num_rows, self.old_num_cols)), remapped_matrix.eval()) @@ -222,7 +222,7 @@ class LoadAndRemapMatrixTest(test.TestCase): initializing_values=initializing_values, num_rows=num_rows, num_cols=num_cols) - with self.test_session(): + with self.cached_session(): self.assertAllClose( np.reshape(initializing_values, (num_rows, num_cols)), remapped_matrix.eval()) @@ -243,7 +243,7 @@ class LoadAndRemapMatrixTest(test.TestCase): initializing_values=[], num_rows=len(invalid_remapping), num_cols=self.old_num_cols) - with self.test_session(), self.assertRaises(errors.UnimplementedError): + with self.cached_session(), self.assertRaises(errors.UnimplementedError): remapped_matrix.eval() # Invalid column remapping. @@ -255,7 +255,7 @@ class LoadAndRemapMatrixTest(test.TestCase): initializing_values=[], num_rows=self.old_num_rows, num_cols=len(invalid_remapping)) - with self.test_session(), self.assertRaises(errors.UnimplementedError): + with self.cached_session(), self.assertRaises(errors.UnimplementedError): remapped_matrix.eval() def test_load_and_remap_incorrect_initializing_values(self): @@ -272,7 +272,7 @@ class LoadAndRemapMatrixTest(test.TestCase): initializing_values=[], num_rows=3, num_cols=2) - with self.test_session(), self.assertRaises(errors.InvalidArgumentError): + with self.cached_session(), self.assertRaises(errors.InvalidArgumentError): remapped_matrix.eval() remapped_matrix = gen_checkpoint_ops.load_and_remap_matrix( @@ -284,7 +284,7 @@ class LoadAndRemapMatrixTest(test.TestCase): initializing_values=[0] * 5, num_rows=3, num_cols=2) - with self.test_session(), self.assertRaises(errors.InvalidArgumentError): + with self.cached_session(), self.assertRaises(errors.InvalidArgumentError): remapped_matrix.eval() @@ -306,7 +306,7 @@ class LoadAndRemapMatrixWithMaxRowsTest(test.TestCase): initializer=constant_op.constant(np_value, dtype=dtypes.float32), partitioner=partitioner) - with self.test_session() as sess: + with self.cached_session() as sess: ckpt_path = os.path.join(test.get_temp_dir(), 'temp_ckpt') save = saver.Saver([matrix]) variables.global_variables_initializer().run() diff --git a/tensorflow/python/kernel_tests/clip_ops_test.py b/tensorflow/python/kernel_tests/clip_ops_test.py index de52a70cc0..bb7b645da2 100644 --- a/tensorflow/python/kernel_tests/clip_ops_test.py +++ b/tensorflow/python/kernel_tests/clip_ops_test.py @@ -39,7 +39,7 @@ class ClipTest(test.TestCase): min_val = constant_op.constant([0.5, 0.5, 0.5, 0.5], dtype=dtypes.float32) max_val = constant_op.constant([3.5, 3.5, 3.5, 3.5], dtype=dtypes.float32) outputs_2 = clip_ops.clip_by_value(inputs, min_val, max_val) - with self.test_session(): + with self.cached_session(): error_1 = gradient_checker.compute_gradient_error(inputs, [4], outputs_1, [4]) self.assertLess(error_1, 1e-4) @@ -139,7 +139,7 @@ class ClipTest(test.TestCase): def testClipByValueNonFinite(self): # TODO(b/78016351): Enable test on GPU once the bug is fixed. - with self.test_session(): + with self.cached_session(): x = constant_op.constant([float('NaN'), float('Inf'), -float('Inf')]) np_ans = [float('NaN'), 4.0, -4.0] clip_value = 4.0 diff --git a/tensorflow/python/kernel_tests/concat_op_test.py b/tensorflow/python/kernel_tests/concat_op_test.py index c22934ce47..0e59ce6972 100644 --- a/tensorflow/python/kernel_tests/concat_op_test.py +++ b/tensorflow/python/kernel_tests/concat_op_test.py @@ -383,7 +383,7 @@ class ConcatOpTest(test.TestCase): np.random.random_sample(x_shape).astype(np.float64) for x_shape in x_shapes ] - with self.test_session(): + with self.cached_session(): xs = [constant_op.constant(x_val) for x_val in x_vals] output = array_ops.concat(xs, 0) err = gradient_checker.compute_gradient_error(xs, x_shapes, output, @@ -397,7 +397,7 @@ class ConcatOpTest(test.TestCase): np.random.random_sample(x_shape).astype(np.float64) for x_shape in x_shapes ] - with self.test_session(): + with self.cached_session(): xs = [constant_op.constant(x_val) for x_val in x_vals] output = array_ops.concat(xs, 1) err = gradient_checker.compute_gradient_error(xs, x_shapes, output, @@ -411,7 +411,7 @@ class ConcatOpTest(test.TestCase): np.random.random_sample(x_shape).astype(np.float64) for x_shape in x_shapes ] - with self.test_session(): + with self.cached_session(): xs = [constant_op.constant(x_val) for x_val in x_vals] x_concat = array_ops.concat(xs, 0) output = array_ops.gather(x_concat, [1, 2, 0, 5]) @@ -426,7 +426,7 @@ class ConcatOpTest(test.TestCase): np.random.random_sample(x_shape).astype(np.float64) for x_shape in x_shapes ] - with self.test_session(): + with self.cached_session(): xs = [constant_op.constant(x_val) for x_val in x_vals] x_concat = array_ops.concat(xs, 1) output = array_ops.gather(x_concat, [1, 2, 0, 5]) @@ -441,7 +441,7 @@ class ConcatOpTest(test.TestCase): np.random.random_sample(x_shape).astype(np.float64) for x_shape in x_shapes ] - with self.test_session(): + with self.cached_session(): xs = [constant_op.constant(x_val) for x_val in x_vals] x_concat = array_ops.concat(xs, 2) output = array_ops.gather(x_concat, [1, 2, 0, 5]) @@ -452,7 +452,7 @@ class ConcatOpTest(test.TestCase): def testIndexedSlicesConcatDim1Grad_UnknownInputDim(self): x_shapes = [[20, 7, 3], [20, 3, 3], [20, 1, 3]] output_shape = [4, 11, 3] - with self.test_session(): + with self.cached_session(): x_1 = array_ops.placeholder(dtypes.float64) x_2 = array_ops.placeholder(dtypes.float64) x_3 = array_ops.placeholder(dtypes.float64) @@ -473,13 +473,13 @@ class ConcatOpTest(test.TestCase): def testConcatTuple(self): c1 = np.random.rand(4, 4) c2 = np.random.rand(4, 4) - with self.test_session(): + with self.cached_session(): concat_list_t = array_ops.concat([c1, c2], 0) concat_tuple_t = array_ops.concat((c1, c2), 0) self.assertAllEqual(concat_list_t.eval(), concat_tuple_t.eval()) def testConcatNoScalars(self): - with self.test_session(): + with self.cached_session(): scalar = constant_op.constant(7) dim = array_ops.placeholder(dtypes.int32) with self.assertRaisesRegexp( @@ -554,7 +554,7 @@ class ConcatOpTest(test.TestCase): def _testGradientsForAxis( self, inp_tensors, axis, output_shape, feed_dict=None): - with self.test_session(): + with self.cached_session(): c = array_ops.concat(inp_tensors, axis) grad_inp = np.random.rand(*output_shape).astype("f") grad_tensor = constant_op.constant( @@ -566,7 +566,7 @@ class ConcatOpTest(test.TestCase): def _testIndexedSlicesGradientsForAxis( self, inp_tensors, axis, output_shape, gather_indexes, feed_dict=None): - with self.test_session(): + with self.cached_session(): c = array_ops.gather( array_ops.concat(inp_tensors, axis), gather_indexes) grad_inp = np.random.rand(*output_shape).astype("f") @@ -631,7 +631,7 @@ class ConcatOffsetTest(test.TestCase): self.assertAllEqual(ans, [[0, 0, 0], [0, 3, 0], [0, 10, 0]]) def testNotVector(self): - with self.test_session() as sess: + with self.cached_session() as sess: cdim = constant_op.constant(1, dtypes.int32) s0 = constant_op.constant([[2, 3, 5]], dtypes.int32) s1 = constant_op.constant([[2, 7, 5]], dtypes.int32) @@ -641,7 +641,7 @@ class ConcatOffsetTest(test.TestCase): sess.run(off) def testConcatDimOutOfRange(self): - with self.test_session() as sess: + with self.cached_session() as sess: cdim = constant_op.constant(4, dtypes.int32) s0 = constant_op.constant([2, 3, 5], dtypes.int32) s1 = constant_op.constant([2, 7, 5], dtypes.int32) @@ -651,7 +651,7 @@ class ConcatOffsetTest(test.TestCase): sess.run(off) def testDimMismatch(self): - with self.test_session() as sess: + with self.cached_session() as sess: cdim = constant_op.constant(1, dtypes.int32) s0 = constant_op.constant([2, 3, 5], dtypes.int32) s1 = constant_op.constant([2, 7, 5, 10], dtypes.int32) @@ -661,7 +661,7 @@ class ConcatOffsetTest(test.TestCase): sess.run(off) def testSizeMismatch(self): - with self.test_session() as sess: + with self.cached_session() as sess: cdim = constant_op.constant(1, dtypes.int32) s0 = constant_op.constant([2, 3, 5], dtypes.int32) s1 = constant_op.constant([2, 7, 10], dtypes.int32) diff --git a/tensorflow/python/kernel_tests/cond_v2_test.py b/tensorflow/python/kernel_tests/cond_v2_test.py index 0dc3c53bc0..377c041675 100644 --- a/tensorflow/python/kernel_tests/cond_v2_test.py +++ b/tensorflow/python/kernel_tests/cond_v2_test.py @@ -41,7 +41,7 @@ class CondV2Test(test.TestCase): def _testCond(self, true_fn, false_fn, train_vals, feed_dict=None): if not feed_dict: feed_dict = {} - with self.test_session(graph=ops.get_default_graph()) as sess: + with self.session(graph=ops.get_default_graph()) as sess: pred = array_ops.placeholder(dtypes.bool, name="pred") expected = control_flow_ops.cond(pred, true_fn, false_fn, name="expected") @@ -107,7 +107,7 @@ class CondV2Test(test.TestCase): self._testCond(true_fn, false_fn, [y]) def testNoInputs(self): - with self.test_session() as sess: + with self.cached_session() as sess: pred = array_ops.placeholder(dtypes.bool, name="pred") def true_fn(): @@ -131,7 +131,7 @@ class CondV2Test(test.TestCase): def false_fn(): return x + 1 - return cond_v2.cond_v2(pred, true_fn, false_fn, name=name)[0].op + return cond_v2.cond_v2(pred, true_fn, false_fn, name=name).op def testDefaultName(self): with ops.Graph().as_default(): @@ -382,7 +382,7 @@ class CondV2Test(test.TestCase): with ops.Graph().as_default(): grads, pred_outer, pred_inner = build_graph() - with self.test_session(graph=ops.get_default_graph()) as sess: + with self.session(graph=ops.get_default_graph()) as sess: self.assertSequenceEqual( sess.run(grads, { pred_outer: True, @@ -445,7 +445,7 @@ class CondV2Test(test.TestCase): with ops.Graph().as_default(): grads, pred_outer, pred_inner = build_graph() - with self.test_session(graph=ops.get_default_graph()) as sess: + with self.session(graph=ops.get_default_graph()) as sess: self.assertSequenceEqual( sess.run(grads, { pred_outer: True, @@ -504,7 +504,7 @@ class CondV2Test(test.TestCase): with ops.Graph().as_default(): grads, pred_outer, pred_inner = build_graph() - with self.test_session(graph=ops.get_default_graph()) as sess: + with self.session(graph=ops.get_default_graph()) as sess: self.assertSequenceEqual( sess.run(grads, { pred_outer: True, @@ -527,7 +527,7 @@ class CondV2Test(test.TestCase): }), [5., 0.]) def testSecondDerivative(self): - with self.test_session() as sess: + with self.cached_session() as sess: pred = array_ops.placeholder(dtypes.bool, name="pred") x = constant_op.constant(3.0, name="x") @@ -569,12 +569,11 @@ class CondV2Test(test.TestCase): ops.add_to_collection("pred", pred) cond = cond_v2.cond_v2(pred, true_fn, false_fn, name="cond") - for c in cond: - ops.add_to_collection("cond", c) + ops.add_to_collection("cond", cond) meta_graph = saver.export_meta_graph() with ops.Graph().as_default() as g: - with self.test_session(graph=g) as sess: + with self.session(graph=g) as sess: saver.import_meta_graph(meta_graph) x = ops.get_collection("x")[0] pred = ops.get_collection("pred")[0] @@ -598,7 +597,7 @@ class CondV2Test(test.TestCase): def testLowering(self): with ops.Graph().as_default() as g: - with self.test_session(graph=g) as sess: + with self.session(graph=g) as sess: out_cond = self._createCond("cond") run_options = config_pb2.RunOptions(output_partition_graphs=True) @@ -624,7 +623,7 @@ class CondV2Test(test.TestCase): "An `If` op was found, but it should be lowered.") def testLoweringDisabledInXLA(self): - with self.test_session(graph=ops.Graph()) as sess: + with self.session(graph=ops.Graph()) as sess: # Build the cond_v2 in an XLA context xla_context = control_flow_ops.XLAControlFlowContext() xla_context.Enter() @@ -661,7 +660,7 @@ class CondV2CollectionTest(test.TestCase): def testCollectionIntValueAccessInCond(self): """Read values from graph collections inside of cond_v2.""" with ops.Graph().as_default() as g: - with self.test_session(graph=g): + with self.session(graph=g): x = 2 y = 5 ops.add_to_collection("x", x) @@ -672,12 +671,12 @@ class CondV2CollectionTest(test.TestCase): return math_ops.add(x_const, y_const) cnd = cond_v2.cond_v2(True, fn, fn) - self.assertEquals(cnd[0].eval(), 7) + self.assertEquals(cnd.eval(), 7) def testCollectionTensorValueAccessInCond(self): """Read tensors from collections inside of cond_v2 & use them.""" with ops.Graph().as_default() as g: - with self.test_session(graph=g): + with self.session(graph=g): x = constant_op.constant(2) y = constant_op.constant(5) ops.add_to_collection("x", x) @@ -689,12 +688,12 @@ class CondV2CollectionTest(test.TestCase): return math_ops.add(x_read, y_read) cnd = cond_v2.cond_v2(math_ops.less(x, y), fn, fn) - self.assertEquals(cnd[0].eval(), 7) + self.assertEquals(cnd.eval(), 7) def testCollectionIntValueWriteInCond(self): """Make sure Int writes to collections work inside of cond_v2.""" with ops.Graph().as_default() as g: - with self.test_session(graph=g): + with self.session(graph=g): x = constant_op.constant(2) y = constant_op.constant(5) def true_fn(): @@ -709,7 +708,7 @@ class CondV2CollectionTest(test.TestCase): cnd = cond_v2.cond_v2( True, true_fn, false_fn) - self.assertEquals(cnd[0].eval(), 14) + self.assertEquals(cnd.eval(), 14) read_z_collection = ops.get_collection("z") self.assertEquals(read_z_collection, [7]) @@ -725,7 +724,7 @@ class CondV2ContainerTest(test.TestCase): """ self.skipTest("b/113048653") with ops.Graph().as_default() as g: - with self.test_session(graph=g): + with self.session(graph=g): v0 = variables.Variable([0]) q0 = data_flow_ops.FIFOQueue(1, dtypes.float32) @@ -782,10 +781,10 @@ class CondV2ContainerTest(test.TestCase): with ops.container("l1"): cnd_true = cond_v2.cond_v2(True, true_fn, false_fn) - self.assertEquals(cnd_true[0].eval(), 2) + self.assertEquals(cnd_true.eval(), 2) cnd_false = cond_v2.cond_v2(False, true_fn, false_fn) - self.assertEquals(cnd_false[0].eval(), 6) + self.assertEquals(cnd_false.eval(), 6) v4 = variables.Variable([3]) q4 = data_flow_ops.FIFOQueue(1, dtypes.float32) @@ -801,9 +800,8 @@ class CondV2ContainerTest(test.TestCase): class CondV2ColocationGroupAndDeviceTest(test.TestCase): def testColocateWithBeforeCond(self): - self.skipTest("b/112414483") with ops.Graph().as_default() as g: - with self.test_session(graph=g): + with self.session(graph=g): a = constant_op.constant([2.0], name="a") b = constant_op.constant([2.0], name="b") @@ -814,7 +812,7 @@ class CondV2ColocationGroupAndDeviceTest(test.TestCase): return c with ops.colocate_with(a.op): - self.assertEquals(cond_v2.cond_v2(True, fn, fn)[0].eval(), 3) + self.assertEquals(cond_v2.cond_v2(True, fn, fn).eval(), 3) def fn2(): c = constant_op.constant(3.0) @@ -823,12 +821,11 @@ class CondV2ColocationGroupAndDeviceTest(test.TestCase): with ops.colocate_with(a.op): with ops.colocate_with(b.op): - self.assertEquals(cond_v2.cond_v2(True, fn2, fn2)[0].eval(), 3) + self.assertEquals(cond_v2.cond_v2(True, fn2, fn2).eval(), 3) def testColocateWithInAndOutOfCond(self): - self.skipTest("b/112414483") with ops.Graph().as_default() as g: - with self.test_session(graph=g): + with self.session(graph=g): a = constant_op.constant([2.0], name="a") b = constant_op.constant([2.0], name="b") @@ -840,7 +837,7 @@ class CondV2ColocationGroupAndDeviceTest(test.TestCase): return c with ops.colocate_with(a.op): - self.assertEquals(cond_v2.cond_v2(True, fn2, fn2)[0].eval(), 3) + self.assertEquals(cond_v2.cond_v2(True, fn2, fn2).eval(), 3) d = constant_op.constant([2.0], name="d") self.assertEqual([b"loc:@a"], d.op.colocation_groups()) @@ -861,7 +858,7 @@ class CondV2ColocationGroupAndDeviceTest(test.TestCase): with ops.colocate_with(b.op): c = math_ops.add(a, a, name="c") return c - out_cond_2 = cond_v2.cond_v2(True, fn, fn)[0] + out_cond_2 = cond_v2.cond_v2(True, fn, fn) run_options = config_pb2.RunOptions(output_partition_graphs=True) run_metadata = config_pb2.RunMetadata() @@ -874,16 +871,16 @@ class CondV2ColocationGroupAndDeviceTest(test.TestCase): self.assertTrue(len(run_metadata.partition_graphs) >= 2) def testDeviceBeforeCond(self): - self.skipTest("b/112166045") with ops.Graph().as_default() as g: - with self.test_session(graph=g): + with self.session(graph=g): + def fn(): c = constant_op.constant(3.0) self.assertEqual("/device:CPU:0", c.op.device) return c with ops.device("/device:CPU:0"): - self.assertEquals(cond_v2.cond_v2(True, fn, fn)[0].eval(), 3) + self.assertEquals(cond_v2.cond_v2(True, fn, fn).eval(), 3) def fn2(): c = constant_op.constant(3.0) @@ -891,19 +888,21 @@ class CondV2ColocationGroupAndDeviceTest(test.TestCase): return c with ops.device("/device:GPU:0"): - self.assertEquals(cond_v2.cond_v2(True, fn2, fn2)[0].eval(), 3) + self.assertEquals(cond_v2.cond_v2(True, fn2, fn2).eval(), 3) def testDeviceInAndOutOfCond(self): with ops.Graph().as_default() as g: - with self.test_session(graph=g): + with self.test_session( + graph=g, config=config_pb2.ConfigProto(device_count={"CPU": 2})): + def fn2(): - with ops.device("/device:GPU:0"): + with ops.device("/device:CPU:1"): c = constant_op.constant(3.0) - self.assertEqual("/device:GPU:0", c.op.device) + self.assertEqual("/device:CPU:1", c.op.device) return c with ops.device("/device:CPU:0"): - self.assertEquals(cond_v2.cond_v2(True, fn2, fn2)[0].eval(), 3) + self.assertEquals(cond_v2.cond_v2(True, fn2, fn2).eval(), 3) d = constant_op.constant(4.0) self.assertEqual("/device:CPU:0", d.op.device) @@ -922,7 +921,7 @@ class CondV2ColocationGroupAndDeviceTest(test.TestCase): with ops.device("/device:CPU:0"): a = constant_op.constant([2.0], name="a") - out_cond_2 = cond_v2.cond_v2(True, fn, fn)[0] + out_cond_2 = cond_v2.cond_v2(True, fn, fn) run_options = config_pb2.RunOptions(output_partition_graphs=True) run_metadata = config_pb2.RunMetadata() diff --git a/tensorflow/python/kernel_tests/conditional_accumulator_test.py b/tensorflow/python/kernel_tests/conditional_accumulator_test.py index 86802664d1..97ab23fe49 100644 --- a/tensorflow/python/kernel_tests/conditional_accumulator_test.py +++ b/tensorflow/python/kernel_tests/conditional_accumulator_test.py @@ -80,26 +80,26 @@ class ConditionalAccumulatorTest(test.TestCase): """, q.accumulator_ref.op.node_def) def testAccumulatorSizeEmpty(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.ConditionalAccumulator(dtypes_lib.float32, name="Q") self.assertEqual(q.num_accumulated().eval(), 0) def testAccumulatorSetGlobalStep(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.ConditionalAccumulator( dtypes_lib.float32, name="Q", shape=tensor_shape.TensorShape([1])) set_global_step_op = q.set_global_step(1) set_global_step_op.run() def testAccumulatorApplyGradFloat32(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.ConditionalAccumulator( dtypes_lib.float32, name="Q", shape=tensor_shape.TensorShape([1])) accum_op = q.apply_grad((10.0,)) accum_op.run() def testDtypes(self): - with self.test_session() as sess: + with self.cached_session() as sess: dtypes = [dtypes_lib.float16, dtypes_lib.float32, dtypes_lib.float64] for i in range(len(dtypes)): @@ -116,7 +116,7 @@ class ConditionalAccumulatorTest(test.TestCase): self.assertEqual(sum(elems) / len(elems), result) def testAccumulatorMultipleAccumulators(self): - with self.test_session(): + with self.cached_session(): q_f32_0 = data_flow_ops.ConditionalAccumulator( dtypes_lib.float32, name="Q", shape=tensor_shape.TensorShape([1])) q_f32_1 = data_flow_ops.ConditionalAccumulator( @@ -135,7 +135,7 @@ class ConditionalAccumulatorTest(test.TestCase): self.assertEqual(result, i + 10.0) def testAccumulatorApplyAndTakeGradWithShape(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.ConditionalAccumulator( dtypes_lib.float32, name="Q", shape=(3, 2)) elems = [[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], @@ -166,7 +166,7 @@ class ConditionalAccumulatorTest(test.TestCase): q.apply_grad([[1.0], [2.0], [3.0]]) def testAccumulatorDynamicShape(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.ConditionalAccumulator( dtypes_lib.float32, name="Q", shape=None) @@ -191,7 +191,7 @@ class ConditionalAccumulatorTest(test.TestCase): self.assertTrue(is_all_equal) def testAccumulatorWrongDynamicShape(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.ConditionalAccumulator( dtypes_lib.float32, name="Q", shape=None) @@ -209,7 +209,7 @@ class ConditionalAccumulatorTest(test.TestCase): sess.run(accum_op, feed_dict={x: [[1.0], [2.0], [3.0]]}) def testAccumulatorSizeAfterApplyGrad(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.ConditionalAccumulator( dtypes_lib.float32, name="Q", shape=tensor_shape.TensorShape([1])) accum_op = q.apply_grad((10.0,)) @@ -220,7 +220,7 @@ class ConditionalAccumulatorTest(test.TestCase): self.assertEqual(q.num_accumulated().eval(), 2) def testAccumulatorSizeAfterApplyGradAndTakeGrad(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.ConditionalAccumulator( dtypes_lib.float32, name="Q", shape=tensor_shape.TensorShape([1])) accum_op = q.apply_grad((10.0,)) @@ -248,7 +248,7 @@ class ConditionalAccumulatorTest(test.TestCase): self.assertEqual(q.num_accumulated().eval(), 0) def testAccumulatorTakeGradMean(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.ConditionalAccumulator( dtypes_lib.float32, name="Q", shape=tensor_shape.TensorShape([1])) elems = [10.0, 20.0] @@ -272,7 +272,7 @@ class ConditionalAccumulatorTest(test.TestCase): self.assertEqual(15.0, val) def testAccumulatorTakeGradSum(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.ConditionalAccumulator( dtypes_lib.float32, name="Q", @@ -307,7 +307,7 @@ class ConditionalAccumulatorTest(test.TestCase): reduction_type="Invalid") def testAccumulatorInvalidTakeGrad(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.ConditionalAccumulator( dtypes_lib.float32, name="Q", shape=tensor_shape.TensorShape([1])) elems = [10.0, 20.0] @@ -322,7 +322,7 @@ class ConditionalAccumulatorTest(test.TestCase): takeg_t.eval() def testAccumulatorRepeatedTakeGradMean(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.ConditionalAccumulator( dtypes_lib.float32, name="Q", shape=tensor_shape.TensorShape([1])) @@ -349,7 +349,7 @@ class ConditionalAccumulatorTest(test.TestCase): self.assertEqual(elems_ave + 0.0, val) def testAccumulatorRepeatedTakeGradSum(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.ConditionalAccumulator( dtypes_lib.float32, name="Q", @@ -379,7 +379,7 @@ class ConditionalAccumulatorTest(test.TestCase): self.assertEqual(elems_sum, val) def testAccumulatorIncrementGlobalStep(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.ConditionalAccumulator( dtypes_lib.float32, name="Q", shape=tensor_shape.TensorShape([1])) @@ -395,7 +395,7 @@ class ConditionalAccumulatorTest(test.TestCase): inc_global_step.eval() def testAccumulatorSetGlobalStepPreventsAccumulation(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.ConditionalAccumulator( dtypes_lib.float32, name="Q", shape=tensor_shape.TensorShape([1])) @@ -416,7 +416,7 @@ class ConditionalAccumulatorTest(test.TestCase): if x >= ls), val) def testParallelApplyGrad(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.ConditionalAccumulator( dtypes_lib.float32, name="Q", shape=tensor_shape.TensorShape([1])) elems = [10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0, 100.0] @@ -441,7 +441,7 @@ class ConditionalAccumulatorTest(test.TestCase): self.assertEqual(val, sum(elems) / len(elems)) def testParallelTakeGrad(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.ConditionalAccumulator( dtypes_lib.float32, name="Q", shape=tensor_shape.TensorShape([1])) elems = [e for e in range(10)] @@ -473,7 +473,7 @@ class ConditionalAccumulatorTest(test.TestCase): self.assertItemsEqual(elems, results) def testAccumulatorApplyAndBlockingTake(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.ConditionalAccumulator( dtypes_lib.float32, name="Q", shape=tensor_shape.TensorShape([1])) @@ -506,7 +506,7 @@ class ConditionalAccumulatorTest(test.TestCase): sess.run(takeg_op) def testAccumulatorCancel(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.ConditionalAccumulator( dtypes_lib.float32, name="Q", shape=tensor_shape.TensorShape([1])) takeg_t = q.take_grad(1) diff --git a/tensorflow/python/kernel_tests/confusion_matrix_test.py b/tensorflow/python/kernel_tests/confusion_matrix_test.py index 93f5323c41..bc24345261 100644 --- a/tensorflow/python/kernel_tests/confusion_matrix_test.py +++ b/tensorflow/python/kernel_tests/confusion_matrix_test.py @@ -37,7 +37,7 @@ class ConfusionMatrixTest(test.TestCase): @test_util.run_in_graph_and_eager_modes def testExample(self): """This is a test of the example provided in pydoc.""" - with self.test_session(): + with self.cached_session(): self.assertAllEqual([ [0, 0, 0, 0, 0], [0, 0, 1, 0, 0], @@ -49,7 +49,7 @@ class ConfusionMatrixTest(test.TestCase): def _testConfMatrix(self, labels, predictions, truth, weights=None, num_classes=None): - with self.test_session(): + with self.cached_session(): dtype = predictions.dtype ans = confusion_matrix.confusion_matrix( labels, predictions, dtype=dtype, weights=weights, @@ -78,7 +78,7 @@ class ConfusionMatrixTest(test.TestCase): self._testBasic(dtype=np.int64) def _testConfMatrixOnTensors(self, tf_dtype, np_dtype): - with self.test_session() as sess: + with self.cached_session() as sess: m_neg = array_ops.placeholder(dtype=dtypes.float32) m_pos = array_ops.placeholder(dtype=dtypes.float32) s = array_ops.placeholder(dtype=dtypes.float32) @@ -229,7 +229,7 @@ class ConfusionMatrixTest(test.TestCase): def testOutputIsInt32(self): labels = np.arange(2) predictions = np.arange(2) - with self.test_session(): + with self.cached_session(): cm = confusion_matrix.confusion_matrix( labels, predictions, dtype=dtypes.int32) tf_cm = cm.eval() @@ -238,7 +238,7 @@ class ConfusionMatrixTest(test.TestCase): def testOutputIsInt64(self): labels = np.arange(2) predictions = np.arange(2) - with self.test_session(): + with self.cached_session(): cm = confusion_matrix.confusion_matrix( labels, predictions, dtype=dtypes.int64) tf_cm = cm.eval() @@ -260,7 +260,7 @@ class RemoveSqueezableDimensionsTest(test.TestCase): confusion_matrix.remove_squeezable_dimensions( labels_placeholder, predictions_placeholder)) - with self.test_session(): + with self.cached_session(): self.assertAllEqual(label_values, static_labels.eval()) self.assertAllEqual(prediction_values, static_predictions.eval()) feed_dict = { @@ -285,7 +285,7 @@ class RemoveSqueezableDimensionsTest(test.TestCase): confusion_matrix.remove_squeezable_dimensions( labels_placeholder, predictions_placeholder)) - with self.test_session(): + with self.cached_session(): self.assertAllEqual(label_values, static_labels.eval()) self.assertAllEqual(prediction_values, static_predictions.eval()) feed_dict = { @@ -310,7 +310,7 @@ class RemoveSqueezableDimensionsTest(test.TestCase): confusion_matrix.remove_squeezable_dimensions( labels_placeholder, predictions_placeholder, expected_rank_diff=0)) - with self.test_session(): + with self.cached_session(): self.assertAllEqual(label_values, static_labels.eval()) self.assertAllEqual(prediction_values, static_predictions.eval()) feed_dict = { @@ -336,7 +336,7 @@ class RemoveSqueezableDimensionsTest(test.TestCase): labels_placeholder, predictions_placeholder)) expected_label_values = np.reshape(label_values, newshape=(2, 3)) - with self.test_session(): + with self.cached_session(): self.assertAllEqual(expected_label_values, static_labels.eval()) self.assertAllEqual(prediction_values, static_predictions.eval()) feed_dict = { @@ -362,7 +362,7 @@ class RemoveSqueezableDimensionsTest(test.TestCase): labels_placeholder, predictions_placeholder, expected_rank_diff=1)) expected_label_values = np.reshape(label_values, newshape=(2, 3)) - with self.test_session(): + with self.cached_session(): self.assertAllEqual(expected_label_values, static_labels.eval()) self.assertAllEqual(prediction_values, static_predictions.eval()) feed_dict = { @@ -388,7 +388,7 @@ class RemoveSqueezableDimensionsTest(test.TestCase): labels_placeholder, predictions_placeholder)) expected_prediction_values = np.reshape(prediction_values, newshape=(2, 3)) - with self.test_session(): + with self.cached_session(): self.assertAllEqual(label_values, static_labels.eval()) self.assertAllEqual(expected_prediction_values, static_predictions.eval()) feed_dict = { @@ -415,7 +415,7 @@ class RemoveSqueezableDimensionsTest(test.TestCase): labels_placeholder, predictions_placeholder, expected_rank_diff=-1)) expected_prediction_values = np.reshape(prediction_values, newshape=(2, 3)) - with self.test_session(): + with self.cached_session(): self.assertAllEqual(label_values, static_labels.eval()) self.assertAllEqual(expected_prediction_values, static_predictions.eval()) feed_dict = { @@ -441,7 +441,7 @@ class RemoveSqueezableDimensionsTest(test.TestCase): confusion_matrix.remove_squeezable_dimensions( labels_placeholder, predictions_placeholder)) - with self.test_session(): + with self.cached_session(): feed_dict = { labels_placeholder: label_values, predictions_placeholder: prediction_values @@ -466,7 +466,7 @@ class RemoveSqueezableDimensionsTest(test.TestCase): confusion_matrix.remove_squeezable_dimensions( labels_placeholder, predictions_placeholder)) - with self.test_session(): + with self.cached_session(): feed_dict = { labels_placeholder: label_values, predictions_placeholder: prediction_values diff --git a/tensorflow/python/kernel_tests/constant_op_test.py b/tensorflow/python/kernel_tests/constant_op_test.py index 107ee37fab..d1e4e5477f 100644 --- a/tensorflow/python/kernel_tests/constant_op_test.py +++ b/tensorflow/python/kernel_tests/constant_op_test.py @@ -162,18 +162,18 @@ class ConstantTest(test.TestCase): logging_const_op.run() def testStringWithNulls(self): - with self.test_session(): + with self.cached_session(): val = ops.convert_to_tensor(b"\0\0\0\0").eval() self.assertEqual(len(val), 4) self.assertEqual(val, b"\0\0\0\0") - with self.test_session(): + with self.cached_session(): val = ops.convert_to_tensor(b"xx\0xx").eval() self.assertEqual(len(val), 5) self.assertAllEqual(val, b"xx\0xx") nested = [[b"\0\0\0\0", b"xx\0xx"], [b"\0_\0_\0_\0", b"\0"]] - with self.test_session(): + with self.cached_session(): val = ops.convert_to_tensor(nested).eval() # NOTE(mrry): Do not use assertAllEqual, because it converts nested to a # numpy array, which loses the null terminators. @@ -279,7 +279,7 @@ class AsTensorTest(test.TestCase): self.assertTrue(isinstance(x, ops.Tensor)) def testAsTensorForShapeInput(self): - with self.test_session(): + with self.cached_session(): x = ops.convert_to_tensor(tensor_shape.TensorShape([])) self.assertEqual(dtypes_lib.int32, x.dtype) self.assertAllEqual([], x.eval()) @@ -331,7 +331,7 @@ class AsTensorTest(test.TestCase): tensor_shape.TensorShape([1, 2, 3]), dtype=dtypes_lib.float32) def testAsTensorForDimensionInput(self): - with self.test_session(): + with self.cached_session(): x = ops.convert_to_tensor(tensor_shape.TensorShape([1, 2, 3])[1]) self.assertEqual(dtypes_lib.int32, x.dtype) self.assertAllEqual(2, x.eval()) @@ -367,7 +367,7 @@ class IdentityOpTest(test.TestCase): class ZerosTest(test.TestCase): def _Zeros(self, shape): - with self.test_session(): + with self.cached_session(): ret = array_ops.zeros(shape) self.assertEqual(shape, ret.get_shape()) return ret.eval() @@ -379,13 +379,13 @@ class ZerosTest(test.TestCase): def testScalar(self): self.assertEqual(0, self._Zeros([])) self.assertEqual(0, self._Zeros(())) - with self.test_session(): + with self.cached_session(): scalar = array_ops.zeros(constant_op.constant([], dtype=dtypes_lib.int32)) self.assertEqual(0, scalar.eval()) def testDynamicSizes(self): np_ans = np.array([[0] * 3] * 2) - with self.test_session(): + with self.cached_session(): # Creates a tensor of 2 x 3. d = array_ops.fill([2, 3], 12., name="fill") # Constructs a tensor of zeros of the same dimensions as "d". @@ -396,7 +396,7 @@ class ZerosTest(test.TestCase): self.assertShapeEqual(np_ans, z) def testDtype(self): - with self.test_session(): + with self.cached_session(): d = array_ops.fill([2, 3], 12., name="fill") self.assertEqual(d.get_shape(), [2, 3]) # Test default type for both constant size and dynamic size @@ -489,7 +489,7 @@ class ZerosLikeTest(test.TestCase): def testZerosLikeDtype(self): # Make sure zeros_like works even for dtypes that cannot be cast between - with self.test_session(): + with self.cached_session(): shape = (3, 5) dtypes = np.float32, np.complex64 for in_type in dtypes: @@ -533,7 +533,7 @@ class ZerosLikeTest(test.TestCase): class OnesTest(test.TestCase): def _Ones(self, shape): - with self.test_session(): + with self.cached_session(): ret = array_ops.ones(shape) self.assertEqual(shape, ret.get_shape()) return ret.eval() @@ -544,13 +544,13 @@ class OnesTest(test.TestCase): def testScalar(self): self.assertEqual(1, self._Ones([])) self.assertEqual(1, self._Ones(())) - with self.test_session(): + with self.cached_session(): scalar = array_ops.ones(constant_op.constant([], dtype=dtypes_lib.int32)) self.assertEqual(1, scalar.eval()) def testDynamicSizes(self): np_ans = np.array([[1] * 3] * 2) - with self.test_session(): + with self.cached_session(): # Creates a tensor of 2 x 3. d = array_ops.fill([2, 3], 12., name="fill") # Constructs a tensor of ones of the same dimensions as "d". @@ -561,7 +561,7 @@ class OnesTest(test.TestCase): self.assertShapeEqual(np_ans, z) def testAutoPack(self): - with self.test_session(): + with self.cached_session(): h = array_ops.placeholder(dtypes_lib.int32, shape=[]) w = array_ops.placeholder(dtypes_lib.int32, shape=[]) z = array_ops.ones([h, w]) @@ -569,7 +569,7 @@ class OnesTest(test.TestCase): self.assertAllEqual(out, np.array([[1] * 16] * 4)) def testDtype(self): - with self.test_session(): + with self.cached_session(): d = array_ops.fill([2, 3], 12., name="fill") self.assertEqual(d.get_shape(), [2, 3]) # Test default type for both constant size and dynamic size @@ -606,7 +606,7 @@ class OnesLikeTest(test.TestCase): dtypes_lib.complex128 ]: numpy_dtype = dtype.as_numpy_dtype - with self.test_session(): + with self.cached_session(): # Creates a tensor of non-zero values with shape 2 x 3. d = constant_op.constant( np.ones( @@ -672,7 +672,7 @@ class FillTest(test.TestCase): self.assertAllEqual(np_ans, tf_ans) def testFillNegative(self): - with self.test_session(): + with self.cached_session(): for shape in (-1,), (2, -1), (-1, 2), (-2), (-3): with self.assertRaises(ValueError): array_ops.fill(shape, 7) @@ -703,7 +703,7 @@ class FillTest(test.TestCase): self.assertEqual([None, 17], f.get_shape().as_list()) def testGradient(self): - with self.test_session(): + with self.cached_session(): in_v = constant_op.constant(5.0) out_shape = [3, 2] out_filled = array_ops.fill(out_shape, in_v) @@ -715,7 +715,7 @@ class FillTest(test.TestCase): class PlaceholderTest(test.TestCase): def testDtype(self): - with self.test_session(): + with self.cached_session(): p = array_ops.placeholder(dtypes_lib.float32, shape=(10, 10), name="p") p_identity = array_ops.identity(p) feed_array = np.random.rand(10, 10) @@ -727,7 +727,7 @@ class PlaceholderTest(test.TestCase): p_identity.eval() def testShape(self): - with self.test_session(): + with self.cached_session(): p = array_ops.placeholder(dtypes_lib.float32, shape=(10, 10), name="p") p_identity = array_ops.identity(p) feed_array = np.random.rand(10, 10) @@ -744,7 +744,7 @@ class PlaceholderTest(test.TestCase): p_identity.eval(feed_dict={p: feed_array[:5, :5]}) def testUnknownShape(self): - with self.test_session(): + with self.cached_session(): p = array_ops.placeholder(dtypes_lib.float32, shape=None, name="p") p_identity = array_ops.identity(p) # can feed anything @@ -756,13 +756,13 @@ class PlaceholderTest(test.TestCase): p_identity.eval(feed_dict={p: feed_array}), feed_array) def testScalarShape(self): - with self.test_session(): + with self.cached_session(): p = array_ops.placeholder(dtypes_lib.float32, shape=[], name="p") p_identity = array_ops.identity(p) self.assertAllClose(p_identity.eval(feed_dict={p: 5}), 5) def testPartialShape(self): - with self.test_session(): + with self.cached_session(): p = array_ops.placeholder(dtypes_lib.float32, shape=[None, 3], name="p") p_identity = array_ops.identity(p) feed_array = np.random.rand(10, 3) @@ -774,7 +774,7 @@ class PlaceholderTest(test.TestCase): p_identity.eval(feed_dict={p: feed_array[:5, :2]}) def testPartialShapeWhenNotFed(self): - with self.test_session(): + with self.cached_session(): p = array_ops.placeholder(dtypes_lib.float32, shape=[None, 3], name="p") p_identity = array_ops.identity(p) @@ -784,7 +784,7 @@ class PlaceholderTest(test.TestCase): p_identity.eval() def testControlDependency(self): - with self.test_session(): + with self.cached_session(): p = array_ops.placeholder(dtypes_lib.int32, shape=[], name="p") with ops.control_dependencies([p]): c = constant_op.constant(5, dtypes_lib.int32) @@ -872,7 +872,7 @@ versions { """ gdef = graph_pb2.GraphDef() text_format.Merge(graph, gdef) - with self.test_session(): + with self.cached_session(): p, ret = importer.import_graph_def( gdef, return_elements=["Placeholder:0", "add:0"]) diff --git a/tensorflow/python/kernel_tests/control_flow_ops_py_test.py b/tensorflow/python/kernel_tests/control_flow_ops_py_test.py index eac97af4ed..fc4d2a3809 100644 --- a/tensorflow/python/kernel_tests/control_flow_ops_py_test.py +++ b/tensorflow/python/kernel_tests/control_flow_ops_py_test.py @@ -129,7 +129,7 @@ def isum(s, maximum_iterations=None): class ControlFlowTest(test.TestCase): def testRefIdentity(self): - with self.test_session(): + with self.cached_session(): v = variables.Variable(7) v = control_flow_ops._Identity(v) @@ -141,7 +141,7 @@ class ControlFlowTest(test.TestCase): self.assertEqual(9, v2.eval()) def testRefEnter(self): - with self.test_session(): + with self.cached_session(): v = variables.Variable(7) enter_v = control_flow_ops._Enter(v, "foo_1", is_constant=True) @@ -154,7 +154,7 @@ class ControlFlowTest(test.TestCase): self.assertEqual(9, v3.eval()) def testRefSwitch(self): - with self.test_session(): + with self.cached_session(): v = variables.Variable(7) p = constant_op.constant(True) @@ -164,7 +164,7 @@ class ControlFlowTest(test.TestCase): self.assertEqual(9, v2.eval()) def testEnterMulExit(self): - with self.test_session(): + with self.cached_session(): data = constant_op.constant([1, 2, 3, 4, 5, 6], name="data") enter_data = gen_control_flow_ops.enter(data, "foo_1", False) five = constant_op.constant(5) @@ -176,7 +176,7 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(np.array([x * 5 for x in [1, 2, 3, 4, 5, 6]]), result) def testEnterShapePropagation(self): - with self.test_session(): + with self.cached_session(): v = variables.Variable([0.0, 0.0], dtype=dtypes.float32) # If is_constant=True, the shape information should be propagated. @@ -190,7 +190,7 @@ class ControlFlowTest(test.TestCase): self.assertEqual(enter_v_non_constant.shape, None) def testSwitchMergeIndexedSlices(self): - with self.test_session(): + with self.cached_session(): values = constant_op.constant([1, 2, 3, 4, 5, 6]) indices = constant_op.constant([0, 2, 4, 6, 8, 10]) data = ops.IndexedSlices(values, indices) @@ -204,7 +204,7 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(np.arange(0, 12, 2), ind) def testSwitchDeadBranch(self): - with self.test_session(): + with self.cached_session(): data = constant_op.constant([1, 2, 3, 4, 5, 6], name="data") ports = ops.convert_to_tensor(True, name="ports") switch_op = control_flow_ops.switch(data, ports) @@ -216,7 +216,7 @@ class ControlFlowTest(test.TestCase): dead_branch.eval() def testSwitchMergeLess(self): - with self.test_session(): + with self.cached_session(): data = constant_op.constant([1, 2, 3, 4, 5, 6], name="data") zero = ops.convert_to_tensor(0) one = ops.convert_to_tensor(1) @@ -228,7 +228,7 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(np.arange(1, 7), result) def testSwitchMergeAddIdentity(self): - with self.test_session(): + with self.cached_session(): data = constant_op.constant([1, 2, 3, 4, 5, 6], name="data") ports = ops.convert_to_tensor(False, name="ports") switch_op = control_flow_ops.switch(data, ports) @@ -241,7 +241,7 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(np.array([x + 1 for x in [1, 2, 3, 4, 5, 6]]), result) def testSwitchMergeAddMul(self): - with self.test_session(): + with self.cached_session(): data = constant_op.constant([1, 2, 3, 4, 5, 6], name="data") ports = ops.convert_to_tensor(True, name="ports") switch_op = control_flow_ops.switch(data, ports) @@ -255,7 +255,7 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(np.array([x * 5 for x in [1, 2, 3, 4, 5, 6]]), result) def testLoop_false(self): - with self.test_session(): + with self.cached_session(): false = ops.convert_to_tensor(False) n = constant_op.constant(10) @@ -272,7 +272,7 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(10, result) def testLoop_1(self): - with self.test_session(): + with self.cached_session(): zero = constant_op.constant(0) one = constant_op.constant(1) n = constant_op.constant(10) @@ -298,7 +298,7 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(10, result) def testLoop_2(self): - with self.test_session(): + with self.cached_session(): zero = constant_op.constant(0) one = constant_op.constant(1) n = constant_op.constant(10) @@ -324,7 +324,7 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(10, result) def testDifferentFrame(self): - with self.test_session(): + with self.cached_session(): data = array_ops.placeholder(dtypes.float32, shape=[]) enter_1 = gen_control_flow_ops.enter(data, "foo_1", False) enter_2 = gen_control_flow_ops.enter(data, "foo_2", False) @@ -333,7 +333,7 @@ class ControlFlowTest(test.TestCase): res.eval(feed_dict={data: 1.0}) def testCondBool(self): - if control_flow_ops._ENABLE_COND_V2: + if control_flow_ops.ENABLE_COND_V2: return unittest.skip("b/113296297") values = constant_op.constant(10) @@ -352,7 +352,7 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual([None], grad) def testFetchable(self): - with self.test_session() as sess: + with self.cached_session() as sess: x = array_ops.placeholder(dtypes.float32) control_flow_ops.cond( constant_op.constant(True), lambda: x + 2, lambda: x + 0) @@ -367,7 +367,7 @@ class ControlFlowTest(test.TestCase): sess.run(t, feed_dict={x: 3}) def testFeedable(self): - with self.test_session() as sess: + with self.cached_session() as sess: c = constant_op.constant(2) i0 = constant_op.constant(0) r = control_flow_ops.while_loop(lambda i: i < 1000, @@ -384,10 +384,10 @@ class ControlFlowTest(test.TestCase): sess.run(r, feed_dict={t: 3}) def testCondIndexedSlices(self): - if control_flow_ops._ENABLE_COND_V2: + if control_flow_ops.ENABLE_COND_V2: return unittest.skip("b/113296180") - with self.test_session(): + with self.cached_session(): values = constant_op.constant(10) indices = constant_op.constant(0) x = ops.IndexedSlices(values, indices) @@ -402,10 +402,10 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(0, ind) def testCondSparseTensor(self): - if control_flow_ops._ENABLE_COND_V2: + if control_flow_ops.ENABLE_COND_V2: return unittest.skip("b/113296161 (SparseTensors)") - with self.test_session(): + with self.cached_session(): values = constant_op.constant([2.0, 4.0], name="values") indices = constant_op.constant( [[0], [3]], dtype=dtypes.int64, name="indices") @@ -422,10 +422,8 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(r.values.get_shape(), (2,)) def testCondResource(self): - if control_flow_ops._ENABLE_COND_V2: - return unittest.skip("b/111124878 (don't return tuple)") - with self.test_session(): + with self.cached_session(): rv = resource_variable_ops.ResourceVariable(True) variables.global_variables_initializer().run() t = ops.convert_to_tensor(1.0) @@ -438,10 +436,10 @@ class ControlFlowTest(test.TestCase): self.assertEqual(1.0, control_flow_ops.cond(rv, case, lambda: t).eval()) def testCondIndexedSlicesDifferentTypes(self): - if control_flow_ops._ENABLE_COND_V2: + if control_flow_ops.ENABLE_COND_V2: return unittest.skip("b/113293074") - with self.test_session(): + with self.cached_session(): values = constant_op.constant(10) i_32 = ops.convert_to_tensor(0, name="one", dtype=dtypes.int32) i_64 = ops.convert_to_tensor(0, name="one", dtype=dtypes.int64) @@ -484,17 +482,14 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(11, result) def testCond_1(self): - if control_flow_ops._ENABLE_COND_V2: - return unittest.skip("b/111124878 (don't return tuple)") self._testCond_1(use_gpu=False) - self._testCond_1(use_gpu=True) + # TODO(b/116526896): Enable GPU tests. + # self._testCond_1(use_gpu=True) def testCond_2(self): - if control_flow_ops._ENABLE_COND_V2: - return unittest.skip("b/111124878 (don't return tuple)") - with self.test_session(): + with self.cached_session(): x = constant_op.constant(10) r = control_flow_ops.cond( math_ops.less(1, 0), lambda: math_ops.add(x, 1), @@ -503,10 +498,8 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(9, result) def testCond_3(self): - if control_flow_ops._ENABLE_COND_V2: - return unittest.skip("b/111124878 (don't return tuple)") - with self.test_session(): + with self.cached_session(): x = constant_op.constant(10) pred = math_ops.less(1, 2) fn1 = lambda: math_ops.add(x, 1) @@ -518,10 +511,10 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(12, result) def testCond_4(self): - if control_flow_ops._ENABLE_COND_V2: + if control_flow_ops.ENABLE_COND_V2: return unittest.skip("b/113324949 (ref vars)") - with self.test_session(): + with self.cached_session(): v1 = variables.Variable(7) v2 = variables.Variable(7) v3 = variables.Variable(7) @@ -542,7 +535,7 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(7, v3.eval()) def testCond_5(self): - with self.test_session(): + with self.cached_session(): alive = constant_op.constant(True, name="alive") count = constant_op.constant(0, name="count") @@ -556,10 +549,8 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(4, count.eval()) def testCond_6(self): - if control_flow_ops._ENABLE_COND_V2: - return unittest.skip("b/111124878 (don't return tuple)") - with self.test_session(): + with self.cached_session(): v1 = variables.Variable([7]) age = constant_op.constant(3) @@ -573,7 +564,7 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(np.array([7]), result) def testCond_7(self): - with self.test_session() as sess: + with self.cached_session() as sess: x = constant_op.constant(10) y = constant_op.constant(200) pred = math_ops.less(1, 2) @@ -583,10 +574,8 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual([11, 12], sess.run(r)) def testCondRef(self): - if control_flow_ops._ENABLE_COND_V2: - return unittest.skip("b/111124878 (don't return tuple)") - with self.test_session(): + with self.cached_session(): x = gen_state_ops.variable( shape=[1], dtype=dtypes.float32, @@ -599,10 +588,10 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual([2.0], r.eval()) def testCondWithControl(self): - if control_flow_ops._ENABLE_COND_V2: + if control_flow_ops.ENABLE_COND_V2: return unittest.skip("b/79881896") - with self.test_session() as sess: + with self.cached_session(): control_holder = array_ops.placeholder(dtypes.float32, shape=()) a = constant_op.constant(3) @@ -617,7 +606,7 @@ class ControlFlowTest(test.TestCase): self.assertEqual(5, r.eval()) def testUninitializedRefIdentity(self): - with self.test_session() as sess: + with self.cached_session() as sess: v = gen_state_ops.variable( shape=[1], dtype=dtypes.float32, @@ -641,7 +630,7 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual([1.0], sess.run(merged_op.output)) def testCondSwitchIdentity(self): - if control_flow_ops._ENABLE_COND_V2: + if control_flow_ops.ENABLE_COND_V2: return unittest.skip("b/112477618 (Operation returned from cond)") # Make sure the recv identity is not removed by optimization. @@ -658,7 +647,7 @@ class ControlFlowTest(test.TestCase): sess.run(r) def testCondRecvIdentity(self): - if control_flow_ops._ENABLE_COND_V2: + if control_flow_ops.ENABLE_COND_V2: return unittest.skip("b/112477618 (Operation returned from cond)") # Make sure the switch identity is not removed by optimization. @@ -677,7 +666,7 @@ class ControlFlowTest(test.TestCase): sess.run(r) def testCondGrad_1(self): - if control_flow_ops._ENABLE_COND_V2: + if control_flow_ops.ENABLE_COND_V2: return unittest.skip("b/113346829 (gpu failure)") graph = ops.Graph() @@ -689,11 +678,11 @@ class ControlFlowTest(test.TestCase): r = control_flow_ops.cond(pred, fn1, fn2) grad = gradients_impl.gradients(r, [x])[0] - with self.test_session(): + with self.cached_session(): self.assertAllEqual(1.0, grad.eval()) def testCondGrad_2(self): - with self.test_session(): + with self.cached_session(): c = array_ops.placeholder(dtypes.int32, shape=[]) x = constant_op.constant(10.0) pred = math_ops.less(c, 2) @@ -706,10 +695,10 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(3.0, grad.eval(feed_dict={c: 3})) def testCondGrad_3(self): - if control_flow_ops._ENABLE_COND_V2: + if control_flow_ops.ENABLE_COND_V2: return unittest.skip("b/110550782 (gradient w.r.t external variable)") - with self.test_session(): + with self.cached_session(): c = array_ops.placeholder(dtypes.int32, shape=[]) ox = constant_op.constant(10.0) pred = math_ops.less(c, 2) @@ -726,7 +715,7 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(30.0, r.eval(feed_dict={c: 3})) def testNestedCond_Simple(self): - with self.test_session(): + with self.cached_session(): x = constant_op.constant(0., name="X") y = control_flow_ops.cond( constant_op.constant(True), lambda: x, @@ -741,10 +730,10 @@ class ControlFlowTest(test.TestCase): self.assertEqual(1.0, result.eval()) def testCondGrad_Gather(self): - if control_flow_ops._ENABLE_COND_V2: + if control_flow_ops.ENABLE_COND_V2: return unittest.skip("b/113327884") - with self.test_session() as sess: + with self.cached_session() as sess: v1 = variables.Variable([1.0, 42.0]) c = array_ops.placeholder(dtypes.int32, shape=[]) pred = math_ops.less(c, 2) @@ -768,7 +757,7 @@ class ControlFlowTest(test.TestCase): # Microbenchmark: 256,000 iterations/s. def testWhile_1(self): - with self.test_session(): + with self.cached_session(): n = constant_op.constant(0) c = lambda x: math_ops.less(x, 10000) b = lambda x: math_ops.add(x, 1) @@ -776,7 +765,7 @@ class ControlFlowTest(test.TestCase): self.assertEqual(10000, r.eval()) def testWhileExternalControlDependencies(self): - with self.test_session(): + with self.cached_session(): v = variables.Variable(0.0) v.initializer.run() increment = v.assign_add(1.0) @@ -791,7 +780,7 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(v.eval(), 1.0) def testWhileExternalControlDependenciesNoInput(self): - with self.test_session(): + with self.cached_session(): v = variables.Variable(0.0) v.initializer.run() increment = v.assign_add(1.0) @@ -806,7 +795,7 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(v.eval(), 1.0) def testWhileWithRefs_1(self): - with self.test_session() as sess: + with self.cached_session() as sess: x = variables.Variable(0)._ref() # pylint: disable=protected-access i = constant_op.constant(0) c = lambda i, x: math_ops.less(i, 100) @@ -830,19 +819,19 @@ class ControlFlowTest(test.TestCase): self.assertEqual(0, value_x) def testWhile_2(self): - with self.test_session(): + with self.cached_session(): s = constant_op.constant(0) r = isum(s) self.assertAllEqual(45, r.eval()) def testWhileWithMaximumIterations(self): - with self.test_session(): + with self.cached_session(): s = constant_op.constant([1, 2, 3, 4, 5]) r = isum(s, maximum_iterations=3) self.assertAllEqual([1 + 3, 2 + 3, 3 + 3, 4 + 3, 5 + 3], r.eval()) def testWhileWithMaximumIterationsAndSingleArgument(self): - with self.test_session(): + with self.cached_session(): r = control_flow_ops.while_loop( lambda i: i < 3, lambda i: i + 1, [0], maximum_iterations=1) self.assertEqual(1, r.eval()) @@ -916,7 +905,7 @@ class ControlFlowTest(test.TestCase): _ = gradients_impl.gradients(loop_with_maxiter, v) def testInvalidMaximumIterationsFromSiblingContextWhileLoopInXLAContext(self): - if control_flow_ops._ENABLE_COND_V2: + if control_flow_ops.ENABLE_COND_V2: return unittest.skip("b/113294340 (enable while_v2)") v = constant_op.constant(1.0) @@ -1019,7 +1008,7 @@ class ControlFlowTest(test.TestCase): # Have more than 10 parallel iterations and hence exercise k-bound # most of the time. def testWhile_3(self): - with self.test_session(): + with self.cached_session(): def compute(i, m, c, o): m, c = [math_ops.add(m, 1), math_ops.add(c, 1)] @@ -1039,7 +1028,7 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(10100, result) def testWhile_4(self): - with self.test_session(): + with self.cached_session(): def compute(i, m, c, o): m, c = [array_ops.gather(x, i), array_ops.gather(x, i)] @@ -1060,7 +1049,7 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(42, result) def testWhile_5(self): - with self.test_session(): + with self.cached_session(): def compute(i, c, o): c = array_ops.strided_slice(x, array_ops.expand_dims(i, 0), @@ -1088,7 +1077,7 @@ class ControlFlowTest(test.TestCase): trace_level=config_pb2.RunOptions.FULL_TRACE) run_metadata = config_pb2.RunMetadata() - with self.test_session() as sess: + with self.cached_session() as sess: with ops.device("/cpu:0"): c = constant_op.constant(2) i0 = constant_op.constant(0) @@ -1134,7 +1123,7 @@ class ControlFlowTest(test.TestCase): self._testWhile_Gpu_1(use_gpu=True) def testWhileShape(self): - with self.test_session(): + with self.cached_session(): i = constant_op.constant(0) m = array_ops.ones([2, 2]) c = lambda i, j: math_ops.less(i, 2) @@ -1151,7 +1140,7 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(np.ones((8, 8)), r.eval()) def testWhileWithNonTensorInput_Scalar(self): - with self.test_session(): + with self.cached_session(): n = 0 c = lambda x: x < 10000 b = lambda x: x + 1 @@ -1159,7 +1148,7 @@ class ControlFlowTest(test.TestCase): self.assertEqual(10000, r.eval()) def testWhileWithNonTensorInput_Vector(self): - with self.test_session(): + with self.cached_session(): n = np.array([0]) # Note, [0] would not work here; that is a list c = lambda x: x[0] < 10000 b = lambda x: array_ops.stack([x[0] + 1]) @@ -1167,7 +1156,7 @@ class ControlFlowTest(test.TestCase): self.assertEqual([10000], r.eval()) def testWhileShapeInference(self): - with self.test_session(): + with self.cached_session(): i = constant_op.constant(0) m = array_ops.ones([2, 2]) c = lambda i, j: math_ops.less(i, 2) @@ -1192,7 +1181,7 @@ class ControlFlowTest(test.TestCase): r = control_flow_ops.while_loop(c, b, [i, m]) def testWhileShapeInferenceSparseTensor(self): - with self.test_session(): + with self.cached_session(): values = constant_op.constant([2.0, 4.0], name="values") indices = constant_op.constant( [[0], [3]], dtype=dtypes.int64, name="indices") @@ -1223,7 +1212,7 @@ class ControlFlowTest(test.TestCase): [i.get_shape(), tensor_shape.TensorShape([5])]) def testWhileShapeInferenceIndexedSlices(self): - with self.test_session(): + with self.cached_session(): values = constant_op.constant([[2.0, 4.0], [3.0, 5.0]], name="values") indices = constant_op.constant([0, 3], name="indices") shape = constant_op.constant([10, 2], name="dense_shape") @@ -1313,7 +1302,7 @@ class ControlFlowTest(test.TestCase): self._testNestedWhile_2(use_gpu=True) def testWhileWithControl_1(self): - with self.test_session(): + with self.cached_session(): n = constant_op.constant(0) r = constant_op.constant(0) condition = lambda n_, r_: math_ops.less(n_, 10) @@ -1329,7 +1318,7 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(12, res[1].eval()) def testWhileWithControl_2(self): - with self.test_session(): + with self.cached_session(): r = constant_op.constant(0) condition = lambda r_: math_ops.less(r_, 10) @@ -1343,7 +1332,7 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(12, res.eval()) def testWhileWithControl_3(self): - with self.test_session() as sess: + with self.cached_session() as sess: b = array_ops.placeholder(dtypes.bool) c = constant_op.constant(1) x0 = constant_op.constant(0) @@ -1352,7 +1341,7 @@ class ControlFlowTest(test.TestCase): self.assertEqual(10, sess.run(r, {b: True})) def testWhileWithControl_4(self): - with self.test_session() as sess: + with self.cached_session() as sess: b = array_ops.placeholder(dtypes.bool) c = constant_op.constant(1) x0 = constant_op.constant(0) @@ -1362,7 +1351,7 @@ class ControlFlowTest(test.TestCase): self.assertEqual(10, sess.run(r, {b: True})) def testWhileWithControl_5(self): - with self.test_session() as sess: + with self.cached_session() as sess: b = array_ops.placeholder(dtypes.bool) c = constant_op.constant(1) x0 = constant_op.constant(0) @@ -1375,12 +1364,12 @@ class ControlFlowTest(test.TestCase): self.assertEqual(10, sess.run(r, {b: True})) def testWhileCondWithControl(self): - if control_flow_ops._ENABLE_COND_V2: + if control_flow_ops.ENABLE_COND_V2: return unittest.skip("b/113294377 (unknown shape)") # Ensure that no control edges by an outer control dependency context are # added to nodes inside cond/while contexts. - with self.test_session() as sess: + with self.cached_session() as sess: const_true = lambda: constant_op.constant(True) const_false = lambda: constant_op.constant(False) cond = lambda i: control_flow_ops.cond(i > 0, const_true, const_false) @@ -1392,10 +1381,10 @@ class ControlFlowTest(test.TestCase): self.assertEqual(0, sess.run(loop)) def testWhileCondWithControl_1(self): - if control_flow_ops._ENABLE_COND_V2: + if control_flow_ops.ENABLE_COND_V2: return unittest.skip("b/113324949 (ref vars)") - with self.test_session(): + with self.cached_session(): v = variable_scope.get_variable( "v", [], initializer=init_ops.constant_initializer(2)) i0 = constant_op.constant(0) @@ -1417,10 +1406,10 @@ class ControlFlowTest(test.TestCase): self.assertAllClose(65536.0, v.eval()) def testWhileCondExitControl(self): - if control_flow_ops._ENABLE_COND_V2: + if control_flow_ops.ENABLE_COND_V2: return unittest.skip("b/113294340 (enable while_v2)") - with self.test_session(): + with self.cached_session(): v = variables.Variable(1) def false_branch(): @@ -1443,10 +1432,10 @@ class ControlFlowTest(test.TestCase): self.assertEqual(99, v.eval()) def testCondWhile_1(self): - if control_flow_ops._ENABLE_COND_V2: - return unittest.skip("b/111124878 (don't return tuple)") + if control_flow_ops.ENABLE_COND_V2: + return unittest.skip("b/113294340 (enable while_v2)") - with self.test_session(): + with self.cached_session(): n = ops.convert_to_tensor(0, name="n") c = lambda x: math_ops.less(x, 10) b = lambda x: math_ops.add(x, 1) @@ -1456,10 +1445,10 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(10, r.eval()) def testCondWhile_2(self): - if control_flow_ops._ENABLE_COND_V2: - return unittest.skip("b/111124878 (don't return tuple)") + if control_flow_ops.ENABLE_COND_V2: + return unittest.skip("b/113294340 (enable while_v2)") - with self.test_session(): + with self.cached_session(): n = ops.convert_to_tensor(0) c = lambda x: math_ops.less(x, 10) b = lambda x: math_ops.add(x, 1) @@ -1469,7 +1458,7 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(10, r.eval()) def _testCondWhile_3(self, use_gpu): - if control_flow_ops._ENABLE_COND_V2: + if control_flow_ops.ENABLE_COND_V2: return unittest.skip("b/113294340 (enable while_v2)") with self.test_session(use_gpu=use_gpu) as sess: @@ -1498,10 +1487,10 @@ class ControlFlowTest(test.TestCase): self._testCondWhile_3(use_gpu=True) def testWhileCond_1(self): - if control_flow_ops._ENABLE_COND_V2: + if control_flow_ops.ENABLE_COND_V2: return unittest.skip("b/113294377 (unknown shape)") - with self.test_session(): + with self.cached_session(): i = ops.convert_to_tensor(0, name="i") n = ops.convert_to_tensor(10, name="n") one = ops.convert_to_tensor(1, name="one") @@ -1516,10 +1505,10 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(10, r.eval()) def testWhileCond_2(self): - if control_flow_ops._ENABLE_COND_V2: + if control_flow_ops.ENABLE_COND_V2: return unittest.skip("b/113294377 (unknown shape)") - with self.test_session(): + with self.cached_session(): n = ops.convert_to_tensor(0, name="n") c = lambda x: math_ops.less(x, 10) b = lambda x: control_flow_ops.cond(constant_op.constant(True), lambda: math_ops.add(x, 1), lambda: n) @@ -1527,10 +1516,10 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(10, r.eval()) def testWhileCond_3(self): - if control_flow_ops._ENABLE_COND_V2: + if control_flow_ops.ENABLE_COND_V2: return unittest.skip("b/113294377 (unknown shape)") - with self.test_session(): + with self.cached_session(): n = ops.convert_to_tensor(0) c = lambda x: math_ops.less(x, 10) # pylint: disable=undefined-variable @@ -1544,7 +1533,7 @@ class ControlFlowTest(test.TestCase): # NOTE: It is ok to have parallel_iterations > 1 def testWhileUpdateVariable_1(self): - with self.test_session(): + with self.cached_session(): select = variables.Variable([3.0, 4.0, 5.0]) n = constant_op.constant(0) @@ -1566,7 +1555,7 @@ class ControlFlowTest(test.TestCase): self.assertAllClose(np.array([10.0, 10.0, 10.0]), result) def testWhileUpdateVariable_2(self): - with self.test_session(): + with self.cached_session(): select1 = variables.Variable([3.0, 4.0, 5.0]) select2 = variables.Variable([3.0, 4.0, 5.0]) n = constant_op.constant(0) @@ -1592,7 +1581,7 @@ class ControlFlowTest(test.TestCase): self.assertAllClose(np.array([10.0, 10.0, 10.0]), result2) def testWhileUpdateVariable_3(self): - with self.test_session(): + with self.cached_session(): select = variables.Variable([3.0, 4.0, 5.0]) n = constant_op.constant(0) @@ -1614,7 +1603,7 @@ class ControlFlowTest(test.TestCase): # b/24814703 def testWhileUpdateVariable_4(self): - with self.test_session(): + with self.cached_session(): var_a = variables.Variable(0, name="a") var_b = variables.Variable(0, name="b") variables.global_variables_initializer().run() @@ -1642,7 +1631,7 @@ class ControlFlowTest(test.TestCase): # b/24736492 def testWhileUpdateVariable_5(self): - with self.test_session(): + with self.cached_session(): # Create some variables. var_a = variables.Variable(0, name="a") var_b = variables.Variable(0, name="b") @@ -1672,7 +1661,7 @@ class ControlFlowTest(test.TestCase): # b/24814668 def testWhileUpdateVariable_6(self): - with self.test_session(): + with self.cached_session(): # Create some variables. var_a = variables.Variable(0, name="a") var_b = variables.Variable(0, name="b") @@ -1701,7 +1690,7 @@ class ControlFlowTest(test.TestCase): self.assertEqual(10, var_a.eval()) def testWhileQueue_1(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.FIFOQueue(-1, dtypes.int32) i = constant_op.constant(0) @@ -1719,7 +1708,7 @@ class ControlFlowTest(test.TestCase): self.assertEqual([i], q.dequeue().eval()) def testWhileStack_1(self): - with self.test_session(): + with self.cached_session(): s = gen_data_flow_ops.stack_v2(-1, dtypes.int32, stack_name="foo") i = constant_op.constant(0) @@ -1753,7 +1742,7 @@ class ControlFlowTest(test.TestCase): def _testWhileGrad_ColocateGradients(self, colocate): gpu_dev_name = test.gpu_device_name() if test.is_gpu_available( - ) else "/device:GPU:0" + ) else "/device:CPU:0" graph = ops.Graph() with graph.as_default(): @@ -1783,7 +1772,7 @@ class ControlFlowTest(test.TestCase): else: self.assertFalse(gpu_dev_name in dev) - with self.test_session(graph=graph) as sess: + with self.session(graph=graph) as sess: self.assertAllClose(1024.0, sess.run(r)) def testWhileGrad_ColocateGradients(self): @@ -1791,7 +1780,7 @@ class ControlFlowTest(test.TestCase): self._testWhileGrad_ColocateGradients(colocate=True) def testWhileGrad_Square(self): - with self.test_session(): + with self.cached_session(): v = constant_op.constant(2.0, name="v") c = lambda v: math_ops.less(v, 100.0) b = math_ops.square @@ -1802,7 +1791,7 @@ class ControlFlowTest(test.TestCase): self.assertAllClose(1024.0, r.eval()) def testWhileGrad_Shape(self): - with self.test_session(): + with self.cached_session(): x = array_ops.placeholder(dtypes.float32, shape=[None]) v = constant_op.constant([2.0], name="v") n = constant_op.constant(0, name="n") @@ -1819,7 +1808,7 @@ class ControlFlowTest(test.TestCase): self.assertAllClose([810.0, 2560.0], r.eval(feed_dict={x: [3.0, 4.0]})) def testWhileGrad_BaseShape(self): - with self.test_session() as sess: + with self.cached_session() as sess: x = array_ops.placeholder(dtypes.float32, [None]) v0 = constant_op.constant([2.0, 2.0], name="v") c = lambda v: constant_op.constant(False) @@ -1831,7 +1820,7 @@ class ControlFlowTest(test.TestCase): self.assertAllClose([2.0, 4.0], sess.run(r, feed_dict={x: [1.0, 2.0]})) def testWhileGrad_MultipleUses(self): - with self.test_session(): + with self.cached_session(): v = constant_op.constant(2.0, name="v") c = lambda v: math_ops.less(v, 100.0) b = math_ops.square @@ -1842,7 +1831,7 @@ class ControlFlowTest(test.TestCase): self.assertEqual(524288.0, r.eval()) def testWhileGrad_LoopAdd(self): - with self.test_session(): + with self.cached_session(): v = constant_op.constant(2.0, name="v") c = lambda v: math_ops.less(v, 100.0) b = math_ops.square @@ -1872,7 +1861,7 @@ class ControlFlowTest(test.TestCase): self._testWhileGrad_Mul(use_gpu=True, p_iters=10) def _testNestedWhileCondWhileGrad(self, use_gpu): - if control_flow_ops._ENABLE_COND_V2: + if control_flow_ops.ENABLE_COND_V2: return unittest.skip("b/113294377 (unknown shape)") with self.test_session(use_gpu=use_gpu): @@ -1901,7 +1890,7 @@ class ControlFlowTest(test.TestCase): self._testNestedWhileCondWhileGrad(use_gpu=True) def testWhileGrad_Variable(self): - with self.test_session(): + with self.cached_session(): a = variables.Variable(3.0) v = constant_op.constant(2.0, name="v") c = lambda v: math_ops.less(v, 100.0) @@ -1913,10 +1902,10 @@ class ControlFlowTest(test.TestCase): self.assertAllClose(216.0, r[0].eval()) def testWhileGradInCond(self): - if control_flow_ops._ENABLE_COND_V2: + if control_flow_ops.ENABLE_COND_V2: return unittest.skip("b/110550782 (gradient w.r.t external variable)") - with self.test_session(): + with self.cached_session(): n = ops.convert_to_tensor(1.0, name="n") x = array_ops.placeholder(dtypes.float32, shape=None) c = lambda n: math_ops.less(n, 10.0) @@ -1931,7 +1920,7 @@ class ControlFlowTest(test.TestCase): self.assertAllClose(9.0, r.eval(feed_dict={x: 1.0})) def testGradInWhileWrtInitialLoopVal(self): - with self.test_session(): + with self.cached_session(): x = array_ops.placeholder(dtypes.float32, shape=(), name="x") y = x + 1 @@ -1948,7 +1937,7 @@ class ControlFlowTest(test.TestCase): control_flow_ops.while_loop(lambda i, x: i < 3, body, [0, y]) def testWhileGradInWhile(self): - with self.test_session(): + with self.cached_session(): n = ops.convert_to_tensor(1.0, name="n") x = array_ops.placeholder(dtypes.float32, shape=None) c = lambda n: math_ops.less(n, 10.0) @@ -1964,7 +1953,7 @@ class ControlFlowTest(test.TestCase): self.assertAllClose(9.0, r.eval(feed_dict={x: 1.0})) def testCondGradInNestedWhiles(self): - if control_flow_ops._ENABLE_COND_V2: + if control_flow_ops.ENABLE_COND_V2: return unittest.skip("b/113346829 (gpu failure)") def outer_body(i, x): @@ -1978,13 +1967,13 @@ class ControlFlowTest(test.TestCase): i, x = control_flow_ops.while_loop(lambda i, x: i < 3, outer_body, [0, 0.0]) - with self.test_session() as sess: + with self.cached_session() as sess: i_val, x_val = sess.run([i, x]) self.assertEqual(i_val, 3) self.assertAllClose(x_val, 1.0) def testWhile_NestedInput(self): - with self.test_session() as sess: + with self.cached_session() as sess: named = collections.namedtuple("named", ("a", "b")) loop_vars = [ named(a=constant_op.constant(0.0), b=constant_op.constant(1.0)), @@ -2011,7 +2000,7 @@ class ControlFlowTest(test.TestCase): sess.run(r_flattened)) def testWhile_NestedBadArityFails(self): - with self.test_session(): + with self.cached_session(): named = collections.namedtuple("named", ("a", "b")) loop_vars = [ named(a=constant_op.constant(0.0), b=constant_op.constant(1.0)), @@ -2027,7 +2016,7 @@ class ControlFlowTest(test.TestCase): control_flow_ops.while_loop(c, b, loop_vars) def testWhileGrad_ys_xs(self): - with self.test_session(): + with self.cached_session(): x = constant_op.constant(3.0, name="x") y = constant_op.constant(2.0, name="y") @@ -2050,7 +2039,7 @@ class ControlFlowTest(test.TestCase): self.assertAllClose(120.0, r[0].eval()) def testWhileGrad_Dependency(self): - with self.test_session(): + with self.cached_session(): i = constant_op.constant(0, name="i") x = constant_op.constant(2.0, name="x") @@ -2069,7 +2058,7 @@ class ControlFlowTest(test.TestCase): self.assertAllClose(1024.0, r[0].eval()) def testWhileGrad_NoGradient(self): - with self.test_session(): + with self.cached_session(): v = constant_op.constant(2.0, name="v") c = lambda v: math_ops.less(v, 100.0) b = math_ops.square @@ -2079,7 +2068,7 @@ class ControlFlowTest(test.TestCase): self.assertAllClose(1.0, r[0].eval()) def testWhileGrad_NoDependency(self): - with self.test_session() as sess: + with self.cached_session() as sess: variable = variables.Variable(array_ops.ones([2, 3])) duration = array_ops.zeros([], dtype=dtypes.int32) @@ -2099,7 +2088,7 @@ class ControlFlowTest(test.TestCase): self.assertAllClose(np.ones([2, 3]), sess.run(grad[0])) def testWhileGrad_Const(self): - with self.test_session() as sess: + with self.cached_session() as sess: c0 = constant_op.constant(0.0, name="c0") c1 = constant_op.constant(1.0, name="c1") duration = constant_op.constant(0, name="t") @@ -2118,7 +2107,7 @@ class ControlFlowTest(test.TestCase): self.assertAllClose(0.0, sess.run(grad[0])) def testWhileGrad_SerialTwoLoops(self): - with self.test_session(): + with self.cached_session(): i = constant_op.constant(0, name="i") x = constant_op.constant(2.0, name="x") @@ -2136,7 +2125,7 @@ class ControlFlowTest(test.TestCase): self.assertAllClose(1024.0, r[0].eval()) def testWhileGrad_ParallelTwoLoops(self): - with self.test_session(): + with self.cached_session(): i = constant_op.constant(0, name="i") x = constant_op.constant(2.0, name="x") @@ -2155,7 +2144,7 @@ class ControlFlowTest(test.TestCase): self.assertAllClose(64.0, r[0].eval()) def testWhileGrad_OneOutputWithControlDependencyOnSecond(self): - with self.test_session(): + with self.cached_session(): i = constant_op.constant(0, name="i") x = constant_op.constant(1.0, name="x") y = constant_op.constant(1.0, name="y") @@ -2196,7 +2185,7 @@ class ControlFlowTest(test.TestCase): self._testNestedWhileGrad_Simple(use_gpu=True) def testNestedWhileGrad_SerialInner(self): - with self.test_session(): + with self.cached_session(): v = constant_op.constant(1.0) def inner_loop1(s): @@ -2219,7 +2208,7 @@ class ControlFlowTest(test.TestCase): self.assertAllClose(256.0, r.eval()) def testNestedWhileGrad_ParallelInner(self): - with self.test_session(): + with self.cached_session(): v = constant_op.constant(1.0) def inner_loop1(s): @@ -2244,7 +2233,7 @@ class ControlFlowTest(test.TestCase): def testNestedWhileGrad_ParallelIterations(self): # Make sure the stack pushes and pops of an inner loop are executed in # the sequential order of the iterations of its outer loop. - with self.test_session() as sess: + with self.cached_session() as sess: def inner_loop(t): fn = lambda n: n + math_ops.square(var) @@ -2280,14 +2269,14 @@ class ControlFlowTest(test.TestCase): self.assertAllClose(1024.0, r.eval()) def testWhileCondGrad_Simple(self): - if control_flow_ops._ENABLE_COND_V2: + if control_flow_ops.ENABLE_COND_V2: return unittest.skip("b/113294377 (unknown shape)") self._testWhileCondGrad_Simple(use_gpu=False) self._testWhileCondGrad_Simple(use_gpu=True) def testWhileCondGrad_UnknownShape(self): - with self.test_session() as sess: + with self.cached_session() as sess: v = array_ops.placeholder(dtypes.float32) n = ops.convert_to_tensor(100.0, name="n") one = ops.convert_to_tensor(1.0, name="one") @@ -2304,7 +2293,7 @@ class ControlFlowTest(test.TestCase): self.assertAllClose(1024.0, r) def testWhileGrad_Concat(self): - with self.test_session() as sess: + with self.cached_session() as sess: x = variable_scope.get_variable("x", initializer=[[1., 2.]]) i0 = constant_op.constant(0) h0 = array_ops.zeros([0, 2]) @@ -2327,7 +2316,7 @@ class ControlFlowTest(test.TestCase): self.assertAllClose([[0.98000002, 1.98000002]], sess.run(x)) def testWhileWithRefsWithGradients_1(self): - with self.test_session() as sess: + with self.cached_session() as sess: x = variables.Variable(0.)._ref() # pylint: disable=protected-access i = constant_op.constant(0) c = lambda i, x: math_ops.less(i, 10) @@ -2355,7 +2344,7 @@ class ControlFlowTest(test.TestCase): self.assertEqual(73, value_x_grad) def testWhileGrad_IndexedSlices(self): - with self.test_session(): + with self.cached_session(): values = constant_op.constant([2.0, 4.0], name="values") indices = constant_op.constant([0, 3], name="indices") shape = constant_op.constant([10], name="dense_shape") @@ -2376,7 +2365,7 @@ class ControlFlowTest(test.TestCase): self.assertAllClose(np.array([1024.0, 1024.0]), r.eval()) def testWhileGrad_SparseTensor(self): - with self.test_session(): + with self.cached_session(): values = constant_op.constant([2.0, 4.0], name="values") indices = constant_op.constant( [[0], [3]], dtype=dtypes.int64, name="indices") @@ -2398,7 +2387,7 @@ class ControlFlowTest(test.TestCase): self.assertAllClose(np.array([1024.0, 1024.0]), r.eval()) def testCallGradInLoop(self): - with self.test_session() as sess: + with self.cached_session() as sess: i0 = constant_op.constant(0) params = constant_op.constant(5.0) params_1 = math_ops.square(params) @@ -2417,7 +2406,7 @@ class ControlFlowTest(test.TestCase): self.assertAllClose(600.0, sess.run(output_grad)[1]) def testWhileAndTensorArray(self): - with self.test_session() as sess: + with self.cached_session() as sess: param = constant_op.constant(2.0) n0 = constant_op.constant(0) y0 = constant_op.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], name="elems") @@ -2436,7 +2425,7 @@ class ControlFlowTest(test.TestCase): self.assertAllClose(107520.0, sess.run(r)) def testWhileGrad_StopGrad(self): - with self.test_session(): + with self.cached_session(): x = constant_op.constant(3.0, name="x") y = constant_op.constant(2.0, name="y") @@ -2479,7 +2468,7 @@ class ControlFlowTest(test.TestCase): self.assertEqual(32.0, r.eval()) def testWhileGrad_StopGradInside(self): - with self.test_session(): + with self.cached_session(): x = constant_op.constant(3.0, name="x") y = constant_op.constant(2.0, name="y") @@ -2498,7 +2487,7 @@ class ControlFlowTest(test.TestCase): self.assertAllClose(156.0, r.eval()) def testWhileGrad_StopGradInsideNoShape(self): - with self.test_session() as sess: + with self.cached_session() as sess: x = array_ops.placeholder(dtypes.float32) y = array_ops.placeholder(dtypes.float32) @@ -2534,7 +2523,7 @@ class ControlFlowTest(test.TestCase): gradients_impl.gradients(grad_theta_stopped, theta) def testStopGradOnWhileGrad(self): - with self.test_session(): + with self.cached_session(): x = constant_op.constant(2.0, name="x") y = constant_op.constant(2.0, name="y") @@ -2562,7 +2551,7 @@ class ControlFlowTest(test.TestCase): _, y = control_flow_ops.while_loop(cond, body, (math_ops.argmin(q), 0.)) dy_dq, = gradients_impl.gradients(y, q) self.assertIsNotNone(dy_dq) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(q.initializer) self.assertAllClose([0., 0.], sess.run(dy_dq)) @@ -2579,7 +2568,7 @@ class ControlFlowTest(test.TestCase): _, y = control_flow_ops.while_loop(cond, body, (math_ops.argmin(q), 0.)) dy_dq, = gradients_impl.gradients(y, q) self.assertIsNotNone(dy_dq) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(q.initializer) self.assertAllClose([1., 1.], sess.run(dy_dq)) @@ -2607,7 +2596,7 @@ class ControlFlowTest(test.TestCase): self.assertIsNotNone(grad) def testStopGradMultiFlows(self): - with self.test_session(): + with self.cached_session(): def body(i, y, r): x = variable_scope.get_variable( @@ -2633,10 +2622,8 @@ class ControlFlowTest(test.TestCase): self.assertEqual(5.0, result.eval()) def testOneValueCond(self): - if control_flow_ops._ENABLE_COND_V2: - return unittest.skip("b/111124878 (don't return tuple)") - with self.test_session(): + with self.cached_session(): c = array_ops.placeholder(dtypes.int32, shape=[]) one = ops.convert_to_tensor(1, name="one") two = ops.convert_to_tensor(2, name="two") @@ -2651,10 +2638,8 @@ class ControlFlowTest(test.TestCase): self.assertEqual([2], i.eval(feed_dict={c: 0})) def testExampleCond(self): - if control_flow_ops._ENABLE_COND_V2: - return unittest.skip("b/111124878 (don't return tuple)") - with self.test_session(): + with self.cached_session(): x = ops.convert_to_tensor([-2.0, 2.0], name="x") d = array_ops.placeholder(dtypes.int32, shape=[]) @@ -2669,10 +2654,10 @@ class ControlFlowTest(test.TestCase): self.assertAllClose(2.0 * math.sqrt(2), i.eval(feed_dict={d: 2})) def testCase(self): - if control_flow_ops._ENABLE_COND_V2: + if control_flow_ops.ENABLE_COND_V2: return unittest.skip("b/112477618 (Operation returned from cond)") - with self.test_session(): + with self.cached_session(): x = constant_op.constant(1) y = constant_op.constant(2) z = constant_op.constant(3) @@ -2724,10 +2709,10 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(r6.eval(), 0) def testCaseSideEffects(self): - if control_flow_ops._ENABLE_COND_V2: + if control_flow_ops.ENABLE_COND_V2: return unittest.skip("b/112477618 (Operation returned from cond)") - with self.test_session() as sess: + with self.cached_session() as sess: v0 = variables.Variable(-1) v1 = variables.Variable(-1) v2 = variables.Variable(-1) @@ -2762,10 +2747,10 @@ class ControlFlowTest(test.TestCase): self.assertAllEqual(sess.run([v0, v1, v2]), [0, -1, -1]) def testOneOpCond(self): - if control_flow_ops._ENABLE_COND_V2: + if control_flow_ops.ENABLE_COND_V2: return unittest.skip("b/113324949 (ref vars)") - with self.test_session(): + with self.cached_session(): v = variables.Variable(0) c = ops.convert_to_tensor(0) one = ops.convert_to_tensor(1) @@ -2793,7 +2778,7 @@ class ControlFlowTest(test.TestCase): self.assertEqual(2, v.eval()) def testWithOpsDependencies(self): - with self.test_session() as sess: + with self.cached_session() as sess: v = variables.Variable(0.0) c = constant_op.constant(10) @@ -2816,7 +2801,7 @@ class ControlFlowTest(test.TestCase): self.assertAllClose(0.0, real_v_val) def testWithTensorDependencies(self): - with self.test_session(): + with self.cached_session(): v = variables.Variable(0.0) c1 = constant_op.constant(10) c2 = constant_op.constant(20) @@ -2842,7 +2827,7 @@ class ControlFlowTest(test.TestCase): self.assertAllClose(0.0, v.eval()) def testWithIndexedSlicesDependencies(self): - with self.test_session(): + with self.cached_session(): v = variables.Variable( np.array([[0.0, 1.0], [10.0, 11.0], [20.0, 21.0]]).astype(np.float32)) v_at_1 = ops.IndexedSlices(v, constant_op.constant([1])) @@ -2886,7 +2871,7 @@ class ControlFlowTest(test.TestCase): self.assertEqual([b"loc:@vdef"], with_vdef_dep.op.colocation_groups()) def testGroup(self): - with self.test_session() as sess: + with self.cached_session() as sess: v1 = variables.Variable([0.0]) v2 = variables.Variable([1.0]) @@ -2997,7 +2982,7 @@ class ControlFlowTest(test.TestCase): self.assertEqual(None, s.get_shape()) def testRunLoopTensor(self): - with self.test_session() as sess: + with self.cached_session() as sess: tensor_list = [] def condition(t): @@ -3021,7 +3006,7 @@ class ControlFlowTest(test.TestCase): def func(x): return np.square(x) - with self.test_session(): + with self.cached_session(): r = control_flow_ops.while_loop( lambda i, v: i < 4, lambda i, v: [i + 1, script_ops.py_func(func, [v], [dtypes.float32])[0]], @@ -3035,7 +3020,7 @@ class ControlFlowTest(test.TestCase): def func(x): return math_ops.square(math_ops.square(x)) - with self.test_session(): + with self.cached_session(): x = constant_op.constant(2.0, dtypes.float32) r = control_flow_ops.while_loop( lambda i, v: i < 2, lambda i, v: [i + 1, func(v)], @@ -3174,7 +3159,7 @@ class TupleTest(test.TestCase): def testTensors(self): for v1_first in [True, False]: - with self.test_session(): + with self.cached_session(): v1 = variables.Variable([1.0]) add1 = math_ops.add( control_flow_ops.with_dependencies([v1.initializer], v1._ref()), # pylint: disable=protected-access @@ -3204,7 +3189,7 @@ class TupleTest(test.TestCase): def testIndexedSlices(self): for v1_first in [True, False]: - with self.test_session(): + with self.cached_session(): v1 = variables.Variable( np.array([[0.0, 1.0], [10.0, 11.0], [20.0, 21.0]]).astype( np.float32)) @@ -3243,7 +3228,7 @@ class TupleTest(test.TestCase): v1.eval()) def testAcceptTensorsAsControlInputs(self): - with self.test_session(): + with self.cached_session(): var = variables.Variable(0) assign = state_ops.assign(var, 1) t, = control_flow_ops.tuple( @@ -3408,6 +3393,7 @@ class WhileOpBenchmark(test.Benchmark): name="unroll_same_device", iters=iters, wall_time=duration) +@test_util.with_cond_v2 class EagerTest(test.TestCase): def testCond(self): diff --git a/tensorflow/python/kernel_tests/conv1d_test.py b/tensorflow/python/kernel_tests/conv1d_test.py index fcba456004..2d6d8a8051 100644 --- a/tensorflow/python/kernel_tests/conv1d_test.py +++ b/tensorflow/python/kernel_tests/conv1d_test.py @@ -53,7 +53,7 @@ class Conv1DTest(test.TestCase): self.assertAllClose(output, [2 * 1 + 1 * 2, 2 * 3 + 1 * 4]) def testConv1DTranspose(self): - with self.test_session(): + with self.cached_session(): stride = 2 # Input, output: [batch, width, depth] diff --git a/tensorflow/python/kernel_tests/conv2d_backprop_filter_grad_test.py b/tensorflow/python/kernel_tests/conv2d_backprop_filter_grad_test.py index be299beee4..644a151710 100644 --- a/tensorflow/python/kernel_tests/conv2d_backprop_filter_grad_test.py +++ b/tensorflow/python/kernel_tests/conv2d_backprop_filter_grad_test.py @@ -32,7 +32,7 @@ from tensorflow.python.platform import test class Conv2DBackpropFilterGradTest(test.TestCase): def testGradient(self): - with self.test_session(): + with self.cached_session(): for padding in ["SAME", "VALID"]: for stride in [1, 2]: np.random.seed(1) diff --git a/tensorflow/python/kernel_tests/conv2d_transpose_test.py b/tensorflow/python/kernel_tests/conv2d_transpose_test.py index 27804be65c..cbdd2c5991 100644 --- a/tensorflow/python/kernel_tests/conv2d_transpose_test.py +++ b/tensorflow/python/kernel_tests/conv2d_transpose_test.py @@ -37,7 +37,7 @@ from tensorflow.python.platform import test class Conv2DTransposeTest(test.TestCase): def testConv2DTransposeSingleStride(self): - with self.test_session(): + with self.cached_session(): strides = [1, 1, 1, 1] # Input, output: [batch, height, width, depth] @@ -75,7 +75,7 @@ class Conv2DTransposeTest(test.TestCase): self.assertAllClose(target, value[n, h, w, k]) def testConv2DTransposeSame(self): - with self.test_session(): + with self.cached_session(): strides = [1, 2, 2, 1] # Input, output: [batch, height, width, depth] @@ -108,7 +108,7 @@ class Conv2DTransposeTest(test.TestCase): self.assertAllClose(target, value[n, h, w, k]) def testConv2DTransposeValid(self): - with self.test_session(): + with self.cached_session(): strides = [1, 2, 2, 1] # Input, output: [batch, height, width, depth] @@ -163,7 +163,7 @@ class Conv2DTransposeTest(test.TestCase): np.random.seed(1) # Make it reproducible. x_val = np.random.random_sample(x_shape).astype(np.float64) f_val = np.random.random_sample(f_shape).astype(np.float64) - with self.test_session(): + with self.cached_session(): x = constant_op.constant(x_val, name="x", dtype=dtypes.float32) f = constant_op.constant(f_val, name="f", dtype=dtypes.float32) output = nn_ops.conv2d_transpose( diff --git a/tensorflow/python/kernel_tests/conv3d_backprop_filter_v2_grad_test.py b/tensorflow/python/kernel_tests/conv3d_backprop_filter_v2_grad_test.py index 85264ef876..89b64068ac 100644 --- a/tensorflow/python/kernel_tests/conv3d_backprop_filter_v2_grad_test.py +++ b/tensorflow/python/kernel_tests/conv3d_backprop_filter_v2_grad_test.py @@ -32,7 +32,7 @@ from tensorflow.python.platform import test class Conv3DBackpropFilterV2GradTest(test.TestCase): def testGradient(self): - with self.test_session(): + with self.cached_session(): for padding in ["SAME", "VALID"]: for stride in [1, 2]: np.random.seed(1) diff --git a/tensorflow/python/kernel_tests/conv3d_transpose_test.py b/tensorflow/python/kernel_tests/conv3d_transpose_test.py index 289ae29fce..2527b83769 100644 --- a/tensorflow/python/kernel_tests/conv3d_transpose_test.py +++ b/tensorflow/python/kernel_tests/conv3d_transpose_test.py @@ -32,7 +32,7 @@ from tensorflow.python.platform import test class Conv3DTransposeTest(test.TestCase): def testConv3DTransposeSingleStride(self): - with self.test_session(): + with self.cached_session(): strides = [1, 1, 1, 1, 1] # Input, output: [batch, depth, height, width, channel] @@ -82,7 +82,7 @@ class Conv3DTransposeTest(test.TestCase): self.assertAllClose(target, value[n, d, h, w, k]) def testConv3DTransposeSame(self): - with self.test_session(): + with self.cached_session(): strides = [1, 2, 2, 2, 1] # Input, output: [batch, depth, height, width, depth] @@ -134,7 +134,7 @@ class Conv3DTransposeTest(test.TestCase): def testConv3DTransposeOutputShapeType(self): # Test case for GitHub issue 18887 for dtype in [dtypes.int32, dtypes.int64]: - with self.test_session(): + with self.cached_session(): x_shape = [2, 5, 6, 4, 3] y_shape = [2, 5, 6, 4, 2] f_shape = [3, 3, 3, 2, 3] @@ -149,7 +149,7 @@ class Conv3DTransposeTest(test.TestCase): output.eval() def testConv3DTransposeValid(self): - with self.test_session(): + with self.cached_session(): strides = [1, 2, 2, 2, 1] # Input, output: [batch, depth, height, width, depth] @@ -209,7 +209,7 @@ class Conv3DTransposeTest(test.TestCase): np.random.seed(1) # Make it reproducible. x_val = np.random.random_sample(x_shape).astype(np.float64) f_val = np.random.random_sample(f_shape).astype(np.float64) - with self.test_session(): + with self.cached_session(): x = constant_op.constant(x_val, name="x", dtype=dtypes.float32) f = constant_op.constant(f_val, name="f", dtype=dtypes.float32) output = nn_ops.conv3d_transpose( diff --git a/tensorflow/python/kernel_tests/conv_ops_3d_test.py b/tensorflow/python/kernel_tests/conv_ops_3d_test.py index 0b531125f3..6794464e3a 100644 --- a/tensorflow/python/kernel_tests/conv_ops_3d_test.py +++ b/tensorflow/python/kernel_tests/conv_ops_3d_test.py @@ -108,7 +108,7 @@ class Conv3DTest(test.TestCase): use_gpu=use_gpu) results.append(result) - with self.test_session() as sess: + with self.cached_session() as sess: values = sess.run(results) for value in values: print("expected = ", expected) @@ -183,7 +183,7 @@ class Conv3DTest(test.TestCase): expected_results.append(expected) computed_results.append(computed) tolerance = 1e-2 if use_gpu else 1e-5 - with self.test_session() as sess: + with self.cached_session() as sess: expected_values = sess.run(expected_results) computed_values = sess.run(computed_results) for e_value, c_value in zip(expected_values, computed_values): diff --git a/tensorflow/python/kernel_tests/conv_ops_test.py b/tensorflow/python/kernel_tests/conv_ops_test.py index 00de94f004..ea611497d9 100644 --- a/tensorflow/python/kernel_tests/conv_ops_test.py +++ b/tensorflow/python/kernel_tests/conv_ops_test.py @@ -1474,7 +1474,7 @@ class Conv2DTest(test.TestCase): padding="SAME") def testOpEdgeCases(self): - with self.test_session() as sess: + with self.cached_session() as sess: # Illegal strides. with self.assertRaisesRegexp(errors_impl.InvalidArgumentError, "strides in the batch and depth"): @@ -1539,7 +1539,7 @@ class DepthwiseConv2DTest(test.TestCase): # numbers from 1. x1 = [f * 1.0 for f in range(1, total_size_1 + 1)] x2 = [f * 1.0 for f in range(1, total_size_2 + 1)] - with self.test_session() as sess: + with self.cached_session() as sess: t1 = constant_op.constant(x1, shape=tensor_in_sizes) t1.set_shape(tensor_in_sizes) t2 = constant_op.constant(x2, shape=filter_in_sizes) diff --git a/tensorflow/python/kernel_tests/cross_grad_test.py b/tensorflow/python/kernel_tests/cross_grad_test.py index f040ac6055..0bd4006d6a 100644 --- a/tensorflow/python/kernel_tests/cross_grad_test.py +++ b/tensorflow/python/kernel_tests/cross_grad_test.py @@ -27,7 +27,7 @@ from tensorflow.python.platform import test class CrossOpTest(test.TestCase): def testGradientRandomValues(self): - with self.test_session(): + with self.cached_session(): us = [2, 3] u = array_ops.reshape( [0.854, -0.616, 0.767, 0.725, -0.927, 0.159], shape=us) diff --git a/tensorflow/python/kernel_tests/cwise_ops_binary_test.py b/tensorflow/python/kernel_tests/cwise_ops_binary_test.py new file mode 100644 index 0000000000..8028f93a8c --- /dev/null +++ b/tensorflow/python/kernel_tests/cwise_ops_binary_test.py @@ -0,0 +1,878 @@ +# Copyright 2018 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== +"""Functional tests for binary coefficient-wise operations.""" + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import numpy as np + +from tensorflow.python.framework import constant_op +from tensorflow.python.framework import dtypes as dtypes_lib +from tensorflow.python.framework import errors_impl +from tensorflow.python.framework import ops +from tensorflow.python.framework import sparse_tensor +from tensorflow.python.framework import test_util +from tensorflow.python.ops import gradient_checker +from tensorflow.python.ops import gradients_impl +from tensorflow.python.ops import math_ops +from tensorflow.python.ops import nn_grad # pylint: disable=unused-import +from tensorflow.python.ops import variables +from tensorflow.python.platform import test +from tensorflow.python.platform import tf_logging + +_ADD = lambda x, y: x + y +_SUB = lambda x, y: x - y +_MUL = lambda x, y: x * y +_POW = lambda x, y: x**y +_TRUEDIV = lambda x, y: x / y +_FLOORDIV = lambda x, y: x // y +_MOD = lambda x, y: x % y + + +# TODO(zongheng): it'd be great to factor out this function and various random +# SparseTensor gen funcs. +def _sparsify(x, thresh=0.5, index_dtype=np.int64): + x[x < thresh] = 0 + + non_zero = np.where(x) + x_indices = np.vstack(non_zero).astype(index_dtype).T + x_values = x[non_zero] + x_shape = x.shape + + return sparse_tensor.SparseTensor( + indices=x_indices, values=x_values, dense_shape=x_shape), x_values + + +def _default_tolerance(dtype): + """Returns a sensible default tolerance for comparing results of a given type. + + Args: + dtype: A datatype. + """ + if dtype == np.float16: + return 5e-3 + elif dtype in (np.float32, np.complex64): + return 1e-3 + elif dtype in (np.float64, np.complex128): + return 1e-5 + else: + return None # Fail fast for unexpected types + + +class BinaryOpTest(test.TestCase): + + def _compareCpu(self, x, y, np_func, tf_func, also_compare_variables=False): + np_ans = np_func(x, y) + with self.test_session(use_gpu=False): + inx = ops.convert_to_tensor(x) + iny = ops.convert_to_tensor(y) + out = tf_func(inx, iny) + tf_cpu = out.eval() + # Test that the op takes precedence over numpy operators. + np_left = tf_func(x, iny).eval() + np_right = tf_func(inx, y).eval() + + if also_compare_variables: + var_x = variables.Variable(x) + var_y = variables.Variable(y) + variables.global_variables_initializer().run() + print(type(x), type(y), type(var_x), type(var_y)) + print(type(tf_func(x, var_y)), type(tf_func(var_x, y))) + np_var_left = tf_func(x, var_y).eval() + np_var_right = tf_func(var_x, y).eval() + + if np_ans.dtype != np.object: + self.assertAllClose(np_ans, tf_cpu) + self.assertAllClose(np_ans, np_left) + self.assertAllClose(np_ans, np_right) + if also_compare_variables: + self.assertAllClose(np_ans, np_var_left) + self.assertAllClose(np_ans, np_var_right) + self.assertShapeEqual(np_ans, out) + + _GRAD_TOL = { + dtypes_lib.float16: 1e-3, + dtypes_lib.float32: 1e-3, + dtypes_lib.complex64: 1e-2, + dtypes_lib.float64: 1e-5, + dtypes_lib.complex128: 1e-4 + } + + def _compareGradientX(self, + x, + y, + np_func, + tf_func, + numeric_gradient_type=None): + z = np_func(x, y) + zs = list(z.shape) + with self.cached_session(): + inx = ops.convert_to_tensor(x) + iny = ops.convert_to_tensor(y) + if x.dtype in (np.float32, np.float64): + out = 1.1 * tf_func(inx, iny) + else: + out = tf_func(inx, iny) + xs = list(x.shape) + jacob_t, jacob_n = gradient_checker.compute_gradient( + inx, xs, out, zs, x_init_value=x) + if numeric_gradient_type is not None: + xf = x.astype(numeric_gradient_type) + yf = y.astype(numeric_gradient_type) + inxf = ops.convert_to_tensor(xf) + inyf = ops.convert_to_tensor(yf) + outf = tf_func(inxf, inyf) + _, jacob_n = gradient_checker.compute_gradient( + inxf, xs, outf, zs, x_init_value=xf, delta=1e-3) + jacob_n = jacob_n.astype(x.dtype) + tol = self._GRAD_TOL[dtypes_lib.as_dtype(x.dtype)] + self.assertAllClose(jacob_t, jacob_n, rtol=tol, atol=tol) + + def _compareGradientY(self, + x, + y, + np_func, + tf_func, + numeric_gradient_type=None): + z = np_func(x, y) + zs = list(z.shape) + with self.cached_session(): + inx = ops.convert_to_tensor(x) + iny = ops.convert_to_tensor(y) + if x.dtype in (np.float32, np.float64): + out = 1.1 * tf_func(inx, iny) + else: + out = tf_func(inx, iny) + ys = list(np.shape(y)) + jacob_t, jacob_n = gradient_checker.compute_gradient( + iny, ys, out, zs, x_init_value=y) + if numeric_gradient_type is not None: + xf = x.astype(numeric_gradient_type) + yf = y.astype(numeric_gradient_type) + inxf = ops.convert_to_tensor(xf) + inyf = ops.convert_to_tensor(yf) + outf = tf_func(inxf, inyf) + _, jacob_n = gradient_checker.compute_gradient( + inyf, ys, outf, zs, x_init_value=yf) + jacob_n = jacob_n.astype(x.dtype) + tol = self._GRAD_TOL[dtypes_lib.as_dtype(x.dtype)] + self.assertAllClose(jacob_t, jacob_n, rtol=tol, atol=tol) + + def _compareGpu(self, x, y, np_func, tf_func): + np_ans = np_func(x, y) + with self.test_session(force_gpu=test_util.is_gpu_available()): + inx = ops.convert_to_tensor(x) + iny = ops.convert_to_tensor(y) + out = tf_func(inx, iny) + tf_gpu = out.eval() + self.assertAllClose(np_ans, tf_gpu) + self.assertShapeEqual(np_ans, out) + # TODO(zhifengc/ke): make gradient checker work on GPU. + + def _compareBoth(self, x, y, np_func, tf_func, also_compare_variables=False): + self._compareCpu(x, y, np_func, tf_func, also_compare_variables) + if x.dtype in (np.float16, np.float32, np.float64, np.complex64, + np.complex128): + if tf_func not in (_FLOORDIV, math_ops.floordiv, math_ops.zeta, + math_ops.polygamma): + self._compareGradientX(x, y, np_func, tf_func) + self._compareGradientY(x, y, np_func, tf_func) + if tf_func in (math_ops.zeta, math_ops.polygamma): + # These methods only support gradients in the second parameter + self._compareGradientY(x, y, np_func, tf_func) + self._compareGpu(x, y, np_func, tf_func) + + def testFloatBasic(self): + x = np.linspace(-5, 20, 15).reshape(1, 3, 5).astype(np.float32) + y = np.linspace(20, -5, 15).reshape(1, 3, 5).astype(np.float32) + self._compareBoth(x, y, np.add, math_ops.add, also_compare_variables=True) + self._compareBoth(x, y, np.subtract, math_ops.subtract) + self._compareBoth(x, y, np.multiply, math_ops.multiply) + self._compareBoth(x, y + 0.1, np.true_divide, math_ops.truediv) + self._compareBoth(x, y + 0.1, np.floor_divide, math_ops.floordiv) + self._compareBoth(x, y, np.add, _ADD) + self._compareBoth(x, y, np.subtract, _SUB) + self._compareBoth(x, y, np.multiply, _MUL) + self._compareBoth(x, y + 0.1, np.true_divide, _TRUEDIV) + self._compareBoth(x, y + 0.1, np.floor_divide, _FLOORDIV) + self._compareBoth(x, y, np.arctan2, math_ops.atan2) + x1 = np.random.randn(5, 6).astype(np.float32) + x2 = np.random.randn(5, 6).astype(np.float32) + # Remove tiny values--atan2 gradients are flaky near the origin. + x1[np.abs(x1) < 0.05] = 0.05 * np.sign(x1[np.abs(x1) < 0.05]) + x2[np.abs(x2) < 0.05] = 0.05 * np.sign(x2[np.abs(x2) < 0.05]) + self._compareBoth(x1, x2, np.arctan2, math_ops.atan2) + try: + from scipy import special # pylint: disable=g-import-not-at-top + a_pos_small = np.linspace(0.1, 2, 15).reshape(1, 3, 5).astype(np.float32) + x_pos_small = np.linspace(0.1, 10, 15).reshape(1, 3, 5).astype(np.float32) + self._compareBoth(a_pos_small, x_pos_small, special.gammainc, + math_ops.igamma) + self._compareBoth(a_pos_small, x_pos_small, special.gammaincc, + math_ops.igammac) + # Need x > 1 + self._compareBoth(x_pos_small + 1, a_pos_small, special.zeta, + math_ops.zeta) + n_small = np.arange(0, 15).reshape(1, 3, 5).astype(np.float32) + self._compareBoth(n_small, x_pos_small, special.polygamma, + math_ops.polygamma) + except ImportError as e: + tf_logging.warn("Cannot test special functions: %s" % str(e)) + + def testFloatDifferentShapes(self): + x = np.array([1, 2, 3, 4]).reshape(2, 2).astype(np.float32) + y = np.array([1, 2]).reshape(2, 1).astype(np.float32) + with self.cached_session() as sess: + inx = ops.convert_to_tensor(x) + iny = ops.convert_to_tensor(y) + s = math_ops.reduce_sum(inx * iny) + gx, gy = sess.run(gradients_impl.gradients(s, [inx, iny])) + # gx is simply the broadcasted y + self.assertAllEqual(gx, + np.array([1, 1, 2, 2]).reshape(2, 2).astype(np.float32)) + # gy is x's column summed up + self.assertAllEqual(gy, np.array([3, 7]).reshape(2, 1).astype(np.float32)) + + def testFloatVariableOverload(self): + x = np.array([1, 2, 3, 4]).reshape(2, 2).astype(np.int32) + y = np.array([1, 2]).reshape(2, 1).astype(np.int32) + var_x = variables.Variable(x) + var_y = variables.Variable(y) + with self.cached_session() as sess: + sess.run([var_x.initializer, var_y.initializer]) + left_result = (var_x * y).eval() + right_result = (x * var_y).eval() + np_result = x * y + self.assertAllEqual(np_result, left_result) + self.assertAllEqual(np_result, right_result) + + def testDoubleBasic(self): + x = np.linspace(-5, 20, 15).reshape(1, 3, 5).astype(np.float64) + y = np.linspace(20, -5, 15).reshape(1, 3, 5).astype(np.float64) + self._compareBoth(x, y, np.add, math_ops.add) + self._compareBoth(x, y, np.subtract, math_ops.subtract) + self._compareBoth(x, y, np.multiply, math_ops.multiply) + self._compareBoth(x, y + 0.1, np.true_divide, math_ops.truediv) + self._compareBoth(x, y + 0.1, np.floor_divide, math_ops.floordiv) + self._compareBoth(x, y, np.add, _ADD) + self._compareBoth(x, y, np.subtract, _SUB) + self._compareBoth(x, y, np.multiply, _MUL) + self._compareBoth(x, y + 0.1, np.true_divide, _TRUEDIV) + self._compareBoth(x, y + 0.1, np.floor_divide, _FLOORDIV) + self._compareBoth(x, y, np.arctan2, math_ops.atan2) + x1 = np.random.randn(7, 4).astype(np.float64) + x2 = np.random.randn(7, 4).astype(np.float64) + # Remove tiny values--atan2 gradients are flaky near the origin. + x1[np.abs(x1) < 0.5] = 0.5 * np.sign(x1[np.abs(x1) < 0.5]) + x2[np.abs(x2) < 0.5] = 0.5 * np.sign(x2[np.abs(x2) < 0.5]) + self._compareBoth(x1, x2, np.arctan2, math_ops.atan2) + try: + from scipy import special # pylint: disable=g-import-not-at-top + a_pos_small = np.linspace(0.1, 2, 15).reshape(1, 3, 5).astype(np.float32) + x_pos_small = np.linspace(0.1, 10, 15).reshape(1, 3, 5).astype(np.float32) + self._compareBoth(a_pos_small, x_pos_small, special.gammainc, + math_ops.igamma) + self._compareBoth(a_pos_small, x_pos_small, special.gammaincc, + math_ops.igammac) + except ImportError as e: + tf_logging.warn("Cannot test special functions: %s" % str(e)) + + def testUint8Basic(self): + x = np.arange(1, 13, 2).reshape(1, 3, 2).astype(np.uint8) + y = np.arange(1, 7, 1).reshape(1, 3, 2).astype(np.uint8) + self._compareBoth(x, y, np.add, math_ops.add) + + def testInt8Basic(self): + x = np.arange(1, 13, 2).reshape(1, 3, 2).astype(np.int8) + y = np.arange(1, 7, 1).reshape(1, 3, 2).astype(np.int8) + self._compareBoth(x, y, np.multiply, math_ops.multiply) + self._compareBoth(x, y, np.multiply, _MUL) + + def testInt16Basic(self): + x = np.arange(1, 13, 2).reshape(1, 3, 2).astype(np.int16) + y = np.arange(1, 7, 1).reshape(1, 3, 2).astype(np.int16) + self._compareBoth(x, y, np.multiply, math_ops.multiply) + self._compareBoth(x, y, np.multiply, _MUL) + + def testUint16Basic(self): + x = np.arange(1, 13, 2).reshape(1, 3, 2).astype(np.uint16) + y = np.arange(1, 7, 1).reshape(1, 3, 2).astype(np.uint16) + self._compareBoth(x, y, np.multiply, math_ops.multiply) + self._compareBoth(x, y, np.multiply, _MUL) + self._compareBoth(x, y, np.true_divide, math_ops.truediv) + self._compareBoth(x, y, np.floor_divide, math_ops.floordiv) + self._compareBoth(x, y, np.true_divide, _TRUEDIV) + self._compareBoth(x, y, np.floor_divide, _FLOORDIV) + + def testInt32Basic(self): + x = np.arange(1, 13, 2).reshape(1, 3, 2).astype(np.int32) + y = np.arange(1, 7, 1).reshape(1, 3, 2).astype(np.int32) + self._compareBoth(x, y, np.add, math_ops.add) + self._compareBoth(x, y, np.subtract, math_ops.subtract) + self._compareBoth(x, y, np.multiply, math_ops.multiply) + self._compareBoth(x, y, np.true_divide, math_ops.truediv) + self._compareBoth(x, y, np.floor_divide, math_ops.floordiv) + self._compareBoth(x, y, np.mod, math_ops.mod) + self._compareBoth(x, y, np.add, _ADD) + self._compareBoth(x, y, np.subtract, _SUB) + self._compareBoth(x, y, np.multiply, _MUL) + self._compareBoth(x, y, np.true_divide, _TRUEDIV) + self._compareBoth(x, y, np.floor_divide, _FLOORDIV) + self._compareBoth(x, y, np.mod, _MOD) + # _compareBoth tests on GPU only for floating point types, so test + # _MOD for int32 on GPU by calling _compareGpu + self._compareGpu(x, y, np.mod, _MOD) + + def testInt64Basic(self): + x = np.arange(1 << 40, 13 << 40, 2 << 40).reshape(1, 3, 2).astype(np.int64) + y = np.arange(1, 7, 1).reshape(1, 3, 2).astype(np.int64) + self._compareBoth(x, y, np.subtract, math_ops.subtract) + self._compareBoth(x, y, np.multiply, math_ops.multiply) + self._compareBoth(x, y, np.true_divide, math_ops.truediv) + self._compareBoth(x, y, np.floor_divide, math_ops.floordiv) + self._compareBoth(x, y, np.mod, math_ops.mod) + self._compareBoth(x, y, np.subtract, _SUB) + self._compareBoth(x, y, np.multiply, _MUL) + self._compareBoth(x, y, np.true_divide, _TRUEDIV) + self._compareBoth(x, y, np.floor_divide, _FLOORDIV) + self._compareBoth(x, y, np.mod, _MOD) + + def testComplex64Basic(self): + x = np.complex(1, 1) * np.linspace(-10, 10, 6).reshape(1, 3, 2).astype( + np.complex64) + y = np.complex(1, 1) * np.linspace(20, -20, 6).reshape(1, 3, 2).astype( + np.complex64) + self._compareBoth(x, y, np.add, math_ops.add) + self._compareBoth(x, y, np.subtract, math_ops.subtract) + self._compareBoth(x, y, np.multiply, math_ops.multiply) + self._compareBoth(x, y + 0.1, np.true_divide, math_ops.truediv) + self._compareBoth(x, y, np.add, _ADD) + self._compareBoth(x, y, np.subtract, _SUB) + self._compareBoth(x, y, np.multiply, _MUL) + self._compareBoth(x, y + 0.1, np.true_divide, _TRUEDIV) + + def testComplex128Basic(self): + x = np.complex(1, 1) * np.linspace(-10, 10, 6).reshape(1, 3, 2).astype( + np.complex128) + y = np.complex(1, 1) * np.linspace(20, -20, 6).reshape(1, 3, 2).astype( + np.complex128) + self._compareBoth(x, y, np.add, math_ops.add) + self._compareBoth(x, y, np.subtract, math_ops.subtract) + self._compareBoth(x, y, np.multiply, math_ops.multiply) + self._compareBoth(x, y + 0.1, np.true_divide, math_ops.truediv) + self._compareBoth(x, y, np.add, _ADD) + self._compareBoth(x, y, np.subtract, _SUB) + self._compareBoth(x, y, np.multiply, _MUL) + self._compareBoth(x, y + 0.1, np.true_divide, _TRUEDIV) + + def testStringComparison(self): + x = np.array([["abc", "bh"], ["c", ""]]) + y = np.array([["abc", "bh"], ["def", "hi"]]) + with self.test_session(use_gpu=False) as sess: + cmp_eq = math_ops.equal(x, y) + cmp_not_eq = math_ops.not_equal(x, y) + values = sess.run([cmp_eq, cmp_not_eq]) + self.assertAllEqual([[True, True], [False, False]], values[0]) + self.assertAllEqual([[False, False], [True, True]], values[1]) + + def testString(self): + x = np.array([["x_0_0", "x_0_1", "x_0_2"], ["x_1_0", "x_1_1", "x_1_2"], + ["x_2_0", "x_2_1", "x_2_2"]], + dtype=np.object) + y = np.array([["y_0_0", "y_0_1", "y_0_2"], ["y_1_0", "y_1_1", "y_1_2"], + ["y_2_0", "y_2_1", "y_2_2"]], + dtype=np.object) + z = np.array([["z_0", "z_1", "z_2"]], dtype=np.object) + w = np.array("w", dtype=np.object) + self._compareCpu(x, y, _ADD, _ADD) + self._compareCpu(x, z, _ADD, _ADD) + self._compareCpu(x, w, _ADD, _ADD) + self._compareCpu(z, w, _ADD, _ADD) + + def _compareBCast(self, xs, ys, dtype, np_func, tf_func): + if dtype in (np.complex64, np.complex128): + x = (1 + np.linspace(0, 2 + 3j, np.prod(xs))).astype(dtype).reshape(xs) + y = (1 + np.linspace(0, 2 - 2j, np.prod(ys))).astype(dtype).reshape(ys) + else: + x = (1 + np.linspace(0, 5, np.prod(xs))).astype(dtype).reshape(xs) + y = (1 + np.linspace(0, 5, np.prod(ys))).astype(dtype).reshape(ys) + self._compareCpu(x, y, np_func, tf_func) + if x.dtype in (np.float16, np.float32, np.float64): + # TODO(aselle): Make the test work for dtypes: + # (np.complex64, np.complex128). + if tf_func not in (_FLOORDIV, math_ops.floordiv): + if x.dtype == np.float16: + # Compare fp16 theoretical gradients to fp32 numerical gradients, + # since fp16 numerical gradients are too imprecise unless great + # care is taken with choosing the inputs and the delta. This is + # a weaker check (in particular, it does not test the op itself, + # only its gradient), but it's much better than nothing. + self._compareGradientX(x, y, np_func, tf_func, np.float) + self._compareGradientY(x, y, np_func, tf_func, np.float) + else: + self._compareGradientX(x, y, np_func, tf_func) + self._compareGradientY(x, y, np_func, tf_func) + self._compareGpu(x, y, np_func, tf_func) + + # TODO(josh11b,vrv): Refactor this to use parameterized tests. + def _testBCastByFunc(self, funcs, xs, ys): + dtypes = [ + np.float16, + np.float32, + np.float64, + np.int32, + np.int64, + np.complex64, + np.complex128, + ] + for dtype in dtypes: + for (np_func, tf_func) in funcs: + if (dtype in (np.complex64, np.complex128) and + tf_func in (_FLOORDIV, math_ops.floordiv)): + continue # floordiv makes no sense for complex numbers + self._compareBCast(xs, ys, dtype, np_func, tf_func) + self._compareBCast(ys, xs, dtype, np_func, tf_func) + + def _testBCastA(self, xs, ys): + funcs = [ + (np.add, math_ops.add), + (np.add, _ADD), + ] + self._testBCastByFunc(funcs, xs, ys) + + def _testBCastB(self, xs, ys): + funcs = [ + (np.subtract, math_ops.subtract), + (np.subtract, _SUB), + (np.power, math_ops.pow), + ] + self._testBCastByFunc(funcs, xs, ys) + + def _testBCastC(self, xs, ys): + funcs = [ + (np.multiply, math_ops.multiply), + (np.multiply, _MUL), + ] + self._testBCastByFunc(funcs, xs, ys) + + def _testBCastD(self, xs, ys): + funcs = [ + (np.true_divide, math_ops.truediv), + (np.floor_divide, math_ops.floordiv), + (np.true_divide, _TRUEDIV), + (np.floor_divide, _FLOORDIV), + ] + self._testBCastByFunc(funcs, xs, ys) + + def testBCast_0A(self): + self._testBCastA([1, 3, 2], [1]) + + def testBCast_0B(self): + self._testBCastB([1, 3, 2], [1]) + + def testBCast_0C(self): + self._testBCastC([1, 3, 2], [1]) + + def testBCast_0D(self): + self._testBCastD([1, 3, 2], [1]) + + def testBCast_1A(self): + self._testBCastA([1, 3, 2], [2]) + + def testBCast_1B(self): + self._testBCastB([1, 3, 2], [2]) + + def testBCast_1C(self): + self._testBCastC([1, 3, 2], [2]) + + def testBCast_1D(self): + self._testBCastD([1, 3, 2], [2]) + + def testBCast_2A(self): + self._testBCastA([1, 3, 2], [3, 2]) + + def testBCast_2B(self): + self._testBCastB([1, 3, 2], [3, 2]) + + def testBCast_2C(self): + self._testBCastC([1, 3, 2], [3, 2]) + + def testBCast_2D(self): + self._testBCastD([1, 3, 2], [3, 2]) + + def testBCast_3A(self): + self._testBCastA([1, 3, 2], [3, 1]) + + def testBCast_3B(self): + self._testBCastB([1, 3, 2], [3, 1]) + + def testBCast_3C(self): + self._testBCastC([1, 3, 2], [3, 1]) + + def testBCast_3D(self): + self._testBCastD([1, 3, 2], [3, 1]) + + def testBCast_4A(self): + self._testBCastA([1, 3, 2], [1, 3, 2]) + + def testBCast_4B(self): + self._testBCastB([1, 3, 2], [1, 3, 2]) + + def testBCast_4C(self): + self._testBCastC([1, 3, 2], [1, 3, 2]) + + def testBCast_4D(self): + self._testBCastD([1, 3, 2], [1, 3, 2]) + + def testBCast_5A(self): + self._testBCastA([1, 3, 2], [2, 3, 1]) + + def testBCast_5B(self): + self._testBCastB([1, 3, 2], [2, 3, 1]) + + def testBCast_5C(self): + self._testBCastC([1, 3, 2], [2, 3, 1]) + + def testBCast_5D(self): + self._testBCastD([1, 3, 2], [2, 3, 1]) + + def testBCast_6A(self): + self._testBCastA([1, 3, 2], [2, 1, 1]) + + def testBCast_6B(self): + self._testBCastB([1, 3, 2], [2, 1, 1]) + + def testBCast_6C(self): + self._testBCastC([1, 3, 2], [2, 1, 1]) + + def testBCast_6D(self): + self._testBCastD([1, 3, 2], [2, 1, 1]) + + def testBCast_7A(self): + self._testBCastA([1, 3, 2], [1, 3, 1]) + + def testBCast_7B(self): + self._testBCastB([1, 3, 2], [1, 3, 1]) + + def testBCast_7C(self): + self._testBCastC([1, 3, 2], [1, 3, 1]) + + def testBCast_7D(self): + self._testBCastD([1, 3, 2], [1, 3, 1]) + + def testBCast_8A(self): + self._testBCastA([2, 1, 5], [2, 3, 1]) + + def testBCast_8B(self): + self._testBCastB([2, 1, 5], [2, 3, 1]) + + def testBCast_8C(self): + self._testBCastC([2, 1, 5], [2, 3, 1]) + + def testBCast_8D(self): + self._testBCastD([2, 1, 5], [2, 3, 1]) + + def testBCast_9A(self): + self._testBCastA([2, 0, 5], [2, 0, 1]) + + def testBCast_9B(self): + self._testBCastB([2, 0, 5], [2, 0, 1]) + + def testBCast_9C(self): + self._testBCastC([2, 0, 5], [2, 0, 1]) + + def testBCast_9D(self): + self._testBCastD([2, 0, 5], [2, 0, 1]) + + def testBCast_10A(self): + self._testBCastA([2, 3, 0], [2, 3, 1]) + + def testBCast_10B(self): + self._testBCastB([2, 3, 0], [2, 3, 1]) + + def testBCast_10C(self): + self._testBCastC([2, 3, 0], [2, 3, 1]) + + def testBCast_10D(self): + self._testBCastD([2, 3, 0], [2, 3, 1]) + + def testBCast_11A(self): + self._testBCastA([1, 3, 2], [1, 3, 2]) + + def testBCast_11B(self): + self._testBCastB([1, 3, 2], [1, 3, 2]) + + def testBCast_11C(self): + self._testBCastC([1, 3, 2], [1, 3, 2]) + + def testBCast_11D(self): + self._testBCastD([1, 3, 2], [1, 3, 2]) + + def testBCast_12A(self): + self._testBCastA([1, 1, 1, 1, 3, 2], [1, 3, 2]) + + def testBCast_12B(self): + self._testBCastB([1, 1, 1, 1, 3, 2], [1, 3, 2]) + + def testBCast_12C(self): + self._testBCastC([1, 1, 1, 1, 3, 2], [1, 3, 2]) + + def testBCast_12D(self): + self._testBCastD([1, 1, 1, 1, 3, 2], [1, 3, 2]) + + def testBCast_13A(self): + self._testBCastA([1, 3, 2, 1, 1], [1]) + + def testBCast_13B(self): + self._testBCastB([1, 3, 2, 1, 1], [1]) + + def testBCast_13C(self): + self._testBCastC([1, 3, 2, 1, 1], [1]) + + def testBCast_13D(self): + self._testBCastD([1, 3, 2, 1, 1], [1]) + + def testBCast_14A(self): + self._testBCastA([2, 3, 1, 1, 5], [1]) + + def testBCast_14B(self): + self._testBCastB([2, 3, 1, 1, 5], [1]) + + def testBCast_14C(self): + self._testBCastC([2, 3, 1, 1, 5], [1]) + + def testBCast_14D(self): + self._testBCastD([2, 3, 1, 1, 5], [1]) + + def testBCast_15A(self): + self._testBCastA([10, 3, 1, 2], [3, 1, 2]) + + def testBCast_15B(self): + self._testBCastB([10, 3, 1, 2], [3, 1, 2]) + + def testBCast_15C(self): + self._testBCastC([10, 3, 1, 2], [3, 1, 2]) + + def testBCast_15D(self): + self._testBCastD([10, 3, 1, 2], [3, 1, 2]) + + def testMismatchedDimensions(self): + for func in [ + math_ops.add, math_ops.subtract, math_ops.multiply, math_ops.div, _ADD, + _SUB, _MUL, _TRUEDIV, _FLOORDIV + ]: + with self.assertRaisesWithPredicateMatch( + ValueError, lambda e: "Dimensions must" in str(e)): + func( + ops.convert_to_tensor([10.0, 20.0, 30.0]), + ops.convert_to_tensor([[40.0, 50.0], [60.0, 70.0]])) + + def testZeroPowGrad(self): + with self.cached_session(): + for dtype in (np.float16, np.float32, np.float64, np.complex64, + np.complex128): + x = constant_op.constant(0.0, dtype=dtype) + y = constant_op.constant(2.0, dtype=dtype) + z = math_ops.pow(x, y) + error = gradient_checker.compute_gradient_error(y, [], z, []) + self.assertEqual(error, 0) + + def testComplexPowGrad(self): + with self.cached_session(): + for dtype in np.complex64, np.complex128: + for base in 2.0, -2.0: + x = constant_op.constant(base, dtype=dtype) + y = constant_op.constant(2.0, dtype=dtype) + z = math_ops.pow(x, y) + error = gradient_checker.compute_gradient_error(y, [], z, []) + self.assertLess(error, 2e-4) + + def testAtan2SpecialValues(self): + x1l, x2l = zip((+0.0, +0.0), (+0.0, -0.0), (-0.0, +0.0), (-0.0, -0.0), + (1.2345, float("inf")), (1.2345, -float("inf")), + (-4.321, float("inf")), (-4.125, -float("inf")), + (float("inf"), float("inf")), (float("inf"), -float("inf")), + (-float("inf"), float("inf")), + (-float("inf"), -float("inf"))) + for dtype in np.float32, np.float64: + x1 = np.array(x1l).astype(dtype) + x2 = np.array(x2l).astype(dtype) + self._compareCpu(x1, x2, np.arctan2, math_ops.atan2) + self._compareGpu(x1, x2, np.arctan2, math_ops.atan2) + + def testPowNegativeExponent(self): + for dtype in [np.int32, np.int64]: + with self.test_session(use_gpu=False) as sess: + with self.assertRaisesRegexp( + errors_impl.InvalidArgumentError, + "Integers to negative integer powers are not allowed"): + x = np.array([5, 2]).astype(dtype) + y = np.array([-2, 3]).astype(dtype) + sess.run(math_ops.pow(x, y)) + + with self.test_session(use_gpu=False) as sess: + with self.assertRaisesRegexp( + errors_impl.InvalidArgumentError, + "Integers to negative integer powers are not allowed"): + x = np.array([5, 2]).astype(dtype) + y = np.array([2, -3]).astype(dtype) + sess.run(math_ops.pow(x, y)) + + with self.test_session(use_gpu=False) as sess: + with self.assertRaisesRegexp( + errors_impl.InvalidArgumentError, + "Integers to negative integer powers are not allowed"): + x = np.array([5, 2]).astype(dtype) + y = -3 + sess.run(math_ops.pow(x, y)) + + +class ComparisonOpTest(test.TestCase): + + def _compareScalar(self, func, x, y, dtype): + with self.test_session(force_gpu=test_util.is_gpu_available()): + out = func( + ops.convert_to_tensor(np.array([x]).astype(dtype)), + ops.convert_to_tensor(np.array([y]).astype(dtype))) + ret = out.eval() + return ret[0] + + def testScalarCompareScalar(self): + dtypes = [np.float16, np.float32, np.float64, np.int32, np.int64] + data = [-1, 0, 1] + for t in dtypes: + for x in data: + for y in data: + self.assertEqual(self._compareScalar(math_ops.less, x, y, t), x < y) + self.assertEqual( + self._compareScalar(math_ops.less_equal, x, y, t), x <= y) + self.assertEqual( + self._compareScalar(math_ops.greater, x, y, t), x > y) + self.assertEqual( + self._compareScalar(math_ops.greater_equal, x, y, t), x >= y) + self.assertEqual(self._compareScalar(math_ops.equal, x, y, t), x == y) + self.assertEqual( + self._compareScalar(math_ops.not_equal, x, y, t), x != y) + data = [-1, 0, 1, -1j, 1j, 1 + 1j, 1 - 1j] + for t in [np.complex64, np.complex128]: + for x in data: + for y in data: + self.assertEqual(self._compareScalar(math_ops.equal, x, y, t), x == y) + self.assertEqual( + self._compareScalar(math_ops.not_equal, x, y, t), x != y) + + def _compare(self, x, y, np_func, tf_func): + np_ans = np_func(x, y) + with self.test_session(force_gpu=test_util.is_gpu_available()): + out = tf_func(ops.convert_to_tensor(x), ops.convert_to_tensor(y)) + tf_ans = out.eval() + self.assertAllEqual(np_ans, tf_ans) + + def testTensorCompareTensor(self): + x = np.linspace(-15, 15, 6).reshape(1, 3, 2) + y = np.linspace(20, -10, 6).reshape(1, 3, 2) + for t in [np.float16, np.float32, np.float64, np.int32, np.int64]: + xt = x.astype(t) + yt = y.astype(t) + self._compare(xt, yt, np.less, math_ops.less) + self._compare(xt, yt, np.less_equal, math_ops.less_equal) + self._compare(xt, yt, np.greater, math_ops.greater) + self._compare(xt, yt, np.greater_equal, math_ops.greater_equal) + self._compare(xt, yt, np.equal, math_ops.equal) + self._compare(xt, yt, np.not_equal, math_ops.not_equal) + # Complex types do not support ordering but do support equality tests. + for t in [np.complex64, np.complex128]: + xt = x.astype(t) + xt -= 1j * xt + yt = y.astype(t) + yt -= 1j * yt + self._compare(xt, yt, np.equal, math_ops.equal) + self._compare(xt, yt, np.not_equal, math_ops.not_equal) + + def _compareBCast(self, xs, ys, dtype, np_func, tf_func): + x = np.linspace(-15, 15, np.prod(xs)).astype(dtype).reshape(xs) + y = np.linspace(20, -10, np.prod(ys)).astype(dtype).reshape(ys) + if dtype in (np.complex64, np.complex128): + x -= 1j * x + y -= 1j * y + self._compare(x, y, np_func, tf_func) + self._compare(y, x, np_func, tf_func) + + def _testBCastByFunc(self, np_func, tf_func, include_complex=False): + shapes = [ + ([1, 3, 2], [1]), + ([1, 3, 2], [2]), + ([1, 3, 2], [3, 2]), + ([1, 3, 2], [3, 1]), + ([1, 3, 2], [1, 3, 2]), + ([1, 3, 2], [2, 3, 1]), + ([1, 3, 2], [2, 1, 1]), + ([1, 3, 2], [1, 3, 1]), + ([2, 1, 5], [2, 3, 1]), + ([2, 0, 5], [2, 0, 1]), + ([2, 3, 0], [2, 3, 1]), + ] + dtypes = [ + np.float16, + np.float32, + np.float64, + np.int32, + np.int64, + ] + if include_complex: + dtypes.extend([np.complex64, np.complex128]) + + for (xs, ys) in shapes: + for dtype in dtypes: + self._compareBCast(xs, ys, dtype, np_func, tf_func) + + def testBCastLess(self): + self._testBCastByFunc(np.less, math_ops.less) + + def testBCastLessEqual(self): + self._testBCastByFunc(np.less_equal, math_ops.less_equal) + + def testBCastGreater(self): + self._testBCastByFunc(np.greater, math_ops.greater) + + def testBCastGreaterEqual(self): + self._testBCastByFunc(np.greater_equal, math_ops.greater_equal) + + def testBCastEqual(self): + self._testBCastByFunc(np.equal, math_ops.equal, include_complex=True) + + def testBCastNotEqual(self): + self._testBCastByFunc( + np.not_equal, math_ops.not_equal, include_complex=True) + + def testShapeMismatch(self): + dtypes = [np.float16, np.float32, np.float64, np.int32, np.int64] + funcs = [ + math_ops.less, math_ops.less_equal, math_ops.greater, + math_ops.greater_equal, math_ops.equal, math_ops.not_equal + ] + x = np.arange(0, 10).reshape([2, 5]) + y = np.arange(0, 10).reshape([5, 2]) + for t in dtypes: + for f in funcs: + with self.assertRaisesWithPredicateMatch( + ValueError, lambda e: "Dimensions must" in str(e)): + f(x.astype(t), y.astype(t)) + + +if __name__ == "__main__": + test.main() diff --git a/tensorflow/python/kernel_tests/cwise_ops_test.py b/tensorflow/python/kernel_tests/cwise_ops_test.py index b61232cded..c5311ad834 100644 --- a/tensorflow/python/kernel_tests/cwise_ops_test.py +++ b/tensorflow/python/kernel_tests/cwise_ops_test.py @@ -18,25 +18,19 @@ from __future__ import absolute_import from __future__ import division from __future__ import print_function -import math - import numpy as np from tensorflow.python.framework import constant_op from tensorflow.python.framework import dtypes as dtypes_lib -from tensorflow.python.framework import errors_impl from tensorflow.python.framework import ops from tensorflow.python.framework import sparse_tensor from tensorflow.python.framework import test_util from tensorflow.python.ops import array_ops -from tensorflow.python.ops import gen_math_ops from tensorflow.python.ops import gradient_checker -from tensorflow.python.ops import gradients_impl from tensorflow.python.ops import math_ops from tensorflow.python.ops import nn_grad # pylint: disable=unused-import from tensorflow.python.ops import variables from tensorflow.python.platform import test -from tensorflow.python.platform import tf_logging _ADD = lambda x, y: x + y _SUB = lambda x, y: x - y @@ -45,8 +39,6 @@ _POW = lambda x, y: x**y _TRUEDIV = lambda x, y: x / y _FLOORDIV = lambda x, y: x // y _MOD = lambda x, y: x % y -_NEG = lambda x: -x -_ABS = abs _LT = lambda x, y: x < y _LE = lambda x, y: x <= y @@ -74,8 +66,11 @@ def _sparsify(x, thresh=0.5, index_dtype=np.int64): def _default_tolerance(dtype): - """Returns a sensible default tolerance for comparing results of a given - type""" + """Returns a sensible default tolerance for comparing results of a given type. + + Args: + dtype: A datatype. + """ if dtype == np.float16: return 5e-3 elif dtype in (np.float32, np.complex64): @@ -86,1147 +81,6 @@ def _default_tolerance(dtype): return None # Fail fast for unexpected types -class UnaryOpTest(test.TestCase): - - def _compareCpu(self, x, np_func, tf_func, grad_rtol=None, grad_atol=None): - if grad_rtol is None: - grad_rtol = _default_tolerance(x.dtype) - if grad_atol is None: - grad_atol = _default_tolerance(x.dtype) - np_ans = np_func(x) - with self.test_session(use_gpu=False): - inx = ops.convert_to_tensor(x) - if x.dtype in (np.float32, np.float64, - dtypes_lib.bfloat16.as_numpy_dtype): - y = 1.1 * tf_func(inx) - np_ans *= 1.1 - else: - y = tf_func(inx) - tf_cpu = y.eval() - self.assertShapeEqual(np_ans, y) - if x.dtype == np.float16: - self.assertAllClose(np_ans, tf_cpu, rtol=1e-3, atol=1e-3) - elif x.dtype == dtypes_lib.bfloat16.as_numpy_dtype: - self.assertAllClose(np_ans, tf_cpu, rtol=1e-2, atol=1e-2) - else: - self.assertAllClose(np_ans, tf_cpu) - - if x.dtype in (np.complex64, np.complex128) and tf_func == math_ops.sign: - return # Return early - - if x.dtype == np.float16: - s = list(np.shape(x)) - jacob_t, _ = gradient_checker.compute_gradient( - inx, s, y, s, x_init_value=x) - xf = x.astype(np.float) - inxf = ops.convert_to_tensor(xf) - yf = tf_func(inxf) - _, jacob_n = gradient_checker.compute_gradient( - inxf, s, yf, s, x_init_value=xf, delta=1e-2) - jacob_n = jacob_n.astype(np.float16) - self.assertAllClose(jacob_t, jacob_n, rtol=grad_rtol, atol=grad_atol) - elif x.dtype in (np.float32, np.complex64): - s = list(np.shape(x)) - jacob_t, jacob_n = gradient_checker.compute_gradient( - inx, s, y, s, x_init_value=x, delta=1e-3) - self.assertAllClose(jacob_t, jacob_n, rtol=grad_rtol, atol=grad_atol) - elif x.dtype in (np.float64, np.complex128): - s = list(np.shape(x)) - jacob_t, jacob_n = gradient_checker.compute_gradient( - inx, s, y, s, x_init_value=x, delta=1e-5) - self.assertAllClose(jacob_t, jacob_n, rtol=grad_rtol, atol=grad_atol) - - def _check(self, result_tensor, result_np, input_sp_t, tol): - self.assertTrue(isinstance(result_tensor, sparse_tensor.SparseTensor)) - self.assertTrue(isinstance(input_sp_t, sparse_tensor.SparseTensor)) - self.assertAllEqual(input_sp_t.indices.eval(), result_tensor.indices.eval()) - self.assertAllEqual(input_sp_t.dense_shape.eval(), - result_tensor.dense_shape.eval()) - if tol is None: - self.assertAllClose(result_np, result_tensor.values.eval()) - else: - self.assertAllClose( - result_np, result_tensor.values.eval(), rtol=tol, atol=tol) - - def _compareSparseCpu(self, x, np_func, tf_func, tol): - x_sp, x_sp_vals = _sparsify(x) - res_np = np_func(x_sp_vals) - with self.test_session(use_gpu=False): - self._check(tf_func(x_sp), res_np, x_sp, tol) - - def _compareGpu(self, x, np_func, tf_func): - np_ans = np_func(x) - with self.test_session(force_gpu=test_util.is_gpu_available()): - result = tf_func(ops.convert_to_tensor(x)) - tf_gpu = result.eval() - if x.dtype == np.float16: - self.assertAllClose(np_ans, tf_gpu, rtol=1e-3, atol=1e-3) - else: - self.assertAllClose(np_ans, tf_gpu) - # TODO(zhifengc/ke): make gradient checker work on GPU. - - def _compareSparseGpu(self, x, np_func, tf_func, tol): - x_sp, x_sp_vals = _sparsify(x) - res_np = np_func(x_sp_vals) - with self.test_session(force_gpu=test_util.is_gpu_available()): - self._check(tf_func(x_sp), res_np, x_sp, tol) - - def _compareBoth(self, x, np_func, tf_func): - self._compareCpu(x, np_func, tf_func) - self._compareGpu(x, np_func, tf_func) - - def _compareBothSparse(self, x, np_func, tf_func, tol=None): - self._compareSparseCpu(x, np_func, tf_func, tol) - self._compareSparseGpu(x, np_func, tf_func, tol) - - def _inv(self, x): - return 1.0 / x - - def _rsqrt(self, x): - return self._inv(np.sqrt(x)) - - def _sigmoid(self, x): - return 1.0 / (1.0 + np.exp(-x)) - - def _log_sigmoid(self, x): - return np.log(self._sigmoid(x)) - - def _replace_domain_error_with_inf(self, fn): - - def func(x): - try: - return fn(x) - except ValueError as e: - if "domain error" in str(e): - return np.inf * np.ones_like(x) - else: - raise e - - return func - - def testFloatBasic(self): - x = np.arange(-3, 3).reshape(1, 3, 2).astype(np.float32) - w = x - x.min() + 1.02 # all greater than 1 - y = (x + .5).astype(np.float32) # no zero - z = (x + 15.5).astype(np.float32) # all positive - k = np.arange(-0.90, 0.90, 0.25).astype(np.float32) # between -1 and 1 - - self._compareBoth(x, np.abs, math_ops.abs) - self._compareBoth(x, np.abs, _ABS) - self._compareBoth(x, np.negative, math_ops.negative) - self._compareBoth(x, np.negative, _NEG) - self._compareBoth(y, self._inv, math_ops.reciprocal) - self._compareBoth(x, np.square, math_ops.square) - self._compareBoth(z, np.sqrt, math_ops.sqrt) - self._compareBoth(z, self._rsqrt, math_ops.rsqrt) - self._compareBoth(x, np.exp, math_ops.exp) - self._compareBoth(x, np.expm1, math_ops.expm1) - self._compareBoth(z, np.log, math_ops.log) - self._compareBoth(z, np.log1p, math_ops.log1p) - self._compareBoth(x, np.sinh, math_ops.sinh) - self._compareBoth(x, np.cosh, math_ops.cosh) - self._compareBoth(x, np.tanh, math_ops.tanh) - self._compareBoth(x, np.arcsinh, math_ops.asinh) - self._compareBoth(w, np.arccosh, math_ops.acosh) - self._compareBoth(k, np.arctanh, math_ops.atanh) - self._compareBoth(x, self._sigmoid, math_ops.sigmoid) - self._compareBoth(x, self._log_sigmoid, math_ops.log_sigmoid) - self._compareBoth(y, np.sign, math_ops.sign) - self._compareBoth(x, np.sin, math_ops.sin) - self._compareBoth(x, np.cos, math_ops.cos) - self._compareBoth(k, np.arcsin, math_ops.asin) - self._compareBoth(k, np.arccos, math_ops.acos) - self._compareBoth(x, np.arctan, math_ops.atan) - self._compareBoth(x, np.tan, math_ops.tan) - self._compareBoth(y, - np.vectorize( - self._replace_domain_error_with_inf(math.lgamma)), - math_ops.lgamma) - self._compareBoth(x, np.vectorize(math.erf), math_ops.erf) - self._compareBoth(x, np.vectorize(math.erfc), math_ops.erfc) - try: - from scipy import special # pylint: disable=g-import-not-at-top - self._compareBoth(x, special.i0e, math_ops.bessel_i0e) - self._compareBoth(x, special.i1e, math_ops.bessel_i1e) - except ImportError as e: - tf_logging.warn("Cannot test special functions: %s" % str(e)) - - self._compareBothSparse(x, np.abs, math_ops.abs) - self._compareBothSparse(x, np.negative, math_ops.negative) - self._compareBothSparse(x, np.square, math_ops.square) - self._compareBothSparse(z, np.sqrt, math_ops.sqrt, tol=1e-3) - self._compareBothSparse(x, np.tanh, math_ops.tanh) - self._compareBothSparse(y, np.sign, math_ops.sign) - self._compareBothSparse(x, np.vectorize(math.erf), math_ops.erf) - - def testFloatTanhEdge(self): - x = np.arange(40, 40 + 6).reshape(6).astype(np.float32) - self._compareBoth(x, np.tanh, math_ops.tanh) - x = np.arange(-40, -40 + 6).reshape(6).astype(np.float32) - self._compareBoth(x, np.tanh, math_ops.tanh) - - def testFloatEmpty(self): - x = np.empty((2, 0, 5), dtype=np.float32) - self._compareBoth(x, np.abs, math_ops.abs) - self._compareBoth(x, np.abs, _ABS) - self._compareBoth(x, np.negative, math_ops.negative) - self._compareBoth(x, np.negative, _NEG) - self._compareBoth(x, self._inv, math_ops.reciprocal) - self._compareBoth(x, np.square, math_ops.square) - self._compareBoth(x, np.sqrt, math_ops.sqrt) - self._compareBoth(x, self._rsqrt, math_ops.rsqrt) - self._compareBoth(x, np.exp, math_ops.exp) - self._compareBoth(x, np.expm1, math_ops.expm1) - self._compareBoth(x, np.log, math_ops.log) - self._compareBoth(x, np.log1p, math_ops.log1p) - self._compareBoth(x, np.sinh, math_ops.sinh) - self._compareBoth(x, np.arcsinh, math_ops.asinh) - self._compareBoth(x, np.cosh, math_ops.cosh) - self._compareBoth(x, np.tanh, math_ops.tanh) - self._compareBoth(x, self._sigmoid, math_ops.sigmoid) - self._compareBoth(x, np.sign, math_ops.sign) - self._compareBoth(x, np.sin, math_ops.sin) - self._compareBoth(x, np.cos, math_ops.cos) - # Can't use vectorize below, so just use some arbitrary function - self._compareBoth(x, np.sign, math_ops.lgamma) - self._compareBoth(x, np.sign, math_ops.erf) - self._compareBoth(x, np.sign, math_ops.erfc) - self._compareBoth(x, np.tan, math_ops.tan) - self._compareBoth(x, np.arcsin, math_ops.asin) - self._compareBoth(x, np.arccos, math_ops.acos) - self._compareBoth(x, np.arctan, math_ops.atan) - try: - from scipy import special # pylint: disable=g-import-not-at-top - self._compareBoth(x, special.i0e, math_ops.bessel_i0e) - self._compareBoth(x, special.i1e, math_ops.bessel_i1e) - except ImportError as e: - tf_logging.warn("Cannot test special functions: %s" % str(e)) - - self._compareBothSparse(x, np.abs, math_ops.abs) - self._compareBothSparse(x, np.negative, math_ops.negative) - self._compareBothSparse(x, np.square, math_ops.square) - self._compareBothSparse(x, np.sqrt, math_ops.sqrt, tol=1e-3) - self._compareBothSparse(x, np.tanh, math_ops.tanh) - self._compareBothSparse(x, np.sign, math_ops.sign) - self._compareBothSparse(x, np.sign, math_ops.erf) - - def testDoubleBasic(self): - x = np.arange(-3, 3).reshape(1, 3, 2).astype(np.float64) - w = x - x.min() + 1.02 # all greater than 1 - y = (x + .5).astype(np.float64) # no zero - z = (x + 15.5).astype(np.float64) # all positive - k = np.arange(-0.90, 0.90, - 0.35).reshape(1, 3, 2).astype(np.float64) # between -1 and 1 - self._compareBoth(x, np.abs, math_ops.abs) - self._compareBoth(x, np.abs, _ABS) - self._compareBoth(x, np.negative, math_ops.negative) - self._compareBoth(x, np.negative, _NEG) - self._compareBoth(y, self._inv, math_ops.reciprocal) - self._compareBoth(x, np.square, math_ops.square) - self._compareBoth(z, np.sqrt, math_ops.sqrt) - self._compareBoth(z, self._rsqrt, math_ops.rsqrt) - self._compareBoth(x, np.exp, math_ops.exp) - self._compareBoth(x, np.expm1, math_ops.expm1) - self._compareBoth(z, np.log, math_ops.log) - self._compareBoth(z, np.log1p, math_ops.log1p) - self._compareBoth(x, np.sinh, math_ops.sinh) - self._compareBoth(x, np.cosh, math_ops.cosh) - self._compareBoth(x, np.tanh, math_ops.tanh) - self._compareBoth(x, np.arcsinh, math_ops.asinh) - self._compareBoth(w, np.arccosh, math_ops.acosh) - self._compareBoth(k, np.arctanh, math_ops.atanh) - self._compareBoth(x, self._sigmoid, math_ops.sigmoid) - self._compareBoth(y, np.sign, math_ops.sign) - self._compareBoth(x, np.sin, math_ops.sin) - self._compareBoth(x, np.cos, math_ops.cos) - self._compareBoth(y, - np.vectorize( - self._replace_domain_error_with_inf(math.lgamma)), - math_ops.lgamma) - self._compareBoth(x, np.vectorize(math.erf), math_ops.erf) - self._compareBoth(x, np.vectorize(math.erfc), math_ops.erfc) - self._compareBoth(x, np.arctan, math_ops.atan) - self._compareBoth(k, np.arcsin, math_ops.asin) - self._compareBoth(k, np.arccos, math_ops.acos) - self._compareBoth(k, np.tan, math_ops.tan) - try: - from scipy import special # pylint: disable=g-import-not-at-top - self._compareBoth(x, special.i0e, math_ops.bessel_i0e) - self._compareBoth(x, special.i1e, math_ops.bessel_i1e) - except ImportError as e: - tf_logging.warn("Cannot test special functions: %s" % str(e)) - - self._compareBothSparse(x, np.abs, math_ops.abs) - self._compareBothSparse(x, np.negative, math_ops.negative) - self._compareBothSparse(x, np.square, math_ops.square) - self._compareBothSparse(z, np.sqrt, math_ops.sqrt, tol=1e-3) - self._compareBothSparse(x, np.tanh, math_ops.tanh) - self._compareBothSparse(y, np.sign, math_ops.sign) - self._compareBothSparse(x, np.vectorize(math.erf), math_ops.erf) - - def testHalfBasic(self): - x = np.arange(-3, 3).reshape(1, 3, 2).astype(np.float16) - y = (x + .5).astype(np.float16) # no zero - z = (x + 15.5).astype(np.float16) # all positive - self._compareBoth(x, np.abs, math_ops.abs) - self._compareBoth(x, np.abs, _ABS) - self._compareBoth(x, np.negative, math_ops.negative) - self._compareBoth(x, np.negative, _NEG) - self._compareBoth(y, self._inv, math_ops.reciprocal) - self._compareBoth(x, np.square, math_ops.square) - self._compareBoth(z, np.sqrt, math_ops.sqrt) - self._compareBoth(z, self._rsqrt, math_ops.rsqrt) - self._compareBoth(x, np.exp, math_ops.exp) - self._compareBoth(x, np.expm1, math_ops.expm1) - self._compareBoth(z, np.log, math_ops.log) - self._compareBoth(z, np.log1p, math_ops.log1p) - self._compareBoth(x, np.tanh, math_ops.tanh) - self._compareBoth(x, self._sigmoid, math_ops.sigmoid) - self._compareBoth(y, np.sign, math_ops.sign) - self._compareBoth(x, np.sin, math_ops.sin) - self._compareBoth(x, np.cos, math_ops.cos) - self._compareBoth(y, - np.vectorize( - self._replace_domain_error_with_inf(math.lgamma)), - math_ops.lgamma) - self._compareBoth(x, np.vectorize(math.erf), math_ops.erf) - self._compareBoth(x, np.vectorize(math.erfc), math_ops.erfc) - try: - from scipy import special # pylint: disable=g-import-not-at-top - self._compareBoth(x, special.i0e, math_ops.bessel_i0e) - self._compareBoth(x, special.i1e, math_ops.bessel_i1e) - except ImportError as e: - tf_logging.warn("Cannot test special functions: %s" % str(e)) - - self._compareBothSparse(x, np.abs, math_ops.abs) - self._compareBothSparse(x, np.negative, math_ops.negative) - self._compareBothSparse(x, np.square, math_ops.square) - self._compareBothSparse(z, np.sqrt, math_ops.sqrt, tol=1e-3) - self._compareBothSparse(x, np.tanh, math_ops.tanh) - self._compareBothSparse(y, np.sign, math_ops.sign) - self._compareBothSparse(x, np.vectorize(math.erf), math_ops.erf, tol=1e-3) - - def testInt32Basic(self): - x = np.arange(-6, 6, 2).reshape(1, 3, 2).astype(np.int32) - self._compareCpu(x, np.abs, math_ops.abs) - self._compareCpu(x, np.abs, _ABS) - self._compareBoth(x, np.negative, math_ops.negative) - self._compareBoth(x, np.negative, _NEG) - self._compareBoth(x, np.square, math_ops.square) - self._compareCpu(x, np.sign, math_ops.sign) - - self._compareBothSparse(x, np.abs, math_ops.abs) - self._compareBothSparse(x, np.negative, math_ops.negative) - self._compareBothSparse(x, np.square, math_ops.square) - self._compareBothSparse(x, np.sign, math_ops.sign) - - def testInt64Basic(self): - x = np.arange(-6 << 40, 6 << 40, 2 << 40).reshape(1, 3, 2).astype(np.int64) - self._compareCpu(x, np.abs, math_ops.abs) - self._compareCpu(x, np.abs, _ABS) - self._compareCpu(x, np.negative, math_ops.negative) - self._compareCpu(x, np.negative, _NEG) - self._compareCpu(x, np.sign, math_ops.sign) - - self._compareBothSparse(x, np.abs, math_ops.abs) - self._compareBothSparse(x, np.negative, math_ops.negative) - self._compareBothSparse(x, np.sign, math_ops.sign) - - def testInt64Square(self): - x = np.arange(-6 << 20, 6 << 20, 2 << 20).reshape(1, 3, 2).astype(np.int64) - self._compareCpu(x, np.square, math_ops.square) - self._compareBothSparse(x, np.square, math_ops.square) - - def testComplex64Basic(self): - x = np.complex(1, 1) * np.arange(-3, 3).reshape(1, 3, 2).astype( - np.complex64) - y = x + np.complex(0.5, 0.5) # no zeros - self._compareBoth(x, np.abs, math_ops.abs) - self._compareBoth(x, np.abs, _ABS) - self._compareBoth(x, np.negative, math_ops.negative) - self._compareBoth(x, np.negative, _NEG) - self._compareCpu(y, self._inv, math_ops.reciprocal) - self._compareCpu(x, np.square, math_ops.square) - self._compareCpu(y, np.sqrt, math_ops.sqrt) - self._compareCpu(y, self._rsqrt, math_ops.rsqrt) - self._compareBoth(x, np.exp, math_ops.exp) - self._compareCpu(x, np.expm1, math_ops.expm1) - self._compareCpu(y, np.log, math_ops.log) - self._compareCpu(y, np.log1p, math_ops.log1p) - self._compareCpu(x, np.sinh, math_ops.sinh) - self._compareCpu(x, np.cosh, math_ops.cosh) - self._compareCpu(x, np.tanh, math_ops.tanh) - - # Complex64 versions of asinh() and acosh() in libstdc++ only have 6 digits - # of precision. - # Small gradient values + low precision --> High relative error - self._compareCpu(y, np.arcsinh, math_ops.asinh, grad_rtol=1e-2) - self._compareCpu(y, np.arccosh, math_ops.acosh, grad_rtol=1e-2) - - self._compareCpu(y, np.arctanh, math_ops.atanh) - self._compareCpu(x, self._sigmoid, math_ops.sigmoid) - self._compareCpu(x, np.sin, math_ops.sin) - self._compareCpu(x, np.cos, math_ops.cos) - - self._compareBothSparse(x, np.abs, math_ops.abs) - self._compareBothSparse(x, np.negative, math_ops.negative) - self._compareBothSparse(x, np.square, math_ops.square) - self._compareBothSparse(x, np.sqrt, math_ops.sqrt, 1e-3) - self._compareBothSparse(x, np.tanh, math_ops.tanh) - - # Numpy uses an incorrect definition of sign; use the right one instead. - def complex_sign(x): - return x / np.abs(x) - - self._compareBoth(y, complex_sign, math_ops.sign) - self._compareBothSparse(y, complex_sign, math_ops.sign) - - def testComplex128Basic(self): - x = np.complex(1, 1) * np.arange(-3, 3).reshape(1, 3, 2).astype( - np.complex128) - y = x + np.complex(0.5, 0.5) # no zeros - self._compareBoth(x, np.abs, math_ops.abs) - self._compareBoth(x, np.abs, _ABS) - self._compareBoth(x, np.negative, math_ops.negative) - self._compareBoth(x, np.negative, _NEG) - self._compareCpu(y, self._inv, math_ops.reciprocal) - self._compareCpu(x, np.square, math_ops.square) - self._compareCpu(y, np.sqrt, math_ops.sqrt) - self._compareCpu(y, self._rsqrt, math_ops.rsqrt) - self._compareBoth(x, np.exp, math_ops.exp) - self._compareCpu(x, np.expm1, math_ops.expm1) - self._compareCpu(y, np.log, math_ops.log) - self._compareCpu(y, np.log1p, math_ops.log1p) - self._compareCpu(x, np.sinh, math_ops.sinh) - self._compareCpu(x, np.cosh, math_ops.cosh) - self._compareCpu(x, np.tanh, math_ops.tanh) - self._compareCpu(y, np.arcsinh, math_ops.asinh) - self._compareCpu(y, np.arccosh, math_ops.acosh) - self._compareCpu(y, np.arctanh, math_ops.atanh) - self._compareCpu(x, self._sigmoid, math_ops.sigmoid) - self._compareCpu(x, np.sin, math_ops.sin) - self._compareCpu(x, np.cos, math_ops.cos) - - self._compareBothSparse(x, np.abs, math_ops.abs) - self._compareBothSparse(x, np.negative, math_ops.negative) - self._compareBothSparse(x, np.square, math_ops.square) - self._compareBothSparse(x, np.sqrt, math_ops.sqrt, 1e-3) - self._compareBothSparse(x, np.tanh, math_ops.tanh) - - # Numpy uses an incorrect definition of sign; use the right one instead. - def complex_sign(x): - return x / np.abs(x) - - self._compareBoth(y, complex_sign, math_ops.sign) - self._compareBothSparse(y, complex_sign, math_ops.sign) - - def testGradGrad(self): - np.random.seed(7) - shape = (5,) - dtype_tols = [(np.float32, 5e-4), (np.float64, 1e-6), (np.complex64, 5e-4), - (np.complex128, 1e-6)] - op_range = [ - (gen_math_ops.reciprocal_grad, [-2, 2]), - (gen_math_ops.rsqrt_grad, [0.1, 3]), - (gen_math_ops.sigmoid_grad, [-2, 2]), - (gen_math_ops.sqrt_grad, [0.1, 3]), - (gen_math_ops.tanh_grad, [-2, 2]), - ] - - def rand(dtype): - x = np.random.uniform( - real_range[0], real_range[1], size=shape[0]).astype(dtype) - if dtype in (np.complex64, np.complex128): - x += 1j * np.random.uniform(-2, 2, size=shape[0]).astype(dtype) - return x - - for op, real_range in op_range: - with self.test_session(): - for dtype, tol in dtype_tols: - x = constant_op.constant(rand(dtype)) - y = constant_op.constant(rand(dtype)) - z = op(x, y) - grads = gradient_checker.compute_gradient( - [x, y], [shape, shape], - z, - shape, - x_init_value=[rand(dtype), rand(dtype)]) - if isinstance(grads, tuple): - grads = [grads] - for analytical, numerical in grads: - self.assertAllClose(analytical, numerical, rtol=tol, atol=tol) - - -class BinaryOpTest(test.TestCase): - - def _compareCpu(self, x, y, np_func, tf_func, also_compare_variables=False): - np_ans = np_func(x, y) - with self.test_session(use_gpu=False): - inx = ops.convert_to_tensor(x) - iny = ops.convert_to_tensor(y) - out = tf_func(inx, iny) - tf_cpu = out.eval() - # Test that the op takes precedence over numpy operators. - np_left = tf_func(x, iny).eval() - np_right = tf_func(inx, y).eval() - - if also_compare_variables: - var_x = variables.Variable(x) - var_y = variables.Variable(y) - variables.global_variables_initializer().run() - print(type(x), type(y), type(var_x), type(var_y)) - print(type(tf_func(x, var_y)), type(tf_func(var_x, y))) - np_var_left = tf_func(x, var_y).eval() - np_var_right = tf_func(var_x, y).eval() - - if np_ans.dtype != np.object: - self.assertAllClose(np_ans, tf_cpu) - self.assertAllClose(np_ans, np_left) - self.assertAllClose(np_ans, np_right) - if also_compare_variables: - self.assertAllClose(np_ans, np_var_left) - self.assertAllClose(np_ans, np_var_right) - self.assertShapeEqual(np_ans, out) - - _GRAD_TOL = { - dtypes_lib.float16: 1e-3, - dtypes_lib.float32: 1e-3, - dtypes_lib.complex64: 1e-2, - dtypes_lib.float64: 1e-5, - dtypes_lib.complex128: 1e-4 - } - - def _compareGradientX(self, - x, - y, - np_func, - tf_func, - numeric_gradient_type=None): - z = np_func(x, y) - zs = list(z.shape) - with self.test_session(): - inx = ops.convert_to_tensor(x) - iny = ops.convert_to_tensor(y) - if x.dtype in (np.float32, np.float64): - out = 1.1 * tf_func(inx, iny) - else: - out = tf_func(inx, iny) - xs = list(x.shape) - jacob_t, jacob_n = gradient_checker.compute_gradient( - inx, xs, out, zs, x_init_value=x) - if numeric_gradient_type is not None: - xf = x.astype(numeric_gradient_type) - yf = y.astype(numeric_gradient_type) - inxf = ops.convert_to_tensor(xf) - inyf = ops.convert_to_tensor(yf) - outf = tf_func(inxf, inyf) - _, jacob_n = gradient_checker.compute_gradient( - inxf, xs, outf, zs, x_init_value=xf, delta=1e-3) - jacob_n = jacob_n.astype(x.dtype) - tol = self._GRAD_TOL[dtypes_lib.as_dtype(x.dtype)] - self.assertAllClose(jacob_t, jacob_n, rtol=tol, atol=tol) - - def _compareGradientY(self, - x, - y, - np_func, - tf_func, - numeric_gradient_type=None): - z = np_func(x, y) - zs = list(z.shape) - with self.test_session(): - inx = ops.convert_to_tensor(x) - iny = ops.convert_to_tensor(y) - if x.dtype in (np.float32, np.float64): - out = 1.1 * tf_func(inx, iny) - else: - out = tf_func(inx, iny) - ys = list(np.shape(y)) - jacob_t, jacob_n = gradient_checker.compute_gradient( - iny, ys, out, zs, x_init_value=y) - if numeric_gradient_type is not None: - xf = x.astype(numeric_gradient_type) - yf = y.astype(numeric_gradient_type) - inxf = ops.convert_to_tensor(xf) - inyf = ops.convert_to_tensor(yf) - outf = tf_func(inxf, inyf) - _, jacob_n = gradient_checker.compute_gradient( - inyf, ys, outf, zs, x_init_value=yf) - jacob_n = jacob_n.astype(x.dtype) - tol = self._GRAD_TOL[dtypes_lib.as_dtype(x.dtype)] - self.assertAllClose(jacob_t, jacob_n, rtol=tol, atol=tol) - - def _compareGpu(self, x, y, np_func, tf_func): - np_ans = np_func(x, y) - with self.test_session(force_gpu=test_util.is_gpu_available()): - inx = ops.convert_to_tensor(x) - iny = ops.convert_to_tensor(y) - out = tf_func(inx, iny) - tf_gpu = out.eval() - self.assertAllClose(np_ans, tf_gpu) - self.assertShapeEqual(np_ans, out) - # TODO(zhifengc/ke): make gradient checker work on GPU. - - def _compareBoth(self, x, y, np_func, tf_func, also_compare_variables=False): - self._compareCpu(x, y, np_func, tf_func, also_compare_variables) - if x.dtype in (np.float16, np.float32, np.float64, np.complex64, - np.complex128): - if tf_func not in (_FLOORDIV, math_ops.floordiv, math_ops.zeta, - math_ops.polygamma): - self._compareGradientX(x, y, np_func, tf_func) - self._compareGradientY(x, y, np_func, tf_func) - if tf_func in (math_ops.zeta, math_ops.polygamma): - # These methods only support gradients in the second parameter - self._compareGradientY(x, y, np_func, tf_func) - self._compareGpu(x, y, np_func, tf_func) - - def testFloatBasic(self): - x = np.linspace(-5, 20, 15).reshape(1, 3, 5).astype(np.float32) - y = np.linspace(20, -5, 15).reshape(1, 3, 5).astype(np.float32) - self._compareBoth(x, y, np.add, math_ops.add, also_compare_variables=True) - self._compareBoth(x, y, np.subtract, math_ops.subtract) - self._compareBoth(x, y, np.multiply, math_ops.multiply) - self._compareBoth(x, y + 0.1, np.true_divide, math_ops.truediv) - self._compareBoth(x, y + 0.1, np.floor_divide, math_ops.floordiv) - self._compareBoth(x, y, np.add, _ADD) - self._compareBoth(x, y, np.subtract, _SUB) - self._compareBoth(x, y, np.multiply, _MUL) - self._compareBoth(x, y + 0.1, np.true_divide, _TRUEDIV) - self._compareBoth(x, y + 0.1, np.floor_divide, _FLOORDIV) - self._compareBoth(x, y, np.arctan2, math_ops.atan2) - x1 = np.random.randn(5, 6).astype(np.float32) - x2 = np.random.randn(5, 6).astype(np.float32) - # Remove tiny values--atan2 gradients are flaky near the origin. - x1[np.abs(x1) < 0.05] = 0.05 * np.sign(x1[np.abs(x1) < 0.05]) - x2[np.abs(x2) < 0.05] = 0.05 * np.sign(x2[np.abs(x2) < 0.05]) - self._compareBoth(x1, x2, np.arctan2, math_ops.atan2) - try: - from scipy import special # pylint: disable=g-import-not-at-top - a_pos_small = np.linspace(0.1, 2, 15).reshape(1, 3, 5).astype(np.float32) - x_pos_small = np.linspace(0.1, 10, 15).reshape(1, 3, 5).astype(np.float32) - self._compareBoth(a_pos_small, x_pos_small, special.gammainc, - math_ops.igamma) - self._compareBoth(a_pos_small, x_pos_small, special.gammaincc, - math_ops.igammac) - # Need x > 1 - self._compareBoth(x_pos_small + 1, a_pos_small, special.zeta, - math_ops.zeta) - n_small = np.arange(0, 15).reshape(1, 3, 5).astype(np.float32) - self._compareBoth(n_small, x_pos_small, special.polygamma, - math_ops.polygamma) - except ImportError as e: - tf_logging.warn("Cannot test special functions: %s" % str(e)) - - def testFloatDifferentShapes(self): - x = np.array([1, 2, 3, 4]).reshape(2, 2).astype(np.float32) - y = np.array([1, 2]).reshape(2, 1).astype(np.float32) - with self.test_session() as sess: - inx = ops.convert_to_tensor(x) - iny = ops.convert_to_tensor(y) - s = math_ops.reduce_sum(inx * iny) - gx, gy = sess.run(gradients_impl.gradients(s, [inx, iny])) - # gx is simply the broadcasted y - self.assertAllEqual(gx, - np.array([1, 1, 2, 2]).reshape(2, 2).astype(np.float32)) - # gy is x's column summed up - self.assertAllEqual(gy, np.array([3, 7]).reshape(2, 1).astype(np.float32)) - - def testFloatVariableOverload(self): - x = np.array([1, 2, 3, 4]).reshape(2, 2).astype(np.int32) - y = np.array([1, 2]).reshape(2, 1).astype(np.int32) - var_x = variables.Variable(x) - var_y = variables.Variable(y) - with self.test_session() as sess: - sess.run([var_x.initializer, var_y.initializer]) - left_result = (var_x * y).eval() - right_result = (x * var_y).eval() - np_result = x * y - self.assertAllEqual(np_result, left_result) - self.assertAllEqual(np_result, right_result) - - def testDoubleBasic(self): - x = np.linspace(-5, 20, 15).reshape(1, 3, 5).astype(np.float64) - y = np.linspace(20, -5, 15).reshape(1, 3, 5).astype(np.float64) - self._compareBoth(x, y, np.add, math_ops.add) - self._compareBoth(x, y, np.subtract, math_ops.subtract) - self._compareBoth(x, y, np.multiply, math_ops.multiply) - self._compareBoth(x, y + 0.1, np.true_divide, math_ops.truediv) - self._compareBoth(x, y + 0.1, np.floor_divide, math_ops.floordiv) - self._compareBoth(x, y, np.add, _ADD) - self._compareBoth(x, y, np.subtract, _SUB) - self._compareBoth(x, y, np.multiply, _MUL) - self._compareBoth(x, y + 0.1, np.true_divide, _TRUEDIV) - self._compareBoth(x, y + 0.1, np.floor_divide, _FLOORDIV) - self._compareBoth(x, y, np.arctan2, math_ops.atan2) - x1 = np.random.randn(7, 4).astype(np.float64) - x2 = np.random.randn(7, 4).astype(np.float64) - # Remove tiny values--atan2 gradients are flaky near the origin. - x1[np.abs(x1) < 0.5] = 0.5 * np.sign(x1[np.abs(x1) < 0.5]) - x2[np.abs(x2) < 0.5] = 0.5 * np.sign(x2[np.abs(x2) < 0.5]) - self._compareBoth(x1, x2, np.arctan2, math_ops.atan2) - try: - from scipy import special # pylint: disable=g-import-not-at-top - a_pos_small = np.linspace(0.1, 2, 15).reshape(1, 3, 5).astype(np.float32) - x_pos_small = np.linspace(0.1, 10, 15).reshape(1, 3, 5).astype(np.float32) - self._compareBoth(a_pos_small, x_pos_small, special.gammainc, - math_ops.igamma) - self._compareBoth(a_pos_small, x_pos_small, special.gammaincc, - math_ops.igammac) - except ImportError as e: - tf_logging.warn("Cannot test special functions: %s" % str(e)) - - def testUint8Basic(self): - x = np.arange(1, 13, 2).reshape(1, 3, 2).astype(np.uint8) - y = np.arange(1, 7, 1).reshape(1, 3, 2).astype(np.uint8) - self._compareBoth(x, y, np.add, math_ops.add) - - def testInt8Basic(self): - x = np.arange(1, 13, 2).reshape(1, 3, 2).astype(np.int8) - y = np.arange(1, 7, 1).reshape(1, 3, 2).astype(np.int8) - self._compareBoth(x, y, np.multiply, math_ops.multiply) - self._compareBoth(x, y, np.multiply, _MUL) - - def testInt16Basic(self): - x = np.arange(1, 13, 2).reshape(1, 3, 2).astype(np.int16) - y = np.arange(1, 7, 1).reshape(1, 3, 2).astype(np.int16) - self._compareBoth(x, y, np.multiply, math_ops.multiply) - self._compareBoth(x, y, np.multiply, _MUL) - - def testUint16Basic(self): - x = np.arange(1, 13, 2).reshape(1, 3, 2).astype(np.uint16) - y = np.arange(1, 7, 1).reshape(1, 3, 2).astype(np.uint16) - self._compareBoth(x, y, np.multiply, math_ops.multiply) - self._compareBoth(x, y, np.multiply, _MUL) - self._compareBoth(x, y, np.true_divide, math_ops.truediv) - self._compareBoth(x, y, np.floor_divide, math_ops.floordiv) - self._compareBoth(x, y, np.true_divide, _TRUEDIV) - self._compareBoth(x, y, np.floor_divide, _FLOORDIV) - - def testInt32Basic(self): - x = np.arange(1, 13, 2).reshape(1, 3, 2).astype(np.int32) - y = np.arange(1, 7, 1).reshape(1, 3, 2).astype(np.int32) - self._compareBoth(x, y, np.add, math_ops.add) - self._compareBoth(x, y, np.subtract, math_ops.subtract) - self._compareBoth(x, y, np.multiply, math_ops.multiply) - self._compareBoth(x, y, np.true_divide, math_ops.truediv) - self._compareBoth(x, y, np.floor_divide, math_ops.floordiv) - self._compareBoth(x, y, np.mod, math_ops.mod) - self._compareBoth(x, y, np.add, _ADD) - self._compareBoth(x, y, np.subtract, _SUB) - self._compareBoth(x, y, np.multiply, _MUL) - self._compareBoth(x, y, np.true_divide, _TRUEDIV) - self._compareBoth(x, y, np.floor_divide, _FLOORDIV) - self._compareBoth(x, y, np.mod, _MOD) - # _compareBoth tests on GPU only for floating point types, so test - # _MOD for int32 on GPU by calling _compareGpu - self._compareGpu(x, y, np.mod, _MOD) - - def testInt64Basic(self): - x = np.arange(1 << 40, 13 << 40, 2 << 40).reshape(1, 3, 2).astype(np.int64) - y = np.arange(1, 7, 1).reshape(1, 3, 2).astype(np.int64) - self._compareBoth(x, y, np.subtract, math_ops.subtract) - self._compareBoth(x, y, np.multiply, math_ops.multiply) - self._compareBoth(x, y, np.true_divide, math_ops.truediv) - self._compareBoth(x, y, np.floor_divide, math_ops.floordiv) - self._compareBoth(x, y, np.mod, math_ops.mod) - self._compareBoth(x, y, np.subtract, _SUB) - self._compareBoth(x, y, np.multiply, _MUL) - self._compareBoth(x, y, np.true_divide, _TRUEDIV) - self._compareBoth(x, y, np.floor_divide, _FLOORDIV) - self._compareBoth(x, y, np.mod, _MOD) - - def testComplex64Basic(self): - x = np.complex(1, 1) * np.linspace(-10, 10, 6).reshape(1, 3, 2).astype( - np.complex64) - y = np.complex(1, 1) * np.linspace(20, -20, 6).reshape(1, 3, 2).astype( - np.complex64) - self._compareBoth(x, y, np.add, math_ops.add) - self._compareBoth(x, y, np.subtract, math_ops.subtract) - self._compareBoth(x, y, np.multiply, math_ops.multiply) - self._compareBoth(x, y + 0.1, np.true_divide, math_ops.truediv) - self._compareBoth(x, y, np.add, _ADD) - self._compareBoth(x, y, np.subtract, _SUB) - self._compareBoth(x, y, np.multiply, _MUL) - self._compareBoth(x, y + 0.1, np.true_divide, _TRUEDIV) - - def testComplex128Basic(self): - x = np.complex(1, 1) * np.linspace(-10, 10, 6).reshape(1, 3, 2).astype( - np.complex128) - y = np.complex(1, 1) * np.linspace(20, -20, 6).reshape(1, 3, 2).astype( - np.complex128) - self._compareBoth(x, y, np.add, math_ops.add) - self._compareBoth(x, y, np.subtract, math_ops.subtract) - self._compareBoth(x, y, np.multiply, math_ops.multiply) - self._compareBoth(x, y + 0.1, np.true_divide, math_ops.truediv) - self._compareBoth(x, y, np.add, _ADD) - self._compareBoth(x, y, np.subtract, _SUB) - self._compareBoth(x, y, np.multiply, _MUL) - self._compareBoth(x, y + 0.1, np.true_divide, _TRUEDIV) - - def testStringComparison(self): - x = np.array([["abc", "bh"], ["c", ""]]) - y = np.array([["abc", "bh"], ["def", "hi"]]) - with self.test_session(use_gpu=False) as sess: - cmp_eq = math_ops.equal(x, y) - cmp_not_eq = math_ops.not_equal(x, y) - values = sess.run([cmp_eq, cmp_not_eq]) - self.assertAllEqual([[True, True], [False, False]], values[0]) - self.assertAllEqual([[False, False], [True, True]], values[1]) - - def testString(self): - x = np.array( - [["x_0_0", "x_0_1", "x_0_2"], ["x_1_0", "x_1_1", "x_1_2"], - ["x_2_0", "x_2_1", "x_2_2"]], - dtype=np.object) - y = np.array( - [["y_0_0", "y_0_1", "y_0_2"], ["y_1_0", "y_1_1", "y_1_2"], - ["y_2_0", "y_2_1", "y_2_2"]], - dtype=np.object) - z = np.array([["z_0", "z_1", "z_2"]], dtype=np.object) - w = np.array("w", dtype=np.object) - self._compareCpu(x, y, _ADD, _ADD) - self._compareCpu(x, z, _ADD, _ADD) - self._compareCpu(x, w, _ADD, _ADD) - self._compareCpu(z, w, _ADD, _ADD) - - def _compareBCast(self, xs, ys, dtype, np_func, tf_func): - if dtype in (np.complex64, np.complex128): - x = (1 + np.linspace(0, 2 + 3j, np.prod(xs))).astype(dtype).reshape(xs) - y = (1 + np.linspace(0, 2 - 2j, np.prod(ys))).astype(dtype).reshape(ys) - else: - x = (1 + np.linspace(0, 5, np.prod(xs))).astype(dtype).reshape(xs) - y = (1 + np.linspace(0, 5, np.prod(ys))).astype(dtype).reshape(ys) - self._compareCpu(x, y, np_func, tf_func) - if x.dtype in (np.float16, np.float32, np.float64): - # TODO(aselle): Make the test work for dtypes: - # (np.complex64, np.complex128). - if tf_func not in (_FLOORDIV, math_ops.floordiv): - if x.dtype == np.float16: - # Compare fp16 theoretical gradients to fp32 numerical gradients, - # since fp16 numerical gradients are too imprecise unless great - # care is taken with choosing the inputs and the delta. This is - # a weaker check (in particular, it does not test the op itself, - # only its gradient), but it's much better than nothing. - self._compareGradientX(x, y, np_func, tf_func, np.float) - self._compareGradientY(x, y, np_func, tf_func, np.float) - else: - self._compareGradientX(x, y, np_func, tf_func) - self._compareGradientY(x, y, np_func, tf_func) - self._compareGpu(x, y, np_func, tf_func) - - # TODO(josh11b,vrv): Refactor this to use parameterized tests. - def _testBCastByFunc(self, funcs, xs, ys): - dtypes = [ - np.float16, - np.float32, - np.float64, - np.int32, - np.int64, - np.complex64, - np.complex128, - ] - for dtype in dtypes: - for (np_func, tf_func) in funcs: - if (dtype in (np.complex64, np.complex128) and - tf_func in (_FLOORDIV, math_ops.floordiv)): - continue # floordiv makes no sense for complex numbers - self._compareBCast(xs, ys, dtype, np_func, tf_func) - self._compareBCast(ys, xs, dtype, np_func, tf_func) - - def _testBCastA(self, xs, ys): - funcs = [ - (np.add, math_ops.add), - (np.add, _ADD), - ] - self._testBCastByFunc(funcs, xs, ys) - - def _testBCastB(self, xs, ys): - funcs = [ - (np.subtract, math_ops.subtract), - (np.subtract, _SUB), - (np.power, math_ops.pow), - ] - self._testBCastByFunc(funcs, xs, ys) - - def _testBCastC(self, xs, ys): - funcs = [ - (np.multiply, math_ops.multiply), - (np.multiply, _MUL), - ] - self._testBCastByFunc(funcs, xs, ys) - - def _testBCastD(self, xs, ys): - funcs = [ - (np.true_divide, math_ops.truediv), - (np.floor_divide, math_ops.floordiv), - (np.true_divide, _TRUEDIV), - (np.floor_divide, _FLOORDIV), - ] - self._testBCastByFunc(funcs, xs, ys) - - def testBCast_0A(self): - self._testBCastA([1, 3, 2], [1]) - - def testBCast_0B(self): - self._testBCastB([1, 3, 2], [1]) - - def testBCast_0C(self): - self._testBCastC([1, 3, 2], [1]) - - def testBCast_0D(self): - self._testBCastD([1, 3, 2], [1]) - - def testBCast_1A(self): - self._testBCastA([1, 3, 2], [2]) - - def testBCast_1B(self): - self._testBCastB([1, 3, 2], [2]) - - def testBCast_1C(self): - self._testBCastC([1, 3, 2], [2]) - - def testBCast_1D(self): - self._testBCastD([1, 3, 2], [2]) - - def testBCast_2A(self): - self._testBCastA([1, 3, 2], [3, 2]) - - def testBCast_2B(self): - self._testBCastB([1, 3, 2], [3, 2]) - - def testBCast_2C(self): - self._testBCastC([1, 3, 2], [3, 2]) - - def testBCast_2D(self): - self._testBCastD([1, 3, 2], [3, 2]) - - def testBCast_3A(self): - self._testBCastA([1, 3, 2], [3, 1]) - - def testBCast_3B(self): - self._testBCastB([1, 3, 2], [3, 1]) - - def testBCast_3C(self): - self._testBCastC([1, 3, 2], [3, 1]) - - def testBCast_3D(self): - self._testBCastD([1, 3, 2], [3, 1]) - - def testBCast_4A(self): - self._testBCastA([1, 3, 2], [1, 3, 2]) - - def testBCast_4B(self): - self._testBCastB([1, 3, 2], [1, 3, 2]) - - def testBCast_4C(self): - self._testBCastC([1, 3, 2], [1, 3, 2]) - - def testBCast_4D(self): - self._testBCastD([1, 3, 2], [1, 3, 2]) - - def testBCast_5A(self): - self._testBCastA([1, 3, 2], [2, 3, 1]) - - def testBCast_5B(self): - self._testBCastB([1, 3, 2], [2, 3, 1]) - - def testBCast_5C(self): - self._testBCastC([1, 3, 2], [2, 3, 1]) - - def testBCast_5D(self): - self._testBCastD([1, 3, 2], [2, 3, 1]) - - def testBCast_6A(self): - self._testBCastA([1, 3, 2], [2, 1, 1]) - - def testBCast_6B(self): - self._testBCastB([1, 3, 2], [2, 1, 1]) - - def testBCast_6C(self): - self._testBCastC([1, 3, 2], [2, 1, 1]) - - def testBCast_6D(self): - self._testBCastD([1, 3, 2], [2, 1, 1]) - - def testBCast_7A(self): - self._testBCastA([1, 3, 2], [1, 3, 1]) - - def testBCast_7B(self): - self._testBCastB([1, 3, 2], [1, 3, 1]) - - def testBCast_7C(self): - self._testBCastC([1, 3, 2], [1, 3, 1]) - - def testBCast_7D(self): - self._testBCastD([1, 3, 2], [1, 3, 1]) - - def testBCast_8A(self): - self._testBCastA([2, 1, 5], [2, 3, 1]) - - def testBCast_8B(self): - self._testBCastB([2, 1, 5], [2, 3, 1]) - - def testBCast_8C(self): - self._testBCastC([2, 1, 5], [2, 3, 1]) - - def testBCast_8D(self): - self._testBCastD([2, 1, 5], [2, 3, 1]) - - def testBCast_9A(self): - self._testBCastA([2, 0, 5], [2, 0, 1]) - - def testBCast_9B(self): - self._testBCastB([2, 0, 5], [2, 0, 1]) - - def testBCast_9C(self): - self._testBCastC([2, 0, 5], [2, 0, 1]) - - def testBCast_9D(self): - self._testBCastD([2, 0, 5], [2, 0, 1]) - - def testBCast_10A(self): - self._testBCastA([2, 3, 0], [2, 3, 1]) - - def testBCast_10B(self): - self._testBCastB([2, 3, 0], [2, 3, 1]) - - def testBCast_10C(self): - self._testBCastC([2, 3, 0], [2, 3, 1]) - - def testBCast_10D(self): - self._testBCastD([2, 3, 0], [2, 3, 1]) - - def testBCast_11A(self): - self._testBCastA([1, 3, 2], [1, 3, 2]) - - def testBCast_11B(self): - self._testBCastB([1, 3, 2], [1, 3, 2]) - - def testBCast_11C(self): - self._testBCastC([1, 3, 2], [1, 3, 2]) - - def testBCast_11D(self): - self._testBCastD([1, 3, 2], [1, 3, 2]) - - def testBCast_12A(self): - self._testBCastA([1, 1, 1, 1, 3, 2], [1, 3, 2]) - - def testBCast_12B(self): - self._testBCastB([1, 1, 1, 1, 3, 2], [1, 3, 2]) - - def testBCast_12C(self): - self._testBCastC([1, 1, 1, 1, 3, 2], [1, 3, 2]) - - def testBCast_12D(self): - self._testBCastD([1, 1, 1, 1, 3, 2], [1, 3, 2]) - - def testBCast_13A(self): - self._testBCastA([1, 3, 2, 1, 1], [1]) - - def testBCast_13B(self): - self._testBCastB([1, 3, 2, 1, 1], [1]) - - def testBCast_13C(self): - self._testBCastC([1, 3, 2, 1, 1], [1]) - - def testBCast_13D(self): - self._testBCastD([1, 3, 2, 1, 1], [1]) - - def testBCast_14A(self): - self._testBCastA([2, 3, 1, 1, 5], [1]) - - def testBCast_14B(self): - self._testBCastB([2, 3, 1, 1, 5], [1]) - - def testBCast_14C(self): - self._testBCastC([2, 3, 1, 1, 5], [1]) - - def testBCast_14D(self): - self._testBCastD([2, 3, 1, 1, 5], [1]) - - def testBCast_15A(self): - self._testBCastA([10, 3, 1, 2], [3, 1, 2]) - - def testBCast_15B(self): - self._testBCastB([10, 3, 1, 2], [3, 1, 2]) - - def testBCast_15C(self): - self._testBCastC([10, 3, 1, 2], [3, 1, 2]) - - def testBCast_15D(self): - self._testBCastD([10, 3, 1, 2], [3, 1, 2]) - - def testMismatchedDimensions(self): - for func in [ - math_ops.add, math_ops.subtract, math_ops.multiply, math_ops.div, _ADD, - _SUB, _MUL, _TRUEDIV, _FLOORDIV - ]: - with self.assertRaisesWithPredicateMatch( - ValueError, lambda e: "Dimensions must" in str(e)): - func( - ops.convert_to_tensor([10.0, 20.0, 30.0]), - ops.convert_to_tensor([[40.0, 50.0], [60.0, 70.0]])) - - def testZeroPowGrad(self): - with self.test_session(): - for dtype in (np.float16, np.float32, np.float64, np.complex64, - np.complex128): - x = constant_op.constant(0.0, dtype=dtype) - y = constant_op.constant(2.0, dtype=dtype) - z = math_ops.pow(x, y) - error = gradient_checker.compute_gradient_error(y, [], z, []) - self.assertEqual(error, 0) - - def testComplexPowGrad(self): - with self.test_session(): - for dtype in np.complex64, np.complex128: - for base in 2.0, -2.0: - x = constant_op.constant(base, dtype=dtype) - y = constant_op.constant(2.0, dtype=dtype) - z = math_ops.pow(x, y) - error = gradient_checker.compute_gradient_error(y, [], z, []) - self.assertLess(error, 2e-4) - - def testAtan2SpecialValues(self): - x1l, x2l = zip((+0.0, +0.0), (+0.0, -0.0), (-0.0, +0.0), (-0.0, -0.0), - (1.2345, float("inf")), (1.2345, -float("inf")), - (-4.321, float("inf")), (-4.125, -float("inf")), - (float("inf"), float("inf")), (float("inf"), -float("inf")), - (-float("inf"), float("inf")), - (-float("inf"), -float("inf"))) - for dtype in np.float32, np.float64: - x1 = np.array(x1l).astype(dtype) - x2 = np.array(x2l).astype(dtype) - self._compareCpu(x1, x2, np.arctan2, math_ops.atan2) - self._compareGpu(x1, x2, np.arctan2, math_ops.atan2) - - def testPowNegativeExponent(self): - for dtype in [np.int32, np.int64]: - with self.test_session(use_gpu=False) as sess: - with self.assertRaisesRegexp( - errors_impl.InvalidArgumentError, - "Integers to negative integer powers are not allowed"): - x = np.array([5, 2]).astype(dtype) - y = np.array([-2, 3]).astype(dtype) - sess.run(math_ops.pow(x, y)) - - with self.test_session(use_gpu=False) as sess: - with self.assertRaisesRegexp( - errors_impl.InvalidArgumentError, - "Integers to negative integer powers are not allowed"): - x = np.array([5, 2]).astype(dtype) - y = np.array([2, -3]).astype(dtype) - sess.run(math_ops.pow(x, y)) - - with self.test_session(use_gpu=False) as sess: - with self.assertRaisesRegexp( - errors_impl.InvalidArgumentError, - "Integers to negative integer powers are not allowed"): - x = np.array([5, 2]).astype(dtype) - y = -3 - sess.run(math_ops.pow(x, y)) - - class ComparisonOpTest(test.TestCase): def _compareScalar(self, func, x, y, dtype): @@ -1470,7 +324,7 @@ class SelectOpTest(test.TestCase): self.assertShapeEqual(np_ans, out) def _compareGradientX(self, c, x, y, numeric_gradient_type=None): - with self.test_session(): + with self.cached_session(): inx = ops.convert_to_tensor(x) iny = ops.convert_to_tensor(y) out = array_ops.where(c, inx, iny) @@ -1494,7 +348,7 @@ class SelectOpTest(test.TestCase): self.assertAllClose(jacob_t, jacob_n, rtol=1e-5, atol=1e-5) def _compareGradientY(self, c, x, y, numeric_gradient_type=None): - with self.test_session(): + with self.cached_session(): inx = ops.convert_to_tensor(x) iny = ops.convert_to_tensor(y) out = array_ops.where(c, inx, iny) @@ -1582,7 +436,7 @@ class SelectOpTest(test.TestCase): x = np.random.rand(1, 3, 0) * 100 y = np.random.rand(1, 3, 0) * 100 z_expected = np.zeros((1, 3, 0), dtype=np.float32) - with self.test_session(): + with self.cached_session(): xt = x.astype(np.float32) yt = y.astype(np.float32) z = array_ops.where(c, xt, yt).eval() @@ -1590,7 +444,7 @@ class SelectOpTest(test.TestCase): def testNan(self): """Verify that nans don't propagate where they shouldn't.""" - with self.test_session(): + with self.cached_session(): for c in False, True: for a in 7.0, np.nan: for b in 5.0, np.nan: @@ -1614,7 +468,7 @@ class BatchSelectOpTest(test.TestCase): self.assertShapeEqual(np_ans, out) def _compareGradientX(self, c, x, y, numeric_gradient_type=None): - with self.test_session(): + with self.cached_session(): inx = ops.convert_to_tensor(x) iny = ops.convert_to_tensor(y) out = array_ops.where(c, inx, iny) @@ -1638,7 +492,7 @@ class BatchSelectOpTest(test.TestCase): self.assertAllClose(jacob_t, jacob_n, rtol=1e-5, atol=1e-5) def _compareGradientY(self, c, x, y, numeric_gradient_type=None): - with self.test_session(): + with self.cached_session(): inx = ops.convert_to_tensor(x) iny = ops.convert_to_tensor(y) out = array_ops.where(c, inx, iny) @@ -1745,7 +599,7 @@ class MinMaxOpTest(test.TestCase): self._compare(x.astype(t), t(y), use_gpu=True) def _compareGradientX(self, func, x, y): - with self.test_session(): + with self.cached_session(): inx = ops.convert_to_tensor(x) iny = ops.convert_to_tensor(y) out = func(inx, iny) @@ -1760,7 +614,7 @@ class MinMaxOpTest(test.TestCase): self.assertAllClose(jacob_t, jacob_n, rtol=1e-5, atol=1e-5) def _compareGradientY(self, func, x, y): - with self.test_session(): + with self.cached_session(): inx = ops.convert_to_tensor(x) iny = ops.convert_to_tensor(y) out = func(inx, iny) @@ -1932,7 +786,7 @@ class RoundingTest(test.TestCase): def _compare_values(self, x, y=None): y = np.rint(x) if y is None else np.asarray(y) - with self.test_session() as sess: + with self.cached_session() as sess: tf_rint = math_ops.rint(x) np_rint = sess.run(tf_rint) self.assertAllEqual(y, np_rint) @@ -1940,7 +794,7 @@ class RoundingTest(test.TestCase): def _compare(self, x): np_floor, np_ceil = np.floor(x), np.ceil(x) - with self.test_session() as sess: + with self.cached_session() as sess: inx = ops.convert_to_tensor(x) ofloor, oceil = math_ops.floor(inx), math_ops.ceil(inx) tf_floor, tf_ceil = sess.run([ofloor, oceil]) @@ -2099,7 +953,7 @@ class ComplexMakeRealImagTest(test.TestCase): # computes the squared sum. This is obviously the same as sum(real # * real) + sum(imag * imag). We just want to make sure the # gradient function is checked. - with self.test_session(): + with self.cached_session(): inx = ops.convert_to_tensor(x) real, imag = array_ops.split(value=inx, num_or_size_splits=2, axis=1) real, imag = array_ops.reshape(real, [-1]), array_ops.reshape(imag, [-1]) @@ -2116,7 +970,7 @@ class ComplexMakeRealImagTest(test.TestCase): def _compareBroadcastGradient(self, x): x_ = ops.convert_to_tensor(x) epsilon = 1e-3 - with self.test_session(): + with self.cached_session(): for args in [(x_, 0.), (0., x_)]: z = math_ops.reduce_sum(math_ops.abs(math_ops.complex(*args))) jacob_t, jacob_n = gradient_checker.compute_gradient( @@ -2136,7 +990,7 @@ class ComplexMakeRealImagTest(test.TestCase): # data is a float matrix of shape [n, 4]. data[:, 0], data[:, 1], # data[:, 2], data[:, 3] are real parts of x, imaginary parts of # x, real parts of y and imaginary parts of y. - with self.test_session(): + with self.cached_session(): inp = ops.convert_to_tensor(data) xr, xi, yr, yi = array_ops.split(value=inp, num_or_size_splits=4, axis=1) @@ -2166,7 +1020,7 @@ class ComplexMakeRealImagTest(test.TestCase): class AccumulateTest(test.TestCase): def testSimple(self): - with self.test_session(): + with self.cached_session(): random_arrays = [ np.random.rand(16, 16, 16, 16).astype(np.float32) for _ in range(20) ] @@ -2181,20 +1035,20 @@ class AccumulateTest(test.TestCase): self.assertAllClose(np_val, tf_val.eval()) def testZeroArgs(self): - with self.test_session(): + with self.cached_session(): with self.assertRaises(ValueError): tf_val = math_ops.accumulate_n([]) tf_val.eval() def testWrongShape(self): - with self.test_session(): + with self.cached_session(): with self.assertRaises(ValueError): a = variables.Variable(0.2) b = variables.Variable(0.1) math_ops.accumulate_n([a, b], shape=[2, 2]) # Should be shape=[] def testWrongType(self): - with self.test_session(): + with self.cached_session(): with self.assertRaises(TypeError): a = variables.Variable(0.2, dtype=np.float32) b = variables.Variable(0.1, dtype=np.float32) @@ -2202,7 +1056,7 @@ class AccumulateTest(test.TestCase): def testWrongTypeOneInput(self): # Scenario that used to trigger a bug, even when testWrongType() worked - with self.test_session(): + with self.cached_session(): with self.assertRaises(TypeError): a = variables.Variable(0.2, dtype=np.float32) math_ops.accumulate_n([a], tensor_dtype=np.int32) @@ -2214,7 +1068,7 @@ class PolyvalTest(test.TestCase): x = np.random.rand(2, 2).astype(dtype) coeffs = [np.random.rand(2, 2).astype(dtype) for _ in range(degree + 1)] np_val = np.polyval(coeffs, x) - with self.test_session(): + with self.cached_session(): tf_val = math_ops.polyval(coeffs, x) self.assertAllClose(np_val, tf_val.eval()) @@ -2237,7 +1091,7 @@ class PolyvalTest(test.TestCase): for _ in range(degree + 1) ] np_val = np.polyval(coeffs, x) - with self.test_session(): + with self.cached_session(): tf_val = math_ops.polyval(coeffs, x) self.assertAllClose(np_val, tf_val.eval()) @@ -2245,7 +1099,7 @@ class PolyvalTest(test.TestCase): x = np.random.rand(2, 2).astype(np.float32) coeffs = [] np_val = np.polyval(coeffs, x) - with self.test_session(): + with self.cached_session(): tf_val = math_ops.polyval(coeffs, x) self.assertAllClose(np_val, tf_val.eval()) diff --git a/tensorflow/python/kernel_tests/cwise_ops_unary_test.py b/tensorflow/python/kernel_tests/cwise_ops_unary_test.py new file mode 100644 index 0000000000..77f182784e --- /dev/null +++ b/tensorflow/python/kernel_tests/cwise_ops_unary_test.py @@ -0,0 +1,541 @@ +# Copyright 2018 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== +"""Functional tests for unary coefficient-wise operations.""" + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import math + +import numpy as np + +from tensorflow.python.framework import constant_op +from tensorflow.python.framework import dtypes as dtypes_lib +from tensorflow.python.framework import ops +from tensorflow.python.framework import sparse_tensor +from tensorflow.python.framework import test_util +from tensorflow.python.ops import gen_math_ops +from tensorflow.python.ops import gradient_checker +from tensorflow.python.ops import math_ops +from tensorflow.python.ops import nn_grad # pylint: disable=unused-import +from tensorflow.python.platform import test +from tensorflow.python.platform import tf_logging + +_NEG = lambda x: -x +_ABS = abs + + +# TODO(zongheng): it'd be great to factor out this function and various random +# SparseTensor gen funcs. +def _sparsify(x, thresh=0.5, index_dtype=np.int64): + x[x < thresh] = 0 + + non_zero = np.where(x) + x_indices = np.vstack(non_zero).astype(index_dtype).T + x_values = x[non_zero] + x_shape = x.shape + + return sparse_tensor.SparseTensor( + indices=x_indices, values=x_values, dense_shape=x_shape), x_values + + +def _default_tolerance(dtype): + """Returns a sensible default tolerance for comparing results of a given type. + + Args: + dtype: A datatype. + """ + if dtype == np.float16: + return 5e-3 + elif dtype in (np.float32, np.complex64): + return 1e-3 + elif dtype in (np.float64, np.complex128): + return 1e-5 + else: + return None # Fail fast for unexpected types + + +class UnaryOpTest(test.TestCase): + + def _compareCpu(self, x, np_func, tf_func, grad_rtol=None, grad_atol=None): + if grad_rtol is None: + grad_rtol = _default_tolerance(x.dtype) + if grad_atol is None: + grad_atol = _default_tolerance(x.dtype) + np_ans = np_func(x) + with self.test_session(use_gpu=False): + inx = ops.convert_to_tensor(x) + if x.dtype in (np.float32, np.float64, + dtypes_lib.bfloat16.as_numpy_dtype): + y = 1.1 * tf_func(inx) + np_ans *= 1.1 + else: + y = tf_func(inx) + tf_cpu = y.eval() + self.assertShapeEqual(np_ans, y) + if x.dtype == np.float16: + self.assertAllClose(np_ans, tf_cpu, rtol=1e-3, atol=1e-3) + elif x.dtype == dtypes_lib.bfloat16.as_numpy_dtype: + self.assertAllClose(np_ans, tf_cpu, rtol=1e-2, atol=1e-2) + else: + self.assertAllClose(np_ans, tf_cpu) + + if x.dtype in (np.complex64, np.complex128) and tf_func == math_ops.sign: + return # Return early + + if x.dtype == np.float16: + s = list(np.shape(x)) + jacob_t, _ = gradient_checker.compute_gradient( + inx, s, y, s, x_init_value=x) + xf = x.astype(np.float) + inxf = ops.convert_to_tensor(xf) + yf = tf_func(inxf) + _, jacob_n = gradient_checker.compute_gradient( + inxf, s, yf, s, x_init_value=xf, delta=1e-2) + jacob_n = jacob_n.astype(np.float16) + self.assertAllClose(jacob_t, jacob_n, rtol=grad_rtol, atol=grad_atol) + elif x.dtype in (np.float32, np.complex64): + s = list(np.shape(x)) + jacob_t, jacob_n = gradient_checker.compute_gradient( + inx, s, y, s, x_init_value=x, delta=1e-3) + self.assertAllClose(jacob_t, jacob_n, rtol=grad_rtol, atol=grad_atol) + elif x.dtype in (np.float64, np.complex128): + s = list(np.shape(x)) + jacob_t, jacob_n = gradient_checker.compute_gradient( + inx, s, y, s, x_init_value=x, delta=1e-5) + self.assertAllClose(jacob_t, jacob_n, rtol=grad_rtol, atol=grad_atol) + + def _check(self, result_tensor, result_np, input_sp_t, tol): + self.assertTrue(isinstance(result_tensor, sparse_tensor.SparseTensor)) + self.assertTrue(isinstance(input_sp_t, sparse_tensor.SparseTensor)) + self.assertAllEqual(input_sp_t.indices.eval(), result_tensor.indices.eval()) + self.assertAllEqual(input_sp_t.dense_shape.eval(), + result_tensor.dense_shape.eval()) + if tol is None: + self.assertAllClose(result_np, result_tensor.values.eval()) + else: + self.assertAllClose( + result_np, result_tensor.values.eval(), rtol=tol, atol=tol) + + def _compareSparseCpu(self, x, np_func, tf_func, tol): + x_sp, x_sp_vals = _sparsify(x) + res_np = np_func(x_sp_vals) + with self.test_session(use_gpu=False): + self._check(tf_func(x_sp), res_np, x_sp, tol) + + def _compareGpu(self, x, np_func, tf_func): + np_ans = np_func(x) + with self.test_session(force_gpu=test_util.is_gpu_available()): + result = tf_func(ops.convert_to_tensor(x)) + tf_gpu = result.eval() + if x.dtype == np.float16: + self.assertAllClose(np_ans, tf_gpu, rtol=1e-3, atol=1e-3) + else: + self.assertAllClose(np_ans, tf_gpu) + # TODO(zhifengc/ke): make gradient checker work on GPU. + + def _compareSparseGpu(self, x, np_func, tf_func, tol): + x_sp, x_sp_vals = _sparsify(x) + res_np = np_func(x_sp_vals) + with self.test_session(force_gpu=test_util.is_gpu_available()): + self._check(tf_func(x_sp), res_np, x_sp, tol) + + def _compareBoth(self, x, np_func, tf_func): + self._compareCpu(x, np_func, tf_func) + self._compareGpu(x, np_func, tf_func) + + def _compareBothSparse(self, x, np_func, tf_func, tol=None): + self._compareSparseCpu(x, np_func, tf_func, tol) + self._compareSparseGpu(x, np_func, tf_func, tol) + + def _inv(self, x): + return 1.0 / x + + def _rsqrt(self, x): + return self._inv(np.sqrt(x)) + + def _sigmoid(self, x): + return 1.0 / (1.0 + np.exp(-x)) + + def _log_sigmoid(self, x): + return np.log(self._sigmoid(x)) + + def _replace_domain_error_with_inf(self, fn): + + def func(x): + try: + return fn(x) + except ValueError as e: + if "domain error" in str(e): + return np.inf * np.ones_like(x) + else: + raise e + + return func + + def testFloatBasic(self): + x = np.arange(-3, 3).reshape(1, 3, 2).astype(np.float32) + w = x - x.min() + 1.02 # all greater than 1 + y = (x + .5).astype(np.float32) # no zero + z = (x + 15.5).astype(np.float32) # all positive + k = np.arange(-0.90, 0.90, 0.25).astype(np.float32) # between -1 and 1 + + self._compareBoth(x, np.abs, math_ops.abs) + self._compareBoth(x, np.abs, _ABS) + self._compareBoth(x, np.negative, math_ops.negative) + self._compareBoth(x, np.negative, _NEG) + self._compareBoth(y, self._inv, math_ops.reciprocal) + self._compareBoth(x, np.square, math_ops.square) + self._compareBoth(z, np.sqrt, math_ops.sqrt) + self._compareBoth(z, self._rsqrt, math_ops.rsqrt) + self._compareBoth(x, np.exp, math_ops.exp) + self._compareBoth(x, np.expm1, math_ops.expm1) + self._compareBoth(z, np.log, math_ops.log) + self._compareBoth(z, np.log1p, math_ops.log1p) + self._compareBoth(x, np.sinh, math_ops.sinh) + self._compareBoth(x, np.cosh, math_ops.cosh) + self._compareBoth(x, np.tanh, math_ops.tanh) + self._compareBoth(x, np.arcsinh, math_ops.asinh) + self._compareBoth(w, np.arccosh, math_ops.acosh) + self._compareBoth(k, np.arctanh, math_ops.atanh) + self._compareBoth(x, self._sigmoid, math_ops.sigmoid) + self._compareBoth(x, self._log_sigmoid, math_ops.log_sigmoid) + self._compareBoth(y, np.sign, math_ops.sign) + self._compareBoth(x, np.sin, math_ops.sin) + self._compareBoth(x, np.cos, math_ops.cos) + self._compareBoth(k, np.arcsin, math_ops.asin) + self._compareBoth(k, np.arccos, math_ops.acos) + self._compareBoth(x, np.arctan, math_ops.atan) + self._compareBoth(x, np.tan, math_ops.tan) + self._compareBoth( + y, np.vectorize(self._replace_domain_error_with_inf(math.lgamma)), + math_ops.lgamma) + self._compareBoth(x, np.vectorize(math.erf), math_ops.erf) + self._compareBoth(x, np.vectorize(math.erfc), math_ops.erfc) + try: + from scipy import special # pylint: disable=g-import-not-at-top + self._compareBoth(x, special.i0e, math_ops.bessel_i0e) + self._compareBoth(x, special.i1e, math_ops.bessel_i1e) + except ImportError as e: + tf_logging.warn("Cannot test special functions: %s" % str(e)) + + self._compareBothSparse(x, np.abs, math_ops.abs) + self._compareBothSparse(x, np.negative, math_ops.negative) + self._compareBothSparse(x, np.square, math_ops.square) + self._compareBothSparse(z, np.sqrt, math_ops.sqrt, tol=1e-3) + self._compareBothSparse(x, np.tanh, math_ops.tanh) + self._compareBothSparse(y, np.sign, math_ops.sign) + self._compareBothSparse(x, np.vectorize(math.erf), math_ops.erf) + + def testFloatTanhEdge(self): + x = np.arange(40, 40 + 6).reshape(6).astype(np.float32) + self._compareBoth(x, np.tanh, math_ops.tanh) + x = np.arange(-40, -40 + 6).reshape(6).astype(np.float32) + self._compareBoth(x, np.tanh, math_ops.tanh) + + def testFloatEmpty(self): + x = np.empty((2, 0, 5), dtype=np.float32) + self._compareBoth(x, np.abs, math_ops.abs) + self._compareBoth(x, np.abs, _ABS) + self._compareBoth(x, np.negative, math_ops.negative) + self._compareBoth(x, np.negative, _NEG) + self._compareBoth(x, self._inv, math_ops.reciprocal) + self._compareBoth(x, np.square, math_ops.square) + self._compareBoth(x, np.sqrt, math_ops.sqrt) + self._compareBoth(x, self._rsqrt, math_ops.rsqrt) + self._compareBoth(x, np.exp, math_ops.exp) + self._compareBoth(x, np.expm1, math_ops.expm1) + self._compareBoth(x, np.log, math_ops.log) + self._compareBoth(x, np.log1p, math_ops.log1p) + self._compareBoth(x, np.sinh, math_ops.sinh) + self._compareBoth(x, np.arcsinh, math_ops.asinh) + self._compareBoth(x, np.cosh, math_ops.cosh) + self._compareBoth(x, np.tanh, math_ops.tanh) + self._compareBoth(x, self._sigmoid, math_ops.sigmoid) + self._compareBoth(x, np.sign, math_ops.sign) + self._compareBoth(x, np.sin, math_ops.sin) + self._compareBoth(x, np.cos, math_ops.cos) + # Can't use vectorize below, so just use some arbitrary function + self._compareBoth(x, np.sign, math_ops.lgamma) + self._compareBoth(x, np.sign, math_ops.erf) + self._compareBoth(x, np.sign, math_ops.erfc) + self._compareBoth(x, np.tan, math_ops.tan) + self._compareBoth(x, np.arcsin, math_ops.asin) + self._compareBoth(x, np.arccos, math_ops.acos) + self._compareBoth(x, np.arctan, math_ops.atan) + try: + from scipy import special # pylint: disable=g-import-not-at-top + self._compareBoth(x, special.i0e, math_ops.bessel_i0e) + self._compareBoth(x, special.i1e, math_ops.bessel_i1e) + except ImportError as e: + tf_logging.warn("Cannot test special functions: %s" % str(e)) + + self._compareBothSparse(x, np.abs, math_ops.abs) + self._compareBothSparse(x, np.negative, math_ops.negative) + self._compareBothSparse(x, np.square, math_ops.square) + self._compareBothSparse(x, np.sqrt, math_ops.sqrt, tol=1e-3) + self._compareBothSparse(x, np.tanh, math_ops.tanh) + self._compareBothSparse(x, np.sign, math_ops.sign) + self._compareBothSparse(x, np.sign, math_ops.erf) + + def testDoubleBasic(self): + x = np.arange(-3, 3).reshape(1, 3, 2).astype(np.float64) + w = x - x.min() + 1.02 # all greater than 1 + y = (x + .5).astype(np.float64) # no zero + z = (x + 15.5).astype(np.float64) # all positive + k = np.arange(-0.90, 0.90, + 0.35).reshape(1, 3, 2).astype(np.float64) # between -1 and 1 + self._compareBoth(x, np.abs, math_ops.abs) + self._compareBoth(x, np.abs, _ABS) + self._compareBoth(x, np.negative, math_ops.negative) + self._compareBoth(x, np.negative, _NEG) + self._compareBoth(y, self._inv, math_ops.reciprocal) + self._compareBoth(x, np.square, math_ops.square) + self._compareBoth(z, np.sqrt, math_ops.sqrt) + self._compareBoth(z, self._rsqrt, math_ops.rsqrt) + self._compareBoth(x, np.exp, math_ops.exp) + self._compareBoth(x, np.expm1, math_ops.expm1) + self._compareBoth(z, np.log, math_ops.log) + self._compareBoth(z, np.log1p, math_ops.log1p) + self._compareBoth(x, np.sinh, math_ops.sinh) + self._compareBoth(x, np.cosh, math_ops.cosh) + self._compareBoth(x, np.tanh, math_ops.tanh) + self._compareBoth(x, np.arcsinh, math_ops.asinh) + self._compareBoth(w, np.arccosh, math_ops.acosh) + self._compareBoth(k, np.arctanh, math_ops.atanh) + self._compareBoth(x, self._sigmoid, math_ops.sigmoid) + self._compareBoth(y, np.sign, math_ops.sign) + self._compareBoth(x, np.sin, math_ops.sin) + self._compareBoth(x, np.cos, math_ops.cos) + self._compareBoth( + y, np.vectorize(self._replace_domain_error_with_inf(math.lgamma)), + math_ops.lgamma) + self._compareBoth(x, np.vectorize(math.erf), math_ops.erf) + self._compareBoth(x, np.vectorize(math.erfc), math_ops.erfc) + self._compareBoth(x, np.arctan, math_ops.atan) + self._compareBoth(k, np.arcsin, math_ops.asin) + self._compareBoth(k, np.arccos, math_ops.acos) + self._compareBoth(k, np.tan, math_ops.tan) + try: + from scipy import special # pylint: disable=g-import-not-at-top + self._compareBoth(x, special.i0e, math_ops.bessel_i0e) + self._compareBoth(x, special.i1e, math_ops.bessel_i1e) + except ImportError as e: + tf_logging.warn("Cannot test special functions: %s" % str(e)) + + self._compareBothSparse(x, np.abs, math_ops.abs) + self._compareBothSparse(x, np.negative, math_ops.negative) + self._compareBothSparse(x, np.square, math_ops.square) + self._compareBothSparse(z, np.sqrt, math_ops.sqrt, tol=1e-3) + self._compareBothSparse(x, np.tanh, math_ops.tanh) + self._compareBothSparse(y, np.sign, math_ops.sign) + self._compareBothSparse(x, np.vectorize(math.erf), math_ops.erf) + + def testHalfBasic(self): + x = np.arange(-3, 3).reshape(1, 3, 2).astype(np.float16) + y = (x + .5).astype(np.float16) # no zero + z = (x + 15.5).astype(np.float16) # all positive + self._compareBoth(x, np.abs, math_ops.abs) + self._compareBoth(x, np.abs, _ABS) + self._compareBoth(x, np.negative, math_ops.negative) + self._compareBoth(x, np.negative, _NEG) + self._compareBoth(y, self._inv, math_ops.reciprocal) + self._compareBoth(x, np.square, math_ops.square) + self._compareBoth(z, np.sqrt, math_ops.sqrt) + self._compareBoth(z, self._rsqrt, math_ops.rsqrt) + self._compareBoth(x, np.exp, math_ops.exp) + self._compareBoth(x, np.expm1, math_ops.expm1) + self._compareBoth(z, np.log, math_ops.log) + self._compareBoth(z, np.log1p, math_ops.log1p) + self._compareBoth(x, np.tanh, math_ops.tanh) + self._compareBoth(x, self._sigmoid, math_ops.sigmoid) + self._compareBoth(y, np.sign, math_ops.sign) + self._compareBoth(x, np.sin, math_ops.sin) + self._compareBoth(x, np.cos, math_ops.cos) + self._compareBoth( + y, np.vectorize(self._replace_domain_error_with_inf(math.lgamma)), + math_ops.lgamma) + self._compareBoth(x, np.vectorize(math.erf), math_ops.erf) + self._compareBoth(x, np.vectorize(math.erfc), math_ops.erfc) + try: + from scipy import special # pylint: disable=g-import-not-at-top + self._compareBoth(x, special.i0e, math_ops.bessel_i0e) + self._compareBoth(x, special.i1e, math_ops.bessel_i1e) + except ImportError as e: + tf_logging.warn("Cannot test special functions: %s" % str(e)) + + self._compareBothSparse(x, np.abs, math_ops.abs) + self._compareBothSparse(x, np.negative, math_ops.negative) + self._compareBothSparse(x, np.square, math_ops.square) + self._compareBothSparse(z, np.sqrt, math_ops.sqrt, tol=1e-3) + self._compareBothSparse(x, np.tanh, math_ops.tanh) + self._compareBothSparse(y, np.sign, math_ops.sign) + self._compareBothSparse(x, np.vectorize(math.erf), math_ops.erf, tol=1e-3) + + def testInt32Basic(self): + x = np.arange(-6, 6, 2).reshape(1, 3, 2).astype(np.int32) + self._compareCpu(x, np.abs, math_ops.abs) + self._compareCpu(x, np.abs, _ABS) + self._compareBoth(x, np.negative, math_ops.negative) + self._compareBoth(x, np.negative, _NEG) + self._compareBoth(x, np.square, math_ops.square) + self._compareCpu(x, np.sign, math_ops.sign) + + self._compareBothSparse(x, np.abs, math_ops.abs) + self._compareBothSparse(x, np.negative, math_ops.negative) + self._compareBothSparse(x, np.square, math_ops.square) + self._compareBothSparse(x, np.sign, math_ops.sign) + + def testInt64Basic(self): + x = np.arange(-6 << 40, 6 << 40, 2 << 40).reshape(1, 3, 2).astype(np.int64) + self._compareCpu(x, np.abs, math_ops.abs) + self._compareCpu(x, np.abs, _ABS) + self._compareCpu(x, np.negative, math_ops.negative) + self._compareCpu(x, np.negative, _NEG) + self._compareCpu(x, np.sign, math_ops.sign) + + self._compareBothSparse(x, np.abs, math_ops.abs) + self._compareBothSparse(x, np.negative, math_ops.negative) + self._compareBothSparse(x, np.sign, math_ops.sign) + + def testInt64Square(self): + x = np.arange(-6 << 20, 6 << 20, 2 << 20).reshape(1, 3, 2).astype(np.int64) + self._compareCpu(x, np.square, math_ops.square) + self._compareBothSparse(x, np.square, math_ops.square) + + def testComplex64Basic(self): + x = np.complex(1, 1) * np.arange(-3, 3).reshape(1, 3, 2).astype( + np.complex64) + y = x + np.complex(0.5, 0.5) # no zeros + self._compareBoth(x, np.abs, math_ops.abs) + self._compareBoth(x, np.abs, _ABS) + self._compareBoth(x, np.negative, math_ops.negative) + self._compareBoth(x, np.negative, _NEG) + self._compareCpu(y, self._inv, math_ops.reciprocal) + self._compareCpu(x, np.square, math_ops.square) + self._compareCpu(y, np.sqrt, math_ops.sqrt) + self._compareCpu(y, self._rsqrt, math_ops.rsqrt) + self._compareBoth(x, np.exp, math_ops.exp) + self._compareCpu(x, np.expm1, math_ops.expm1) + self._compareCpu(y, np.log, math_ops.log) + self._compareCpu(y, np.log1p, math_ops.log1p) + self._compareCpu(x, np.sinh, math_ops.sinh) + self._compareCpu(x, np.cosh, math_ops.cosh) + self._compareCpu(x, np.tanh, math_ops.tanh) + + # Complex64 versions of asinh() and acosh() in libstdc++ only have 6 digits + # of precision. + # Small gradient values + low precision --> High relative error + self._compareCpu(y, np.arcsinh, math_ops.asinh, grad_rtol=1e-2) + self._compareCpu(y, np.arccosh, math_ops.acosh, grad_rtol=1e-2) + + self._compareCpu(y, np.arctanh, math_ops.atanh) + self._compareCpu(x, self._sigmoid, math_ops.sigmoid) + self._compareCpu(x, np.sin, math_ops.sin) + self._compareCpu(x, np.cos, math_ops.cos) + + self._compareBothSparse(x, np.abs, math_ops.abs) + self._compareBothSparse(x, np.negative, math_ops.negative) + self._compareBothSparse(x, np.square, math_ops.square) + self._compareBothSparse(x, np.sqrt, math_ops.sqrt, 1e-3) + self._compareBothSparse(x, np.tanh, math_ops.tanh) + + # Numpy uses an incorrect definition of sign; use the right one instead. + def complex_sign(x): + return x / np.abs(x) + + self._compareBoth(y, complex_sign, math_ops.sign) + self._compareBothSparse(y, complex_sign, math_ops.sign) + + def testComplex128Basic(self): + x = np.complex(1, 1) * np.arange(-3, 3).reshape(1, 3, 2).astype( + np.complex128) + y = x + np.complex(0.5, 0.5) # no zeros + self._compareBoth(x, np.abs, math_ops.abs) + self._compareBoth(x, np.abs, _ABS) + self._compareBoth(x, np.negative, math_ops.negative) + self._compareBoth(x, np.negative, _NEG) + self._compareCpu(y, self._inv, math_ops.reciprocal) + self._compareCpu(x, np.square, math_ops.square) + self._compareCpu(y, np.sqrt, math_ops.sqrt) + self._compareCpu(y, self._rsqrt, math_ops.rsqrt) + self._compareBoth(x, np.exp, math_ops.exp) + self._compareCpu(x, np.expm1, math_ops.expm1) + self._compareCpu(y, np.log, math_ops.log) + self._compareCpu(y, np.log1p, math_ops.log1p) + self._compareCpu(x, np.sinh, math_ops.sinh) + self._compareCpu(x, np.cosh, math_ops.cosh) + self._compareCpu(x, np.tanh, math_ops.tanh) + self._compareCpu(y, np.arcsinh, math_ops.asinh) + self._compareCpu(y, np.arccosh, math_ops.acosh) + self._compareCpu(y, np.arctanh, math_ops.atanh) + self._compareCpu(x, self._sigmoid, math_ops.sigmoid) + self._compareCpu(x, np.sin, math_ops.sin) + self._compareCpu(x, np.cos, math_ops.cos) + + self._compareBothSparse(x, np.abs, math_ops.abs) + self._compareBothSparse(x, np.negative, math_ops.negative) + self._compareBothSparse(x, np.square, math_ops.square) + self._compareBothSparse(x, np.sqrt, math_ops.sqrt, 1e-3) + self._compareBothSparse(x, np.tanh, math_ops.tanh) + + # Numpy uses an incorrect definition of sign; use the right one instead. + def complex_sign(x): + return x / np.abs(x) + + self._compareBoth(y, complex_sign, math_ops.sign) + self._compareBothSparse(y, complex_sign, math_ops.sign) + + def testGradGrad(self): + np.random.seed(7) + shape = (5,) + dtype_tols = [(np.float32, 5e-4), (np.float64, 1e-6), (np.complex64, 5e-4), + (np.complex128, 1e-6)] + op_range = [ + (gen_math_ops.reciprocal_grad, [-2, 2]), + (gen_math_ops.rsqrt_grad, [0.1, 3]), + (gen_math_ops.sigmoid_grad, [-2, 2]), + (gen_math_ops.sqrt_grad, [0.1, 3]), + (gen_math_ops.tanh_grad, [-2, 2]), + ] + + def rand(dtype, real_range): + x = np.random.uniform( + real_range[0], real_range[1], size=shape[0]).astype(dtype) + if dtype in (np.complex64, np.complex128): + x += 1j * np.random.uniform(-2, 2, size=shape[0]).astype(dtype) + return x + + for op, real_range in op_range: + with self.cached_session(): + for dtype, tol in dtype_tols: + x = constant_op.constant(rand(dtype, real_range)) + y = constant_op.constant(rand(dtype, real_range)) + z = op(x, y) + grads = gradient_checker.compute_gradient( + [x, y], [shape, shape], + z, + shape, + x_init_value=[rand(dtype, real_range), + rand(dtype, real_range)]) + if isinstance(grads, tuple): + grads = [grads] + for analytical, numerical in grads: + self.assertAllClose(analytical, numerical, rtol=tol, atol=tol) + + +if __name__ == "__main__": + test.main() diff --git a/tensorflow/python/kernel_tests/decode_bmp_op_test.py b/tensorflow/python/kernel_tests/decode_bmp_op_test.py index 35f8f76991..eebaffbe13 100644 --- a/tensorflow/python/kernel_tests/decode_bmp_op_test.py +++ b/tensorflow/python/kernel_tests/decode_bmp_op_test.py @@ -60,7 +60,7 @@ class DecodeBmpOpTest(test.TestCase): img_in = constant_op.constant(byte_string, dtype=dtypes.string) decode = array_ops.squeeze(image_ops.decode_bmp(img_in)) - with self.test_session(): + with self.cached_session(): decoded = decode.eval() self.assertAllEqual(decoded, img_bytes) @@ -135,7 +135,7 @@ class DecodeBmpOpTest(test.TestCase): img_in = constant_op.constant(byte_string, dtype=dtypes.string) decode = image_ops.decode_bmp(img_in) - with self.test_session(): + with self.cached_session(): decoded = decode.eval() self.assertAllEqual(decoded, img_bytes) diff --git a/tensorflow/python/kernel_tests/decode_compressed_op_test.py b/tensorflow/python/kernel_tests/decode_compressed_op_test.py index c9bda58ca7..1cc1c7da30 100644 --- a/tensorflow/python/kernel_tests/decode_compressed_op_test.py +++ b/tensorflow/python/kernel_tests/decode_compressed_op_test.py @@ -44,7 +44,7 @@ class DecodeCompressedOpTest(test.TestCase): def testDecompress(self): for compression_type in ["ZLIB", "GZIP", ""]: - with self.test_session(): + with self.cached_session(): in_bytes = array_ops.placeholder(dtypes.string, shape=[2]) decompressed = parsing_ops.decode_compressed( in_bytes, compression_type=compression_type) @@ -57,7 +57,7 @@ class DecodeCompressedOpTest(test.TestCase): def testDecompressWithRaw(self): for compression_type in ["ZLIB", "GZIP", ""]: - with self.test_session(): + with self.cached_session(): in_bytes = array_ops.placeholder(dtypes.string, shape=[None]) decompressed = parsing_ops.decode_compressed( in_bytes, compression_type=compression_type) diff --git a/tensorflow/python/kernel_tests/decode_csv_op_test.py b/tensorflow/python/kernel_tests/decode_csv_op_test.py index 4f49d72676..e9307a6b2f 100644 --- a/tensorflow/python/kernel_tests/decode_csv_op_test.py +++ b/tensorflow/python/kernel_tests/decode_csv_op_test.py @@ -20,28 +20,30 @@ from __future__ import print_function import numpy as np +from tensorflow.python.eager import context +from tensorflow.python.framework import errors +from tensorflow.python.framework import test_util from tensorflow.python.ops import parsing_ops from tensorflow.python.platform import test +@test_util.run_all_in_graph_and_eager_modes class DecodeCSVOpTest(test.TestCase): def _test(self, args, expected_out=None, expected_err_re=None): - with self.test_session() as sess: + if expected_err_re is None: decode = parsing_ops.decode_csv(**args) - - if expected_err_re is None: - out = sess.run(decode) - - for i, field in enumerate(out): - if field.dtype == np.float32 or field.dtype == np.float64: - self.assertAllClose(field, expected_out[i]) - else: - self.assertAllEqual(field, expected_out[i]) - - else: - with self.assertRaisesOpError(expected_err_re): - sess.run(decode) + out = self.evaluate(decode) + + for i, field in enumerate(out): + if field.dtype == np.float32 or field.dtype == np.float64: + self.assertAllClose(field, expected_out[i]) + else: + self.assertAllEqual(field, expected_out[i]) + else: + with self.assertRaisesOpError(expected_err_re): + decode = parsing_ops.decode_csv(**args) + self.evaluate(decode) def testSimple(self): args = { @@ -53,6 +55,31 @@ class DecodeCSVOpTest(test.TestCase): self._test(args, expected_out) + def testSimpleWithScalarDefaults(self): + args = { + "records": ["1,4", "2,5", "3,6"], + "record_defaults": [1, 2], + } + + expected_out = [[1, 2, 3], [4, 5, 6]] + + self._test(args, expected_out) + + def testSimpleWith2DDefaults(self): + args = { + "records": ["1", "2", "3"], + "record_defaults": [[[0]]], + } + + if context.executing_eagerly(): + err_spec = errors.InvalidArgumentError, ( + "Each record default should be at " + "most rank 1.") + else: + err_spec = ValueError, "Shape must be at most rank 1 but is rank 2" + with self.assertRaisesWithPredicateMatch(*err_spec): + self._test(args) + def testSimpleNoQuoteDelimiter(self): args = { "records": ["1", "2", '"3"'], diff --git a/tensorflow/python/kernel_tests/decode_image_op_test.py b/tensorflow/python/kernel_tests/decode_image_op_test.py index 58280432d6..7f73fbaa84 100644 --- a/tensorflow/python/kernel_tests/decode_image_op_test.py +++ b/tensorflow/python/kernel_tests/decode_image_op_test.py @@ -111,7 +111,7 @@ class DecodeImageOpTest(test.TestCase): def testInvalidBytes(self): image_bytes = b"ThisIsNotAnImage!" decode = image_ops.decode_image(image_bytes) - with self.test_session(): + with self.cached_session(): with self.assertRaises(errors_impl.InvalidArgumentError): decode.eval() diff --git a/tensorflow/python/kernel_tests/decode_png_op_test.py b/tensorflow/python/kernel_tests/decode_png_op_test.py index d2e03938ee..8f36343667 100644 --- a/tensorflow/python/kernel_tests/decode_png_op_test.py +++ b/tensorflow/python/kernel_tests/decode_png_op_test.py @@ -46,7 +46,7 @@ class DecodePngOpTest(test.TestCase): image_ops.decode_png( img_in, dtype=dtypes.uint16)) - with self.test_session(): + with self.cached_session(): decoded = decode.eval() self.assertAllEqual(decoded, img_bytes) diff --git a/tensorflow/python/kernel_tests/decode_raw_op_test.py b/tensorflow/python/kernel_tests/decode_raw_op_test.py index 122a9ed469..dcc984811c 100644 --- a/tensorflow/python/kernel_tests/decode_raw_op_test.py +++ b/tensorflow/python/kernel_tests/decode_raw_op_test.py @@ -29,7 +29,7 @@ from tensorflow.python.platform import test class DecodeRawOpTest(test.TestCase): def testToUint8(self): - with self.test_session(): + with self.cached_session(): in_bytes = array_ops.placeholder(dtypes.string, shape=[2]) decode = parsing_ops.decode_raw(in_bytes, out_type=dtypes.uint8) self.assertEqual([2, None], decode.get_shape().as_list()) @@ -47,7 +47,7 @@ class DecodeRawOpTest(test.TestCase): decode.eval(feed_dict={in_bytes: ["short", "longer"]}) def testToInt16(self): - with self.test_session(): + with self.cached_session(): in_bytes = array_ops.placeholder(dtypes.string, shape=[None]) decode = parsing_ops.decode_raw(in_bytes, out_type=dtypes.int16) self.assertEqual([None, None], decode.get_shape().as_list()) @@ -62,7 +62,7 @@ class DecodeRawOpTest(test.TestCase): decode.eval(feed_dict={in_bytes: ["123", "456"]}) def testEndianness(self): - with self.test_session(): + with self.cached_session(): in_bytes = array_ops.placeholder(dtypes.string, shape=[None]) decode_le = parsing_ops.decode_raw( in_bytes, out_type=dtypes.int32, little_endian=True) @@ -74,18 +74,18 @@ class DecodeRawOpTest(test.TestCase): self.assertAllEqual([[0x01020304]], result) def testToFloat16(self): - with self.test_session(): + with self.cached_session(): in_bytes = array_ops.placeholder(dtypes.string, shape=[None]) decode = parsing_ops.decode_raw(in_bytes, out_type=dtypes.float16) self.assertEqual([None, None], decode.get_shape().as_list()) - expected_result = np.matrix([[1, -2, -3, 4]], dtype=np.float16) + expected_result = np.matrix([[1, -2, -3, 4]], dtype="<f2") result = decode.eval(feed_dict={in_bytes: [expected_result.tostring()]}) self.assertAllEqual(expected_result, result) def testEmptyStringInput(self): - with self.test_session(): + with self.cached_session(): in_bytes = array_ops.placeholder(dtypes.string, shape=[None]) decode = parsing_ops.decode_raw(in_bytes, out_type=dtypes.float16) @@ -94,7 +94,7 @@ class DecodeRawOpTest(test.TestCase): self.assertEqual((num_inputs, 0), result.shape) def testToUInt16(self): - with self.test_session(): + with self.cached_session(): in_bytes = array_ops.placeholder(dtypes.string, shape=[None]) decode = parsing_ops.decode_raw(in_bytes, out_type=dtypes.uint16) self.assertEqual([None, None], decode.get_shape().as_list()) diff --git a/tensorflow/python/kernel_tests/dense_update_ops_no_tsan_test.py b/tensorflow/python/kernel_tests/dense_update_ops_no_tsan_test.py index d33bf1ba12..affbaf159d 100644 --- a/tensorflow/python/kernel_tests/dense_update_ops_no_tsan_test.py +++ b/tensorflow/python/kernel_tests/dense_update_ops_no_tsan_test.py @@ -33,7 +33,7 @@ class AssignOpTest(test.TestCase): # contain benign and deliberate data races when multiple threads update # the same parameters without a lock. def testParallelUpdateWithoutLocking(self): - with self.test_session() as sess: + with self.cached_session() as sess: ones_t = array_ops.fill([1024, 1024], 1.0) p = variables.Variable(array_ops.zeros([1024, 1024])) adds = [ @@ -60,7 +60,7 @@ class AssignOpTest(test.TestCase): self.assertTrue((vals <= ones * 20).all()) def testParallelAssignWithoutLocking(self): - with self.test_session() as sess: + with self.cached_session() as sess: ones_t = array_ops.fill([1024, 1024], float(1)) p = variables.Variable(array_ops.zeros([1024, 1024])) assigns = [ @@ -92,7 +92,7 @@ class AssignOpTest(test.TestCase): # returning the output tensors. This issue will be resolved with the new # resource variables. def testParallelUpdateWithLocking(self): - with self.test_session() as sess: + with self.cached_session() as sess: zeros_t = array_ops.fill([1024, 1024], 0.0) ones_t = array_ops.fill([1024, 1024], 1.0) p = variables.Variable(zeros_t) @@ -119,7 +119,7 @@ class AssignOpTest(test.TestCase): self.assertAllEqual(vals, ones * 20) def testParallelAssignWithLocking(self): - with self.test_session() as sess: + with self.cached_session() as sess: zeros_t = array_ops.fill([1024, 1024], 0.0) ones_t = array_ops.fill([1024, 1024], 1.0) p = variables.Variable(zeros_t) diff --git a/tensorflow/python/kernel_tests/dense_update_ops_test.py b/tensorflow/python/kernel_tests/dense_update_ops_test.py index 4dda9f093b..06c3271850 100644 --- a/tensorflow/python/kernel_tests/dense_update_ops_test.py +++ b/tensorflow/python/kernel_tests/dense_update_ops_test.py @@ -85,7 +85,7 @@ class AssignOpTest(test.TestCase): self._testTypes(np.arange(0, 20).reshape([4, 5])) def testAssignNonStrictShapeChecking(self): - with self.test_session(): + with self.cached_session(): data = array_ops.fill([1024, 1024], 0) p = variables.Variable([1]) a = state_ops.assign(p, data, validate_shape=False) @@ -99,14 +99,14 @@ class AssignOpTest(test.TestCase): self.assertAllEqual(p.eval(), data2.eval()) def testInitRequiredAssignAdd(self): - with self.test_session(): + with self.cached_session(): p = variables.Variable(array_ops.fill([1024, 1024], 1), dtypes.int32) a = state_ops.assign_add(p, array_ops.fill([1024, 1024], 0)) with self.assertRaisesOpError("use uninitialized"): a.op.run() def testInitRequiredAssignSub(self): - with self.test_session(): + with self.cached_session(): p = variables.Variable(array_ops.fill([1024, 1024], 1), dtypes.int32) a = state_ops.assign_sub(p, array_ops.fill([1024, 1024], 0)) with self.assertRaisesOpError("use uninitialized"): diff --git a/tensorflow/python/kernel_tests/depthwise_conv_op_test.py b/tensorflow/python/kernel_tests/depthwise_conv_op_test.py index 5741f2ec64..6d1ead20be 100644 --- a/tensorflow/python/kernel_tests/depthwise_conv_op_test.py +++ b/tensorflow/python/kernel_tests/depthwise_conv_op_test.py @@ -128,7 +128,7 @@ class DepthwiseConv2DTest(test.TestCase): x2 = [f * 1.0 / filter_size for f in range(1, filter_size + 1)] ops.reset_default_graph() graph = ops.get_default_graph() - with self.test_session(graph=graph, use_gpu=use_gpu) as sess: + with self.session(graph=graph, use_gpu=use_gpu) as sess: tolerance = { dtypes.float16: 4e-2, dtypes.float32: 1e-8, @@ -191,7 +191,7 @@ class DepthwiseConv2DTest(test.TestCase): tf_logging.info( "Testing DepthwiseConv2D, %dth config: %r * %r, stride: %d, padding: " "%s", index, input_size, filter_size, stride, padding) - for data_type in [dtypes.float16, dtypes.float32, dtypes.float64]: + for data_type in [dtypes.float32, dtypes.float64]: tf_logging.info("Testing without grouped_conv") self._VerifyValues( input_size, filter_size, stride, padding, data_type, use_gpu=True) @@ -227,7 +227,7 @@ class DepthwiseConv2DTest(test.TestCase): tf_logging.info( "Testing DepthwiseConv2DFormat, %dth config: %r * %r, stride: %d, " "padding: %s", index, input_size, filter_size, stride, padding) - for data_type in [dtypes.float16, dtypes.float32, dtypes.float64]: + for data_type in [dtypes.float32, dtypes.float64]: self._VerifyValues( input_size, filter_size, @@ -366,7 +366,7 @@ class DepthwiseConv2DTest(test.TestCase): filter_data = [x * 1.0 / filter_size for x in range(0, filter_size)] ops.reset_default_graph() graph = ops.get_default_graph() - with self.test_session(graph=graph, use_gpu=use_gpu) as sess: + with self.session(graph=graph, use_gpu=use_gpu) as sess: tolerance = { dtypes.float16: 4e-0, dtypes.float32: 8e-4, @@ -434,7 +434,7 @@ class DepthwiseConv2DTest(test.TestCase): tf_logging.info( "Testing DepthwiseConv2DInputGrad, %dth config: %r * %r, stride: %d, " "padding: %s", index, input_size, filter_size, stride, padding) - for data_type in [dtypes.float16, dtypes.float32, dtypes.float64]: + for data_type in [dtypes.float32, dtypes.float64]: self._ConstructAndTestGradient( input_size, filter_size, @@ -465,7 +465,7 @@ class DepthwiseConv2DTest(test.TestCase): "Testing DepthwiseConv2DInputGradFormat, %dth config: %r * %r, " "stride: %d, padding: %s", index, input_size, filter_size, stride, padding) - for data_type in [dtypes.float16, dtypes.float32, dtypes.float64]: + for data_type in [dtypes.float32, dtypes.float64]: self._ConstructAndTestGradient( input_size, filter_size, @@ -483,7 +483,7 @@ class DepthwiseConv2DTest(test.TestCase): tf_logging.info( "Testing DepthwiseConv2DFilterGrad, %dth config: %r * %r, stride: " "%d, padding: %s", index, input_size, filter_size, stride, padding) - for data_type in [dtypes.float16, dtypes.float32, dtypes.float64]: + for data_type in [dtypes.float32, dtypes.float64]: self._ConstructAndTestGradient( input_size, filter_size, @@ -504,7 +504,7 @@ class DepthwiseConv2DTest(test.TestCase): "Testing DepthwiseConv2DFilterGradFormat, %dth config: %r * %r, " "stride: %d, padding: %s", index, input_size, filter_size, stride, padding) - for data_type in [dtypes.float16, dtypes.float32, dtypes.float64]: + for data_type in [dtypes.float32, dtypes.float64]: self._ConstructAndTestGradient( input_size, filter_size, diff --git a/tensorflow/python/kernel_tests/division_future_test.py b/tensorflow/python/kernel_tests/division_future_test.py index e681b32856..e477bdc73b 100644 --- a/tensorflow/python/kernel_tests/division_future_test.py +++ b/tensorflow/python/kernel_tests/division_future_test.py @@ -50,7 +50,7 @@ class DivisionTestCase(test.TestCase): self.assertEqual(x, y) checks.append(f) - with self.test_session() as sess: + with self.cached_session() as sess: for dtype in dtypes: for x in map(dtype, values): for y in map(dtype, values): diff --git a/tensorflow/python/kernel_tests/division_past_test.py b/tensorflow/python/kernel_tests/division_past_test.py index 9ddd62e63c..63951b5b38 100644 --- a/tensorflow/python/kernel_tests/division_past_test.py +++ b/tensorflow/python/kernel_tests/division_past_test.py @@ -49,7 +49,7 @@ class DivisionTestCase(test.TestCase): self.assertEqual(x, y) checks.append(f) - with self.test_session() as sess: + with self.cached_session() as sess: for dtype in dtypes: for x in map(dtype, values): for y in map(dtype, values): diff --git a/tensorflow/python/kernel_tests/duplicate_op_test.py b/tensorflow/python/kernel_tests/duplicate_op_test.py index 529d3dd0b3..654267a582 100644 --- a/tensorflow/python/kernel_tests/duplicate_op_test.py +++ b/tensorflow/python/kernel_tests/duplicate_op_test.py @@ -34,7 +34,7 @@ class DuplicateOpTest(test.TestCase): self.assertEqual(len(duplicate.OP_LIST.op), 0) - with self.test_session(): + with self.cached_session(): self.assertEqual(math_ops.add(1, 41).eval(), 42) diff --git a/tensorflow/python/kernel_tests/dynamic_partition_op_test.py b/tensorflow/python/kernel_tests/dynamic_partition_op_test.py index 5e8937ad2c..9557e30993 100644 --- a/tensorflow/python/kernel_tests/dynamic_partition_op_test.py +++ b/tensorflow/python/kernel_tests/dynamic_partition_op_test.py @@ -288,7 +288,7 @@ class DynamicPartitionTest(test.TestCase): self.assertAllEqual([], partition_vals[i]) def testErrorIndexOutOfRange(self): - with self.test_session() as sess: + with self.cached_session() as sess: data = constant_op.constant([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11], [12, 13, 14]]) indices = constant_op.constant([0, 2, 99, 2, 2]) @@ -298,7 +298,7 @@ class DynamicPartitionTest(test.TestCase): sess.run(partitions) def testScalarIndexOutOfRange(self): - with self.test_session() as sess: + with self.cached_session() as sess: bad = 17 data = np.zeros(5) partitions = data_flow_ops.dynamic_partition(data, bad, num_partitions=7) @@ -306,7 +306,7 @@ class DynamicPartitionTest(test.TestCase): sess.run(partitions) def testHigherRankIndexOutOfRange(self): - with self.test_session() as sess: + with self.cached_session() as sess: shape = (2, 3) indices = array_ops.placeholder(shape=shape, dtype=np.int32) data = np.zeros(shape + (5,)) @@ -334,7 +334,7 @@ class DynamicPartitionTest(test.TestCase): inds += [13]*194 + [14]*194 + [15]*192 self.assertEqual(len(inds), x.shape[0]) partitioned = data_flow_ops.dynamic_partition(x, inds, 16) - with self.test_session() as sess: + with self.cached_session() as sess: res = sess.run(partitioned) self.assertEqual(res[-1].shape[0], 192) diff --git a/tensorflow/python/kernel_tests/dynamic_stitch_op_test.py b/tensorflow/python/kernel_tests/dynamic_stitch_op_test.py index 49b9569e2b..3a1036e52a 100644 --- a/tensorflow/python/kernel_tests/dynamic_stitch_op_test.py +++ b/tensorflow/python/kernel_tests/dynamic_stitch_op_test.py @@ -252,7 +252,7 @@ class ParallelDynamicStitchTest(DynamicStitchTestBase, test.TestCase): # GPU version unit tests def testScalarGPU(self): - with self.test_session(): + with self.cached_session(): indices = [constant_op.constant(0), constant_op.constant(1)] data = [constant_op.constant(40.0), constant_op.constant(60.0)] for step in -1, 1: @@ -263,7 +263,7 @@ class ParallelDynamicStitchTest(DynamicStitchTestBase, test.TestCase): self.assertEqual([2], stitched_t.get_shape().as_list()) def testHigherRankGPU(self): - with self.test_session() as sess: + with self.cached_session() as sess: indices = [ constant_op.constant(6), constant_op.constant([4, 1]), diff --git a/tensorflow/python/kernel_tests/embedding_ops_test.py b/tensorflow/python/kernel_tests/embedding_ops_test.py index dcd435e1ff..40b8548cea 100644 --- a/tensorflow/python/kernel_tests/embedding_ops_test.py +++ b/tensorflow/python/kernel_tests/embedding_ops_test.py @@ -242,7 +242,7 @@ class EmbeddingLookupTest(test.TestCase): # vector is going to be empty. The subsequent DivOp fails because of that. # TODO(keveman): Disabling the test until the underlying problem is fixed. def testSimpleSharded(self): - with self.test_session(): + with self.cached_session(): num_shards = 2 vocab_size = 4 p, params, feed_dict = _EmbeddingParams(num_shards, vocab_size) @@ -258,7 +258,7 @@ class EmbeddingLookupTest(test.TestCase): self.assertShapeEqual(np_result, embedding) def testMaxNorm(self): - with self.test_session(): + with self.cached_session(): embeddings = constant_op.constant([[2.0]]) ids = constant_op.constant([0], dtype=dtypes.int32) @@ -268,7 +268,7 @@ class EmbeddingLookupTest(test.TestCase): self.assertAllEqual(embedding.eval(), [[1.0]]) def testMaxNormNontrivial(self): - with self.test_session(): + with self.cached_session(): embeddings = constant_op.constant([[2.0, 4.0], [3.0, 1.0]]) ids = constant_op.constant([0, 1], dtype=dtypes.int32) @@ -281,7 +281,7 @@ class EmbeddingLookupTest(test.TestCase): self.assertAllEqual(embedding.eval(), 2 * normalized.eval()) def testSimpleShardedPartitionedVariable(self): - with self.test_session() as sess: + with self.cached_session() as sess: num_shards = 2 vocab_size = 4 p, p_variable, params, feed_dict = _EmbeddingParamsAsPartitionedVariable( @@ -303,7 +303,7 @@ class EmbeddingLookupTest(test.TestCase): self.assertShapeEqual(np_result, embedding) def testSimpleShardedPartitionedResourceVariable(self): - with self.test_session() as sess: + with self.cached_session() as sess: num_shards = 2 vocab_size = 4 p, p_variable, params, _ = _EmbeddingParamsAsPartitionedVariable( @@ -326,7 +326,7 @@ class EmbeddingLookupTest(test.TestCase): self.assertShapeEqual(np_result, embedding) def testShardedModPartitioningInt32Ids(self): - with self.test_session(): + with self.cached_session(): num_shards = 5 vocab_size = 13 # Embedding dimensions is 10. The vocab_size x 10 embedding @@ -348,7 +348,7 @@ class EmbeddingLookupTest(test.TestCase): self.assertShapeEqual(np_result, embedding) def testShardedModPartitioningInt64Ids(self): - with self.test_session(): + with self.cached_session(): num_shards = 5 vocab_size = 13 # Embedding dimensions is 10. The vocab_size x 10 embedding @@ -370,7 +370,7 @@ class EmbeddingLookupTest(test.TestCase): self.assertShapeEqual(np_result, embedding) def testShardedDivPartitioningInt32Ids(self): - with self.test_session(): + with self.cached_session(): num_shards = 5 vocab_size = 13 # Embedding dimensions is 10. The vocab_size x 10 embedding @@ -394,7 +394,7 @@ class EmbeddingLookupTest(test.TestCase): self.assertShapeEqual(np_result, embedding) def testShardedDivPartitioningInt32IdsPartitionedVariable(self): - with self.test_session(): + with self.cached_session(): num_shards = 5 vocab_size = 13 # Embedding dimensions is 10. The vocab_size x 10 embedding @@ -419,7 +419,7 @@ class EmbeddingLookupTest(test.TestCase): self.assertShapeEqual(np_result, embedding) def testShardedDivPartitioningInt64Ids(self): - with self.test_session(): + with self.cached_session(): num_shards = 5 vocab_size = 13 # Embedding dimensions is 10. The vocab_size x 10 embedding @@ -443,7 +443,7 @@ class EmbeddingLookupTest(test.TestCase): self.assertShapeEqual(np_result, embedding) def testShardedDivPartitioningUnknownParamShape(self): - with self.test_session(): + with self.cached_session(): num_shards = 5 vocab_size = 13 # Embedding dimensions is 10. The vocab_size x 10 embedding @@ -475,7 +475,7 @@ class EmbeddingLookupTest(test.TestCase): tf_logging.vlog(1, id_vals) for ids_shape in [(10,), (2, 5)]: for num_shards in [1, 3]: - with self.test_session(): + with self.cached_session(): ids = constant_op.constant( id_vals, shape=ids_shape, dtype=dtypes.int32) x, params, _ = _EmbeddingParams(num_shards, vocab_size, shape=[2]) @@ -494,7 +494,7 @@ class EmbeddingLookupTest(test.TestCase): id_vals = list(np.random.randint(vocab_size, size=num_ids)) tf_logging.vlog(1, id_vals) for num_shards in [1, 3]: - with self.test_session(): + with self.cached_session(): ids = constant_op.constant(id_vals, dtype=dtypes.int32) x, params, _ = _EmbeddingParams(num_shards, vocab_size, shape=[2]) # This will force a conversion from IndexedSlices to Tensor. @@ -528,7 +528,7 @@ class EmbeddingLookupTest(test.TestCase): def testHigherRank(self): np.random.seed(8) - with self.test_session(): + with self.cached_session(): for params_shape in (12,), (6, 3): params = np.random.randn(*params_shape) for ids_shape in (3, 2), (4, 3): @@ -548,7 +548,7 @@ class EmbeddingLookupTest(test.TestCase): def testHigherRankMaxNorm(self): np.random.seed(8) - with self.test_session(): + with self.cached_session(): for params_shape in (12,), (6, 3), (6, 2, 3): # Test embedding rank 0, 1, 2. # Note: the first dimension must be a common multiple of procs below. @@ -581,7 +581,7 @@ class EmbeddingLookupTest(test.TestCase): # It always applies max_norm. np.random.seed(8) l2_norm = 2. - with self.test_session(): + with self.cached_session(): # Param values are in [l2_norm, l2_norm+1) so it will always clip. params = np.random.rand(6, 3) + l2_norm params_norm = l2_norm * params / np.sqrt( @@ -667,7 +667,7 @@ class EmbeddingLookupSparseTest(test.TestCase): [dtypes.float16, dtypes.bfloat16, dtypes.float32, dtypes.float64], [True, False]): - with self.test_session(): + with self.cached_session(): p, params, feed_dict = _EmbeddingParams( num_shards, vocab_size, shape=param_shape, dtype=dtype) embedding_sum = embedding_ops.embedding_lookup_sparse( @@ -716,7 +716,7 @@ class EmbeddingLookupSparseTest(test.TestCase): for num_shards, combiner, dtype, ignore_weights in itertools.product( [1, 3], ["sum", "mean", "sqrtn"], [dtypes.float32, dtypes.float64], [True, False]): - with self.test_session(): + with self.cached_session(): x, params, _ = _EmbeddingParams( num_shards, vocab_size, shape=param_shape, dtype=dtype) @@ -734,7 +734,7 @@ class EmbeddingLookupSparseTest(test.TestCase): self.assertLess(err, 1e-5 if dtype == dtypes.float64 else 2e-3) def testIncompatibleShapes(self): - with self.test_session(): + with self.cached_session(): x, _, _ = _EmbeddingParams(1, 10, dtype=dtypes.float32) sp_ids = sparse_tensor.SparseTensor( constant_op.constant([[0, 0], [0, 1], [1, 0]], dtypes.int64), @@ -819,7 +819,7 @@ class SafeEmbeddingLookupSparseTest(test.TestCase): return sparse_ids, sparse_weights def test_safe_embedding_lookup_sparse_return_zero_vector(self): - with self.test_session(): + with self.cached_session(): embedding_weights = self._random_weights() sparse_ids, sparse_weights = self._ids_and_weights_2d() @@ -832,7 +832,7 @@ class SafeEmbeddingLookupSparseTest(test.TestCase): 3.0, [0] * 4, [0] * 4, embedding_weights[0][2], [0] * 4]) def test_safe_embedding_lookup_sparse_return_special_vector(self): - with self.test_session(): + with self.cached_session(): embedding_weights = self._random_weights() sparse_ids, sparse_weights = self._ids_and_weights_2d() @@ -846,7 +846,7 @@ class SafeEmbeddingLookupSparseTest(test.TestCase): embedding_weights[0][2], embedding_weights[0][3]]) def test_safe_embedding_lookup_sparse_no_weights(self): - with self.test_session(): + with self.cached_session(): embedding_weights = self._random_weights() sparse_ids, _ = self._ids_and_weights_2d() @@ -860,7 +860,7 @@ class SafeEmbeddingLookupSparseTest(test.TestCase): embedding_weights[0][0] + embedding_weights[0][1]) / 2.0]) def test_safe_embedding_lookup_sparse_partitioned(self): - with self.test_session(): + with self.cached_session(): embedding_weights = self._random_weights(num_shards=3) sparse_ids, _ = self._ids_and_weights_2d() @@ -874,7 +874,7 @@ class SafeEmbeddingLookupSparseTest(test.TestCase): (embedding_weights[0] + embedding_weights[1]) / 2.0]) def test_safe_embedding_lookup_sparse_partitioned_inconsistent_weights(self): - with self.test_session(): + with self.cached_session(): embedding_weights = self._random_weights(num_shards=3) sparse_ids, sparse_weights = self._ids_and_weights_2d() @@ -889,7 +889,7 @@ class SafeEmbeddingLookupSparseTest(test.TestCase): embedding_weights, sparse_ids, sparse_weights) def test_safe_embedding_lookup_sparse_3d_return_zero_vector(self): - with self.test_session(): + with self.cached_session(): embedding_weights = self._random_weights() sparse_ids, sparse_weights = self._ids_and_weights_3d() @@ -902,7 +902,7 @@ class SafeEmbeddingLookupSparseTest(test.TestCase): ], [embedding_weights[0][2], [0] * 4, [0] * 4]]) def test_safe_embedding_lookup_sparse_3d_return_special_vector(self): - with self.test_session(): + with self.cached_session(): embedding_weights = self._random_weights() sparse_ids, sparse_weights = self._ids_and_weights_3d() @@ -918,7 +918,7 @@ class SafeEmbeddingLookupSparseTest(test.TestCase): ]]) def test_safe_embedding_lookup_sparse_3d_no_weights(self): - with self.test_session(): + with self.cached_session(): embedding_weights = self._random_weights() sparse_ids, _ = self._ids_and_weights_3d() @@ -934,7 +934,7 @@ class SafeEmbeddingLookupSparseTest(test.TestCase): ]]) def test_safe_embedding_lookup_sparse_3d_partitioned(self): - with self.test_session(): + with self.cached_session(): embedding_weights = self._random_weights(num_shards=3) sparse_ids, _ = self._ids_and_weights_3d() @@ -951,7 +951,7 @@ class SafeEmbeddingLookupSparseTest(test.TestCase): def test_safe_embedding_lookup_sparse_3d_partitioned_inconsistent_weights( self): - with self.test_session(): + with self.cached_session(): embedding_weights = self._random_weights(num_shards=3) sparse_ids, sparse_weights = self._ids_and_weights_3d() @@ -1035,7 +1035,7 @@ class DynamicStitchOpTest(test.TestCase): # We expect that the values are merged in order. def testStitchOrder(self): - with self.test_session(): + with self.cached_session(): indices = [] np_values = [] values = [] diff --git a/tensorflow/python/kernel_tests/extract_image_patches_grad_test.py b/tensorflow/python/kernel_tests/extract_image_patches_grad_test.py index e1f5a6b620..7d9d4e5175 100644 --- a/tensorflow/python/kernel_tests/extract_image_patches_grad_test.py +++ b/tensorflow/python/kernel_tests/extract_image_patches_grad_test.py @@ -83,7 +83,7 @@ class ExtractImagePatchesGradTest(test.TestCase): random_seed = 42 random_seed_lib.set_random_seed(random_seed) - with self.test_session(): + with self.cached_session(): for test_case in self._TEST_CASES: np.random.seed(random_seed) in_shape = test_case['in_shape'] diff --git a/tensorflow/python/kernel_tests/extract_volume_patches_op_test.py b/tensorflow/python/kernel_tests/extract_volume_patches_op_test.py new file mode 100644 index 0000000000..64757a3e07 --- /dev/null +++ b/tensorflow/python/kernel_tests/extract_volume_patches_op_test.py @@ -0,0 +1,131 @@ +# Copyright 2018 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== +"""Functional tests for ExtractVolumePatches op.""" + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import numpy as np + +from tensorflow.python.framework import constant_op +from tensorflow.python.ops import array_ops +from tensorflow.python.platform import test + +class ExtractVolumePatches(test.TestCase): + """Functional tests for ExtractVolumePatches op.""" + + def _VerifyValues(self, image, ksizes, strides, padding, patches): + """Tests input-output pairs for the ExtractVolumePatches op. + + Args: + image: Input tensor with shape: + [batch, in_planes, in_rows, in_cols, depth]. + ksizes: Patch size specified as: [ksize_planes, ksize_rows, ksize_cols]. + strides: Output strides, specified as: + [stride_planes, stride_rows, stride_cols]. + padding: Padding type. + patches: Expected output. + + Note: + rates are not supported as of now. + """ + ksizes = [1] + ksizes + [1] + strides = [1] + strides + [1] + + with self.test_session(use_gpu=True): + out_tensor = array_ops.extract_volume_patches( + constant_op.constant(image), + ksizes=ksizes, + strides=strides, + padding=padding, + name="im2col_3d") + self.assertAllClose(patches, out_tensor.eval()) + + # pylint: disable=bad-whitespace + def testKsize1x1x1Stride1x1x1(self): + """Verifies that for 1x1x1 kernel the output equals the input.""" + image = np.arange(2 * 3 * 4 * 5 * 6).reshape([2, 3, 4, 5, 6]) + 1 + patches = image + for padding in ["VALID", "SAME"]: + self._VerifyValues( + image, + ksizes=[1, 1, 1], + strides=[1, 1, 1], + padding=padding, + patches=patches) + + def testKsize1x1x1Stride2x3x4(self): + """Test for 1x1x1 kernel and strides.""" + image = np.arange(6 * 2 * 4 * 5 * 3).reshape([6, 2, 4, 5, 3]) + 1 + patches = image[:, ::2, ::3, ::4, :] + for padding in ["VALID", "SAME"]: + self._VerifyValues( + image, + ksizes=[1, 1, 1], + strides=[2, 3, 4], + padding=padding, + patches=patches) + + def testKsize1x1x2Stride2x2x3(self): + """Test for 1x1x2 kernel and strides.""" + image = np.arange(45).reshape([1, 3, 3, 5, 1]) + 1 + patches = np.array([[[[[ 1, 2], + [ 4, 5]], + [[11, 12], + [14, 15]]], + [[[31, 32], + [34, 35]], + [[41, 42], + [44, 45]]]]]) + for padding in ["VALID", "SAME"]: + self._VerifyValues( + image, + ksizes=[1, 1, 2], + strides=[2, 2, 3], + padding=padding, + patches=patches) + + def testKsize2x2x2Stride1x1x1Valid(self): + """Test for 2x2x2 kernel with VALID padding.""" + image = np.arange(8).reshape([1, 2, 2, 2, 1]) + 1 + patches = np.array([[[[[1, 2, 3, 4, 5, 6, 7, 8]]]]]) + self._VerifyValues( + image, + ksizes=[2, 2, 2], + strides=[1, 1, 1], + padding="VALID", + patches=patches) + + def testKsize2x2x2Stride1x1x1Same(self): + """Test for 2x2x2 kernel with SAME padding.""" + image = np.arange(8).reshape([1, 2, 2, 2, 1]) + 1 + patches = np.array([[[[[1, 2, 3, 4, 5, 6, 7, 8], + [2, 0, 4, 0, 6, 0, 8, 0]], + [[3, 4, 0, 0, 7, 8, 0, 0], + [4, 0, 0, 0, 8, 0, 0, 0]]], + [[[5, 6, 7, 8, 0, 0, 0, 0], + [6, 0, 8, 0, 0, 0, 0, 0]], + [[7, 8, 0, 0, 0, 0, 0, 0], + [8, 0, 0, 0, 0, 0, 0, 0]]]]]) + self._VerifyValues( + image, + ksizes=[2, 2, 2], + strides=[1, 1, 1], + padding="SAME", + patches=patches) + +if __name__ == "__main__": + test.main() diff --git a/tensorflow/python/kernel_tests/fft_ops_test.py b/tensorflow/python/kernel_tests/fft_ops_test.py index 629acedda5..f117934e4b 100644 --- a/tensorflow/python/kernel_tests/fft_ops_test.py +++ b/tensorflow/python/kernel_tests/fft_ops_test.py @@ -496,7 +496,7 @@ class RFFTOpsTest(BaseFFTOpsTest): "Input dimension .* must have length of at least 6 but got: 5"): x = np.zeros((5,) * rank).astype(np.float32) fft_length = [6] * rank - with self.test_session(): + with self.cached_session(): rfft_fn(x, fft_length).eval() with self.assertRaisesWithPredicateMatch( @@ -504,7 +504,7 @@ class RFFTOpsTest(BaseFFTOpsTest): "Input dimension .* must have length of at least .* but got: 3"): x = np.zeros((3,) * rank).astype(np.complex64) fft_length = [6] * rank - with self.test_session(): + with self.cached_session(): irfft_fn(x, fft_length).eval() def testGrad_Simple(self): diff --git a/tensorflow/python/kernel_tests/fifo_queue_test.py b/tensorflow/python/kernel_tests/fifo_queue_test.py index 9e7b528338..a5f8f64e0c 100644 --- a/tensorflow/python/kernel_tests/fifo_queue_test.py +++ b/tensorflow/python/kernel_tests/fifo_queue_test.py @@ -99,19 +99,19 @@ class FIFOQueueTest(test.TestCase): """, q.queue_ref.op.node_def) def testEnqueue(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.FIFOQueue(10, dtypes_lib.float32) enqueue_op = q.enqueue((10.0,)) enqueue_op.run() def testEnqueueHalf(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.FIFOQueue(10, dtypes_lib.float16) enqueue_op = q.enqueue((10.0,)) enqueue_op.run() def testEnqueueWithShape(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.FIFOQueue(10, dtypes_lib.float32, shapes=(3, 2)) enqueue_correct_op = q.enqueue(([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]],)) enqueue_correct_op.run() @@ -120,7 +120,7 @@ class FIFOQueueTest(test.TestCase): self.assertEqual(1, q.size().eval()) def testEnqueueManyWithShape(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.FIFOQueue( 10, [dtypes_lib.int32, dtypes_lib.int32], shapes=[(), (2,)]) q.enqueue_many([[1, 2, 3, 4], [[1, 1], [2, 2], [3, 3], [4, 4]]]).run() @@ -143,7 +143,7 @@ class FIFOQueueTest(test.TestCase): self.assertAllEqual(self.evaluate(q.dequeue()), 1) def testEnqueueDictWithoutNames(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.FIFOQueue(10, dtypes_lib.float32) with self.assertRaisesRegexp(ValueError, "must have names"): q.enqueue({"a": 12.0}) @@ -151,7 +151,7 @@ class FIFOQueueTest(test.TestCase): q.enqueue_many({"a": [12.0, 13.0]}) def testParallelEnqueue(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(10, dtypes_lib.float32) elems = [10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0, 100.0] enqueue_ops = [q.enqueue((x,)) for x in elems] @@ -177,7 +177,7 @@ class FIFOQueueTest(test.TestCase): self.assertItemsEqual(elems, results) def testParallelDequeue(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(10, dtypes_lib.float32) elems = [10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0, 100.0] enqueue_ops = [q.enqueue((x,)) for x in elems] @@ -201,7 +201,7 @@ class FIFOQueueTest(test.TestCase): self.assertItemsEqual(elems, results) def testDequeue(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.FIFOQueue(10, dtypes_lib.float32) elems = [10.0, 20.0, 30.0] enqueue_ops = [q.enqueue((x,)) for x in elems] @@ -215,7 +215,7 @@ class FIFOQueueTest(test.TestCase): self.assertEqual([elems[i]], vals) def testDequeueHalf(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.FIFOQueue(10, dtypes_lib.float16) elems = [10.0, 20.0, 30.0] enqueue_ops = [q.enqueue((x,)) for x in elems] @@ -229,7 +229,7 @@ class FIFOQueueTest(test.TestCase): self.assertEqual([elems[i]], vals) def testEnqueueAndBlockingDequeue(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(3, dtypes_lib.float32) elems = [10.0, 20.0, 30.0] enqueue_ops = [q.enqueue((x,)) for x in elems] @@ -259,7 +259,7 @@ class FIFOQueueTest(test.TestCase): self.assertEqual([elem], result) def testMultiEnqueueAndDequeue(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(10, (dtypes_lib.int32, dtypes_lib.float32)) elems = [(5, 10.0), (10, 20.0), (15, 30.0)] enqueue_ops = [q.enqueue((x, y)) for x, y in elems] @@ -275,12 +275,12 @@ class FIFOQueueTest(test.TestCase): self.assertEqual([y], y_val) def testQueueSizeEmpty(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.FIFOQueue(10, dtypes_lib.float32) self.assertEqual([0], q.size().eval()) def testQueueSizeAfterEnqueueAndDequeue(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.FIFOQueue(10, dtypes_lib.float32) enqueue_op = q.enqueue((10.0,)) dequeued_t = q.dequeue() @@ -293,7 +293,7 @@ class FIFOQueueTest(test.TestCase): self.assertEqual(0, size.eval()) def testEnqueueMany(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.FIFOQueue(10, dtypes_lib.float32) elems = [10.0, 20.0, 30.0, 40.0] enqueue_op = q.enqueue_many((elems,)) @@ -306,7 +306,7 @@ class FIFOQueueTest(test.TestCase): self.assertEqual([elems[i % 4]], vals) def testEmptyEnqueueMany(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.FIFOQueue(10, dtypes_lib.float32) empty_t = constant_op.constant( [], dtype=dtypes_lib.float32, shape=[0, 2, 3]) @@ -318,7 +318,7 @@ class FIFOQueueTest(test.TestCase): self.assertEqual([0], size_t.eval()) def testEmptyDequeueMany(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.FIFOQueue(10, dtypes_lib.float32, shapes=()) enqueue_op = q.enqueue((10.0,)) dequeued_t = q.dequeue_many(0) @@ -328,7 +328,7 @@ class FIFOQueueTest(test.TestCase): self.assertEqual([], dequeued_t.eval().tolist()) def testEmptyDequeueUpTo(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.FIFOQueue(10, dtypes_lib.float32, shapes=()) enqueue_op = q.enqueue((10.0,)) dequeued_t = q.dequeue_up_to(0) @@ -338,14 +338,14 @@ class FIFOQueueTest(test.TestCase): self.assertEqual([], dequeued_t.eval().tolist()) def testEmptyDequeueManyWithNoShape(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.FIFOQueue(10, dtypes_lib.float32) # Expect the operation to fail due to the shape not being constrained. with self.assertRaisesOpError("specified shapes"): q.dequeue_many(0).eval() def testMultiEnqueueMany(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(10, (dtypes_lib.float32, dtypes_lib.int32)) float_elems = [10.0, 20.0, 30.0, 40.0] int_elems = [[1, 2], [3, 4], [5, 6], [7, 8]] @@ -361,7 +361,7 @@ class FIFOQueueTest(test.TestCase): self.assertAllEqual(int_elems[i % 4], int_val) def testDequeueMany(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.FIFOQueue(10, dtypes_lib.float32, ()) elems = [10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0, 100.0] enqueue_op = q.enqueue_many((elems,)) @@ -373,7 +373,7 @@ class FIFOQueueTest(test.TestCase): self.assertAllEqual(elems[4:8], dequeued_t.eval()) def testDequeueUpToNoBlocking(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.FIFOQueue(10, dtypes_lib.float32, ()) elems = [10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0, 100.0] enqueue_op = q.enqueue_many((elems,)) @@ -385,7 +385,7 @@ class FIFOQueueTest(test.TestCase): self.assertAllEqual(elems[4:8], dequeued_t.eval()) def testMultiDequeueMany(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue( 10, (dtypes_lib.float32, dtypes_lib.int32), shapes=((), (2,))) float_elems = [ @@ -416,7 +416,7 @@ class FIFOQueueTest(test.TestCase): self.assertEqual(int_val.shape, dequeued_single_t[1].get_shape()) def testMultiDequeueUpToNoBlocking(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue( 10, (dtypes_lib.float32, dtypes_lib.int32), shapes=((), (2,))) float_elems = [ @@ -440,7 +440,7 @@ class FIFOQueueTest(test.TestCase): self.assertAllEqual(int_elems[4:8], int_val) def testHighDimension(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.FIFOQueue(10, dtypes_lib.int32, (4, 4, 4, 4)) elems = np.array([[[[[x] * 4] * 4] * 4] * 4 for x in range(10)], np.int32) enqueue_op = q.enqueue_many((elems,)) @@ -494,7 +494,7 @@ class FIFOQueueTest(test.TestCase): array_ops.placeholder(dtypes_lib.int32))) def testEnqueueWrongShapeAtRuntime(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(10, (dtypes_lib.int32, dtypes_lib.int32), ( (2, 2), (3, 3))) elems_ok = np.array([1] * 4).reshape((2, 2)).astype(np.int32) @@ -506,7 +506,7 @@ class FIFOQueueTest(test.TestCase): feed_dict={elems_bad: np.array([1] * 12).reshape((3, 4))}) def testEnqueueDequeueManyWrongShape(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(10, (dtypes_lib.int32, dtypes_lib.int32), ( (2, 2), (3, 3))) elems_ok = np.array([1] * 8).reshape((2, 2, 2)).astype(np.int32) @@ -521,7 +521,7 @@ class FIFOQueueTest(test.TestCase): dequeued_t.eval() def testParallelEnqueueMany(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(1000, dtypes_lib.float32, shapes=()) elems = [10.0 * x for x in range(100)] enqueue_op = q.enqueue_many((elems,)) @@ -540,7 +540,7 @@ class FIFOQueueTest(test.TestCase): self.assertItemsEqual(dequeued_t.eval(), elems * 10) def testParallelDequeueMany(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(1000, dtypes_lib.float32, shapes=()) elems = [10.0 * x for x in range(1000)] enqueue_op = q.enqueue_many((elems,)) @@ -562,7 +562,7 @@ class FIFOQueueTest(test.TestCase): self.assertItemsEqual(elems, dequeued_elems) def testParallelDequeueUpTo(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(1000, dtypes_lib.float32, shapes=()) elems = [10.0 * x for x in range(1000)] enqueue_op = q.enqueue_many((elems,)) @@ -586,7 +586,7 @@ class FIFOQueueTest(test.TestCase): self.assertItemsEqual(elems, dequeued_elems) def testParallelEnqueueAndDequeue(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(50, dtypes_lib.float32, shapes=()) initial_elements = [10.0] * 49 q.enqueue_many((initial_elements,)).run() @@ -619,7 +619,7 @@ class FIFOQueueTest(test.TestCase): self.assertTrue(elem in (10.0, 20.0)) def testMixtureOfEnqueueAndEnqueueMany(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(10, dtypes_lib.int32, shapes=()) enqueue_placeholder = array_ops.placeholder(dtypes_lib.int32, shape=()) enqueue_op = q.enqueue((enqueue_placeholder,)) @@ -655,7 +655,7 @@ class FIFOQueueTest(test.TestCase): self.assertEqual(0, q.size().eval()) def testMixtureOfDequeueAndDequeueMany(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(10, dtypes_lib.int32, shapes=()) enqueue_op = q.enqueue_many((np.arange(250, dtype=np.int32),)) dequeued_t = q.dequeue() @@ -689,7 +689,7 @@ class FIFOQueueTest(test.TestCase): self.assertEqual(0, q.size().eval()) def testBlockingDequeueMany(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(10, dtypes_lib.float32, ()) elems = [10.0, 20.0, 30.0, 40.0] enqueue_op = q.enqueue_many((elems,)) @@ -716,7 +716,7 @@ class FIFOQueueTest(test.TestCase): self.assertAllEqual(elems, dequeued_elems) def testBlockingDequeueUpTo(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(10, dtypes_lib.float32, ()) elems = [10.0, 20.0, 30.0, 40.0] enqueue_op = q.enqueue_many((elems,)) @@ -743,7 +743,7 @@ class FIFOQueueTest(test.TestCase): self.assertAllEqual(elems, dequeued_elems) def testDequeueManyWithTensorParameter(self): - with self.test_session(): + with self.cached_session(): # Define a first queue that contains integer counts. dequeue_counts = [random.randint(1, 10) for _ in range(100)] count_q = data_flow_ops.FIFOQueue(100, dtypes_lib.int32, ()) @@ -768,7 +768,7 @@ class FIFOQueueTest(test.TestCase): self.assertEqual(elems, dequeued_elems) def testDequeueFromClosedQueue(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.FIFOQueue(10, dtypes_lib.float32) elems = [10.0, 20.0, 30.0, 40.0] enqueue_op = q.enqueue_many((elems,)) @@ -786,7 +786,7 @@ class FIFOQueueTest(test.TestCase): dequeued_t.eval() def testBlockingDequeueFromClosedQueue(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(10, dtypes_lib.float32) elems = [10.0, 20.0, 30.0, 40.0] enqueue_op = q.enqueue_many((elems,)) @@ -812,7 +812,7 @@ class FIFOQueueTest(test.TestCase): dequeue_thread.join() def testBlockingDequeueFromClosedEmptyQueue(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(10, dtypes_lib.float32) close_op = q.close() dequeued_t = q.dequeue() @@ -832,7 +832,7 @@ class FIFOQueueTest(test.TestCase): dequeue_thread.join() def testBlockingDequeueManyFromClosedQueue(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(10, dtypes_lib.float32, ()) elems = [10.0, 20.0, 30.0, 40.0] enqueue_op = q.enqueue_many((elems,)) @@ -857,7 +857,7 @@ class FIFOQueueTest(test.TestCase): dequeue_thread.join() def testBlockingDequeueManyButNotAllFromClosedQueue(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(10, dtypes_lib.float32, ()) elems = [10.0, 20.0, 30.0, 40.0] enqueue_op = q.enqueue_many((elems,)) @@ -882,7 +882,7 @@ class FIFOQueueTest(test.TestCase): dequeue_thread.join() def testDequeueUpToFromClosedQueueReturnsRemainder(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(10, dtypes_lib.float32, ()) elems = [10.0, 20.0, 30.0, 40.0] enqueue_op = q.enqueue_many((elems,)) @@ -904,7 +904,7 @@ class FIFOQueueTest(test.TestCase): dequeue_thread.join() def testEnqueueManyLargerThanCapacityWithConcurrentDequeueMany(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(4, dtypes_lib.float32, ()) elems = [10.0, 20.0, 30.0, 40.0] enqueue_op = q.enqueue_many((elems,)) @@ -941,7 +941,7 @@ class FIFOQueueTest(test.TestCase): close_thread.join() def testClosedBlockingDequeueManyRestoresPartialBatch(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(4, (dtypes_lib.float32, dtypes_lib.float32), ( (), ())) elems_a = [1.0, 2.0, 3.0] @@ -974,7 +974,7 @@ class FIFOQueueTest(test.TestCase): self.assertEqual(0, q.size().eval()) def testBlockingDequeueManyFromClosedEmptyQueue(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(10, dtypes_lib.float32, ()) close_op = q.close() dequeued_t = q.dequeue_many(4) @@ -994,7 +994,7 @@ class FIFOQueueTest(test.TestCase): dequeue_thread.join() def testBlockingDequeueUpToFromClosedEmptyQueue(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(10, dtypes_lib.float32, ()) close_op = q.close() dequeued_t = q.dequeue_up_to(4) @@ -1014,7 +1014,7 @@ class FIFOQueueTest(test.TestCase): dequeue_thread.join() def testEnqueueToClosedQueue(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.FIFOQueue(10, dtypes_lib.float32) enqueue_op = q.enqueue((10.0,)) close_op = q.close() @@ -1027,7 +1027,7 @@ class FIFOQueueTest(test.TestCase): enqueue_op.run() def testEnqueueManyToClosedQueue(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.FIFOQueue(10, dtypes_lib.float32) elems = [10.0, 20.0, 30.0, 40.0] enqueue_op = q.enqueue_many((elems,)) @@ -1041,7 +1041,7 @@ class FIFOQueueTest(test.TestCase): enqueue_op.run() def testBlockingEnqueueToFullQueue(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(4, dtypes_lib.float32) elems = [10.0, 20.0, 30.0, 40.0] enqueue_op = q.enqueue_many((elems,)) @@ -1064,7 +1064,7 @@ class FIFOQueueTest(test.TestCase): thread.join() def testBlockingEnqueueManyToFullQueue(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(4, dtypes_lib.float32) elems = [10.0, 20.0, 30.0, 40.0] enqueue_op = q.enqueue_many((elems,)) @@ -1091,7 +1091,7 @@ class FIFOQueueTest(test.TestCase): thread.join() def testBlockingEnqueueBeforeClose(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(4, dtypes_lib.float32) elems = [10.0, 20.0, 30.0, 40.0] enqueue_op = q.enqueue_many((elems,)) @@ -1128,7 +1128,7 @@ class FIFOQueueTest(test.TestCase): self.assertEqual(0, q.size().eval()) def testBlockingEnqueueManyBeforeClose(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(4, dtypes_lib.float32) elems = [10.0, 20.0, 30.0] enqueue_op = q.enqueue_many((elems,)) @@ -1161,7 +1161,7 @@ class FIFOQueueTest(test.TestCase): self.assertEqual(elem, dequeued_t.eval()) def testDoesNotLoseValue(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.FIFOQueue(1, dtypes_lib.float32) enqueue_op = q.enqueue((10.0,)) size_t = q.size() @@ -1171,7 +1171,7 @@ class FIFOQueueTest(test.TestCase): self.assertEqual(size_t.eval(), [1]) def testSharedQueueSameSession(self): - with self.test_session(): + with self.cached_session(): q1 = data_flow_ops.FIFOQueue( 1, dtypes_lib.float32, shared_name="shared_queue") q1.enqueue((10.0,)).run() @@ -1201,7 +1201,7 @@ class FIFOQueueTest(test.TestCase): self.assertEqual(q2_size_t.eval(), [0]) def testIncompatibleSharedQueueErrors(self): - with self.test_session(): + with self.cached_session(): q_a_1 = data_flow_ops.FIFOQueue(10, dtypes_lib.float32, shared_name="q_a") q_a_2 = data_flow_ops.FIFOQueue(15, dtypes_lib.float32, shared_name="q_a") q_a_1.queue_ref.op.run() @@ -1244,7 +1244,7 @@ class FIFOQueueTest(test.TestCase): q_f_2.queue_ref.op.run() def testSelectQueue(self): - with self.test_session(): + with self.cached_session(): num_queues = 10 qlist = list() for _ in xrange(num_queues): @@ -1257,7 +1257,7 @@ class FIFOQueueTest(test.TestCase): self.assertEqual(q.dequeue().eval(), 10.0) def testSelectQueueOutOfRange(self): - with self.test_session(): + with self.cached_session(): q1 = data_flow_ops.FIFOQueue(10, dtypes_lib.float32) q2 = data_flow_ops.FIFOQueue(15, dtypes_lib.float32) enq_q = data_flow_ops.FIFOQueue.from_list(3, [q1, q2]) @@ -1281,7 +1281,7 @@ class FIFOQueueTest(test.TestCase): sess.run(enqueue_many_op) def testResetOfBlockingOperation(self): - with self.test_session() as sess: + with self.cached_session() as sess: q_empty = data_flow_ops.FIFOQueue(5, dtypes_lib.float32, ()) dequeue_op = q_empty.dequeue() dequeue_many_op = q_empty.dequeue_many(1) @@ -1309,7 +1309,7 @@ class FIFOQueueTest(test.TestCase): t.join() def testBigEnqueueMany(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(5, dtypes_lib.int32, ((),)) elem = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] enq = q.enqueue_many((elem,)) @@ -1354,7 +1354,7 @@ class FIFOQueueTest(test.TestCase): self.assertAllEqual(elem, results) def testBigDequeueMany(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(2, dtypes_lib.int32, ((),)) elem = np.arange(4, dtype=np.int32) enq_list = [q.enqueue((e,)) for e in elem] @@ -1380,7 +1380,7 @@ class FIFOQueueTest(test.TestCase): self.assertAllEqual(elem, results) def testDtypes(self): - with self.test_session() as sess: + with self.cached_session() as sess: dtypes = [ dtypes_lib.float32, dtypes_lib.float64, dtypes_lib.int32, dtypes_lib.uint8, dtypes_lib.int16, dtypes_lib.int8, dtypes_lib.int64, @@ -1411,7 +1411,7 @@ class FIFOQueueTest(test.TestCase): self.assertAllEqual(input_elem, output_elem) def testDequeueEnqueueFail(self): - with self.test_session() as session: + with self.cached_session() as session: q = data_flow_ops.FIFOQueue(10, [dtypes_lib.int32], shapes=[()]) a = q.dequeue() b = control_flow_ops.Assert(False, ["Before enqueue"]) @@ -1474,7 +1474,7 @@ class FIFOQueueDictTest(test.TestCase): self.assertEqual(["i", "f"], q.names) def testEnqueueDequeueOneComponent(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue( 10, dtypes_lib.float32, shapes=((),), names="f") # Verify that enqueue() checks that when using names we must enqueue a @@ -1519,7 +1519,7 @@ class FIFOQueueDictTest(test.TestCase): self.assertEqual([40.0, 50.0], list(f)) def testEnqueueDequeueMultipleComponent(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue( 10, (dtypes_lib.float32, dtypes_lib.int32, dtypes_lib.string), shapes=((), (), ()), @@ -1600,7 +1600,7 @@ class FIFOQueueWithTimeoutTest(test.TestCase): sess.run(dequeued_t) def testReusableAfterTimeout(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.FIFOQueue(10, dtypes_lib.float32) dequeued_t = q.dequeue() enqueue_op = q.enqueue(37) diff --git a/tensorflow/python/kernel_tests/fractional_avg_pool_op_test.py b/tensorflow/python/kernel_tests/fractional_avg_pool_op_test.py index faac7d8365..f89d2062f1 100644 --- a/tensorflow/python/kernel_tests/fractional_avg_pool_op_test.py +++ b/tensorflow/python/kernel_tests/fractional_avg_pool_op_test.py @@ -127,7 +127,7 @@ class FractionalAvgTest(test.TestCase): Returns: None """ - with self.test_session() as sess: + with self.cached_session() as sess: p, r, c = nn_ops.fractional_avg_pool( input_tensor, pooling_ratio, @@ -160,7 +160,7 @@ class FractionalAvgTest(test.TestCase): overlapping)) rand_mat = self._PRNG.randint(10, size=tensor_shape) pooling_ratio = [1, math.sqrt(2), math.sqrt(2), 1] - with self.test_session() as sess: + with self.cached_session() as sess: p, r, c = nn_ops.fractional_avg_pool( rand_mat.astype(np.float32), pooling_ratio, @@ -234,7 +234,7 @@ class FractionalAvgTest(test.TestCase): [4, 4, 5, 9, 7, 2] ]) # pyformat: enable - with self.test_session() as sess: + with self.cached_session() as sess: # Since deterministic = True, seed and seed2 are fixed. Therefore r, and c # are the same each time. We can have an expected result precomputed. # r = [0, 2, 4, 6] @@ -314,7 +314,7 @@ class FractionalAvgTest(test.TestCase): def testDifferentInputTensorShape(self): """Runs the operation in one session with different input tensor shapes.""" - with self.test_session() as sess: + with self.cached_session() as sess: input_holder = array_ops.placeholder(dtypes.float32, [None, None, None, 3]) pooling_ratio = [1, 1.5, 1.5, 1] @@ -389,7 +389,7 @@ class FractionalAvgPoolGradTest(test.TestCase): num_cols = col_window_size * 7 for num_channels in [1, 2]: input_shape = (num_batches, num_rows, num_cols, num_channels) - with self.test_session() as _: + with self.cached_session() as _: input_tensor = constant_op.constant( self._GenerateRandomInputTensor(input_shape).astype( np.float32)) @@ -428,7 +428,7 @@ class FractionalAvgPoolGradTest(test.TestCase): num_cols = (col_window_size - 1) * 7 + 1 for num_channels in [1, 2]: input_shape = (num_batches, num_rows, num_cols, num_channels) - with self.test_session() as _: + with self.cached_session() as _: input_tensor = constant_op.constant( self._GenerateRandomInputTensor(input_shape).astype( np.float32)) @@ -468,7 +468,7 @@ class FractionalAvgPoolGradTest(test.TestCase): for pseudo_random in True, False: for overlapping in True, False: - with self.test_session() as _: + with self.cached_session() as _: input_tensor = constant_op.constant(input_data, shape=input_shape) output_tensor, unused_a, unused_b = nn_ops.fractional_avg_pool( input_tensor, @@ -501,7 +501,7 @@ class FractionalAvgPoolGradTest(test.TestCase): for num_channels in [1, 3]: input_shape = (num_batches, num_rows, num_cols, num_channels) input_data = self._GenerateRandomInputTensor(input_shape) - with self.test_session() as _: + with self.cached_session() as _: input_tensor = constant_op.constant(input_data, shape=input_shape) output_tensor, unused_a, unused_b = nn_ops.fractional_avg_pool( input_tensor, @@ -532,7 +532,7 @@ class FractionalAvgPoolGradTest(test.TestCase): overlapping = True pseudo_random = False - with self.test_session() as _: + with self.cached_session() as _: input_tensor = constant_op.constant(input_data, shape=input_shape) output_tensor, unused_a, unused_b = nn_ops.fractional_avg_pool( input_tensor, diff --git a/tensorflow/python/kernel_tests/fractional_max_pool_op_test.py b/tensorflow/python/kernel_tests/fractional_max_pool_op_test.py index 6477c9ebc4..9b94ca8554 100644 --- a/tensorflow/python/kernel_tests/fractional_max_pool_op_test.py +++ b/tensorflow/python/kernel_tests/fractional_max_pool_op_test.py @@ -127,7 +127,7 @@ class FractionalMaxPoolTest(test.TestCase): Returns: None """ - with self.test_session() as sess: + with self.cached_session() as sess: p, r, c = nn_ops.fractional_max_pool( input_tensor, pooling_ratio, @@ -160,7 +160,7 @@ class FractionalMaxPoolTest(test.TestCase): overlapping)) rand_mat = self._PRNG.randint(10, size=tensor_shape) pooling_ratio = [1, math.sqrt(2), math.sqrt(2), 1] - with self.test_session() as sess: + with self.cached_session() as sess: p, r, c = nn_ops.fractional_max_pool( rand_mat, pooling_ratio, @@ -285,7 +285,7 @@ class FractionalMaxPoolTest(test.TestCase): def testDifferentInputTensorShape(self): """Runs the operation in one session with different input tensor shapes.""" - with self.test_session() as sess: + with self.cached_session() as sess: input_holder = array_ops.placeholder(dtypes.float32, [None, None, None, 3]) pooling_ratio = [1, 1.5, 1.5, 1] @@ -374,7 +374,7 @@ class FractionalMaxPoolGradTest(test.TestCase): num_cols = col_window_size * 7 for num_channels in [1, 2]: input_shape = (num_batches, num_rows, num_cols, num_channels) - with self.test_session() as _: + with self.cached_session() as _: input_tensor = constant_op.constant( self._GenerateUniqueRandomInputTensor(input_shape)) window_size = [1, row_window_size, col_window_size, 1] @@ -409,7 +409,7 @@ class FractionalMaxPoolGradTest(test.TestCase): num_cols = (col_window_size - 1) * 7 + 1 for num_channels in [1, 2]: input_shape = (num_batches, num_rows, num_cols, num_channels) - with self.test_session() as _: + with self.cached_session() as _: input_tensor = constant_op.constant( self._GenerateUniqueRandomInputTensor(input_shape)) window_size = [1, row_window_size, col_window_size, 1] @@ -447,7 +447,7 @@ class FractionalMaxPoolGradTest(test.TestCase): for pseudo_random in True, False: for overlapping in True, False: - with self.test_session() as _: + with self.cached_session() as _: input_tensor = constant_op.constant(input_data, shape=input_shape) output_tensor, unused_a, unused_b = nn_ops.fractional_max_pool( input_tensor, @@ -482,7 +482,7 @@ class FractionalMaxPoolGradTest(test.TestCase): input_data = self._GenerateUniqueRandomInputTensor(input_shape) # Add some randomness to make input_data not so 'integer' input_data += self._PRNG.random_sample(input_shape) - with self.test_session() as _: + with self.cached_session() as _: input_tensor = constant_op.constant(input_data, shape=input_shape) output_tensor, unused_a, unused_b = nn_ops.fractional_max_pool( input_tensor, @@ -515,7 +515,7 @@ class FractionalMaxPoolGradTest(test.TestCase): overlapping = True pseudo_random = False - with self.test_session() as _: + with self.cached_session() as _: input_tensor = constant_op.constant(input_data, shape=input_shape) output_tensor, unused_a, unused_b = nn_ops.fractional_max_pool( input_tensor, @@ -579,7 +579,7 @@ class FractionalMaxPoolGradTest(test.TestCase): 0.0, 0.0, 0.0, 0.0, 6.0, 0.0, 21.0, 0.0], input_size) # pyformat: disable - with self.test_session() as _: + with self.cached_session() as _: # Test when overlapping is False input_tensor = constant_op.constant(input_data, shape=input_size) output_tensor = constant_op.constant( diff --git a/tensorflow/python/kernel_tests/functional_ops_test.py b/tensorflow/python/kernel_tests/functional_ops_test.py index e39daf1371..30d11852c7 100644 --- a/tensorflow/python/kernel_tests/functional_ops_test.py +++ b/tensorflow/python/kernel_tests/functional_ops_test.py @@ -735,7 +735,7 @@ class FunctionalOpsTest(test.TestCase): def Run(sess, n): return sess.run(functional_ops.While([n, 0.], Cond, Body))[1] - with self.test_session(graph=g, use_gpu=use_gpu) as sess: + with self.session(graph=g, use_gpu=use_gpu) as sess: self.assertAllEqual(Run(sess, 20.), 210.) self.assertAllEqual(Run(sess, 100.), 5050.) @@ -765,7 +765,7 @@ class FunctionalOpsTest(test.TestCase): fetch = outputs[1] else: fetch = "my_while:1" - with self.test_session(graph=g, use_gpu=use_gpu) as sess: + with self.session(graph=g, use_gpu=use_gpu) as sess: return sess.run(fetch) self.assertAllEqual(Run(20., False), 210.) @@ -793,7 +793,7 @@ class FunctionalOpsTest(test.TestCase): def BodyReturnsTooManyArgs(n, x): return n - 1, x + n, x - with self.test_session(graph=g, use_gpu=use_gpu): + with self.session(graph=g, use_gpu=use_gpu): with self.assertRaisesRegexp( errors.InvalidArgumentError, "Expected a single scalar.*got 2 tensors."): @@ -818,7 +818,7 @@ class FunctionalOpsTest(test.TestCase): def Body(n, x): return n - 1, x + n - with self.test_session(graph=g, use_gpu=use_gpu) as sess: + with self.session(graph=g, use_gpu=use_gpu) as sess: n = array_ops.placeholder(dtypes.float32) _, result = functional_ops.While([n, 0.], Cond, Body) c = constant_op.constant(37.) @@ -831,7 +831,7 @@ class FunctionalOpsTest(test.TestCase): def _tfSum(self, use_gpu, rewrite_with_while): with ops.Graph().as_default() as g: - with self.test_session(graph=g, use_gpu=use_gpu) as sess: + with self.session(graph=g, use_gpu=use_gpu) as sess: @function.Defun(dtypes.int32, dtypes.float32) def Body(n, x): diff --git a/tensorflow/python/kernel_tests/gather_op_test.py b/tensorflow/python/kernel_tests/gather_op_test.py index 033fa95935..85bf969068 100644 --- a/tensorflow/python/kernel_tests/gather_op_test.py +++ b/tensorflow/python/kernel_tests/gather_op_test.py @@ -147,7 +147,7 @@ class GatherTest(test.TestCase): def testString(self): params = np.array([[b"asdf", b"zxcv"], [b"qwer", b"uiop"]]) - with self.test_session(): + with self.cached_session(): self.assertAllEqual([b"qwer", b"uiop"], array_ops.gather(params, 1, axis=0).eval()) self.assertAllEqual([b"asdf", b"qwer"], @@ -157,7 +157,7 @@ class GatherTest(test.TestCase): for unsigned_type in (dtypes.uint32, dtypes.uint64): params = self._buildParams( np.array([[1, 2, 3], [7, 8, 9]]), unsigned_type) - with self.test_session(): + with self.cached_session(): self.assertAllEqual([7, 8, 9], array_ops.gather(params, 1, axis=0).eval()) self.assertAllEqual([1, 7], array_ops.gather(params, 0, axis=1).eval()) diff --git a/tensorflow/python/kernel_tests/gradient_correctness_test.py b/tensorflow/python/kernel_tests/gradient_correctness_test.py index e93c6235f7..291a69ebac 100644 --- a/tensorflow/python/kernel_tests/gradient_correctness_test.py +++ b/tensorflow/python/kernel_tests/gradient_correctness_test.py @@ -30,7 +30,7 @@ from tensorflow.python.platform import test class GradientCorrectnessTest(test.TestCase): def testMultipleOutputChainedGradients(self): - with self.test_session() as sess: + with self.cached_session() as sess: x = constant_op.constant(1.0, dtype=dtypes.float32) yexp = math_ops.exp(x) yexplog = math_ops.log(yexp) @@ -43,13 +43,13 @@ class GradientCorrectnessTest(test.TestCase): def testIdentityGradient(self): x = constant_op.constant(3.) dx_dx, = gradients_impl.gradients(x, x) - with self.test_session() as sess: + with self.cached_session() as sess: self.assertAllClose(1., sess.run(dx_dx)) def testIntegerIdentityGradient(self): x = constant_op.constant(3) dx_dx, = gradients_impl.gradients(x, x) - with self.test_session() as sess: + with self.cached_session() as sess: self.assertAllClose(1, sess.run(dx_dx)) def testGradientWithIntegerPath(self): @@ -57,7 +57,7 @@ class GradientCorrectnessTest(test.TestCase): k = math_ops.to_float(math_ops.to_int32(x)) y = x * k dy_dx, = gradients_impl.gradients(y, x) - with self.test_session() as sess: + with self.cached_session() as sess: self.assertAllClose([3., 4.], sess.run(dy_dx)) def testNoIntegerGradient1(self): diff --git a/tensorflow/python/kernel_tests/identity_n_op_py_test.py b/tensorflow/python/kernel_tests/identity_n_op_py_test.py index 408b173981..518733cd8e 100644 --- a/tensorflow/python/kernel_tests/identity_n_op_py_test.py +++ b/tensorflow/python/kernel_tests/identity_n_op_py_test.py @@ -28,7 +28,7 @@ from tensorflow.python.platform import test class IdentityNOpTest(test.TestCase): def testInt32String_6(self): - with self.test_session() as sess: + with self.cached_session() as sess: [value0, value1] = sess.run( array_ops.identity_n([[1, 2, 3, 4, 5, 6], [b"a", b"b", b"C", b"d", b"E", b"f", b"g"]])) @@ -37,7 +37,7 @@ class IdentityNOpTest(test.TestCase): np.array([b"a", b"b", b"C", b"d", b"E", b"f", b"g"]), value1) def testInt32_shapes(self): - with self.test_session() as sess: + with self.cached_session() as sess: inp0 = constant_op.constant([10, 20, 30, 40, 50, 60], shape=[2, 3]) inp1 = constant_op.constant([11, 21, 31, 41, 51, 61], shape=[3, 2]) inp2 = constant_op.constant( @@ -52,12 +52,12 @@ class IdentityNOpTest(test.TestCase): def testString(self): source = [b"A", b"b", b"C", b"d", b"E", b"f"] - with self.test_session() as sess: + with self.cached_session() as sess: [value] = sess.run(array_ops.identity_n([source])) self.assertAllEqual(source, value) def testIdentityShape(self): - with self.test_session(): + with self.cached_session(): shape = [2, 3] array_2x3 = [[1, 2, 3], [6, 5, 4]] tensor = constant_op.constant(array_2x3) diff --git a/tensorflow/python/kernel_tests/identity_op_py_test.py b/tensorflow/python/kernel_tests/identity_op_py_test.py index 49fb76d5b4..37f9f716f8 100644 --- a/tensorflow/python/kernel_tests/identity_op_py_test.py +++ b/tensorflow/python/kernel_tests/identity_op_py_test.py @@ -31,24 +31,24 @@ from tensorflow.python.platform import test class IdentityOpTest(test.TestCase): def testInt32_6(self): - with self.test_session(): + with self.cached_session(): value = array_ops.identity([1, 2, 3, 4, 5, 6]).eval() self.assertAllEqual(np.array([1, 2, 3, 4, 5, 6]), value) def testInt32_2_3(self): - with self.test_session(): + with self.cached_session(): inp = constant_op.constant([10, 20, 30, 40, 50, 60], shape=[2, 3]) value = array_ops.identity(inp).eval() self.assertAllEqual(np.array([[10, 20, 30], [40, 50, 60]]), value) def testString(self): source = [b"A", b"b", b"C", b"d", b"E", b"f"] - with self.test_session(): + with self.cached_session(): value = array_ops.identity(source).eval() self.assertAllEqual(source, value) def testIdentityShape(self): - with self.test_session(): + with self.cached_session(): shape = [2, 3] array_2x3 = [[1, 2, 3], [6, 5, 4]] tensor = constant_op.constant(array_2x3) @@ -59,7 +59,7 @@ class IdentityOpTest(test.TestCase): array_ops.identity(np.array(array_2x3)).get_shape()) def testRefIdentityShape(self): - with self.test_session(): + with self.cached_session(): shape = [2, 3] tensor = variables.Variable( constant_op.constant( diff --git a/tensorflow/python/kernel_tests/in_topk_op_test.py b/tensorflow/python/kernel_tests/in_topk_op_test.py index fafeea8ec0..6fdb497bc6 100644 --- a/tensorflow/python/kernel_tests/in_topk_op_test.py +++ b/tensorflow/python/kernel_tests/in_topk_op_test.py @@ -30,7 +30,7 @@ class InTopKTest(test.TestCase): def _validateInTopK(self, predictions, target, k, expected): np_ans = np.array(expected) - with self.test_session(): + with self.cached_session(): precision = nn_ops.in_top_k(predictions, target, k) out = precision.eval() self.assertAllClose(np_ans, out) @@ -65,7 +65,7 @@ class InTopKTest(test.TestCase): def testBadTarget(self): predictions = [[0.1, 0.3, 0.2, 0.4], [0.1, 0.2, 0.3, 0.4]] target = [0, 80000] - with self.test_session(): + with self.cached_session(): with self.assertRaisesRegexp(errors_impl.InvalidArgumentError, "target.*out of range"): nn_ops.in_top_k(predictions, target, 2).eval() @@ -75,7 +75,7 @@ class InTopKTest(test.TestCase): target = [0, 2] k = constant_op.constant(3) np_ans = np.array([False, True]) - with self.test_session(): + with self.cached_session(): precision = nn_ops.in_top_k(predictions, target, k) out = precision.eval() self.assertAllClose(np_ans, out) diff --git a/tensorflow/python/kernel_tests/init_ops_test.py b/tensorflow/python/kernel_tests/init_ops_test.py index f6097ad489..292679e4b9 100644 --- a/tensorflow/python/kernel_tests/init_ops_test.py +++ b/tensorflow/python/kernel_tests/init_ops_test.py @@ -343,7 +343,7 @@ class UniformUnitScalingInitializationTest(test.TestCase): def testZeroSize(self): shape = [0, 2] - with self.test_session(): + with self.cached_session(): x = variable_scope.get_variable( "x", shape=shape, @@ -522,7 +522,7 @@ class LinSpaceTest(test.TestCase): def _LinSpace(self, start, stop, num): # NOTE(touts): Needs to pass a graph to get a new session each time. with ops.Graph().as_default() as graph: - with self.test_session(graph=graph, force_gpu=self.force_gpu): + with self.session(graph=graph, force_gpu=self.force_gpu): tf_ans = math_ops.linspace(start, stop, num, name="linspace") self.assertEqual([num], tf_ans.get_shape()) return tf_ans.eval() @@ -606,7 +606,7 @@ class OrthogonalInitializerTest(test.TestCase): def testInvalidShape(self): init1 = init_ops.orthogonal_initializer() - with self.test_session(graph=ops.Graph(), use_gpu=True): + with self.session(graph=ops.Graph(), use_gpu=True): self.assertRaises(ValueError, init1, shape=[5]) def testGain(self): @@ -614,7 +614,7 @@ class OrthogonalInitializerTest(test.TestCase): for dtype in [dtypes.float32, dtypes.float64]: init1 = init_ops.orthogonal_initializer(seed=1, dtype=dtype) init2 = init_ops.orthogonal_initializer(gain=3.14, seed=1, dtype=dtype) - with self.test_session(graph=ops.Graph(), use_gpu=True): + with self.session(graph=ops.Graph(), use_gpu=True): t1 = init1(shape).eval() t2 = init2(shape).eval() return np.allclose(t1, t2 / 3.14, rtol=1e-15, atol=1e-15) @@ -624,7 +624,7 @@ class OrthogonalInitializerTest(test.TestCase): for shape in [(10, 10), (10, 9, 8), (100, 5, 5), (50, 40), (40, 50)]: init = init_ops.orthogonal_initializer(dtype=dtype) tol = 1e-5 if dtype == dtypes.float32 else 1e-12 - with self.test_session(graph=ops.Graph(), use_gpu=True): + with self.session(graph=ops.Graph(), use_gpu=True): # Check the shape t = init(shape).eval() self.assertAllEqual(shape, t.shape) @@ -663,7 +663,7 @@ class ConvolutionDeltaOrthogonalInitializerTest(test.TestCase): def testInvalidShape(self): init1 = init_ops.convolutional_delta_orthogonal() - with self.test_session(graph=ops.Graph(), use_gpu=True): + with self.session(graph=ops.Graph(), use_gpu=True): self.assertRaises(ValueError, init1, shape=[3, 3, 6, 5]) def testGain(self): @@ -672,7 +672,7 @@ class ConvolutionDeltaOrthogonalInitializerTest(test.TestCase): init1 = init_ops.convolutional_delta_orthogonal(seed=1, dtype=dtype) init2 = init_ops.convolutional_delta_orthogonal(gain=3.14, seed=1, dtype=dtype) - with self.test_session(graph=ops.Graph(), use_gpu=True): + with self.session(graph=ops.Graph(), use_gpu=True): t1 = init1(shape).eval() t2 = init2(shape).eval() return np.allclose(t1, t2 / 3.14, rtol=1e-15, atol=1e-15) @@ -763,7 +763,7 @@ class ConvolutionOrthogonal1dInitializerTest(test.TestCase): def testInvalidShape(self): init1 = init_ops.convolutional_orthogonal_1d() - with self.test_session(graph=ops.Graph(), use_gpu=True): + with self.session(graph=ops.Graph(), use_gpu=True): self.assertRaises(ValueError, init1, shape=[3, 6, 5]) def testGain(self): @@ -772,7 +772,7 @@ class ConvolutionOrthogonal1dInitializerTest(test.TestCase): init1 = init_ops.convolutional_orthogonal_1d(seed=1, dtype=dtype) init2 = init_ops.convolutional_orthogonal_1d(gain=3.14, seed=1, dtype=dtype) - with self.test_session(graph=ops.Graph(), use_gpu=True): + with self.session(graph=ops.Graph(), use_gpu=True): t1 = init1(shape).eval() t2 = init2(shape).eval() return np.allclose(t1, t2 / 3.14, rtol=1e-15, atol=1e-15) @@ -877,7 +877,7 @@ class ConvolutionOrthogonal2dInitializerTest(test.TestCase): def testInvalidShape(self): init1 = init_ops.convolutional_orthogonal_2d() - with self.test_session(graph=ops.Graph(), use_gpu=True): + with self.session(graph=ops.Graph(), use_gpu=True): self.assertRaises(ValueError, init1, shape=[3, 3, 6, 5]) def testGain(self): @@ -886,7 +886,7 @@ class ConvolutionOrthogonal2dInitializerTest(test.TestCase): init1 = init_ops.convolutional_orthogonal_2d(seed=1, dtype=dtype) init2 = init_ops.convolutional_orthogonal_2d(gain=3.14, seed=1, dtype=dtype) - with self.test_session(graph=ops.Graph(), use_gpu=True): + with self.session(graph=ops.Graph(), use_gpu=True): t1 = init1(shape).eval() t2 = init2(shape).eval() return np.allclose(t1, t2 / 3.14, rtol=1e-15, atol=1e-15) @@ -972,7 +972,7 @@ class ConvolutionOrthogonal3dInitializerTest(test.TestCase): def testInvalidShape(self): init1 = init_ops.convolutional_orthogonal_3d() - with self.test_session(graph=ops.Graph(), use_gpu=True): + with self.session(graph=ops.Graph(), use_gpu=True): self.assertRaises(ValueError, init1, shape=[3, 3, 3, 6, 5]) def testGain(self): @@ -981,7 +981,7 @@ class ConvolutionOrthogonal3dInitializerTest(test.TestCase): init1 = init_ops.convolutional_orthogonal_3d(seed=1, dtype=dtype) init2 = init_ops.convolutional_orthogonal_3d(gain=3.14, seed=1, dtype=dtype) - with self.test_session(graph=ops.Graph(), use_gpu=True): + with self.session(graph=ops.Graph(), use_gpu=True): t1 = init1(shape).eval() t2 = init2(shape).eval() return np.allclose(t1, t2 / 3.14, rtol=1e-15, atol=1e-15) @@ -1080,7 +1080,7 @@ class IdentityInitializerTest(test.TestCase): def testInvalidShape(self): init = init_ops.identity_initializer() - with self.test_session(graph=ops.Graph(), use_gpu=True): + with self.session(graph=ops.Graph(), use_gpu=True): self.assertRaises(ValueError, init, shape=[5, 7, 7]) self.assertRaises(ValueError, init, shape=[5]) self.assertRaises(ValueError, init, shape=[]) @@ -1088,7 +1088,7 @@ class IdentityInitializerTest(test.TestCase): def testNonSquare(self): init = init_ops.identity_initializer() shape = (10, 5) - with self.test_session(graph=ops.Graph(), use_gpu=True): + with self.session(graph=ops.Graph(), use_gpu=True): self.assertAllClose(init(shape).eval(), np.eye(*shape)) def testGain(self): @@ -1096,16 +1096,16 @@ class IdentityInitializerTest(test.TestCase): for dtype in [dtypes.float32, dtypes.float64]: init_default = init_ops.identity_initializer(dtype=dtype) init_custom = init_ops.identity_initializer(gain=0.9, dtype=dtype) - with self.test_session(graph=ops.Graph(), use_gpu=True): + with self.session(graph=ops.Graph(), use_gpu=True): self.assertAllClose(init_default(shape).eval(), np.eye(*shape)) - with self.test_session(graph=ops.Graph(), use_gpu=True): + with self.session(graph=ops.Graph(), use_gpu=True): self.assertAllClose(init_custom(shape).eval(), np.eye(*shape) * 0.9) def testPartitions(self): shape = (10, 10) init = init_ops.identity_initializer() partitioner = partitioned_variables.variable_axis_size_partitioner(1) - with self.test_session(graph=ops.Graph(), use_gpu=True): + with self.session(graph=ops.Graph(), use_gpu=True): with variable_scope.variable_scope( "foo", partitioner=partitioner, initializer=init): v = array_ops.identity(variable_scope.get_variable("bar", shape=shape)) diff --git a/tensorflow/python/kernel_tests/inplace_ops_test.py b/tensorflow/python/kernel_tests/inplace_ops_test.py index 6e894365af..90759c23ae 100644 --- a/tensorflow/python/kernel_tests/inplace_ops_test.py +++ b/tensorflow/python/kernel_tests/inplace_ops_test.py @@ -153,7 +153,7 @@ class InplaceOpsTest(test_util.TensorFlowTestCase): self.assertAllClose(vy, vz) def testError(self): - with self.test_session(): + with self.cached_session(): with self.assertRaisesRegexp(errors.InvalidArgumentError, "must be a vector"): _ = inplace_ops.inplace_update([[1.]], [[0]], [[10]]).eval() diff --git a/tensorflow/python/kernel_tests/io_ops_test.py b/tensorflow/python/kernel_tests/io_ops_test.py index 61944f7e31..afa24195cb 100644 --- a/tensorflow/python/kernel_tests/io_ops_test.py +++ b/tensorflow/python/kernel_tests/io_ops_test.py @@ -37,7 +37,7 @@ class IoOpsTest(test.TestCase): with tempfile.NamedTemporaryFile( prefix='ReadFileTest', dir=self.get_temp_dir(), delete=False) as temp: temp.write(contents) - with self.test_session(): + with self.cached_session(): read = io_ops.read_file(temp.name) self.assertEqual([], read.get_shape()) self.assertEqual(read.eval(), contents) @@ -51,7 +51,7 @@ class IoOpsTest(test.TestCase): prefix='WriteFileTest', dir=self.get_temp_dir(), delete=False) as temp: pass - with self.test_session() as sess: + with self.cached_session() as sess: w = io_ops.write_file(temp.name, contents) sess.run(w) with open(temp.name, 'rb') as f: @@ -65,7 +65,7 @@ class IoOpsTest(test.TestCase): contents = compat.as_bytes(contents) subdir = os.path.join(self.get_temp_dir(), 'subdir1') filepath = os.path.join(subdir, 'subdir2', 'filename') - with self.test_session() as sess: + with self.cached_session() as sess: w = io_ops.write_file(filepath, contents) sess.run(w) with open(filepath, 'rb') as f: @@ -88,7 +88,7 @@ class IoOpsTest(test.TestCase): prefix=c, dir=self.get_temp_dir(), delete=True) for c in cases ] - with self.test_session(): + with self.cached_session(): # Test exact match without wildcards. for f in files: self.assertEqual( diff --git a/tensorflow/python/kernel_tests/linalg/BUILD b/tensorflow/python/kernel_tests/linalg/BUILD index f4ec3e3996..be2e31cb5a 100644 --- a/tensorflow/python/kernel_tests/linalg/BUILD +++ b/tensorflow/python/kernel_tests/linalg/BUILD @@ -25,6 +25,22 @@ cuda_py_test( ) cuda_py_test( + name = "linear_operator_addition_test", + size = "small", + srcs = ["linear_operator_addition_test.py"], + additional_deps = [ + "//tensorflow/python/ops/linalg", + "//third_party/py/numpy", + "//tensorflow/python:array_ops", + "//tensorflow/python:client_testlib", + "//tensorflow/python:framework_for_generated_wrappers", + "//tensorflow/python:framework_test_lib", + "//tensorflow/python:math_ops", + "//tensorflow/python:platform_test", + ], +) + +cuda_py_test( name = "linear_operator_block_diag_test", size = "medium", srcs = ["linear_operator_block_diag_test.py"], diff --git a/tensorflow/python/kernel_tests/linalg/linear_operator_addition_test.py b/tensorflow/python/kernel_tests/linalg/linear_operator_addition_test.py new file mode 100644 index 0000000000..cf56168d63 --- /dev/null +++ b/tensorflow/python/kernel_tests/linalg/linear_operator_addition_test.py @@ -0,0 +1,412 @@ +# Copyright 2016 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import numpy as np + +from tensorflow.python.framework import random_seed +from tensorflow.python.ops import linalg_ops +from tensorflow.python.ops.linalg import linalg as linalg_lib +from tensorflow.python.ops.linalg import linear_operator_addition +from tensorflow.python.platform import test + +linalg = linalg_lib +random_seed.set_random_seed(23) +rng = np.random.RandomState(0) + +add_operators = linear_operator_addition.add_operators + + +# pylint: disable=unused-argument +class _BadAdder(linear_operator_addition._Adder): + """Adder that will fail if used.""" + + def can_add(self, op1, op2): + raise AssertionError("BadAdder.can_add called!") + + def _add(self, op1, op2, operator_name, hints): + raise AssertionError("This line should not be reached") + + +# pylint: enable=unused-argument + + +class LinearOperatorAdditionCorrectnessTest(test.TestCase): + """Tests correctness of addition with combinations of a few Adders. + + Tests here are done with the _DEFAULT_ADDITION_TIERS, which means + add_operators should reduce all operators resulting in one single operator. + + This shows that we are able to correctly combine adders using the tiered + system. All Adders should be tested separately, and there is no need to test + every Adder within this class. + """ + + def test_one_operator_is_returned_unchanged(self): + op_a = linalg.LinearOperatorDiag([1., 1.]) + op_sum = add_operators([op_a]) + self.assertEqual(1, len(op_sum)) + self.assertIs(op_sum[0], op_a) + + def test_at_least_one_operators_required(self): + with self.assertRaisesRegexp(ValueError, "must contain at least one"): + add_operators([]) + + def test_attempting_to_add_numbers_raises(self): + with self.assertRaisesRegexp(TypeError, "contain only LinearOperator"): + add_operators([1, 2]) + + def test_two_diag_operators(self): + op_a = linalg.LinearOperatorDiag( + [1., 1.], is_positive_definite=True, name="A") + op_b = linalg.LinearOperatorDiag( + [2., 2.], is_positive_definite=True, name="B") + with self.cached_session(): + op_sum = add_operators([op_a, op_b]) + self.assertEqual(1, len(op_sum)) + op = op_sum[0] + self.assertIsInstance(op, linalg_lib.LinearOperatorDiag) + self.assertAllClose([[3., 0.], [0., 3.]], op.to_dense().eval()) + # Adding positive definite operators produces positive def. + self.assertTrue(op.is_positive_definite) + # Real diagonal ==> self-adjoint. + self.assertTrue(op.is_self_adjoint) + # Positive definite ==> non-singular + self.assertTrue(op.is_non_singular) + # Enforce particular name for this simple case + self.assertEqual("Add/B__A/", op.name) + + def test_three_diag_operators(self): + op1 = linalg.LinearOperatorDiag( + [1., 1.], is_positive_definite=True, name="op1") + op2 = linalg.LinearOperatorDiag( + [2., 2.], is_positive_definite=True, name="op2") + op3 = linalg.LinearOperatorDiag( + [3., 3.], is_positive_definite=True, name="op3") + with self.cached_session(): + op_sum = add_operators([op1, op2, op3]) + self.assertEqual(1, len(op_sum)) + op = op_sum[0] + self.assertTrue(isinstance(op, linalg_lib.LinearOperatorDiag)) + self.assertAllClose([[6., 0.], [0., 6.]], op.to_dense().eval()) + # Adding positive definite operators produces positive def. + self.assertTrue(op.is_positive_definite) + # Real diagonal ==> self-adjoint. + self.assertTrue(op.is_self_adjoint) + # Positive definite ==> non-singular + self.assertTrue(op.is_non_singular) + + def test_diag_tril_diag(self): + op1 = linalg.LinearOperatorDiag( + [1., 1.], is_non_singular=True, name="diag_a") + op2 = linalg.LinearOperatorLowerTriangular( + [[2., 0.], [0., 2.]], + is_self_adjoint=True, + is_non_singular=True, + name="tril") + op3 = linalg.LinearOperatorDiag( + [3., 3.], is_non_singular=True, name="diag_b") + with self.cached_session(): + op_sum = add_operators([op1, op2, op3]) + self.assertEqual(1, len(op_sum)) + op = op_sum[0] + self.assertIsInstance(op, linalg_lib.LinearOperatorLowerTriangular) + self.assertAllClose([[6., 0.], [0., 6.]], op.to_dense().eval()) + + # The diag operators will be self-adjoint (because real and diagonal). + # The TriL operator has the self-adjoint hint set. + self.assertTrue(op.is_self_adjoint) + + # Even though op1/2/3 are non-singular, this does not imply op is. + # Since no custom hint was provided, we default to None (unknown). + self.assertEqual(None, op.is_non_singular) + + def test_matrix_diag_tril_diag_uses_custom_name(self): + op0 = linalg.LinearOperatorFullMatrix( + [[-1., -1.], [-1., -1.]], name="matrix") + op1 = linalg.LinearOperatorDiag([1., 1.], name="diag_a") + op2 = linalg.LinearOperatorLowerTriangular( + [[2., 0.], [1.5, 2.]], name="tril") + op3 = linalg.LinearOperatorDiag([3., 3.], name="diag_b") + with self.cached_session(): + op_sum = add_operators([op0, op1, op2, op3], operator_name="my_operator") + self.assertEqual(1, len(op_sum)) + op = op_sum[0] + self.assertIsInstance(op, linalg_lib.LinearOperatorFullMatrix) + self.assertAllClose([[5., -1.], [0.5, 5.]], op.to_dense().eval()) + self.assertEqual("my_operator", op.name) + + def test_incompatible_domain_dimensions_raises(self): + op1 = linalg.LinearOperatorFullMatrix(rng.rand(2, 3)) + op2 = linalg.LinearOperatorDiag(rng.rand(2, 4)) + with self.assertRaisesRegexp(ValueError, "must.*same domain dimension"): + add_operators([op1, op2]) + + def test_incompatible_range_dimensions_raises(self): + op1 = linalg.LinearOperatorFullMatrix(rng.rand(2, 3)) + op2 = linalg.LinearOperatorDiag(rng.rand(3, 3)) + with self.assertRaisesRegexp(ValueError, "must.*same range dimension"): + add_operators([op1, op2]) + + def test_non_broadcastable_batch_shape_raises(self): + op1 = linalg.LinearOperatorFullMatrix(rng.rand(2, 3, 3)) + op2 = linalg.LinearOperatorDiag(rng.rand(4, 3, 3)) + with self.assertRaisesRegexp(ValueError, "Incompatible shapes"): + add_operators([op1, op2]) + + +class LinearOperatorOrderOfAdditionTest(test.TestCase): + """Test that the order of addition is done as specified by tiers.""" + + def test_tier_0_additions_done_in_tier_0(self): + diag1 = linalg.LinearOperatorDiag([1.]) + diag2 = linalg.LinearOperatorDiag([1.]) + diag3 = linalg.LinearOperatorDiag([1.]) + addition_tiers = [ + [linear_operator_addition._AddAndReturnDiag()], + [_BadAdder()], + ] + # Should not raise since all were added in tier 0, and tier 1 (with the + # _BadAdder) was never reached. + op_sum = add_operators([diag1, diag2, diag3], addition_tiers=addition_tiers) + self.assertEqual(1, len(op_sum)) + self.assertIsInstance(op_sum[0], linalg.LinearOperatorDiag) + + def test_tier_1_additions_done_by_tier_1(self): + diag1 = linalg.LinearOperatorDiag([1.]) + diag2 = linalg.LinearOperatorDiag([1.]) + tril = linalg.LinearOperatorLowerTriangular([[1.]]) + addition_tiers = [ + [linear_operator_addition._AddAndReturnDiag()], + [linear_operator_addition._AddAndReturnTriL()], + [_BadAdder()], + ] + # Should not raise since all were added by tier 1, and the + # _BadAdder) was never reached. + op_sum = add_operators([diag1, diag2, tril], addition_tiers=addition_tiers) + self.assertEqual(1, len(op_sum)) + self.assertIsInstance(op_sum[0], linalg.LinearOperatorLowerTriangular) + + def test_tier_1_additions_done_by_tier_1_with_order_flipped(self): + diag1 = linalg.LinearOperatorDiag([1.]) + diag2 = linalg.LinearOperatorDiag([1.]) + tril = linalg.LinearOperatorLowerTriangular([[1.]]) + addition_tiers = [ + [linear_operator_addition._AddAndReturnTriL()], + [linear_operator_addition._AddAndReturnDiag()], + [_BadAdder()], + ] + # Tier 0 could convert to TriL, and this converted everything to TriL, + # including the Diags. + # Tier 1 was never used. + # Tier 2 was never used (therefore, _BadAdder didn't raise). + op_sum = add_operators([diag1, diag2, tril], addition_tiers=addition_tiers) + self.assertEqual(1, len(op_sum)) + self.assertIsInstance(op_sum[0], linalg.LinearOperatorLowerTriangular) + + def test_cannot_add_everything_so_return_more_than_one_operator(self): + diag1 = linalg.LinearOperatorDiag([1.]) + diag2 = linalg.LinearOperatorDiag([2.]) + tril5 = linalg.LinearOperatorLowerTriangular([[5.]]) + addition_tiers = [ + [linear_operator_addition._AddAndReturnDiag()], + ] + # Tier 0 (the only tier) can only convert to Diag, so it combines the two + # diags, but the TriL is unchanged. + # Result should contain two operators, one Diag, one TriL. + op_sum = add_operators([diag1, diag2, tril5], addition_tiers=addition_tiers) + self.assertEqual(2, len(op_sum)) + found_diag = False + found_tril = False + with self.cached_session(): + for op in op_sum: + if isinstance(op, linalg.LinearOperatorDiag): + found_diag = True + self.assertAllClose([[3.]], op.to_dense().eval()) + if isinstance(op, linalg.LinearOperatorLowerTriangular): + found_tril = True + self.assertAllClose([[5.]], op.to_dense().eval()) + self.assertTrue(found_diag and found_tril) + + def test_intermediate_tier_is_not_skipped(self): + diag1 = linalg.LinearOperatorDiag([1.]) + diag2 = linalg.LinearOperatorDiag([1.]) + tril = linalg.LinearOperatorLowerTriangular([[1.]]) + addition_tiers = [ + [linear_operator_addition._AddAndReturnDiag()], + [_BadAdder()], + [linear_operator_addition._AddAndReturnTriL()], + ] + # tril cannot be added in tier 0, and the intermediate tier 1 with the + # BadAdder will catch it and raise. + with self.assertRaisesRegexp(AssertionError, "BadAdder.can_add called"): + add_operators([diag1, diag2, tril], addition_tiers=addition_tiers) + + +class AddAndReturnScaledIdentityTest(test.TestCase): + + def setUp(self): + self._adder = linear_operator_addition._AddAndReturnScaledIdentity() + + def test_identity_plus_identity(self): + id1 = linalg.LinearOperatorIdentity(num_rows=2) + id2 = linalg.LinearOperatorIdentity(num_rows=2, batch_shape=[3]) + hints = linear_operator_addition._Hints( + is_positive_definite=True, is_non_singular=True) + + self.assertTrue(self._adder.can_add(id1, id2)) + operator = self._adder.add(id1, id2, "my_operator", hints) + self.assertIsInstance(operator, linalg.LinearOperatorScaledIdentity) + + with self.cached_session(): + self.assertAllClose(2 * + linalg_ops.eye(num_rows=2, batch_shape=[3]).eval(), + operator.to_dense().eval()) + self.assertTrue(operator.is_positive_definite) + self.assertTrue(operator.is_non_singular) + self.assertEqual("my_operator", operator.name) + + def test_identity_plus_scaled_identity(self): + id1 = linalg.LinearOperatorIdentity(num_rows=2, batch_shape=[3]) + id2 = linalg.LinearOperatorScaledIdentity(num_rows=2, multiplier=2.2) + hints = linear_operator_addition._Hints( + is_positive_definite=True, is_non_singular=True) + + self.assertTrue(self._adder.can_add(id1, id2)) + operator = self._adder.add(id1, id2, "my_operator", hints) + self.assertIsInstance(operator, linalg.LinearOperatorScaledIdentity) + + with self.cached_session(): + self.assertAllClose(3.2 * + linalg_ops.eye(num_rows=2, batch_shape=[3]).eval(), + operator.to_dense().eval()) + self.assertTrue(operator.is_positive_definite) + self.assertTrue(operator.is_non_singular) + self.assertEqual("my_operator", operator.name) + + def test_scaled_identity_plus_scaled_identity(self): + id1 = linalg.LinearOperatorScaledIdentity( + num_rows=2, multiplier=[2.2, 2.2, 2.2]) + id2 = linalg.LinearOperatorScaledIdentity(num_rows=2, multiplier=-1.0) + hints = linear_operator_addition._Hints( + is_positive_definite=True, is_non_singular=True) + + self.assertTrue(self._adder.can_add(id1, id2)) + operator = self._adder.add(id1, id2, "my_operator", hints) + self.assertIsInstance(operator, linalg.LinearOperatorScaledIdentity) + + with self.cached_session(): + self.assertAllClose(1.2 * + linalg_ops.eye(num_rows=2, batch_shape=[3]).eval(), + operator.to_dense().eval()) + self.assertTrue(operator.is_positive_definite) + self.assertTrue(operator.is_non_singular) + self.assertEqual("my_operator", operator.name) + + +class AddAndReturnDiagTest(test.TestCase): + + def setUp(self): + self._adder = linear_operator_addition._AddAndReturnDiag() + + def test_identity_plus_identity_returns_diag(self): + id1 = linalg.LinearOperatorIdentity(num_rows=2) + id2 = linalg.LinearOperatorIdentity(num_rows=2, batch_shape=[3]) + hints = linear_operator_addition._Hints( + is_positive_definite=True, is_non_singular=True) + + self.assertTrue(self._adder.can_add(id1, id2)) + operator = self._adder.add(id1, id2, "my_operator", hints) + self.assertIsInstance(operator, linalg.LinearOperatorDiag) + + with self.cached_session(): + self.assertAllClose(2 * + linalg_ops.eye(num_rows=2, batch_shape=[3]).eval(), + operator.to_dense().eval()) + self.assertTrue(operator.is_positive_definite) + self.assertTrue(operator.is_non_singular) + self.assertEqual("my_operator", operator.name) + + def test_diag_plus_diag(self): + diag1 = rng.rand(2, 3, 4) + diag2 = rng.rand(4) + op1 = linalg.LinearOperatorDiag(diag1) + op2 = linalg.LinearOperatorDiag(diag2) + hints = linear_operator_addition._Hints( + is_positive_definite=True, is_non_singular=True) + + self.assertTrue(self._adder.can_add(op1, op2)) + operator = self._adder.add(op1, op2, "my_operator", hints) + self.assertIsInstance(operator, linalg.LinearOperatorDiag) + + with self.cached_session(): + self.assertAllClose( + linalg.LinearOperatorDiag(diag1 + diag2).to_dense().eval(), + operator.to_dense().eval()) + self.assertTrue(operator.is_positive_definite) + self.assertTrue(operator.is_non_singular) + self.assertEqual("my_operator", operator.name) + + +class AddAndReturnTriLTest(test.TestCase): + + def setUp(self): + self._adder = linear_operator_addition._AddAndReturnTriL() + + def test_diag_plus_tril(self): + diag = linalg.LinearOperatorDiag([1., 2.]) + tril = linalg.LinearOperatorLowerTriangular([[10., 0.], [30., 0.]]) + hints = linear_operator_addition._Hints( + is_positive_definite=True, is_non_singular=True) + + self.assertTrue(self._adder.can_add(diag, diag)) + self.assertTrue(self._adder.can_add(diag, tril)) + operator = self._adder.add(diag, tril, "my_operator", hints) + self.assertIsInstance(operator, linalg.LinearOperatorLowerTriangular) + + with self.cached_session(): + self.assertAllClose([[11., 0.], [30., 2.]], operator.to_dense().eval()) + self.assertTrue(operator.is_positive_definite) + self.assertTrue(operator.is_non_singular) + self.assertEqual("my_operator", operator.name) + + +class AddAndReturnMatrixTest(test.TestCase): + + def setUp(self): + self._adder = linear_operator_addition._AddAndReturnMatrix() + + def test_diag_plus_diag(self): + diag1 = linalg.LinearOperatorDiag([1., 2.]) + diag2 = linalg.LinearOperatorDiag([-1., 3.]) + hints = linear_operator_addition._Hints( + is_positive_definite=False, is_non_singular=False) + + self.assertTrue(self._adder.can_add(diag1, diag2)) + operator = self._adder.add(diag1, diag2, "my_operator", hints) + self.assertIsInstance(operator, linalg.LinearOperatorFullMatrix) + + with self.cached_session(): + self.assertAllClose([[0., 0.], [0., 5.]], operator.to_dense().eval()) + self.assertFalse(operator.is_positive_definite) + self.assertFalse(operator.is_non_singular) + self.assertEqual("my_operator", operator.name) + + +if __name__ == "__main__": + test.main() diff --git a/tensorflow/python/kernel_tests/linalg/linear_operator_circulant_test.py b/tensorflow/python/kernel_tests/linalg/linear_operator_circulant_test.py index 7261d4bb3b..f1e151ebd8 100644 --- a/tensorflow/python/kernel_tests/linalg/linear_operator_circulant_test.py +++ b/tensorflow/python/kernel_tests/linalg/linear_operator_circulant_test.py @@ -37,8 +37,10 @@ class LinearOperatorCirculantBaseTest(object): """Common class for circulant tests.""" @contextlib.contextmanager - def test_session(self, *args, **kwargs): - with test.TestCase.test_session(self, *args, **kwargs) as sess: + def _constrain_devices_and_set_default(self, sess, use_gpu, force_gpu): + """We overwrite the FFT operation mapping for testing.""" + with test.TestCase._constrain_devices_and_set_default( + self, sess, use_gpu, force_gpu) as sess: with spectral_ops_test_util.fft_kernel_label_map(): yield sess @@ -110,8 +112,7 @@ class LinearOperatorCirculantTestSelfAdjointOperator( lin_op_spectrum = spectrum if use_placeholder: - lin_op_spectrum = array_ops.placeholder_with_default( - spectrum, shape=None) + lin_op_spectrum = array_ops.placeholder_with_default(spectrum, shape=None) operator = linalg.LinearOperatorCirculant( lin_op_spectrum, is_self_adjoint=True, input_output_dtype=dtype) @@ -121,7 +122,7 @@ class LinearOperatorCirculantTestSelfAdjointOperator( return operator, mat def test_simple_hermitian_spectrum_gives_operator_with_zero_imag_part(self): - with self.test_session(): + with self.cached_session(): spectrum = math_ops.cast([1., 1j, -1j], dtypes.complex64) operator = linalg.LinearOperatorCirculant( spectrum, input_output_dtype=dtypes.complex64) @@ -171,8 +172,7 @@ class LinearOperatorCirculantTestHermitianSpectrum( lin_op_spectrum = spectrum if use_placeholder: - lin_op_spectrum = array_ops.placeholder_with_default( - spectrum, shape=None) + lin_op_spectrum = array_ops.placeholder_with_default(spectrum, shape=None) operator = linalg.LinearOperatorCirculant( lin_op_spectrum, input_output_dtype=dtype) @@ -182,7 +182,7 @@ class LinearOperatorCirculantTestHermitianSpectrum( return operator, mat def test_simple_hermitian_spectrum_gives_operator_with_zero_imag_part(self): - with self.test_session(): + with self.cached_session(): spectrum = math_ops.cast([1., 1j, -1j], dtypes.complex64) operator = linalg.LinearOperatorCirculant( spectrum, input_output_dtype=dtypes.complex64) @@ -217,8 +217,7 @@ class LinearOperatorCirculantTestNonHermitianSpectrum( lin_op_spectrum = spectrum if use_placeholder: - lin_op_spectrum = array_ops.placeholder_with_default( - spectrum, shape=None) + lin_op_spectrum = array_ops.placeholder_with_default(spectrum, shape=None) operator = linalg.LinearOperatorCirculant( lin_op_spectrum, input_output_dtype=dtype) @@ -228,7 +227,7 @@ class LinearOperatorCirculantTestNonHermitianSpectrum( return operator, mat def test_simple_hermitian_spectrum_gives_operator_with_zero_imag_part(self): - with self.test_session(): + with self.cached_session(): spectrum = math_ops.cast([1., 1j, -1j], dtypes.complex64) operator = linalg.LinearOperatorCirculant( spectrum, input_output_dtype=dtypes.complex64) @@ -238,7 +237,7 @@ class LinearOperatorCirculantTestNonHermitianSpectrum( np.testing.assert_allclose(0, imag_matrix.eval(), rtol=0, atol=eps * 3) def test_simple_positive_real_spectrum_gives_self_adjoint_pos_def_oper(self): - with self.test_session() as sess: + with self.cached_session() as sess: spectrum = math_ops.cast([6., 4, 2], dtypes.complex64) operator = linalg.LinearOperatorCirculant( spectrum, input_output_dtype=dtypes.complex64) @@ -250,7 +249,7 @@ class LinearOperatorCirculantTestNonHermitianSpectrum( operator.assert_self_adjoint().run() # Should not fail def test_defining_operator_using_real_convolution_kernel(self): - with self.test_session(): + with self.cached_session(): convolution_kernel = [1., 2., 1.] spectrum = math_ops.fft( math_ops.cast(convolution_kernel, dtypes.complex64)) @@ -266,7 +265,7 @@ class LinearOperatorCirculantTestNonHermitianSpectrum( np.testing.assert_allclose(0, np.imag(matrix), atol=1e-6) def test_hermitian_spectrum_gives_operator_with_zero_imag_part(self): - with self.test_session(): + with self.cached_session(): # Make spectrum the FFT of a real convolution kernel h. This ensures that # spectrum is Hermitian. h = linear_operator_test_util.random_normal(shape=(3, 4)) @@ -281,7 +280,7 @@ class LinearOperatorCirculantTestNonHermitianSpectrum( def test_convolution_kernel_same_as_first_row_of_to_dense(self): spectrum = [[3., 2., 1.], [2., 1.5, 1.]] - with self.test_session(): + with self.cached_session(): operator = linalg.LinearOperatorCirculant(spectrum) h = operator.convolution_kernel() c = operator.to_dense() @@ -293,27 +292,27 @@ class LinearOperatorCirculantTestNonHermitianSpectrum( def test_assert_non_singular_fails_for_singular_operator(self): spectrum = math_ops.cast([0, 4, 2j + 2], dtypes.complex64) operator = linalg.LinearOperatorCirculant(spectrum) - with self.test_session(): + with self.cached_session(): with self.assertRaisesOpError("Singular operator"): operator.assert_non_singular().run() def test_assert_non_singular_does_not_fail_for_non_singular_operator(self): spectrum = math_ops.cast([-3j, 4, 2j + 2], dtypes.complex64) operator = linalg.LinearOperatorCirculant(spectrum) - with self.test_session(): + with self.cached_session(): operator.assert_non_singular().run() # Should not fail def test_assert_positive_definite_fails_for_non_positive_definite(self): spectrum = math_ops.cast([6., 4, 2j], dtypes.complex64) operator = linalg.LinearOperatorCirculant(spectrum) - with self.test_session(): + with self.cached_session(): with self.assertRaisesOpError("Not positive definite"): operator.assert_positive_definite().run() def test_assert_positive_definite_does_not_fail_when_pos_def(self): spectrum = math_ops.cast([6., 4, 2j + 2], dtypes.complex64) operator = linalg.LinearOperatorCirculant(spectrum) - with self.test_session(): + with self.cached_session(): operator.assert_positive_definite().run() # Should not fail def test_real_spectrum_and_not_self_adjoint_hint_raises(self): @@ -331,8 +330,10 @@ class LinearOperatorCirculant2DBaseTest(object): """Common class for 2D circulant tests.""" @contextlib.contextmanager - def test_session(self, *args, **kwargs): - with test.TestCase.test_session(self, *args, **kwargs) as sess: + def _constrain_devices_and_set_default(self, sess, use_gpu, force_gpu): + """We overwrite the FFT operation mapping for testing.""" + with test.TestCase._constrain_devices_and_set_default( + self, sess, use_gpu, force_gpu) as sess: with spectral_ops_test_util.fft_kernel_label_map(): yield sess @@ -446,8 +447,7 @@ class LinearOperatorCirculant2DTestHermitianSpectrum( lin_op_spectrum = spectrum if use_placeholder: - lin_op_spectrum = array_ops.placeholder_with_default( - spectrum, shape=None) + lin_op_spectrum = array_ops.placeholder_with_default(spectrum, shape=None) operator = linalg.LinearOperatorCirculant2D( lin_op_spectrum, input_output_dtype=dtype) @@ -482,8 +482,7 @@ class LinearOperatorCirculant2DTestNonHermitianSpectrum( lin_op_spectrum = spectrum if use_placeholder: - lin_op_spectrum = array_ops.placeholder_with_default( - spectrum, shape=None) + lin_op_spectrum = array_ops.placeholder_with_default(spectrum, shape=None) operator = linalg.LinearOperatorCirculant2D( lin_op_spectrum, input_output_dtype=dtype) @@ -493,7 +492,7 @@ class LinearOperatorCirculant2DTestNonHermitianSpectrum( return operator, mat def test_real_hermitian_spectrum_gives_real_symmetric_operator(self): - with self.test_session() as sess: + with self.cached_session() as sess: # This is a real and hermitian spectrum. spectrum = [[1., 2., 2.], [3., 4., 4.], [3., 4., 4.]] operator = linalg.LinearOperatorCirculant(spectrum) @@ -510,7 +509,7 @@ class LinearOperatorCirculant2DTestNonHermitianSpectrum( self.assertAllClose(matrix, matrix_transpose, atol=0) def test_real_spectrum_gives_self_adjoint_operator(self): - with self.test_session() as sess: + with self.cached_session() as sess: # This is a real and hermitian spectrum. spectrum = linear_operator_test_util.random_normal( shape=(3, 3), dtype=dtypes.float32) @@ -526,27 +525,27 @@ class LinearOperatorCirculant2DTestNonHermitianSpectrum( def test_assert_non_singular_fails_for_singular_operator(self): spectrum = math_ops.cast([[0, 4], [2j + 2, 3.]], dtypes.complex64) operator = linalg.LinearOperatorCirculant2D(spectrum) - with self.test_session(): + with self.cached_session(): with self.assertRaisesOpError("Singular operator"): operator.assert_non_singular().run() def test_assert_non_singular_does_not_fail_for_non_singular_operator(self): spectrum = math_ops.cast([[-3j, 4], [2j + 2, 3.]], dtypes.complex64) operator = linalg.LinearOperatorCirculant2D(spectrum) - with self.test_session(): + with self.cached_session(): operator.assert_non_singular().run() # Should not fail def test_assert_positive_definite_fails_for_non_positive_definite(self): spectrum = math_ops.cast([[6., 4], [2j, 3.]], dtypes.complex64) operator = linalg.LinearOperatorCirculant2D(spectrum) - with self.test_session(): + with self.cached_session(): with self.assertRaisesOpError("Not positive definite"): operator.assert_positive_definite().run() def test_assert_positive_definite_does_not_fail_when_pos_def(self): spectrum = math_ops.cast([[6., 4], [2j + 2, 3.]], dtypes.complex64) operator = linalg.LinearOperatorCirculant2D(spectrum) - with self.test_session(): + with self.cached_session(): operator.assert_positive_definite().run() # Should not fail def test_real_spectrum_and_not_self_adjoint_hint_raises(self): @@ -574,13 +573,15 @@ class LinearOperatorCirculant3DTest(test.TestCase): """Simple test of the 3D case. See also the 1D and 2D tests.""" @contextlib.contextmanager - def test_session(self, *args, **kwargs): - with test.TestCase.test_session(self, *args, **kwargs) as sess: + def _constrain_devices_and_set_default(self, sess, use_gpu, force_gpu): + """We overwrite the FFT operation mapping for testing.""" + with test.TestCase._constrain_devices_and_set_default( + self, sess, use_gpu, force_gpu) as sess: with spectral_ops_test_util.fft_kernel_label_map(): yield sess def test_real_spectrum_gives_self_adjoint_operator(self): - with self.test_session() as sess: + with self.cached_session() as sess: # This is a real and hermitian spectrum. spectrum = linear_operator_test_util.random_normal( shape=(2, 2, 3, 5), dtype=dtypes.float32) @@ -597,7 +598,7 @@ class LinearOperatorCirculant3DTest(test.TestCase): self.assertAllClose(matrix, matrix_h) def test_defining_operator_using_real_convolution_kernel(self): - with self.test_session(): + with self.cached_session(): convolution_kernel = linear_operator_test_util.random_normal( shape=(2, 2, 3, 5), dtype=dtypes.float32) # Convolution kernel is real ==> spectrum is Hermitian. @@ -615,7 +616,7 @@ class LinearOperatorCirculant3DTest(test.TestCase): np.testing.assert_allclose(0, np.imag(matrix), atol=1e-6) def test_defining_spd_operator_by_taking_real_part(self): - with self.test_session() as sess: + with self.cached_session() as sess: # S is real and positive. s = linear_operator_test_util.random_uniform( shape=(10, 2, 3, 4), dtype=dtypes.float32, minval=1., maxval=2.) diff --git a/tensorflow/python/kernel_tests/linalg_grad_test.py b/tensorflow/python/kernel_tests/linalg_grad_test.py index 0e4e58409e..e52f303fe0 100644 --- a/tensorflow/python/kernel_tests/linalg_grad_test.py +++ b/tensorflow/python/kernel_tests/linalg_grad_test.py @@ -40,7 +40,7 @@ def _AddTest(test, op_name, testcase_name, fn): class ShapeTest(test_lib.TestCase): def testBatchGradientUnknownSize(self): - with self.test_session(): + with self.cached_session(): batch_size = constant_op.constant(3) matrix_size = constant_op.constant(4) batch_identity = array_ops.tile( @@ -120,7 +120,7 @@ def _GetMatrixBinaryFunctorGradientTest(functor_, delta = epsilon**(1.0 / 3.0) # tolerance obtained by looking at actual differences using # np.linalg.norm(theoretical-numerical, np.inf) on -mavx build - tol = 1e-6 if dtype_ == np.float64 else float32_tol_fudge * 0.04 + tol = 1e-6 if dtype_ == np.float64 else float32_tol_fudge * 0.05 # The gradients for a and b may be of very different magnitudes, # so to not get spurious failures we test them separately. for factor, factor_init in [a, a_np], [b, b_np]: diff --git a/tensorflow/python/kernel_tests/linalg_ops_test.py b/tensorflow/python/kernel_tests/linalg_ops_test.py index 2f28d37eff..aa17f727d0 100644 --- a/tensorflow/python/kernel_tests/linalg_ops_test.py +++ b/tensorflow/python/kernel_tests/linalg_ops_test.py @@ -128,7 +128,7 @@ class AdjointTest(test.TestCase): matrix_np = np.array([[1 + 1j, 2 + 2j, 3 + 3j], [4 + 4j, 5 + 5j, 6 + 6j]]).astype(dtype) expected_transposed = np.conj(matrix_np.T) - with self.test_session(): + with self.cached_session(): matrix = ops.convert_to_tensor(matrix_np) transposed = linalg.adjoint(matrix) self.assertEqual((3, 2), transposed.get_shape()) diff --git a/tensorflow/python/kernel_tests/listdiff_op_test.py b/tensorflow/python/kernel_tests/listdiff_op_test.py index ee86cf0b24..baeb40dd63 100644 --- a/tensorflow/python/kernel_tests/listdiff_op_test.py +++ b/tensorflow/python/kernel_tests/listdiff_op_test.py @@ -42,7 +42,7 @@ class ListDiffTest(test.TestCase): out = [compat.as_bytes(str(a)) for a in out] for diff_func in [array_ops.setdiff1d]: for index_dtype in [dtypes.int32, dtypes.int64]: - with self.test_session() as sess: + with self.cached_session() as sess: x_tensor = ops.convert_to_tensor(x, dtype=dtype) y_tensor = ops.convert_to_tensor(y, dtype=dtype) out_tensor, idx_tensor = diff_func(x_tensor, y_tensor, diff --git a/tensorflow/python/kernel_tests/logging_ops_logging_level_test.py b/tensorflow/python/kernel_tests/logging_ops_logging_level_test.py new file mode 100644 index 0000000000..0e8197dccb --- /dev/null +++ b/tensorflow/python/kernel_tests/logging_ops_logging_level_test.py @@ -0,0 +1,70 @@ +# Copyright 2018 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== +"""Tests for tensorflow.kernels.logging_ops.""" + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import sys + +from tensorflow.python.framework import test_util +from tensorflow.python.ops import logging_ops +from tensorflow.python.ops import math_ops +from tensorflow.python.platform import test +from tensorflow.python.platform import tf_logging + + +class PrintV2LoggingLevelTest(test.TestCase): + + @test_util.run_in_graph_and_eager_modes() + def testPrintOneTensorLogInfo(self): + with self.cached_session(): + tensor = math_ops.range(10) + with self.captureWritesToStream(sys.stderr) as printed: + print_op = logging_ops.print_v2( + tensor, output_stream=tf_logging.info) + self.evaluate(print_op) + self.assertTrue("I" in printed.contents()) + expected = "[0 1 2 ... 7 8 9]" + self.assertTrue(expected in printed.contents()) + + @test_util.run_in_graph_and_eager_modes() + def testPrintOneTensorLogWarning(self): + with self.cached_session(): + tensor = math_ops.range(10) + with self.captureWritesToStream(sys.stderr) as printed: + print_op = logging_ops.print_v2( + tensor, output_stream=tf_logging.warning) + self.evaluate(print_op) + self.assertTrue("W" in printed.contents()) + expected = "[0 1 2 ... 7 8 9]" + self.assertTrue(expected in printed.contents()) + + @test_util.run_in_graph_and_eager_modes() + def testPrintOneTensorLogError(self): + with self.cached_session(): + tensor = math_ops.range(10) + with self.captureWritesToStream(sys.stderr) as printed: + print_op = logging_ops.print_v2( + tensor, output_stream=tf_logging.error) + self.evaluate(print_op) + self.assertTrue("E" in printed.contents()) + expected = "[0 1 2 ... 7 8 9]" + self.assertTrue(expected in printed.contents()) + + +if __name__ == "__main__": + test.main() diff --git a/tensorflow/python/kernel_tests/logging_ops_test.py b/tensorflow/python/kernel_tests/logging_ops_test.py index e635a71c78..4beddd00bb 100644 --- a/tensorflow/python/kernel_tests/logging_ops_test.py +++ b/tensorflow/python/kernel_tests/logging_ops_test.py @@ -18,20 +18,28 @@ from __future__ import absolute_import from __future__ import division from __future__ import print_function +import sys + +from tensorflow.python.eager import context +from tensorflow.python.eager import function from tensorflow.python.framework import constant_op +from tensorflow.python.framework import dtypes from tensorflow.python.framework import ops +from tensorflow.python.framework import sparse_tensor from tensorflow.python.framework import test_util from tensorflow.python.ops import control_flow_ops from tensorflow.python.ops import gradients_impl from tensorflow.python.ops import logging_ops from tensorflow.python.ops import math_ops +from tensorflow.python.ops import string_ops +from tensorflow.python.ops import variables from tensorflow.python.platform import test class LoggingOpsTest(test.TestCase): def testAssertDivideByZero(self): - with self.test_session() as sess: + with self.cached_session() as sess: epsilon = ops.convert_to_tensor(1e-20) x = ops.convert_to_tensor(0.0) y = ops.convert_to_tensor(1.0) @@ -57,6 +65,269 @@ class LoggingOpsTest(test.TestCase): out.eval() +class PrintV2Test(test.TestCase): + + @test_util.run_in_graph_and_eager_modes() + def testPrintOneTensor(self): + with self.cached_session(): + tensor = math_ops.range(10) + with self.captureWritesToStream(sys.stderr) as printed: + print_op = logging_ops.print_v2(tensor) + self.evaluate(print_op) + + expected = "[0 1 2 ... 7 8 9]" + self.assertTrue((expected + "\n") in printed.contents()) + + @test_util.run_in_graph_and_eager_modes() + def testPrintOneTensorVarySummarize(self): + with self.cached_session(): + tensor = math_ops.range(10) + with self.captureWritesToStream(sys.stderr) as printed: + print_op = logging_ops.print_v2(tensor, summarize=1) + self.evaluate(print_op) + + expected = "[0 ... 9]" + self.assertTrue((expected + "\n") in printed.contents()) + + with self.cached_session(): + tensor = math_ops.range(10) + with self.captureWritesToStream(sys.stderr) as printed: + print_op = logging_ops.print_v2(tensor, summarize=2) + self.evaluate(print_op) + + expected = "[0 1 ... 8 9]" + self.assertTrue((expected + "\n") in printed.contents()) + + with self.cached_session(): + tensor = math_ops.range(10) + with self.captureWritesToStream(sys.stderr) as printed: + print_op = logging_ops.print_v2(tensor, summarize=3) + self.evaluate(print_op) + + expected = "[0 1 2 ... 7 8 9]" + self.assertTrue((expected + "\n") in printed.contents()) + + with self.cached_session(): + tensor = math_ops.range(10) + with self.captureWritesToStream(sys.stderr) as printed: + print_op = logging_ops.print_v2(tensor, summarize=-1) + self.evaluate(print_op) + + expected = "[0 1 2 3 4 5 6 7 8 9]" + self.assertTrue((expected + "\n") in printed.contents()) + + @test_util.run_in_graph_and_eager_modes() + def testPrintOneVariable(self): + with self.cached_session(): + var = variables.Variable(math_ops.range(10)) + if not context.executing_eagerly(): + variables.global_variables_initializer().run() + with self.captureWritesToStream(sys.stderr) as printed: + print_op = logging_ops.print_v2(var) + self.evaluate(print_op) + expected = "[0 1 2 ... 7 8 9]" + self.assertTrue((expected + "\n") in printed.contents()) + + @test_util.run_in_graph_and_eager_modes() + def testPrintTwoVariablesInStructWithAssignAdd(self): + with self.cached_session(): + var_one = variables.Variable(2.14) + plus_one = var_one.assign_add(1.0) + var_two = variables.Variable(math_ops.range(10)) + if not context.executing_eagerly(): + variables.global_variables_initializer().run() + with self.captureWritesToStream(sys.stderr) as printed: + self.evaluate(plus_one) + print_op = logging_ops.print_v2(var_one, {"second": var_two}) + self.evaluate(print_op) + expected = "3.14 {'second': [0 1 2 ... 7 8 9]}" + self.assertTrue((expected + "\n") in printed.contents()) + + @test_util.run_in_graph_and_eager_modes() + def testPrintTwoTensors(self): + with self.cached_session(): + tensor = math_ops.range(10) + with self.captureWritesToStream(sys.stderr) as printed: + print_op = logging_ops.print_v2(tensor, tensor * 10) + self.evaluate(print_op) + expected = "[0 1 2 ... 7 8 9] [0 10 20 ... 70 80 90]" + self.assertTrue((expected + "\n") in printed.contents()) + + @test_util.run_in_graph_and_eager_modes() + def testPrintPlaceholderGeneration(self): + with self.cached_session(): + tensor = math_ops.range(10) + with self.captureWritesToStream(sys.stderr) as printed: + print_op = logging_ops.print_v2("{}6", {"{}": tensor * 10}) + self.evaluate(print_op) + expected = "{}6 {'{}': [0 10 20 ... 70 80 90]}" + self.assertTrue((expected + "\n") in printed.contents()) + + @test_util.run_in_graph_and_eager_modes() + def testPrintNoTensors(self): + with self.cached_session(): + with self.captureWritesToStream(sys.stderr) as printed: + print_op = logging_ops.print_v2(23, [23, 5], {"6": 12}) + self.evaluate(print_op) + expected = "23 [23, 5] {'6': 12}" + self.assertTrue((expected + "\n") in printed.contents()) + + @test_util.run_in_graph_and_eager_modes() + def testPrintFloatScalar(self): + with self.cached_session(): + tensor = ops.convert_to_tensor(434.43) + with self.captureWritesToStream(sys.stderr) as printed: + print_op = logging_ops.print_v2(tensor) + self.evaluate(print_op) + expected = "434.43" + self.assertTrue((expected + "\n") in printed.contents()) + + @test_util.run_in_graph_and_eager_modes() + def testPrintStringScalar(self): + with self.cached_session(): + tensor = ops.convert_to_tensor("scalar") + with self.captureWritesToStream(sys.stderr) as printed: + print_op = logging_ops.print_v2(tensor) + self.evaluate(print_op) + expected = "scalar" + self.assertTrue((expected + "\n") in printed.contents()) + + @test_util.run_in_graph_and_eager_modes() + def testPrintComplexTensorStruct(self): + with self.cached_session(): + tensor = math_ops.range(10) + small_tensor = constant_op.constant([0.3, 12.4, -16.1]) + big_tensor = math_ops.mul(tensor, 10) + with self.captureWritesToStream(sys.stderr) as printed: + print_op = logging_ops.print_v2( + "first:", tensor, "middle:", + {"small": small_tensor, "Big": big_tensor}, 10, + [tensor * 2, tensor]) + self.evaluate(print_op) + # Note that the keys in the dict will always be sorted, + # so 'Big' comes before 'small' + expected = ("first: [0 1 2 ... 7 8 9] " + "middle: {'Big': [0 10 20 ... 70 80 90], " + "'small': [0.3 12.4 -16.1]} " + "10 [[0 2 4 ... 14 16 18], [0 1 2 ... 7 8 9]]") + self.assertTrue((expected + "\n") in printed.contents()) + + @test_util.run_in_graph_and_eager_modes() + def testPrintSparseTensor(self): + with self.cached_session(): + ind = [[0, 0], [1, 0], [1, 3], [4, 1], [1, 4], [3, 2], [3, 3]] + val = [0, 10, 13, 4, 14, 32, 33] + shape = [5, 6] + + sparse = sparse_tensor.SparseTensor( + constant_op.constant(ind, dtypes.int64), + constant_op.constant(val, dtypes.int64), + constant_op.constant(shape, dtypes.int64)) + + with self.captureWritesToStream(sys.stderr) as printed: + print_op = logging_ops.print_v2(sparse) + self.evaluate(print_op) + expected = ("'SparseTensor(indices=[[0 0]\n" + " [1 0]\n" + " [1 3]\n" + " ...\n" + " [1 4]\n" + " [3 2]\n" + " [3 3]], values=[0 10 13 ... 14 32 33], shape=[5 6])'") + self.assertTrue((expected + "\n") in printed.contents()) + + @test_util.run_in_graph_and_eager_modes() + def testPrintSparseTensorInDataStruct(self): + with self.cached_session(): + ind = [[0, 0], [1, 0], [1, 3], [4, 1], [1, 4], [3, 2], [3, 3]] + val = [0, 10, 13, 4, 14, 32, 33] + shape = [5, 6] + + sparse = sparse_tensor.SparseTensor( + constant_op.constant(ind, dtypes.int64), + constant_op.constant(val, dtypes.int64), + constant_op.constant(shape, dtypes.int64)) + + with self.captureWritesToStream(sys.stderr) as printed: + print_op = logging_ops.print_v2([sparse]) + self.evaluate(print_op) + expected = ("['SparseTensor(indices=[[0 0]\n" + " [1 0]\n" + " [1 3]\n" + " ...\n" + " [1 4]\n" + " [3 2]\n" + " [3 3]], values=[0 10 13 ... 14 32 33], shape=[5 6])']") + self.assertTrue((expected + "\n") in printed.contents()) + + @test_util.run_in_graph_and_eager_modes() + def testPrintOneTensorStdout(self): + with self.cached_session(): + tensor = math_ops.range(10) + with self.captureWritesToStream(sys.stdout) as printed: + print_op = logging_ops.print_v2( + tensor, output_stream=sys.stdout) + self.evaluate(print_op) + expected = "[0 1 2 ... 7 8 9]" + self.assertTrue((expected + "\n") in printed.contents()) + + @test_util.run_in_graph_and_eager_modes() + def testInvalidOutputStreamRaisesError(self): + with self.cached_session(): + tensor = math_ops.range(10) + with self.assertRaises(ValueError): + print_op = logging_ops.print_v2( + tensor, output_stream="unknown") + self.evaluate(print_op) + + def testPrintOpName(self): + with self.cached_session(): + tensor = math_ops.range(10) + print_op = logging_ops.print_v2(tensor, name="print_name") + self.assertEqual(print_op.name, "print_name") + + def testNoDuplicateFormatOpGraphModeAfterExplicitFormat(self): + with self.cached_session(): + tensor = math_ops.range(10) + formatted_string = string_ops.string_format("{}", tensor) + print_op = logging_ops.print_v2(formatted_string) + self.evaluate(print_op) + graph_ops = ops.get_default_graph().get_operations() + format_ops = [op for op in graph_ops if op.type == "StringFormat"] + # Should be only 1 format_op for graph mode. + self.assertEqual(len(format_ops), 1) + + def testPrintOneTensorEagerOnOpCreate(self): + with self.cached_session(): + with context.eager_mode(): + tensor = math_ops.range(10) + expected = "[0 1 2 ... 7 8 9]" + with self.captureWritesToStream(sys.stderr) as printed: + logging_ops.print_v2(tensor) + self.assertTrue((expected + "\n") in printed.contents()) + + @test_util.run_in_graph_and_eager_modes() + def testPrintInDefunWithoutExplicitEvalOfPrint(self): + @function.defun + def f(): + tensor = math_ops.range(10) + logging_ops.print_v2(tensor) + return tensor + + expected = "[0 1 2 ... 7 8 9]" + with self.captureWritesToStream(sys.stderr) as printed_one: + x = f() + self.evaluate(x) + self.assertTrue((expected + "\n") in printed_one.contents()) + + # We execute the function again to make sure it doesn't only print on the + # first call. + with self.captureWritesToStream(sys.stderr) as printed_two: + y = f() + self.evaluate(y) + self.assertTrue((expected + "\n") in printed_two.contents()) + + class PrintGradientTest(test.TestCase): @test_util.run_in_graph_and_eager_modes @@ -65,8 +336,13 @@ class PrintGradientTest(test.TestCase): inp_printed = logging_ops.Print(inp, [inp]) self.assertEqual(inp.get_shape(), inp_printed.get_shape()) + def testPrintString(self): + inp = constant_op.constant(2.0, shape=[100, 32]) + inp_printed = logging_ops.Print(inp, ["hello"]) + self.assertEqual(inp.get_shape(), inp_printed.get_shape()) + def testPrintGradient(self): - with self.test_session(): + with self.cached_session(): inp = constant_op.constant(2.0, shape=[100, 32], name="in") w = constant_op.constant(4.0, shape=[10, 100], name="w") wx = math_ops.matmul(w, inp, name="wx") diff --git a/tensorflow/python/kernel_tests/lookup_ops_test.py b/tensorflow/python/kernel_tests/lookup_ops_test.py index 5f08339fe5..6791a03e2e 100644 --- a/tensorflow/python/kernel_tests/lookup_ops_test.py +++ b/tensorflow/python/kernel_tests/lookup_ops_test.py @@ -21,6 +21,7 @@ import os import numpy as np from tensorflow.python.client import session +from tensorflow.python.eager import context from tensorflow.python.framework import constant_op from tensorflow.python.framework import dtypes from tensorflow.python.framework import errors_impl @@ -29,6 +30,7 @@ from tensorflow.python.framework import sparse_tensor from tensorflow.python.framework import test_util from tensorflow.python.ops import array_ops from tensorflow.python.ops import lookup_ops +from tensorflow.python.ops import variables from tensorflow.python.platform import test from tensorflow.python.training import server_lib @@ -36,7 +38,7 @@ from tensorflow.python.training import server_lib class HashTableOpTest(test.TestCase): def testHashTable(self): - with self.test_session(): + with self.cached_session(): default_val = -1 keys = constant_op.constant(["brain", "salad", "surgery"]) values = constant_op.constant([0, 1, 2], dtypes.int64) @@ -53,8 +55,14 @@ class HashTableOpTest(test.TestCase): result = output.eval() self.assertAllEqual([0, 1, -1], result) + exported_keys_tensor, exported_values_tensor = table.export() + + self.assertItemsEqual([b"brain", b"salad", b"surgery"], + exported_keys_tensor.eval()) + self.assertItemsEqual([0, 1, 2], exported_values_tensor.eval()) + def testHashTableFindHighRank(self): - with self.test_session(): + with self.cached_session(): default_val = -1 keys = constant_op.constant(["brain", "salad", "surgery"]) values = constant_op.constant([0, 1, 2], dtypes.int64) @@ -72,7 +80,7 @@ class HashTableOpTest(test.TestCase): self.assertAllEqual([[0, 1], [-1, -1]], result) def testHashTableInitWithPythonArrays(self): - with self.test_session(): + with self.cached_session(): default_val = -1 keys = ["brain", "salad", "surgery"] values = [0, 1, 2] @@ -90,7 +98,7 @@ class HashTableOpTest(test.TestCase): self.assertAllEqual([0, 1, -1], result) def testHashTableInitWithNumPyArrays(self): - with self.test_session(): + with self.cached_session(): default_val = -1 keys = np.array(["brain", "salad", "surgery"], dtype=np.str) values = np.array([0, 1, 2], dtype=np.int64) @@ -107,7 +115,7 @@ class HashTableOpTest(test.TestCase): self.assertAllEqual([0, 1, -1], result) def testMultipleHashTables(self): - with self.test_session() as sess: + with self.cached_session() as sess: default_val = -1 keys = constant_op.constant(["brain", "salad", "surgery"]) values = constant_op.constant([0, 1, 2], dtypes.int64) @@ -135,7 +143,7 @@ class HashTableOpTest(test.TestCase): self.assertAllEqual([0, 1, -1], out3) def testHashTableWithTensorDefault(self): - with self.test_session(): + with self.cached_session(): default_val = constant_op.constant(-1, dtypes.int64) keys = constant_op.constant(["brain", "salad", "surgery"]) values = constant_op.constant([0, 1, 2], dtypes.int64) @@ -150,7 +158,7 @@ class HashTableOpTest(test.TestCase): self.assertAllEqual([0, 1, -1], result) def testHashTableWithSparseTensorInput(self): - with self.test_session() as sess: + with self.cached_session() as sess: default_val = constant_op.constant(-1, dtypes.int64) keys = constant_op.constant(["brain", "salad", "surgery"]) values = constant_op.constant([0, 1, 2], dtypes.int64) @@ -173,7 +181,7 @@ class HashTableOpTest(test.TestCase): self.assertAllEqual(sp_shape, out_shape) def testSignatureMismatch(self): - with self.test_session(): + with self.cached_session(): default_val = -1 keys = constant_op.constant(["brain", "salad", "surgery"]) values = constant_op.constant([0, 1, 2], dtypes.int64) @@ -181,6 +189,11 @@ class HashTableOpTest(test.TestCase): lookup_ops.KeyValueTensorInitializer(keys, values), default_val) table.init.run() + # Ref types do not produce a lookup signature mismatch. + input_string_ref = variables.Variable("brain") + variables.global_variables_initializer().run() + self.assertEqual(0, table.lookup(input_string_ref).eval()) + input_string = constant_op.constant([1, 2, 3], dtypes.int64) with self.assertRaises(TypeError): table.lookup(input_string) @@ -190,7 +203,7 @@ class HashTableOpTest(test.TestCase): lookup_ops.KeyValueTensorInitializer(keys, values), "UNK") def testDTypes(self): - with self.test_session(): + with self.cached_session(): default_val = -1 with self.assertRaises(TypeError): lookup_ops.HashTable( @@ -198,7 +211,7 @@ class HashTableOpTest(test.TestCase): dtypes.int64), default_val) def testNotInitialized(self): - with self.test_session(): + with self.cached_session(): default_val = -1 table = lookup_ops.HashTable( lookup_ops.KeyValueTensorInitializer( @@ -211,7 +224,7 @@ class HashTableOpTest(test.TestCase): output.eval() def testInitializeTwice(self): - with self.test_session(): + with self.cached_session(): default_val = -1 keys = constant_op.constant(["brain", "salad", "surgery"]) values = constant_op.constant([0, 1, 2], dtypes.int64) @@ -223,7 +236,7 @@ class HashTableOpTest(test.TestCase): table.init.run() def testInitializationWithInvalidDimensions(self): - with self.test_session(): + with self.cached_session(): default_val = -1 keys = constant_op.constant(["brain", "salad", "surgery"]) values = constant_op.constant([0, 1, 2, 3, 4], dtypes.int64) @@ -261,6 +274,21 @@ class HashTableOpTest(test.TestCase): table.init.run() self.assertAllEqual(3, table.size().eval()) + def testHashTableInt32String(self): + with self.cached_session(): + default_val = "n/a" + keys = constant_op.constant([0, 1, 2], dtypes.int32) + values = constant_op.constant(["brain", "salad", "surgery"]) + table = lookup_ops.HashTable( + lookup_ops.KeyValueTensorInitializer(keys, values), default_val) + table.init.run() + + input_tensor = constant_op.constant([0, 1, -1]) + output = table.lookup(input_tensor) + + result = output.eval() + self.assertAllEqual([b"brain", b"salad", b"n/a"], result) + class IndexTableFromFile(test.TestCase): @@ -272,7 +300,7 @@ class IndexTableFromFile(test.TestCase): def test_string_index_table_from_file(self): vocabulary_file = self._createVocabFile("f2i_vocab1.txt") - with self.test_session(): + with self.cached_session(): table = lookup_ops.index_table_from_file( vocabulary_file=vocabulary_file, num_oov_buckets=1) ids = table.lookup(constant_op.constant(["salad", "surgery", "tarkus"])) @@ -284,7 +312,7 @@ class IndexTableFromFile(test.TestCase): def test_string_index_table_from_multicolumn_file(self): vocabulary_file = self._createVocabFile( "f2i_vocab1.txt", values=("brain\t300", "salad\t20", "surgery\t1")) - with self.test_session(): + with self.cached_session(): table = lookup_ops.index_table_from_file( vocabulary_file=vocabulary_file, num_oov_buckets=1, @@ -299,7 +327,7 @@ class IndexTableFromFile(test.TestCase): def test_string_index_table_from_multicolumn_file_custom_delimiter(self): vocabulary_file = self._createVocabFile( "f2i_vocab1.txt", values=("brain 300", "salad 20", "surgery 1")) - with self.test_session(): + with self.cached_session(): table = lookup_ops.index_table_from_file( vocabulary_file=vocabulary_file, num_oov_buckets=1, @@ -314,7 +342,7 @@ class IndexTableFromFile(test.TestCase): def test_string_index_table_from_file_tensor_filename(self): vocabulary_file = self._createVocabFile("f2i_vocab1.txt") - with self.test_session(): + with self.cached_session(): vocabulary_file = constant_op.constant(vocabulary_file) table = lookup_ops.index_table_from_file( vocabulary_file=vocabulary_file, num_oov_buckets=1) @@ -328,13 +356,14 @@ class IndexTableFromFile(test.TestCase): def test_string_index_table_from_file_placeholder_filename(self): vocabulary_file = self._createVocabFile("f2i_vocab1.txt") - with self.test_session(): + with self.cached_session(): vocabulary_placeholder = array_ops.placeholder(dtypes.string, []) table = lookup_ops.index_table_from_file( vocabulary_file=vocabulary_placeholder, num_oov_buckets=1) ids = table.lookup(constant_op.constant(["salad", "surgery", "tarkus"])) self.assertRaises(errors_impl.OpError, ids.eval) + feed_dict = {vocabulary_placeholder.name: vocabulary_file} lookup_ops.tables_initializer().run(feed_dict=feed_dict) self.assertAllEqual((1, 2, 3), ids.eval()) @@ -344,7 +373,7 @@ class IndexTableFromFile(test.TestCase): def test_int32_index_table_from_file(self): vocabulary_file = self._createVocabFile( "f2i_vocab2.txt", values=("42", "1", "-1000")) - with self.test_session(): + with self.cached_session(): table = lookup_ops.index_table_from_file( vocabulary_file=vocabulary_file, num_oov_buckets=1, @@ -359,7 +388,7 @@ class IndexTableFromFile(test.TestCase): def test_int64_index_table_from_file(self): vocabulary_file = self._createVocabFile( "f2i_vocab3.txt", values=("42", "1", "-1000")) - with self.test_session(): + with self.cached_session(): table = lookup_ops.index_table_from_file( vocabulary_file=vocabulary_file, num_oov_buckets=1, @@ -374,7 +403,7 @@ class IndexTableFromFile(test.TestCase): def test_index_table_from_file_with_default_value(self): default_value = -42 vocabulary_file = self._createVocabFile("f2i_vocab4.txt") - with self.test_session(): + with self.cached_session(): table = lookup_ops.index_table_from_file( vocabulary_file=vocabulary_file, default_value=default_value) ids = table.lookup(constant_op.constant(["salad", "surgery", "tarkus"])) @@ -385,7 +414,7 @@ class IndexTableFromFile(test.TestCase): def test_index_table_from_file_with_oov_buckets(self): vocabulary_file = self._createVocabFile("f2i_vocab5.txt") - with self.test_session(): + with self.cached_session(): table = lookup_ops.index_table_from_file( vocabulary_file=vocabulary_file, num_oov_buckets=1000) ids = table.lookup( @@ -432,7 +461,7 @@ class IndexTableFromFile(test.TestCase): def test_index_table_from_file_with_vocab_size_too_small(self): vocabulary_file = self._createVocabFile("f2i_vocab6.txt") - with self.test_session(): + with self.cached_session(): table = lookup_ops.index_table_from_file( vocabulary_file=vocabulary_file, vocab_size=2) ids = table.lookup(constant_op.constant(["salad", "surgery", "tarkus"])) @@ -444,7 +473,7 @@ class IndexTableFromFile(test.TestCase): def test_index_table_from_file_with_vocab_size_too_large(self): vocabulary_file = self._createVocabFile("f2i_vocab7.txt") - with self.test_session(): + with self.cached_session(): table = lookup_ops.index_table_from_file( vocabulary_file=vocabulary_file, vocab_size=4) self.assertRaisesRegexp(errors_impl.InvalidArgumentError, @@ -459,7 +488,7 @@ class IndexTableFromFile(test.TestCase): vocabulary_file=vocabulary_file, vocab_size=0) - with self.test_session(): + with self.cached_session(): table = lookup_ops.index_table_from_file( vocabulary_file=vocabulary_file, vocab_size=3) ids = table.lookup(constant_op.constant(["salad", "surgery", "tarkus"])) @@ -471,7 +500,7 @@ class IndexTableFromFile(test.TestCase): def test_index_table_from_file_with_invalid_hashers(self): vocabulary_file = self._createVocabFile("invalid_hasher.txt") - with self.test_session(): + with self.cached_session(): with self.assertRaises(TypeError): lookup_ops.index_table_from_file( vocabulary_file=vocabulary_file, @@ -490,14 +519,14 @@ class IndexTableFromFile(test.TestCase): def test_index_table_from_file_table_ref_with_oov_buckets(self): vocabulary_file = self._createVocabFile("f2i_vocab9.txt") - with self.test_session(): + with self.cached_session(): table = lookup_ops.index_table_from_file( vocabulary_file=vocabulary_file, num_oov_buckets=1) self.assertIsNotNone(table.table_ref) def test_index_table_from_file_table_ref_without_oov_buckets(self): vocabulary_file = self._createVocabFile("f2i_vocab10.txt") - with self.test_session(): + with self.cached_session(): table = lookup_ops.index_table_from_file( vocabulary_file=vocabulary_file, num_oov_buckets=0) self.assertIsNotNone(table.table_ref) @@ -506,21 +535,21 @@ class IndexTableFromFile(test.TestCase): class KeyValueTensorInitializerTest(test.TestCase): def test_string(self): - with ops.Graph().as_default(), self.test_session(): + with ops.Graph().as_default(), self.cached_session(): init = lookup_ops.KeyValueTensorInitializer( ("brain", "salad", "surgery"), (0, 1, 2), dtypes.string, dtypes.int64) table = lookup_ops.HashTable(init, default_value=-1) table.init.run() def test_int64(self): - with ops.Graph().as_default(), self.test_session(): + with ops.Graph().as_default(), self.cached_session(): init = lookup_ops.KeyValueTensorInitializer((42, 1, -1000), (0, 1, 2), dtypes.int64, dtypes.int64) table = lookup_ops.HashTable(init, default_value=-1) table.init.run() def test_int32(self): - with ops.Graph().as_default(), self.test_session(): + with ops.Graph().as_default(), self.cached_session(): init = lookup_ops.KeyValueTensorInitializer((42, 1, -1000), (0, 1, 2), dtypes.int32, dtypes.int64) table = lookup_ops.HashTable(init, default_value=-1) @@ -531,18 +560,25 @@ class KeyValueTensorInitializerTest(test.TestCase): class IndexTableFromTensor(test.TestCase): + @test_util.run_in_graph_and_eager_modes def test_index_table_from_tensor_with_tensor_init(self): - with self.test_session(): + table = lookup_ops.index_table_from_tensor( + vocabulary_list=("brain", "salad", "surgery"), num_oov_buckets=1) + + if not context.executing_eagerly(): + with self.assertRaises(errors_impl.OpError): + self.evaluate( + table.lookup(constant_op.constant(("salad", "surgery", "tarkus")))) + else: + # Reinitializing a table in eager should work. table = lookup_ops.index_table_from_tensor( vocabulary_list=("brain", "salad", "surgery"), num_oov_buckets=1) - ids = table.lookup(constant_op.constant(("salad", "surgery", "tarkus"))) - - self.assertRaises(errors_impl.OpError, ids.eval) - lookup_ops.tables_initializer().run() - self.assertAllEqual((1, 2, 3), ids.eval()) + self.evaluate(lookup_ops.tables_initializer()) + ids = table.lookup(constant_op.constant(("salad", "surgery", "tarkus"))) + self.assertAllEqual((1, 2, 3), self.evaluate(ids)) def test_int32_index_table_from_tensor_with_tensor_init(self): - with self.test_session(): + with self.cached_session(): table = lookup_ops.index_table_from_tensor( vocabulary_list=(42, 1, -1000), num_oov_buckets=1, dtype=dtypes.int32) ids = table.lookup( @@ -553,7 +589,7 @@ class IndexTableFromTensor(test.TestCase): self.assertAllEqual((1, 2, 3), ids.eval()) def test_int64_index_table_from_tensor_with_tensor_init(self): - with self.test_session(): + with self.cached_session(): table = lookup_ops.index_table_from_tensor( vocabulary_list=(42, 1, -1000), num_oov_buckets=1, dtype=dtypes.int64) ids = table.lookup( @@ -565,7 +601,7 @@ class IndexTableFromTensor(test.TestCase): def test_index_table_from_tensor_with_default_value(self): default_value = -42 - with self.test_session(): + with self.cached_session(): table = lookup_ops.index_table_from_tensor( vocabulary_list=["brain", "salad", "surgery"], default_value=default_value) @@ -576,14 +612,14 @@ class IndexTableFromTensor(test.TestCase): self.assertAllEqual((1, 2, default_value), ids.eval()) def test_index_table_from_tensor_missing_vocabulary_list(self): - with self.test_session(): + with self.cached_session(): with self.assertRaisesRegexp(ValueError, "vocabulary_list must be specified"): lookup_ops.index_table_from_tensor( vocabulary_list=None, num_oov_buckets=1) def test_index_table_from_tensor_empty_vocabulary_list(self): - with self.test_session(): + with self.cached_session(): table = lookup_ops.index_table_from_tensor( vocabulary_list=np.array([], dtype=np.str_), num_oov_buckets=1) ids = table.lookup(constant_op.constant(["salad", "surgery", "brain"])) @@ -593,7 +629,7 @@ class IndexTableFromTensor(test.TestCase): lookup_ops.tables_initializer().run() def test_index_table_from_tensor_with_invalid_hashers(self): - with self.test_session(): + with self.cached_session(): with self.assertRaises(TypeError): lookup_ops.index_table_from_tensor( vocabulary_list=["brain", "salad", "surgery"], @@ -623,7 +659,7 @@ class IndexToStringTableFromFileTest(test.TestCase): type_funcs = [str, constant_op.constant] for type_func in type_funcs: vocabulary_file = type_func(vocabulary_path) - with self.test_session(): + with self.cached_session(): table = lookup_ops.index_to_string_table_from_file( vocabulary_file=vocabulary_file) features = table.lookup( @@ -636,7 +672,7 @@ class IndexToStringTableFromFileTest(test.TestCase): def test_index_to_string_table_from_multicolumn_file(self): vocabulary_file = self._createVocabFile( "f2i_vocab1.txt", values=("brain\t300", "salad\t20", "surgery\t1")) - with self.test_session(): + with self.cached_session(): table = lookup_ops.index_to_string_table_from_file( vocabulary_file=vocabulary_file, key_column_index=lookup_ops.TextFileIndex.LINE_NUMBER, @@ -650,7 +686,7 @@ class IndexToStringTableFromFileTest(test.TestCase): def test_index_to_string_table_from_multicolumn_file_custom_delimiter(self): vocabulary_file = self._createVocabFile( "f2i_vocab1.txt", values=("brain 300", "salad 20", "surgery 1")) - with self.test_session(): + with self.cached_session(): table = lookup_ops.index_to_string_table_from_file( vocabulary_file=vocabulary_file, key_column_index=lookup_ops.TextFileIndex.LINE_NUMBER, @@ -665,7 +701,7 @@ class IndexToStringTableFromFileTest(test.TestCase): def test_index_to_string_table_with_default_value(self): default_value = b"NONE" vocabulary_file = self._createVocabFile("f2i_vocab2.txt") - with self.test_session(): + with self.cached_session(): table = lookup_ops.index_to_string_table_from_file( vocabulary_file=vocabulary_file, default_value=default_value) features = table.lookup(constant_op.constant([1, 2, 4], dtypes.int64)) @@ -677,7 +713,7 @@ class IndexToStringTableFromFileTest(test.TestCase): def test_index_to_string_table_with_vocab_size_too_small(self): default_value = b"NONE" vocabulary_file = self._createVocabFile("f2i_vocab2.txt") - with self.test_session(): + with self.cached_session(): table = lookup_ops.index_to_string_table_from_file( vocabulary_file=vocabulary_file, vocab_size=2, @@ -690,7 +726,7 @@ class IndexToStringTableFromFileTest(test.TestCase): def test_index_to_string_table_with_vocab_size_too_large(self): vocabulary_file = self._createVocabFile("f2i_vocab6.txt") - with self.test_session(): + with self.cached_session(): table = lookup_ops.index_to_string_table_from_file( vocabulary_file=vocabulary_file, vocab_size=4) features = table.lookup(constant_op.constant([1, 2, 4], dtypes.int64)) @@ -702,7 +738,7 @@ class IndexToStringTableFromFileTest(test.TestCase): def test_index_to_string_table_with_vocab_size(self): vocabulary_file = self._createVocabFile("f2i_vocab7.txt") - with self.test_session(): + with self.cached_session(): table = lookup_ops.index_to_string_table_from_file( vocabulary_file=vocabulary_file, vocab_size=3) features = table.lookup(constant_op.constant([1, 2, 4], dtypes.int64)) @@ -715,7 +751,7 @@ class IndexToStringTableFromFileTest(test.TestCase): class IndexToStringTableFromTensorTest(test.TestCase): def test_index_to_string_table_from_tensor(self): - with self.test_session(): + with self.cached_session(): vocabulary_list = constant_op.constant(["brain", "salad", "surgery"]) table = lookup_ops.index_to_string_table_from_tensor( vocabulary_list=vocabulary_list) @@ -729,7 +765,7 @@ class IndexToStringTableFromTensorTest(test.TestCase): features.eval()) def test_duplicate_entries(self): - with self.test_session(): + with self.cached_session(): vocabulary_list = constant_op.constant(["hello", "hello"]) table = lookup_ops.index_to_string_table_from_tensor( vocabulary_list=vocabulary_list) @@ -740,7 +776,7 @@ class IndexToStringTableFromTensorTest(test.TestCase): def test_index_to_string_with_default_value(self): default_value = b"NONE" - with self.test_session(): + with self.cached_session(): vocabulary_list = constant_op.constant(["brain", "salad", "surgery"]) table = lookup_ops.index_to_string_table_from_tensor( vocabulary_list=vocabulary_list, default_value=default_value) @@ -761,28 +797,26 @@ class InitializeTableFromFileOpTest(test.TestCase): f.write("\n".join(values) + "\n") return vocabulary_file + @test_util.run_in_graph_and_eager_modes def testInitializeStringTable(self): vocabulary_file = self._createVocabFile("one_column_1.txt") + default_value = -1 + table = lookup_ops.HashTable( + lookup_ops.TextFileInitializer( + vocabulary_file, dtypes.string, lookup_ops.TextFileIndex.WHOLE_LINE, + dtypes.int64, lookup_ops.TextFileIndex.LINE_NUMBER), default_value) + self.evaluate(table.init) - with self.test_session(): - default_value = -1 - table = lookup_ops.HashTable( - lookup_ops.TextFileInitializer( - vocabulary_file, dtypes.string, - lookup_ops.TextFileIndex.WHOLE_LINE, dtypes.int64, - lookup_ops.TextFileIndex.LINE_NUMBER), default_value) - table.init.run() - - output = table.lookup(constant_op.constant(["brain", "salad", "tank"])) + output = table.lookup(constant_op.constant(["brain", "salad", "tank"])) - result = output.eval() - self.assertAllEqual([0, 1, -1], result) + result = self.evaluate(output) + self.assertAllEqual([0, 1, -1], result) def testInitializeInt64Table(self): vocabulary_file = self._createVocabFile( "one_column_int64.txt", values=("42", "1", "-1000")) - with self.test_session(): + with self.cached_session(): default_value = -1 table = lookup_ops.HashTable( lookup_ops.TextFileInitializer( @@ -800,7 +834,7 @@ class InitializeTableFromFileOpTest(test.TestCase): def testInitializeIndexTable(self): vocabulary_file = self._createVocabFile("one_column_2.txt") - with self.test_session(): + with self.cached_session(): default_value = "UNK" key_index = lookup_ops.TextFileIndex.LINE_NUMBER value_index = lookup_ops.TextFileIndex.WHOLE_LINE @@ -821,7 +855,7 @@ class InitializeTableFromFileOpTest(test.TestCase): with open(vocabulary_file, "w") as f: f.write("\n".join(["0\tbrain\t1", "1\tsalad\t5", "2\tsurgery\t6"]) + "\n") - with self.test_session(): + with self.cached_session(): default_value = -1 key_index = 1 value_index = 2 @@ -843,7 +877,7 @@ class InitializeTableFromFileOpTest(test.TestCase): with open(vocabulary_file, "w") as f: f.write("\n".join(["0\tbrain\t1", "1\tsalad\t5", "2\tsurgery\t6"]) + "\n") - with self.test_session(): + with self.cached_session(): default_value = -1 key_index = 2 value_index = 1 @@ -857,7 +891,7 @@ class InitializeTableFromFileOpTest(test.TestCase): def testInvalidDataType(self): vocabulary_file = self._createVocabFile("one_column_3.txt") - with self.test_session(): + with self.cached_session(): default_value = "UNK" key_index = lookup_ops.TextFileIndex.WHOLE_LINE value_index = lookup_ops.TextFileIndex.LINE_NUMBER @@ -870,7 +904,7 @@ class InitializeTableFromFileOpTest(test.TestCase): def testInvalidIndex(self): vocabulary_file = self._createVocabFile("one_column_4.txt") - with self.test_session(): + with self.cached_session(): default_value = -1 key_index = 1 # second column of the line value_index = lookup_ops.TextFileIndex.LINE_NUMBER @@ -885,7 +919,7 @@ class InitializeTableFromFileOpTest(test.TestCase): def testInitializeSameTableWithMultipleNodes(self): vocabulary_file = self._createVocabFile("one_column_5.txt") - with self.test_session() as sess: + with self.cached_session() as sess: shared_name = "shared-one-columm" default_value = -1 table1 = lookup_ops.HashTable( @@ -924,7 +958,7 @@ class InitializeTableFromFileOpTest(test.TestCase): self.assertAllEqual([0, 1, -1], out3) def testInitializeTableWithNoFilename(self): - with self.test_session(): + with self.cached_session(): default_value = -1 with self.assertRaises(ValueError): lookup_ops.HashTable( @@ -934,7 +968,7 @@ class InitializeTableFromFileOpTest(test.TestCase): default_value) def testInitializeWithVocabSize(self): - with self.test_session(): + with self.cached_session(): default_value = -1 vocab_size = 3 vocabulary_file1 = self._createVocabFile("one_column6.txt") @@ -982,7 +1016,7 @@ class InitializeTableFromFileOpTest(test.TestCase): def testFeedVocabularyName(self): vocabulary_file = self._createVocabFile("feed_vocabulary.txt") - with self.test_session(): + with self.cached_session(): default_value = -1 table = lookup_ops.HashTable( lookup_ops.TextFileInitializer( @@ -1008,7 +1042,7 @@ class InitializeTableFromFileOpTest(test.TestCase): def testInvalidFilenames(self): vocabulary_file = self._createVocabFile("filename_shape.txt") - with self.test_session(): + with self.cached_session(): default_value = -1 # Invalid data type @@ -1031,7 +1065,7 @@ class InitializeTableFromFileOpTest(test.TestCase): def testIdToStringTable(self): vocab_file = self._createVocabFile("feat_to_id_1.txt") - with self.test_session(): + with self.cached_session(): default_value = "UNK" vocab_size = 3 table = lookup_ops.HashTable( @@ -1048,7 +1082,7 @@ class InitializeTableFromFileOpTest(test.TestCase): def testStringToIdTable(self): vocab_file = self._createVocabFile("feat_to_id_2.txt") - with self.test_session(): + with self.cached_session(): default_value = -1 vocab_size = 3 table = lookup_ops.HashTable( @@ -1065,7 +1099,7 @@ class InitializeTableFromFileOpTest(test.TestCase): def testInt64ToIdTable(self): vocab_file = self._createVocabFile( "feat_to_id_3.txt", values=("42", "1", "-1000")) - with self.test_session(): + with self.cached_session(): default_value = -1 vocab_size = 3 table = lookup_ops.HashTable( @@ -1090,7 +1124,7 @@ class IdTableWithHashBucketsTest(test.TestCase): def testStringIdTableWithHashBuckets(self): vocab_file = self._createVocabFile("feat_to_id_1.txt") - with self.test_session(): + with self.cached_session(): default_value = -1 vocab_size = 3 oov_buckets = 1 @@ -1110,7 +1144,7 @@ class IdTableWithHashBucketsTest(test.TestCase): def testInt32IdTableWithHashBuckets(self): vocab_file = self._createVocabFile("feat_to_id_2.txt", ("42", "1", "-1000")) - with self.test_session(): + with self.cached_session(): default_value = -1 vocab_size = 3 oov_buckets = 1 @@ -1132,7 +1166,7 @@ class IdTableWithHashBucketsTest(test.TestCase): def testInt64IdTableWithHashBuckets(self): vocab_file = self._createVocabFile("feat_to_id_3.txt", ("42", "1", "-1000")) - with self.test_session(): + with self.cached_session(): default_value = -1 vocab_size = 3 oov_buckets = 1 @@ -1151,7 +1185,7 @@ class IdTableWithHashBucketsTest(test.TestCase): self.assertEquals(vocab_size + oov_buckets, table.size().eval()) def testStringIdTableWithOnlyHashBucket(self): - with self.test_session(): + with self.cached_session(): oov_buckets = 5 # Set a table that only uses hash buckets, for each input value returns @@ -1172,7 +1206,7 @@ class IdTableWithHashBucketsTest(test.TestCase): self.assertEquals(oov_buckets, table.size().eval()) def testInt32IdTableWithOnlyHashBucket(self): - with self.test_session(): + with self.cached_session(): oov_buckets = 5 # Set a table that only uses hash buckets, for each input value returns @@ -1194,20 +1228,20 @@ class IdTableWithHashBucketsTest(test.TestCase): self.assertEquals(oov_buckets, table.size().eval()) def testFloat64IdTableWithOnlyHashBucket(self): - with self.test_session(): + with self.cached_session(): with self.assertRaisesRegexp(TypeError, "Invalid key_dtype"): lookup_ops.IdTableWithHashBuckets( None, num_oov_buckets=5, key_dtype=dtypes.float64) def testBoolIdTableWithOnlyHashBucket(self): - with self.test_session(): + with self.cached_session(): with self.assertRaisesRegexp(TypeError, "Invalid key_dtype"): lookup_ops.IdTableWithHashBuckets( None, num_oov_buckets=5, key_dtype=dtypes.bool) def testIdTableWithHashBucketsWithMultipleInitializers(self): vocab_file = self._createVocabFile("feat_to_id_4.txt") - with self.test_session() as sess: + with self.cached_session() as sess: default_value = -1 vocab_size = 3 oov_buckets = 3 @@ -1248,7 +1282,7 @@ class IdTableWithHashBucketsTest(test.TestCase): def testIdTableWithHashBucketsInitializationAcrossSessions(self): vocab_file = self._createVocabFile("feat_to_id_5.txt") shared_name = "across-sessions" - with self.test_session(): + with self.cached_session(): default_value = -1 vocab_size = 3 oov_buckets = 1 @@ -1269,7 +1303,7 @@ class IdTableWithHashBucketsTest(test.TestCase): self.assertAllEqual([0, 1, 2, 3], out1.eval()) self.assertEquals(vocab_size + oov_buckets, table1.size().eval()) - with self.test_session(): + with self.cached_session(): default_value = -1 vocab_size = 3 oov_buckets = 1 @@ -1292,7 +1326,7 @@ class IdTableWithHashBucketsTest(test.TestCase): def testIdTableWithHashBucketsWithMultipleInitializersDifferentDefault(self): vocab_file = self._createVocabFile("feat_to_id_6.txt") - with self.test_session() as sess: + with self.cached_session() as sess: default_value1 = -1 vocab_size = 3 oov_buckets = 0 @@ -1328,7 +1362,7 @@ class IdTableWithHashBucketsTest(test.TestCase): vocab_file = self._createVocabFile("feat_to_id_7.txt") input_indices = [[0, 0], [0, 1], [2, 0], [2, 2], [3, 0]] input_shape = [4, 4] - with self.test_session() as sess: + with self.cached_session() as sess: sp_features = sparse_tensor.SparseTensor( constant_op.constant(input_indices, dtypes.int64), constant_op.constant(["brain", "salad", "brain", "surgery", "tarkus"], @@ -1355,7 +1389,7 @@ class IdTableWithHashBucketsTest(test.TestCase): def testInt32SparseTensor(self): input_indices = [[0, 0], [0, 1], [2, 0], [2, 2], [3, 0]] input_shape = [4, 4] - with self.test_session() as sess: + with self.cached_session() as sess: sp_features = sparse_tensor.SparseTensor( constant_op.constant(input_indices, dtypes.int64), constant_op.constant([42, 1, 42, -1000, 11], dtypes.int32), @@ -1383,7 +1417,7 @@ class IdTableWithHashBucketsTest(test.TestCase): def testInt64SparseTensor(self): input_indices = [[0, 0], [0, 1], [2, 0], [2, 2], [3, 0]] input_shape = [4, 4] - with self.test_session() as sess: + with self.cached_session() as sess: sp_features = sparse_tensor.SparseTensor( constant_op.constant(input_indices, dtypes.int64), constant_op.constant([42, 1, 42, -1000, 11], dtypes.int64), @@ -1410,7 +1444,7 @@ class IdTableWithHashBucketsTest(test.TestCase): def testIdTableWithHashBucketsWithInvalidHashers(self): vocab_file = self._createVocabFile("feat_to_id_4.txt") - with self.test_session(): + with self.cached_session(): default_value = -1 vocab_size = 3 oov_buckets = 1 @@ -1451,7 +1485,7 @@ class IdTableWithHashBucketsTest(test.TestCase): hasher_spec=lookup_ops.StrongHashSpec([None, 2])) def testIdTableWithHashBucketsNoInnerTable(self): - with self.test_session(): + with self.cached_session(): table = lookup_ops.IdTableWithHashBuckets(None, num_oov_buckets=1) self.assertIsNone(table.table_ref) diff --git a/tensorflow/python/kernel_tests/losses_test.py b/tensorflow/python/kernel_tests/losses_test.py index 87fc715783..3ce0b74263 100644 --- a/tensorflow/python/kernel_tests/losses_test.py +++ b/tensorflow/python/kernel_tests/losses_test.py @@ -61,62 +61,62 @@ class AbsoluteDifferenceLossTest(test.TestCase): self._labels = constant_op.constant([1, 9, 2, -5, -2, 6], shape=(2, 3)) def testValueErrorThrownWhenWeightIsNone(self): - with self.test_session(): + with self.cached_session(): with self.assertRaises(ValueError): losses.absolute_difference( self._predictions, self._predictions, weights=None) def testAllCorrectNoLossWeight(self): loss = losses.absolute_difference(self._predictions, self._predictions) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(0.0, loss.eval(), 3) def testNonZeroLoss(self): loss = losses.absolute_difference(self._labels, self._predictions) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(5.5, loss.eval(), 3) def testNonZeroLossWithPythonScalarWeight(self): weights = 2.3 loss = losses.absolute_difference(self._labels, self._predictions, weights) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(5.5 * weights, loss.eval(), 3) def testNonZeroLossWithScalarTensorWeight(self): weights = 2.3 loss = losses.absolute_difference(self._labels, self._predictions, constant_op.constant(weights)) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(5.5 * weights, loss.eval(), 3) def testNonZeroLossWithOneDimBatchSpecificWeights(self): weights = constant_op.constant((1.2, 0.0), shape=(2, 1)) loss = losses.absolute_difference(self._labels, self._predictions, weights) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(5.6, loss.eval(), 3) def testNonZeroLossWithTwoDimBatchSpecificWeights(self): weights = constant_op.constant([1.2, 0.0], shape=[2, 1]) loss = losses.absolute_difference(self._labels, self._predictions, weights) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(5.6, loss.eval(), 3) def testNonZeroLossWithSampleSpecificWeights(self): weights = constant_op.constant([3, 6, 5, 0, 4, 2], shape=[2, 3]) loss = losses.absolute_difference(self._labels, self._predictions, weights) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(16.6, loss.eval(), 3) def testNonZeroLossWithSampleSpecificWeightsMostZero(self): weights = constant_op.constant([0, 0, 0, 0, 0, 2], shape=[2, 3]) loss = losses.absolute_difference(self._labels, self._predictions, weights) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(6.0, loss.eval(), 3) def testLossWithSampleSpecificWeightsAllZero(self): weights = array_ops.zeros((2, 3)) loss = losses.absolute_difference(self._labels, self._predictions, weights) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(0.0, loss.eval(), 3) @test_util.assert_no_new_pyobjects_executing_eagerly @@ -134,12 +134,12 @@ class SoftmaxCrossEntropyLossTest(test.TestCase): logits = constant_op.constant([[10.0, 0.0, 0.0], [0.0, 10.0, 0.0], [0.0, 0.0, 10.0]]) labels = constant_op.constant([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) - with self.test_session(): + with self.cached_session(): with self.assertRaises(ValueError): losses.softmax_cross_entropy(labels, logits, weights=None) def testAllCorrect(self): - with self.test_session(): + with self.cached_session(): logits = constant_op.constant([[10.0, 0.0, 0.0], [0.0, 10.0, 0.0], [0.0, 0.0, 10.0]]) labels = constant_op.constant([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) @@ -152,7 +152,7 @@ class SoftmaxCrossEntropyLossTest(test.TestCase): [0.0, 0.0, 10.0]]) labels = constant_op.constant([[0, 0, 1], [1, 0, 0], [0, 1, 0]]) - with self.test_session(): + with self.cached_session(): loss = losses.softmax_cross_entropy(labels, logits) self.assertEquals(loss.op.name, 'softmax_cross_entropy_loss/value') self.assertAlmostEqual(loss.eval(), 10.0, 3) @@ -162,7 +162,7 @@ class SoftmaxCrossEntropyLossTest(test.TestCase): [0.0, 0.0, 10.0]]) labels = constant_op.constant([[0, 0, 1], [1, 0, 0], [0, 1, 0]]) weights = 2.3 - with self.test_session(): + with self.cached_session(): loss = losses.softmax_cross_entropy(labels, logits, weights) self.assertAlmostEqual(weights * 10.0, loss.eval(), 3) @@ -171,7 +171,7 @@ class SoftmaxCrossEntropyLossTest(test.TestCase): [0.0, 0.0, 10.0]]) labels = constant_op.constant([[0, 0, 1], [1, 0, 0], [0, 1, 0]]) weights = 2.3 - with self.test_session(): + with self.cached_session(): loss = losses.softmax_cross_entropy(labels, logits, constant_op.constant(weights)) self.assertAlmostEqual(weights * 10.0, loss.eval(), 3) @@ -181,7 +181,7 @@ class SoftmaxCrossEntropyLossTest(test.TestCase): [0.0, 0.0, 10.0]]) labels = constant_op.constant([[0, 0, 1], [1, 0, 0], [0, 1, 0]]) weights = constant_op.constant((1.2, 3.4, 5.6)) - with self.test_session(): + with self.cached_session(): loss = losses.softmax_cross_entropy(labels, logits, weights) self.assertAlmostEqual((1.2 + 3.4 + 5.6) * 10.0 / 3.0, loss.eval(), 3) @@ -190,7 +190,7 @@ class SoftmaxCrossEntropyLossTest(test.TestCase): [0.0, 0.0, 10.0]]) labels = constant_op.constant([[0, 0, 1], [1, 0, 0], [0, 1, 0]]) weights = constant_op.constant([0, 0, 0], shape=[3]) - with self.test_session(): + with self.cached_session(): loss = losses.softmax_cross_entropy(labels, logits, weights) self.assertAlmostEqual(0.0, loss.eval(), 3) @@ -199,12 +199,12 @@ class SoftmaxCrossEntropyLossTest(test.TestCase): [0.0, 0.0, 10.0]]) labels = constant_op.constant([[0, 0, 1], [1, 0, 0], [0, 1, 0]]) weights = constant_op.constant([1.2, 0, 0], shape=[3]) - with self.test_session(): + with self.cached_session(): loss = losses.softmax_cross_entropy(labels, logits, weights) self.assertAlmostEqual(12.0, loss.eval(), 3) def testSoftmaxWithMeasurementSpecificWeightsRaisesException(self): - with self.test_session(): + with self.cached_session(): logits = constant_op.constant([[100.0, -100.0, -100.0], [-100.0, 100.0, -100.0], [-100.0, -100.0, 100.0]]) @@ -215,7 +215,7 @@ class SoftmaxCrossEntropyLossTest(test.TestCase): losses.softmax_cross_entropy(labels, logits, weights=weights).eval() def testSoftmaxLabelSmoothing(self): - with self.test_session(): + with self.cached_session(): # Softmax Cross Entropy Loss is: # -\sum_i p_i \log q_i # where for a softmax activation @@ -242,12 +242,12 @@ class SparseSoftmaxCrossEntropyLossTest(test.TestCase): logits = constant_op.constant([[10.0, 0.0, 0.0], [0.0, 10.0, 0.0], [0.0, 0.0, 10.0]]) labels = constant_op.constant([[0], [1], [2]]) - with self.test_session(): + with self.cached_session(): with self.assertRaises(ValueError): losses.sparse_softmax_cross_entropy(labels, logits, weights=None) def testAllCorrectInt32Labels(self): - with self.test_session(): + with self.cached_session(): logits = constant_op.constant([[10.0, 0.0, 0.0], [0.0, 10.0, 0.0], [0.0, 0.0, 10.0]]) labels = constant_op.constant([[0], [1], [2]], dtype=dtypes.int32) @@ -263,7 +263,7 @@ class SparseSoftmaxCrossEntropyLossTest(test.TestCase): losses.sparse_softmax_cross_entropy(labels, logits) def testAllCorrectInt64Labels(self): - with self.test_session(): + with self.cached_session(): logits = constant_op.constant([[10.0, 0.0, 0.0], [0.0, 10.0, 0.0], [0.0, 0.0, 10.0]]) labels = constant_op.constant([[0], [1], [2]], dtype=dtypes.int64) @@ -272,7 +272,7 @@ class SparseSoftmaxCrossEntropyLossTest(test.TestCase): self.assertAlmostEqual(loss.eval(), 0.0, 3) def testAllCorrectNonColumnLabels(self): - with self.test_session(): + with self.cached_session(): logits = constant_op.constant([[10.0, 0.0, 0.0], [0.0, 10.0, 0.0], [0.0, 0.0, 10.0]]) labels = constant_op.constant([0, 1, 2]) @@ -285,7 +285,7 @@ class SparseSoftmaxCrossEntropyLossTest(test.TestCase): [0.0, 0.0, 10.0]]) labels = constant_op.constant([[2], [0], [1]], dtype=dtypes.int32) - with self.test_session(): + with self.cached_session(): loss = losses.sparse_softmax_cross_entropy(labels, logits) self.assertEquals(loss.op.name, 'sparse_softmax_cross_entropy_loss/value') self.assertAlmostEqual(loss.eval(), 10.0, 3) @@ -295,7 +295,7 @@ class SparseSoftmaxCrossEntropyLossTest(test.TestCase): [0.0, 0.0, 10.0]]) labels = constant_op.constant([[2], [0], [1]], dtype=dtypes.int64) - with self.test_session(): + with self.cached_session(): loss = losses.sparse_softmax_cross_entropy(labels, logits) self.assertEquals(loss.op.name, 'sparse_softmax_cross_entropy_loss/value') self.assertAlmostEqual(loss.eval(), 10.0, 3) @@ -305,7 +305,7 @@ class SparseSoftmaxCrossEntropyLossTest(test.TestCase): [0.0, 0.0, 10.0]]) labels = constant_op.constant([2, 0, 1]) - with self.test_session(): + with self.cached_session(): loss = losses.sparse_softmax_cross_entropy(labels, logits) self.assertEquals(loss.op.name, 'sparse_softmax_cross_entropy_loss/value') self.assertAlmostEqual(loss.eval(), 10.0, 3) @@ -315,7 +315,7 @@ class SparseSoftmaxCrossEntropyLossTest(test.TestCase): [0.0, 0.0, 10.0]]) labels = constant_op.constant([[2], [0], [1]]) weights = 2.3 - with self.test_session(): + with self.cached_session(): loss = losses.sparse_softmax_cross_entropy(labels, logits, weights) self.assertAlmostEqual(weights * 10.0, loss.eval(), 3) @@ -324,7 +324,7 @@ class SparseSoftmaxCrossEntropyLossTest(test.TestCase): [0.0, 0.0, 10.0]]) labels = constant_op.constant([[2], [0], [1]]) weights = 2.3 - with self.test_session(): + with self.cached_session(): loss = losses.sparse_softmax_cross_entropy(labels, logits, constant_op.constant(weights)) self.assertAlmostEqual(weights * 10.0, loss.eval(), 3) @@ -334,7 +334,7 @@ class SparseSoftmaxCrossEntropyLossTest(test.TestCase): [0.0, 0.0, 10.0]]) labels = constant_op.constant([[2], [0], [1]]) weights = 2.3 - with self.test_session(): + with self.cached_session(): loss = losses.sparse_softmax_cross_entropy( labels, logits, constant_op.constant((weights,))) self.assertAlmostEqual(weights * 10.0, loss.eval(), 3) @@ -345,7 +345,7 @@ class SparseSoftmaxCrossEntropyLossTest(test.TestCase): [0.0, 0.0, 10.0]]) labels = constant_op.constant([[2], [0], [1]]) weights = array_ops.placeholder(dtypes.float32) - with self.test_session() as sess: + with self.cached_session() as sess: loss = losses.sparse_softmax_cross_entropy(labels, logits, weights) loss_val = sess.run(loss, feed_dict={weights: ((1.2,), (3.4,), (5.6,))}) @@ -355,7 +355,7 @@ class SparseSoftmaxCrossEntropyLossTest(test.TestCase): logits = array_ops.placeholder(dtypes.float32) labels = array_ops.placeholder(dtypes.int32) weights = 1.0 - with self.test_session() as sess: + with self.cached_session() as sess: loss = losses.sparse_softmax_cross_entropy(labels, logits, weights) loss_val = sess.run(loss, feed_dict={ @@ -370,7 +370,7 @@ class SparseSoftmaxCrossEntropyLossTest(test.TestCase): logits = array_ops.placeholder(dtypes.float32, shape=(None, 3)) labels = array_ops.placeholder(dtypes.int32, shape=(None, 1)) weights = array_ops.placeholder(dtypes.float32) - with self.test_session() as sess: + with self.cached_session() as sess: loss = losses.sparse_softmax_cross_entropy(labels, logits, weights) loss_val = sess.run(loss, feed_dict={ @@ -387,7 +387,7 @@ class SparseSoftmaxCrossEntropyLossTest(test.TestCase): [0.0, 0.0, 10.0]]) labels = constant_op.constant([[2], [0], [1]]) weights = constant_op.constant([1.2, 3.4, 5.6], shape=(3, 1)) - with self.test_session(): + with self.cached_session(): loss = losses.sparse_softmax_cross_entropy(labels, logits, weights) self.assertAlmostEqual((1.2 + 3.4 + 5.6) * 10.0 / 3.0, loss.eval(), 3) @@ -396,7 +396,7 @@ class SparseSoftmaxCrossEntropyLossTest(test.TestCase): [0.0, 0.0, 10.0]]) labels = constant_op.constant([[2], [0], [1]]) weights = constant_op.constant([[1.2], [3.4], [5.6]]) - with self.test_session(): + with self.cached_session(): loss = losses.sparse_softmax_cross_entropy(labels, logits, weights) self.assertAlmostEqual((1.2 + 3.4 + 5.6) * 10.0 / 3.0, loss.eval(), 3) @@ -405,7 +405,7 @@ class SparseSoftmaxCrossEntropyLossTest(test.TestCase): [0.0, 0.0, 10.0]]) labels = constant_op.constant([[2], [0], [1]]) weights = constant_op.constant([0, 0, 0], shape=(3, 1)) - with self.test_session(): + with self.cached_session(): loss = losses.sparse_softmax_cross_entropy(labels, logits, weights) self.assertAlmostEqual(0.0, loss.eval(), 3) @@ -414,12 +414,12 @@ class SparseSoftmaxCrossEntropyLossTest(test.TestCase): [0.0, 0.0, 10.0]]) labels = constant_op.constant([[2], [0], [1]]) weights = constant_op.constant([1.2, 0, 0], shape=(3, 1)) - with self.test_session(): + with self.cached_session(): loss = losses.sparse_softmax_cross_entropy(labels, logits, weights) self.assertAlmostEqual(12.0, loss.eval(), 3) def testMeasurementSpecificWeightsRaisesException(self): - with self.test_session(): + with self.cached_session(): logits = constant_op.constant([[100.0, -100.0, -100.0], [-100.0, 100.0, -100.0], [-100.0, -100.0, 100.0]]) @@ -432,7 +432,7 @@ class SparseSoftmaxCrossEntropyLossTest(test.TestCase): def testInconsistentWeightSizeRaisesException(self): """The weight tensor has incorrect number of elements.""" - with self.test_session(): + with self.cached_session(): logits = constant_op.constant([[100.0, -100.0, -100.0], [-100.0, 100.0, -100.0], [-100.0, -100.0, 100.0]]) @@ -445,7 +445,7 @@ class SparseSoftmaxCrossEntropyLossTest(test.TestCase): def testInconsistentLabelSizeRaisesException(self): """The label tensor has incorrect number of elements.""" - with self.test_session(): + with self.cached_session(): logits = constant_op.constant([[100.0, -100.0, -100.0], [-100.0, 100.0, -100.0], [-100.0, -100.0, 100.0]]) @@ -458,7 +458,7 @@ class SparseSoftmaxCrossEntropyLossTest(test.TestCase): def testInconsistentWeightShapeRaisesException(self): """The weight tensor has incorrect shape.""" - with self.test_session(): + with self.cached_session(): logits = constant_op.constant([[100.0, -100.0, -100.0, -100.0], [-100.0, 100.0, -100.0, -100.0], [-100.0, -100.0, 100.0, -100.0], @@ -472,7 +472,7 @@ class SparseSoftmaxCrossEntropyLossTest(test.TestCase): def testInconsistentLabelShapeRaisesException(self): """The label tensor has incorrect shape.""" - with self.test_session(): + with self.cached_session(): logits = constant_op.constant([[100.0, -100.0, -100.0, -100.0], [-100.0, 100.0, -100.0, -100.0], [-100.0, -100.0, 100.0, -100.0], @@ -488,7 +488,7 @@ class SparseSoftmaxCrossEntropyLossTest(test.TestCase): class SigmoidCrossEntropyLossTest(test.TestCase): def testAllCorrectSigmoid(self): - with self.test_session(): + with self.cached_session(): logits = constant_op.constant([[100.0, -100.0, -100.0], [-100.0, 100.0, -100.0], [-100.0, -100.0, 100.0]]) @@ -506,7 +506,7 @@ class SigmoidCrossEntropyLossTest(test.TestCase): loss = losses.sigmoid_cross_entropy(labels, logits, weights) self.assertEquals(logits.dtype, loss.dtype) - with self.test_session() as sess: + with self.cached_session() as sess: loss = sess.run(loss, feed_dict={ logits: np.ones((32, 1)), @@ -522,7 +522,7 @@ class SigmoidCrossEntropyLossTest(test.TestCase): loss = losses.sigmoid_cross_entropy(labels, logits, weights) self.assertEquals(logits.dtype, loss.dtype) - with self.test_session() as sess: + with self.cached_session() as sess: loss = sess.run(loss, feed_dict={ logits: np.ones((32, 2)), @@ -531,7 +531,7 @@ class SigmoidCrossEntropyLossTest(test.TestCase): self.assertAlmostEqual(0.313, loss, 3) def testAllWrongSigmoid(self): - with self.test_session(): + with self.cached_session(): logits = constant_op.constant([[100.0, -100.0, -100.0], [-100.0, 100.0, -100.0], [-100.0, -100.0, 100.0]]) @@ -542,7 +542,7 @@ class SigmoidCrossEntropyLossTest(test.TestCase): self.assertAlmostEqual(loss.eval(), 600.0 / 9.0, 3) def testAllWrongSigmoidWithMeasurementSpecificWeights(self): - with self.test_session(): + with self.cached_session(): logits = constant_op.constant([[100.0, -100.0, -100.0], [-100.0, 100.0, -100.0], [-100.0, -100.0, 100.0]]) @@ -562,7 +562,7 @@ class SigmoidCrossEntropyLossTest(test.TestCase): self.assertEquals(logits.dtype, loss.dtype) self.assertEquals('sigmoid_cross_entropy_loss/value', loss.op.name) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(0.0, loss.eval(), 3) def testSigmoidFloat64(self): @@ -577,7 +577,7 @@ class SigmoidCrossEntropyLossTest(test.TestCase): loss = losses.sigmoid_cross_entropy(labels, logits) self.assertEquals(logits.dtype, loss.dtype) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(44.444, loss.eval(), 3) def testSigmoidNoReduction(self): @@ -590,7 +590,7 @@ class SigmoidCrossEntropyLossTest(test.TestCase): labels, logits, reduction=losses.Reduction.NONE) self.assertEquals(logits.dtype, loss.dtype) - with self.test_session(): + with self.cached_session(): self.assertAllClose(( (0., 0., 0.), (0., 100., 100.), @@ -598,7 +598,7 @@ class SigmoidCrossEntropyLossTest(test.TestCase): ), loss.eval(), 3) def testSigmoidLabelSmoothingCorrect(self): - with self.test_session(): + with self.cached_session(): logits = constant_op.constant([[100.0, -100.0, -100.0]]) labels = constant_op.constant([[1, 0, 1]]) # Sigmoid cross entropy loss is: @@ -621,7 +621,7 @@ class SigmoidCrossEntropyLossTest(test.TestCase): self.assertAlmostEqual(loss.eval(), expected_value, 3) def testSigmoidLabelSmoothingEqualsSoftmaxTwoLabel(self): - with self.test_session(): + with self.cached_session(): label_smoothing = 0.1 sigmoid_logits = constant_op.constant([[100.0, -100.0, -100.0]]) sigmoid_labels = constant_op.constant([[1, 0, 1]]) @@ -656,33 +656,33 @@ class LogLossTest(test.TestCase): self._labels = constant_op.constant(labels) def testValueErrorThrownWhenWeightIsNone(self): - with self.test_session(): + with self.cached_session(): with self.assertRaises(ValueError): losses.log_loss(self._labels, self._labels, weights=None) def testAllCorrectNoLossWeight(self): loss = losses.log_loss(self._labels, self._labels) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(0.0, loss.eval(), 3) def testAllCorrectNoLossWeightWithPlaceholder(self): tf_predictions = array_ops.placeholder( dtypes.float32, shape=self._np_labels.shape) loss = losses.log_loss(self._labels, tf_predictions) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual( 0.0, loss.eval(feed_dict={tf_predictions: self._np_labels}), 3) def testNonZeroLoss(self): loss = losses.log_loss(self._labels, self._predictions) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(-np.sum(self._expected_losses) / 6.0, loss.eval(), 3) def testNonZeroLossWithPythonScalarWeight(self): weights = 2.3 loss = losses.log_loss(self._labels, self._predictions, weights) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(weights * -np.sum(self._expected_losses) / 6.0, loss.eval(), 3) @@ -690,7 +690,7 @@ class LogLossTest(test.TestCase): weights = 2.3 loss = losses.log_loss(self._labels, self._predictions, constant_op.constant(weights)) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(weights * -np.sum(self._expected_losses) / 6.0, loss.eval(), 3) @@ -700,7 +700,7 @@ class LogLossTest(test.TestCase): weights = 2.3 loss = losses.log_loss(self._labels, tf_predictions, constant_op.constant(weights)) - with self.test_session() as sess: + with self.cached_session() as sess: loss = sess.run(loss, feed_dict={tf_predictions: self._np_predictions}) self.assertAlmostEqual(weights * -np.sum(self._expected_losses) / 6.0, loss, 3) @@ -710,7 +710,7 @@ class LogLossTest(test.TestCase): weights = 2.3 loss = losses.log_loss(self._labels, tf_predictions, constant_op.constant(weights)) - with self.test_session() as sess: + with self.cached_session() as sess: loss = sess.run(loss, feed_dict={tf_predictions: self._np_predictions}) self.assertAlmostEqual(weights * -np.sum(self._expected_losses) / 6.0, loss, 3) @@ -721,7 +721,7 @@ class LogLossTest(test.TestCase): self._expected_losses, np.asarray([1.2, 1.2, 1.2, 3.4, 3.4, 3.4]).reshape((2, 3))) loss = losses.log_loss(self._labels, self._predictions, weights) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(-np.sum(expected_losses) / 6.0, loss.eval(), 3) def testNonZeroLossWithOneDimBatchSpecificWeightsSomeZero(self): @@ -730,7 +730,7 @@ class LogLossTest(test.TestCase): np.asarray([1.2, 1.2, 1.2, 0, 0, 0]).reshape( (2, 3))) loss = losses.log_loss(self._labels, self._predictions, weights) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(-np.sum(expected_losses) / 3.0, loss.eval(), 3) def testNonZeroLossWithTwoDimBatchSpecificWeightsSomeZero(self): @@ -739,12 +739,12 @@ class LogLossTest(test.TestCase): np.asarray([1.2, 1.2, 1.2, 0, 0, 0]).reshape( (2, 3))) loss = losses.log_loss(self._labels, self._predictions, weights) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(-np.sum(expected_losses) / 3.0, loss.eval(), 3) def testWeightsWithSameNumDimsButWrongShapeThrowsException(self): weights = constant_op.constant(np.random.normal(size=(2, 4)), shape=[2, 4]) - with self.test_session(): + with self.cached_session(): with self.assertRaises(ValueError): losses.log_loss(self._labels, self._predictions, weights) @@ -757,7 +757,7 @@ class LogLossTest(test.TestCase): self._predictions, constant_op.constant( weights, shape=(2, 3))) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(-np.sum(expected_losses) / 5.0, loss.eval(), 3) def testNonZeroLossWithMeasurementSpecificWeightsWithPlaceholder(self): @@ -771,7 +771,7 @@ class LogLossTest(test.TestCase): constant_op.constant( weights, shape=(2, 3))) - with self.test_session() as sess: + with self.cached_session() as sess: loss = sess.run(loss, feed_dict={tf_predictions: self._np_predictions}) self.assertAlmostEqual(-np.sum(expected_losses) / 5.0, loss, 3) @@ -784,7 +784,7 @@ class LogLossTest(test.TestCase): self._predictions, constant_op.constant( weights, shape=(2, 3))) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(-np.sum(expected_losses), loss.eval(), 3) def testNonZeroLossWithSampleSpecificWeightsMostZeroWithPlaceholder(self): @@ -795,35 +795,35 @@ class LogLossTest(test.TestCase): tf_weights = constant_op.constant(weights, shape=(2, 3)) loss = losses.log_loss(self._labels, tf_predictions, tf_weights) - with self.test_session() as sess: + with self.cached_session() as sess: loss = sess.run(loss, feed_dict={tf_predictions: self._np_predictions}) self.assertAlmostEqual(-np.sum(expected_losses), loss, 3) def testLossWithSampleSpecificWeightsAllZero(self): tf_weights = array_ops.zeros(shape=(2, 3)) loss = losses.log_loss(self._labels, self._predictions, tf_weights) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(0.0, loss.eval(), 3) class HingeLossTest(test.TestCase): def testIncompatibleShapes(self): - with self.test_session(): + with self.cached_session(): logits = constant_op.constant([[-1.0], [2.1]]) labels = constant_op.constant([0.0, 1.0]) with self.assertRaises(ValueError): _ = losses.hinge_loss(labels, logits).eval() def testAllOutsideMargin(self): - with self.test_session(): + with self.cached_session(): logits = constant_op.constant([1.2, -1.4, -1.0, 2.1]) labels = constant_op.constant([1.0, 0.0, 0.0, 1.0]) loss = losses.hinge_loss(labels, logits) self.assertAllClose(loss.eval(), 0.0, atol=1e-3) def testSomeInsideMargin(self): - with self.test_session(): + with self.cached_session(): logits = constant_op.constant([[-0.7], [-1.4], [1.4], [0.6]]) labels = constant_op.constant([[0.0], [0.0], [1.0], [1.0]]) loss = losses.hinge_loss(labels, logits) @@ -832,7 +832,7 @@ class HingeLossTest(test.TestCase): self.assertAllClose(loss.eval(), 0.175, atol=1e-3) def testSomeMisclassified(self): - with self.test_session(): + with self.cached_session(): logits = constant_op.constant([[[1.2], [0.4], [-1.0], [-1.1]]]) labels = constant_op.constant([[[1.0], [0.0], [0.0], [1.0]]]) loss = losses.hinge_loss(labels, logits) @@ -844,14 +844,14 @@ class HingeLossTest(test.TestCase): class HuberLossTest(test.TestCase): def testIncompatibleShapes(self): - with self.test_session(): + with self.cached_session(): predictions = constant_op.constant([[-1.0], [2.1]]) labels = constant_op.constant([0.0, 1.0]) with self.assertRaises(ValueError): _ = losses.huber_loss(labels, predictions).eval() def testAllQuadratic(self): - with self.test_session(): + with self.cached_session(): predictions = constant_op.constant([1.5, -1.4, -1.0, 0.0]) labels = constant_op.constant([1.0, -1.0, 0.0, 0.5]) loss = losses.huber_loss(labels, predictions) @@ -859,7 +859,7 @@ class HuberLossTest(test.TestCase): 0.5 * (0.25 + 0.16 + 1.0 + 0.25) / 4., atol=1e-5) def testAllLinear(self): - with self.test_session(): + with self.cached_session(): predictions = constant_op.constant([1.5, -1.4, -1.0, 0.0]) labels = constant_op.constant([0.0, 1.0, 0.0, 1.5]) loss = losses.huber_loss(labels, predictions) @@ -867,7 +867,7 @@ class HuberLossTest(test.TestCase): (1.5 + 2.4 + 1.0 + 1.5) / 4. - 0.5, atol=1e-5) def testMixedQuadraticLinear(self): - with self.test_session(): + with self.cached_session(): predictions = constant_op.constant([[1.5, -1.4, -1.0, 0.0], [1.5, -1.4, -1.0, 0.0]]) labels = constant_op.constant([[1.0, -1.0, 0.0, 0.5], @@ -879,7 +879,7 @@ class HuberLossTest(test.TestCase): self.assertAllClose(loss.eval(), expected_loss, atol=1e-5) def testAllQuadraticDelta(self): - with self.test_session(): + with self.cached_session(): delta = 0.5 predictions = constant_op.constant([1.5, -1.4, -0.5, 0.0]) labels = constant_op.constant([1.0, -1.0, 0.0, 0.5]) @@ -894,7 +894,7 @@ class HuberLossTest(test.TestCase): expected = delta * np.array([1.5, 2.4, 1.0, 1.5]).mean() expected -= 0.5 * delta**2 loss = losses.huber_loss(labels, predictions, delta=delta) - with self.test_session(): + with self.cached_session(): self.assertAllClose(expected, loss.eval(), atol=1e-5) @@ -906,13 +906,13 @@ class MeanSquaredErrorTest(test.TestCase): self._labels = constant_op.constant([1, 9, 2, -5, -2, 6], shape=(2, 3)) def testValueErrorThrownWhenWeightIsNone(self): - with self.test_session(): + with self.cached_session(): with self.assertRaises(ValueError): losses.mean_squared_error( self._predictions, self._predictions, weights=None) def testScalar(self): - with self.test_session(): + with self.cached_session(): self.assertEqual( 0.0, losses.mean_squared_error(predictions=constant_op.constant(0), @@ -920,55 +920,55 @@ class MeanSquaredErrorTest(test.TestCase): def testAllCorrectNoLossWeight(self): loss = losses.mean_squared_error(self._predictions, self._predictions) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(0.0, loss.eval(), 3) def testNonZeroLoss(self): loss = losses.mean_squared_error(self._labels, self._predictions) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(49.5, loss.eval(), 3) def testNonZeroLossWithPythonScalarWeight(self): weights = 2.3 loss = losses.mean_squared_error(self._labels, self._predictions, weights) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(49.5 * weights, loss.eval(), 3) def testNonZeroLossWithScalarTensorWeight(self): weights = 2.3 loss = losses.mean_squared_error(self._labels, self._predictions, constant_op.constant(weights)) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(49.5 * weights, loss.eval(), 3) def testNonZeroLossWithOneDimBatchSpecificWeights(self): weights = constant_op.constant([1.2, 3.4], shape=(2, 1)) loss = losses.mean_squared_error(self._labels, self._predictions, weights) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(767.8 / 6.0, loss.eval(), 3) def testNonZeroLossWithTwoDimBatchSpecificWeights(self): weights = constant_op.constant([1.2, 3.4], shape=[2, 1]) loss = losses.mean_squared_error(self._labels, self._predictions, weights) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(767.8 / 6.0, loss.eval(), 3) def testNonZeroLossWithSampleSpecificWeights(self): weights = constant_op.constant([3, 6, 5, 0, 4, 2], shape=[2, 3]) loss = losses.mean_squared_error(self._labels, self._predictions, weights) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(587 / 5.0, loss.eval(), 3) def testNonZeroLossWithSampleSpecificWeightsMostZero(self): weights = constant_op.constant([0, 0, 0, 0, 0, 2], shape=[2, 3]) loss = losses.mean_squared_error(self._labels, self._predictions, weights) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(18.0, loss.eval(), 3) def testLossWithSampleSpecificWeightsAllZero(self): weights = array_ops.zeros((2, 3)) loss = losses.mean_squared_error(self._labels, self._predictions, weights) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(0.0, loss.eval(), 3) @@ -994,7 +994,7 @@ class MeanPairwiseSquaredErrorTest(test.TestCase): self._expected_losses = np.divide(total, 3.0) def testValueErrorThrownWhenWeightIsNone(self): - with self.test_session(): + with self.cached_session(): with self.assertRaises(ValueError): losses.mean_pairwise_squared_error( predictions=constant_op.constant(self._labels), @@ -1003,7 +1003,7 @@ class MeanPairwiseSquaredErrorTest(test.TestCase): def _test_valid_weights( self, labels, predictions, expected_loss, weights=1.0): - with self.test_session(): + with self.cached_session(): static_inputs_op = losses.mean_pairwise_squared_error( predictions=predictions, labels=labels, weights=weights) self.assertAlmostEqual(expected_loss, static_inputs_op.eval(), places=3) @@ -1054,7 +1054,7 @@ class MeanPairwiseSquaredErrorTest(test.TestCase): init_op = variables.global_variables_initializer() - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(init_op) for grad, _ in gradients_to_variables: np_grad = sess.run(grad) @@ -1073,7 +1073,7 @@ class MeanPairwiseSquaredErrorTest(test.TestCase): predictions=constant_op.constant(self._predictions), labels=constant_op.constant(self._labels), weights=constant_op.constant(weights)) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(weights * np.sum(self._expected_losses), loss.eval(), 3) @@ -1122,7 +1122,7 @@ class MeanPairwiseSquaredErrorTest(test.TestCase): predictions=predictions_placeholder, labels=labels_placeholder, weights=weights_placeholder) - with self.test_session(): + with self.cached_session(): with self.assertRaisesRegexp(errors_impl.OpError, expected_error_msg): dynamic_inputs_op.eval(feed_dict={ predictions_placeholder: predictions, @@ -1191,7 +1191,7 @@ class MeanPairwiseSquaredErrorTest(test.TestCase): labels=array_ops.concat([labels0, labels1], 0), predictions=array_ops.concat([predictions0, predictions1], 0)) - with self.test_session() as session: + with self.cached_session() as session: loss0, loss1, loss0_1 = session.run([loss0, loss1, loss0_1]) self.assertTrue(loss0 > 0) @@ -1216,7 +1216,7 @@ class CosineDistanceLossTest(test.TestCase): [0, 0, 1], [0, 1, 0]]).reshape((3, 2, 3)) def testValueErrorThrownWhenWeightIsNone(self): - with self.test_session(): + with self.cached_session(): with self.assertRaises(ValueError): losses.cosine_distance( predictions=constant_op.constant(self._labels), @@ -1229,7 +1229,7 @@ class CosineDistanceLossTest(test.TestCase): predictions=constant_op.constant(self._labels), labels=constant_op.constant(self._labels), dim=2) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(0, loss.eval(), 5) def testPartiallyCorrectWithIntegerValues(self): @@ -1237,7 +1237,7 @@ class CosineDistanceLossTest(test.TestCase): predictions=constant_op.constant(self._predictions), labels=constant_op.constant(self._labels), dim=2) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(1, loss.eval(), 5) def testPartiallyCorrectFloatingPointValues(self): @@ -1255,7 +1255,7 @@ class CosineDistanceLossTest(test.TestCase): labels, shape=(3, 1, 3), dtype=dtypes.float32) loss = losses.cosine_distance(tf_labels, tf_preds, dim=2) - with self.test_session(): + with self.cached_session(): self.assertAlmostEqual(1.0, loss.eval(), 5) def testSampleSpecificWeights(self): @@ -1264,7 +1264,7 @@ class CosineDistanceLossTest(test.TestCase): labels=constant_op.constant(self._labels), dim=2, weights=np.asarray((1, 0, 0)).reshape((3, 1, 1))) - with self.test_session(): + with self.cached_session(): self.assertEqual(1.0, loss.eval()) def testMeasurementSpecificWeights(self): @@ -1274,7 +1274,7 @@ class CosineDistanceLossTest(test.TestCase): dim=2, weights=constant_op.constant( [1, 0, 0, 1, 1, 1], shape=(3, 2, 1))) - with self.test_session(): + with self.cached_session(): self.assertEqual(3.0 / 4.0, loss.eval()) def testMeasurementSpecificWeightsWithPlaceholderWithShape(self): @@ -1286,7 +1286,7 @@ class CosineDistanceLossTest(test.TestCase): dim=2, weights=constant_op.constant( [1, 0, 0, 1, 1, 1], shape=(3, 2, 1))) - with self.test_session() as sess: + with self.cached_session() as sess: loss = sess.run(loss, feed_dict={tf_predictions: self._predictions}) self.assertEqual(3.0 / 4.0, loss) @@ -1296,7 +1296,7 @@ class CosineDistanceLossTest(test.TestCase): labels=constant_op.constant(self._labels), dim=2, weights=array_ops.zeros((3, 1, 1))) - with self.test_session(): + with self.cached_session(): self.assertEqual(0, loss.eval()) def testZeroLossWhenAllMeasurementSpecificWeightsAreZero(self): @@ -1305,7 +1305,7 @@ class CosineDistanceLossTest(test.TestCase): labels=constant_op.constant(self._labels), dim=2, weights=array_ops.zeros((3, 2, 1))) - with self.test_session(): + with self.cached_session(): self.assertEqual(0, loss.eval()) @@ -1411,7 +1411,7 @@ class ComputeWeightedLossTest(test.TestCase): weighted_loss = losses.compute_weighted_loss( self._raw_losses, weights=weight) self.assertEqual(1, len(util.get_losses())) - with self.test_session(): + with self.cached_session(): self.assertAllClose( np.mean(weight * self._raw_losses), weighted_loss.eval()) @@ -1429,7 +1429,7 @@ class ComputeWeightedLossTest(test.TestCase): weighted_loss = losses.compute_weighted_loss( self._raw_losses, weights=weights_placeholder) self.assertEqual(1, len(util.get_losses())) - with self.test_session(): + with self.cached_session(): with self.assertRaisesRegexp(errors_impl.OpError, expected_error_msg): weighted_loss.eval(feed_dict={weights_placeholder: weights}) @@ -1452,7 +1452,7 @@ class ComputeWeightedLossTest(test.TestCase): weighted_loss = losses.compute_weighted_loss( raw_losses, weights=weights_placeholder) self.assertEqual(1, len(util.get_losses())) - with self.test_session(): + with self.cached_session(): with self.assertRaisesRegexp(errors_impl.OpError, expected_error_msg): weighted_loss.eval(feed_dict={weights_placeholder: weights}) diff --git a/tensorflow/python/kernel_tests/manip_ops_test.py b/tensorflow/python/kernel_tests/manip_ops_test.py index dc3ea38671..f71857a3cb 100644 --- a/tensorflow/python/kernel_tests/manip_ops_test.py +++ b/tensorflow/python/kernel_tests/manip_ops_test.py @@ -42,12 +42,12 @@ class RollTest(test_util.TensorFlowTestCase): def _testRoll(self, np_input, shift, axis): expected_roll = np.roll(np_input, shift, axis) - with self.test_session(): + with self.cached_session(): roll = manip_ops.roll(np_input, shift, axis) self.assertAllEqual(roll.eval(), expected_roll) def _testGradient(self, np_input, shift, axis): - with self.test_session(): + with self.cached_session(): inx = constant_op.constant(np_input.tolist()) xs = list(np_input.shape) y = manip_ops.roll(inx, shift, axis) @@ -94,7 +94,7 @@ class RollTest(test_util.TensorFlowTestCase): self._testAll(np.random.randint(-100, 100, (5)).astype(np.int32), 3, -1) self._testAll(np.random.randint(-100, 100, (4, 4)).astype(np.int32), 3, -2) # Make sure negative axis should be 0 <= axis + dims < dims - with self.test_session(): + with self.cached_session(): with self.assertRaisesRegexp(errors_impl.InvalidArgumentError, "is out of range"): manip_ops.roll(np.random.randint(-100, 100, (4, 4)).astype(np.int32), @@ -111,7 +111,7 @@ class RollTest(test_util.TensorFlowTestCase): tensor = array_ops.placeholder(dtype=dtypes.int32) shift = 1 axis = 0 - with self.test_session(): + with self.cached_session(): with self.assertRaisesRegexp(errors_impl.InvalidArgumentError, "input must be 1-D or higher"): manip_ops.roll(tensor, shift, axis).eval(feed_dict={tensor: 7}) @@ -127,7 +127,7 @@ class RollTest(test_util.TensorFlowTestCase): tensor = [[1, 2], [3, 4]] shift = 1 axis = array_ops.placeholder(dtype=dtypes.int32) - with self.test_session(): + with self.cached_session(): with self.assertRaisesRegexp(errors_impl.InvalidArgumentError, "axis must be a scalar or a 1-D vector"): manip_ops.roll(tensor, shift, axis).eval(feed_dict={axis: [[0, 1]]}) @@ -143,7 +143,7 @@ class RollTest(test_util.TensorFlowTestCase): tensor = [[1, 2], [3, 4]] shift = array_ops.placeholder(dtype=dtypes.int32) axis = 1 - with self.test_session(): + with self.cached_session(): with self.assertRaisesRegexp(errors_impl.InvalidArgumentError, "shift must be a scalar or a 1-D vector"): manip_ops.roll(tensor, shift, axis).eval(feed_dict={shift: [[0, 1]]}) @@ -158,7 +158,7 @@ class RollTest(test_util.TensorFlowTestCase): tensor = [[1, 2], [3, 4]] shift = array_ops.placeholder(dtype=dtypes.int32) axis = [0, 1] - with self.test_session(): + with self.cached_session(): with self.assertRaisesRegexp(errors_impl.InvalidArgumentError, "shift and axis must have the same size"): manip_ops.roll(tensor, shift, axis).eval(feed_dict={shift: [1]}) @@ -167,7 +167,7 @@ class RollTest(test_util.TensorFlowTestCase): tensor = [1, 2] shift = 1 axis = 1 - with self.test_session(): + with self.cached_session(): with self.assertRaisesRegexp(errors_impl.InvalidArgumentError, "is out of range"): manip_ops.roll(tensor, shift, axis).eval() diff --git a/tensorflow/python/kernel_tests/matmul_op_test.py b/tensorflow/python/kernel_tests/matmul_op_test.py index b167278984..309da8f184 100644 --- a/tensorflow/python/kernel_tests/matmul_op_test.py +++ b/tensorflow/python/kernel_tests/matmul_op_test.py @@ -206,7 +206,7 @@ class MatMulInfixOperatorTest(test_lib.TestCase): b = ops.convert_to_tensor([[40.0, 50.0], [60.0, 70.0], [80.0, 90.0]]) c = infix_matmul(a, b) d = math_ops.matmul(a, b) - with self.test_session(): + with self.cached_session(): self.assertAllEqual(c.eval(), d.eval()) diff --git a/tensorflow/python/kernel_tests/matrix_inverse_op_test.py b/tensorflow/python/kernel_tests/matrix_inverse_op_test.py index f41967ff98..720ba806e9 100644 --- a/tensorflow/python/kernel_tests/matrix_inverse_op_test.py +++ b/tensorflow/python/kernel_tests/matrix_inverse_op_test.py @@ -114,7 +114,7 @@ class InverseOpTest(test.TestCase): def testNotInvertible(self): # The input should be invertible. - with self.test_session(): + with self.cached_session(): with self.assertRaisesOpError("Input is not invertible."): # All rows of the matrix below add to zero. tensor3 = constant_op.constant([[1., 0., -1.], [-1., 1., 0.], diff --git a/tensorflow/python/kernel_tests/matrix_triangular_solve_op_test.py b/tensorflow/python/kernel_tests/matrix_triangular_solve_op_test.py index 33288392c0..dd01ba11af 100644 --- a/tensorflow/python/kernel_tests/matrix_triangular_solve_op_test.py +++ b/tensorflow/python/kernel_tests/matrix_triangular_solve_op_test.py @@ -143,7 +143,7 @@ class MatrixTriangularSolveOpTest(test.TestCase): def testNonSquareMatrix(self): # A non-square matrix should cause an error. matrix = np.array([[1., 2., 3.], [3., 4., 5.]]) - with self.test_session(): + with self.cached_session(): with self.assertRaises(ValueError): self._verifySolve(matrix, matrix) with self.assertRaises(ValueError): @@ -154,7 +154,7 @@ class MatrixTriangularSolveOpTest(test.TestCase): # right-hand sides. matrix = np.array([[1., 0.], [0., 1.]]) rhs = np.array([[1., 0.]]) - with self.test_session(): + with self.cached_session(): with self.assertRaises(ValueError): self._verifySolve(matrix, rhs) with self.assertRaises(ValueError): @@ -164,7 +164,7 @@ class MatrixTriangularSolveOpTest(test.TestCase): # The input should be invertible. # The matrix is singular because it has a zero on the diagonal. singular_matrix = np.array([[1., 0., -1.], [-1., 0., 1.], [0., -1., 1.]]) - with self.test_session(): + with self.cached_session(): with self.assertRaisesOpError("Input matrix is not invertible."): self._verifySolve(singular_matrix, singular_matrix) with self.assertRaisesOpError("Input matrix is not invertible."): diff --git a/tensorflow/python/kernel_tests/metrics_test.py b/tensorflow/python/kernel_tests/metrics_test.py index 55653489af..5dcdb9e420 100644 --- a/tensorflow/python/kernel_tests/metrics_test.py +++ b/tensorflow/python/kernel_tests/metrics_test.py @@ -192,7 +192,7 @@ class MeanTest(test.TestCase): self.assertListEqual(ops.get_collection(my_collection_name), [update_op]) def testBasic(self): - with self.test_session() as sess: + with self.cached_session() as sess: values_queue = data_flow_ops.FIFOQueue( 4, dtypes=dtypes_lib.float32, shapes=(1, 2)) _enqueue_vector(sess, values_queue, [0, 1]) @@ -209,7 +209,7 @@ class MeanTest(test.TestCase): self.assertAlmostEqual(1.65, sess.run(mean), 5) def testUpdateOpsReturnsCurrentValue(self): - with self.test_session() as sess: + with self.cached_session() as sess: values_queue = data_flow_ops.FIFOQueue( 4, dtypes=dtypes_lib.float32, shapes=(1, 2)) _enqueue_vector(sess, values_queue, [0, 1]) @@ -253,7 +253,7 @@ class MeanTest(test.TestCase): metrics.mean(values, weights=np.ones((3, 2, 4, 1))), metrics.mean(values, weights=np.ones((3, 2, 4, 1, 1))),) expected = np.mean(values) - with self.test_session(): + with self.cached_session(): variables.local_variables_initializer().run() for mean_result in mean_results: mean, update_op = mean_result @@ -266,7 +266,7 @@ class MeanTest(test.TestCase): np.sum(np.multiply(weights, np.ones_like(values))) ) mean, update_op = metrics.mean(values, weights=weights) - with self.test_session(): + with self.cached_session(): variables.local_variables_initializer().run() self.assertAlmostEqual(expected, update_op.eval(), places=5) self.assertAlmostEqual(expected, mean.eval(), places=5) @@ -330,7 +330,7 @@ class MeanTest(test.TestCase): # Dynamic shapes. with self.assertRaisesRegexp(errors_impl.OpError, expected_error_msg): - with self.test_session(): + with self.cached_session(): _, update_op = metrics.mean(values_placeholder, invalid_weight) variables.local_variables_initializer().run() update_op.eval(feed_dict={values_placeholder: values}) @@ -359,7 +359,7 @@ class MeanTensorTest(test.TestCase): self.assertListEqual(ops.get_collection(my_collection_name), [update_op]) def testBasic(self): - with self.test_session() as sess: + with self.cached_session() as sess: values_queue = data_flow_ops.FIFOQueue( 4, dtypes=dtypes_lib.float32, shapes=(1, 2)) _enqueue_vector(sess, values_queue, [0, 1]) @@ -376,7 +376,7 @@ class MeanTensorTest(test.TestCase): self.assertAllClose([[-0.9 / 4., 3.525]], sess.run(mean)) def testMultiDimensional(self): - with self.test_session() as sess: + with self.cached_session() as sess: values_queue = data_flow_ops.FIFOQueue( 2, dtypes=dtypes_lib.float32, shapes=(2, 2, 2)) _enqueue_vector( @@ -397,7 +397,7 @@ class MeanTensorTest(test.TestCase): self.assertAllClose([[[1, 2], [1, 2]], [[2, 3], [5, 6]]], sess.run(mean)) def testUpdateOpsReturnsCurrentValue(self): - with self.test_session() as sess: + with self.cached_session() as sess: values_queue = data_flow_ops.FIFOQueue( 4, dtypes=dtypes_lib.float32, shapes=(1, 2)) _enqueue_vector(sess, values_queue, [0, 1]) @@ -418,7 +418,7 @@ class MeanTensorTest(test.TestCase): self.assertAllClose([[-0.9 / 4., 3.525]], sess.run(mean), 5) def testBinaryWeighted1d(self): - with self.test_session() as sess: + with self.cached_session() as sess: # Create the queue that populates the values. values_queue = data_flow_ops.FIFOQueue( 4, dtypes=dtypes_lib.float32, shapes=(1, 2)) @@ -445,7 +445,7 @@ class MeanTensorTest(test.TestCase): self.assertAllClose([[3.25, 0.5]], sess.run(mean), 5) def testWeighted1d(self): - with self.test_session() as sess: + with self.cached_session() as sess: # Create the queue that populates the values. values_queue = data_flow_ops.FIFOQueue( 4, dtypes=dtypes_lib.float32, shapes=(1, 2)) @@ -472,7 +472,7 @@ class MeanTensorTest(test.TestCase): self.assertAllClose([[0.8, 3.52]], sess.run(mean), 5) def testWeighted2d_1(self): - with self.test_session() as sess: + with self.cached_session() as sess: # Create the queue that populates the values. values_queue = data_flow_ops.FIFOQueue( 4, dtypes=dtypes_lib.float32, shapes=(1, 2)) @@ -499,7 +499,7 @@ class MeanTensorTest(test.TestCase): self.assertAllClose([[-2.1, 0.5]], sess.run(mean), 5) def testWeighted2d_2(self): - with self.test_session() as sess: + with self.cached_session() as sess: # Create the queue that populates the values. values_queue = data_flow_ops.FIFOQueue( 4, dtypes=dtypes_lib.float32, shapes=(1, 2)) @@ -575,7 +575,7 @@ class AccuracyTest(test.TestCase): (10, 3), maxval=3, dtype=dtypes_lib.int64, seed=1) accuracy, update_op = metrics.accuracy(labels, predictions) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) # Run several updates. @@ -588,7 +588,7 @@ class AccuracyTest(test.TestCase): self.assertEqual(initial_accuracy, accuracy.eval()) def testMultipleUpdates(self): - with self.test_session() as sess: + with self.cached_session() as sess: # Create the queue that populates the predictions. preds_queue = data_flow_ops.FIFOQueue( 4, dtypes=dtypes_lib.float32, shapes=(1, 1)) @@ -618,7 +618,7 @@ class AccuracyTest(test.TestCase): def testEffectivelyEquivalentSizes(self): predictions = array_ops.ones((40, 1)) labels = array_ops.ones((40,)) - with self.test_session() as sess: + with self.cached_session() as sess: accuracy, update_op = metrics.accuracy(labels, predictions) sess.run(variables.local_variables_initializer()) @@ -628,7 +628,7 @@ class AccuracyTest(test.TestCase): def testEffectivelyEquivalentSizesWithScalarWeight(self): predictions = array_ops.ones((40, 1)) labels = array_ops.ones((40,)) - with self.test_session() as sess: + with self.cached_session() as sess: accuracy, update_op = metrics.accuracy(labels, predictions, weights=2.0) sess.run(variables.local_variables_initializer()) @@ -642,7 +642,7 @@ class AccuracyTest(test.TestCase): weights = array_ops.expand_dims(ops.convert_to_tensor([100, 1, 1]), 1) # shape 3, 1 - with self.test_session() as sess: + with self.cached_session() as sess: accuracy, update_op = metrics.accuracy(labels, predictions, weights) sess.run(variables.local_variables_initializer()) @@ -662,7 +662,7 @@ class AccuracyTest(test.TestCase): dtype=dtypes_lib.int32, name='weights') feed_dict = {weights_placeholder: weights} - with self.test_session() as sess: + with self.cached_session() as sess: accuracy, update_op = metrics.accuracy(labels, predictions, weights_placeholder) @@ -674,7 +674,7 @@ class AccuracyTest(test.TestCase): self.assertGreater(accuracy.eval(feed_dict=feed_dict), .95) def testMultipleUpdatesWithWeightedValues(self): - with self.test_session() as sess: + with self.cached_session() as sess: # Create the queue that populates the predictions. preds_queue = data_flow_ops.FIFOQueue( 4, dtypes=dtypes_lib.float32, shapes=(1, 1)) @@ -746,7 +746,7 @@ class PrecisionTest(test.TestCase): (10, 3), maxval=1, dtype=dtypes_lib.int64, seed=1) precision, update_op = metrics.precision(labels, predictions) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) # Run several updates. @@ -765,7 +765,7 @@ class PrecisionTest(test.TestCase): labels = constant_op.constant(inputs) precision, update_op = metrics.precision(labels, predictions) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertAlmostEqual(1, sess.run(update_op)) self.assertAlmostEqual(1, precision.eval()) @@ -778,7 +778,7 @@ class PrecisionTest(test.TestCase): constant_op.constant([0, 1, 1, 0], shape=(1, 4)), dtype=dtype) precision, update_op = metrics.precision(labels, predictions) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertAlmostEqual(0.5, update_op.eval()) self.assertAlmostEqual(0.5, precision.eval()) @@ -789,7 +789,7 @@ class PrecisionTest(test.TestCase): precision, update_op = metrics.precision( labels, predictions, weights=constant_op.constant([[2], [5]])) - with self.test_session(): + with self.cached_session(): variables.local_variables_initializer().run() weighted_tp = 2.0 + 5.0 weighted_positives = (2.0 + 2.0) + (5.0 + 5.0) @@ -806,7 +806,7 @@ class PrecisionTest(test.TestCase): } precision, update_op = metrics.precision(labels, predictions, weights=2) - with self.test_session(): + with self.cached_session(): variables.local_variables_initializer().run() weighted_tp = 2.0 + 2.0 weighted_positives = (2.0 + 2.0) + (2.0 + 2.0) @@ -826,7 +826,7 @@ class PrecisionTest(test.TestCase): precision, update_op = metrics.precision( labels, predictions, weights=constant_op.constant([[2], [5]])) - with self.test_session(): + with self.cached_session(): variables.local_variables_initializer().run() weighted_tp = 2.0 + 5.0 weighted_positives = (2.0 + 2.0) + (5.0 + 5.0) @@ -844,7 +844,7 @@ class PrecisionTest(test.TestCase): predictions, weights=constant_op.constant([[1, 2, 3, 4], [4, 3, 2, 1]])) - with self.test_session(): + with self.cached_session(): variables.local_variables_initializer().run() weighted_tp = 3.0 + 4.0 weighted_positives = (1.0 + 3.0) + (4.0 + 2.0) @@ -864,7 +864,7 @@ class PrecisionTest(test.TestCase): predictions, weights=constant_op.constant([[1, 2, 3, 4], [4, 3, 2, 1]])) - with self.test_session(): + with self.cached_session(): variables.local_variables_initializer().run() weighted_tp = 3.0 + 4.0 weighted_positives = (1.0 + 3.0) + (4.0 + 2.0) @@ -881,7 +881,7 @@ class PrecisionTest(test.TestCase): labels = constant_op.constant(1 - inputs) precision, update_op = metrics.precision(labels, predictions) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) sess.run(update_op) self.assertAlmostEqual(0, precision.eval()) @@ -891,7 +891,7 @@ class PrecisionTest(test.TestCase): labels = constant_op.constant([0, 0, 0, 0]) precision, update_op = metrics.precision(labels, predictions) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) sess.run(update_op) self.assertEqual(0.0, precision.eval()) @@ -933,7 +933,7 @@ class RecallTest(test.TestCase): (10, 3), maxval=1, dtype=dtypes_lib.int64, seed=1) recall, update_op = metrics.recall(labels, predictions) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) # Run several updates. @@ -952,7 +952,7 @@ class RecallTest(test.TestCase): labels = constant_op.constant(np_inputs) recall, update_op = metrics.recall(labels, predictions) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) sess.run(update_op) self.assertEqual(1, recall.eval()) @@ -965,7 +965,7 @@ class RecallTest(test.TestCase): constant_op.constant([0, 1, 1, 0], shape=(1, 4)), dtype=dtype) recall, update_op = metrics.recall(labels, predictions) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertAlmostEqual(0.5, update_op.eval()) self.assertAlmostEqual(0.5, recall.eval()) @@ -976,7 +976,7 @@ class RecallTest(test.TestCase): weights = constant_op.constant([[2], [5]]) recall, update_op = metrics.recall(labels, predictions, weights=weights) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) weighted_tp = 2.0 + 5.0 weighted_t = (2.0 + 2.0) + (5.0 + 5.0) @@ -990,7 +990,7 @@ class RecallTest(test.TestCase): weights = constant_op.constant([[1, 2, 3, 4], [4, 3, 2, 1]]) recall, update_op = metrics.recall(labels, predictions, weights=weights) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) weighted_tp = 3.0 + 1.0 weighted_t = (2.0 + 3.0) + (4.0 + 1.0) @@ -1005,7 +1005,7 @@ class RecallTest(test.TestCase): labels = constant_op.constant(1 - np_inputs) recall, update_op = metrics.recall(labels, predictions) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) sess.run(update_op) self.assertEqual(0, recall.eval()) @@ -1015,7 +1015,7 @@ class RecallTest(test.TestCase): labels = array_ops.zeros((1, 4)) recall, update_op = metrics.recall(labels, predictions) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) sess.run(update_op) self.assertEqual(0, recall.eval()) @@ -1055,7 +1055,7 @@ class AUCTest(test.TestCase): (10, 3), maxval=1, dtype=dtypes_lib.int64, seed=1) auc, update_op = metrics.auc(labels, predictions) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) # Run several updates. @@ -1073,7 +1073,7 @@ class AUCTest(test.TestCase): def allCorrectAsExpected(self, curve): inputs = np.random.randint(0, 2, size=(100, 1)) - with self.test_session() as sess: + with self.cached_session() as sess: predictions = constant_op.constant(inputs, dtype=dtypes_lib.float32) labels = constant_op.constant(inputs) auc, update_op = metrics.auc(labels, predictions, curve=curve) @@ -1084,7 +1084,7 @@ class AUCTest(test.TestCase): self.assertEqual(1, auc.eval()) def testSomeCorrect_multipleLabelDtypes(self): - with self.test_session() as sess: + with self.cached_session() as sess: for label_dtype in ( dtypes_lib.bool, dtypes_lib.int32, dtypes_lib.float32): predictions = constant_op.constant( @@ -1099,7 +1099,7 @@ class AUCTest(test.TestCase): self.assertAlmostEqual(0.5, auc.eval()) def testWeighted1d(self): - with self.test_session() as sess: + with self.cached_session() as sess: predictions = constant_op.constant( [1, 0, 1, 0], shape=(1, 4), dtype=dtypes_lib.float32) labels = constant_op.constant([0, 1, 1, 0], shape=(1, 4)) @@ -1112,7 +1112,7 @@ class AUCTest(test.TestCase): self.assertAlmostEqual(0.5, auc.eval(), 5) def testWeighted2d(self): - with self.test_session() as sess: + with self.cached_session() as sess: predictions = constant_op.constant( [1, 0, 1, 0], shape=(1, 4), dtype=dtypes_lib.float32) labels = constant_op.constant([0, 1, 1, 0], shape=(1, 4)) @@ -1127,7 +1127,7 @@ class AUCTest(test.TestCase): # Regarding the AUC-PR tests: note that the preferred method when # calculating AUC-PR is summation_method='careful_interpolation'. def testCorrectAUCPRSpecialCase(self): - with self.test_session() as sess: + with self.cached_session() as sess: predictions = constant_op.constant( [0.1, 0.4, 0.35, 0.8], shape=(1, 4), dtype=dtypes_lib.float32) labels = constant_op.constant([0, 0, 1, 1], shape=(1, 4)) @@ -1141,7 +1141,7 @@ class AUCTest(test.TestCase): self.assertAlmostEqual(expected, auc.eval(), delta=1e-3) def testCorrectAnotherAUCPRSpecialCase(self): - with self.test_session() as sess: + with self.cached_session() as sess: predictions = constant_op.constant( [0.1, 0.4, 0.35, 0.8, 0.1, 0.135, 0.81], shape=(1, 7), @@ -1157,7 +1157,7 @@ class AUCTest(test.TestCase): self.assertAlmostEqual(expected, auc.eval(), delta=1e-3) def testThirdCorrectAUCPRSpecialCase(self): - with self.test_session() as sess: + with self.cached_session() as sess: predictions = constant_op.constant( [0.0, 0.1, 0.2, 0.33, 0.3, 0.4, 0.5], shape=(1, 7), @@ -1173,7 +1173,7 @@ class AUCTest(test.TestCase): self.assertAlmostEqual(expected, auc.eval(), delta=1e-3) def testIncorrectAUCPRSpecialCase(self): - with self.test_session() as sess: + with self.cached_session() as sess: predictions = constant_op.constant( [0.1, 0.4, 0.35, 0.8], shape=(1, 4), dtype=dtypes_lib.float32) labels = constant_op.constant([0, 0, 1, 1], shape=(1, 4)) @@ -1186,7 +1186,7 @@ class AUCTest(test.TestCase): self.assertAlmostEqual(0.79166, auc.eval(), delta=1e-3) def testAnotherIncorrectAUCPRSpecialCase(self): - with self.test_session() as sess: + with self.cached_session() as sess: predictions = constant_op.constant( [0.1, 0.4, 0.35, 0.8, 0.1, 0.135, 0.81], shape=(1, 7), @@ -1201,7 +1201,7 @@ class AUCTest(test.TestCase): self.assertAlmostEqual(0.610317, auc.eval(), delta=1e-3) def testThirdIncorrectAUCPRSpecialCase(self): - with self.test_session() as sess: + with self.cached_session() as sess: predictions = constant_op.constant( [0.0, 0.1, 0.2, 0.33, 0.3, 0.4, 0.5], shape=(1, 7), @@ -1218,7 +1218,7 @@ class AUCTest(test.TestCase): def testAllIncorrect(self): inputs = np.random.randint(0, 2, size=(100, 1)) - with self.test_session() as sess: + with self.cached_session() as sess: predictions = constant_op.constant(inputs, dtype=dtypes_lib.float32) labels = constant_op.constant(1 - inputs, dtype=dtypes_lib.float32) auc, update_op = metrics.auc(labels, predictions) @@ -1229,7 +1229,7 @@ class AUCTest(test.TestCase): self.assertAlmostEqual(0, auc.eval()) def testZeroTruePositivesAndFalseNegativesGivesOneAUC(self): - with self.test_session() as sess: + with self.cached_session() as sess: predictions = array_ops.zeros([4], dtype=dtypes_lib.float32) labels = array_ops.zeros([4]) auc, update_op = metrics.auc(labels, predictions) @@ -1240,7 +1240,7 @@ class AUCTest(test.TestCase): self.assertAlmostEqual(1, auc.eval(), 6) def testRecallOneAndPrecisionOneGivesOnePRAUC(self): - with self.test_session() as sess: + with self.cached_session() as sess: predictions = array_ops.ones([4], dtype=dtypes_lib.float32) labels = array_ops.ones([4]) auc, update_op = metrics.auc(labels, predictions, curve='PR') @@ -1301,7 +1301,7 @@ class AUCTest(test.TestCase): scale=1.0, size=num_samples)): expected_auc = self.np_auc(predictions, labels, weights) - with self.test_session() as sess: + with self.cached_session() as sess: enqueue_ops = [[] for i in range(num_batches)] tf_predictions = _enqueue_as_batches(predictions, enqueue_ops) tf_labels = _enqueue_as_batches(labels, enqueue_ops) @@ -1370,7 +1370,7 @@ class SpecificityAtSensitivityTest(test.TestCase): specificity, update_op = metrics.specificity_at_sensitivity( labels, predictions, sensitivity=0.7) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) # Run several updates. @@ -1390,7 +1390,7 @@ class SpecificityAtSensitivityTest(test.TestCase): specificity, update_op = metrics.specificity_at_sensitivity( labels, predictions, sensitivity=0.7) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertEqual(1, sess.run(update_op)) self.assertEqual(1, specificity.eval()) @@ -1405,7 +1405,7 @@ class SpecificityAtSensitivityTest(test.TestCase): specificity, update_op = metrics.specificity_at_sensitivity( labels, predictions, sensitivity=0.8) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertAlmostEqual(1.0, sess.run(update_op)) self.assertAlmostEqual(1.0, specificity.eval()) @@ -1420,7 +1420,7 @@ class SpecificityAtSensitivityTest(test.TestCase): specificity, update_op = metrics.specificity_at_sensitivity( labels, predictions, sensitivity=0.4) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertAlmostEqual(0.6, sess.run(update_op)) @@ -1439,7 +1439,7 @@ class SpecificityAtSensitivityTest(test.TestCase): specificity, update_op = metrics.specificity_at_sensitivity( labels, predictions, weights=weights, sensitivity=0.4) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertAlmostEqual(0.6, sess.run(update_op)) @@ -1457,7 +1457,7 @@ class SpecificityAtSensitivityTest(test.TestCase): specificity, update_op = metrics.specificity_at_sensitivity( labels, predictions, weights=weights, sensitivity=0.4) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertAlmostEqual(8.0 / 15.0, sess.run(update_op)) @@ -1507,7 +1507,7 @@ class SensitivityAtSpecificityTest(test.TestCase): sensitivity, update_op = metrics.sensitivity_at_specificity( labels, predictions, specificity=0.7) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) # Run several updates. @@ -1527,7 +1527,7 @@ class SensitivityAtSpecificityTest(test.TestCase): specificity, update_op = metrics.sensitivity_at_specificity( labels, predictions, specificity=0.7) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertEqual(1, sess.run(update_op)) self.assertEqual(1, specificity.eval()) @@ -1542,7 +1542,7 @@ class SensitivityAtSpecificityTest(test.TestCase): specificity, update_op = metrics.sensitivity_at_specificity( labels, predictions, specificity=0.8) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertAlmostEqual(0.8, sess.run(update_op)) self.assertAlmostEqual(0.8, specificity.eval()) @@ -1557,7 +1557,7 @@ class SensitivityAtSpecificityTest(test.TestCase): specificity, update_op = metrics.sensitivity_at_specificity( labels, predictions, specificity=0.4) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertAlmostEqual(0.6, sess.run(update_op)) self.assertAlmostEqual(0.6, specificity.eval()) @@ -1576,7 +1576,7 @@ class SensitivityAtSpecificityTest(test.TestCase): specificity, update_op = metrics.sensitivity_at_specificity( labels, predictions, weights=weights, specificity=0.4) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertAlmostEqual(0.675, sess.run(update_op)) self.assertAlmostEqual(0.675, specificity.eval()) @@ -1638,7 +1638,7 @@ class PrecisionRecallThresholdsTest(test.TestCase): thresholds) rec, rec_op = metrics.recall_at_thresholds(labels, predictions, thresholds) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) # Run several updates, then verify idempotency. @@ -1654,7 +1654,7 @@ class PrecisionRecallThresholdsTest(test.TestCase): def testAllCorrect(self): inputs = np.random.randint(0, 2, size=(100, 1)) - with self.test_session() as sess: + with self.cached_session() as sess: predictions = constant_op.constant(inputs, dtype=dtypes_lib.float32) labels = constant_op.constant(inputs) thresholds = [0.5] @@ -1670,7 +1670,7 @@ class PrecisionRecallThresholdsTest(test.TestCase): self.assertEqual(1, rec.eval()) def testSomeCorrect_multipleLabelDtypes(self): - with self.test_session() as sess: + with self.cached_session() as sess: for label_dtype in ( dtypes_lib.bool, dtypes_lib.int32, dtypes_lib.float32): predictions = constant_op.constant( @@ -1692,7 +1692,7 @@ class PrecisionRecallThresholdsTest(test.TestCase): def testAllIncorrect(self): inputs = np.random.randint(0, 2, size=(100, 1)) - with self.test_session() as sess: + with self.cached_session() as sess: predictions = constant_op.constant(inputs, dtype=dtypes_lib.float32) labels = constant_op.constant(1 - inputs, dtype=dtypes_lib.float32) thresholds = [0.5] @@ -1708,7 +1708,7 @@ class PrecisionRecallThresholdsTest(test.TestCase): self.assertAlmostEqual(0, rec.eval()) def testWeights1d(self): - with self.test_session() as sess: + with self.cached_session() as sess: predictions = constant_op.constant( [[1, 0], [1, 0]], shape=(2, 2), dtype=dtypes_lib.float32) labels = constant_op.constant([[0, 1], [1, 0]], shape=(2, 2)) @@ -1738,7 +1738,7 @@ class PrecisionRecallThresholdsTest(test.TestCase): self.assertAlmostEqual(0.0, rec_high.eval(), places=5) def testWeights2d(self): - with self.test_session() as sess: + with self.cached_session() as sess: predictions = constant_op.constant( [[1, 0], [1, 0]], shape=(2, 2), dtype=dtypes_lib.float32) labels = constant_op.constant([[0, 1], [1, 0]], shape=(2, 2)) @@ -1768,7 +1768,7 @@ class PrecisionRecallThresholdsTest(test.TestCase): self.assertAlmostEqual(0.0, rec_high.eval(), places=5) def testExtremeThresholds(self): - with self.test_session() as sess: + with self.cached_session() as sess: predictions = constant_op.constant( [1, 0, 1, 0], shape=(1, 4), dtype=dtypes_lib.float32) labels = constant_op.constant([0, 1, 1, 1], shape=(1, 4)) @@ -1792,7 +1792,7 @@ class PrecisionRecallThresholdsTest(test.TestCase): self.assertAlmostEqual(0.0, rec_high.eval()) def testZeroLabelsPredictions(self): - with self.test_session() as sess: + with self.cached_session() as sess: predictions = array_ops.zeros([4], dtype=dtypes_lib.float32) labels = array_ops.zeros([4]) thresholds = [0.5] @@ -1842,7 +1842,7 @@ class PrecisionRecallThresholdsTest(test.TestCase): labels = labels.astype(np.float32) predictions = predictions.astype(np.float32) - with self.test_session() as sess: + with self.cached_session() as sess: # Reshape the data so its easy to queue up: predictions_batches = predictions.reshape((batch_size, num_batches)) labels_batches = labels.reshape((batch_size, num_batches)) @@ -2801,7 +2801,7 @@ class MeanAbsoluteErrorTest(test.TestCase): labels = random_ops.random_normal((10, 3), seed=2) error, update_op = metrics.mean_absolute_error(labels, predictions) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) # Run several updates. @@ -2822,7 +2822,7 @@ class MeanAbsoluteErrorTest(test.TestCase): error, update_op = metrics.mean_absolute_error(labels, predictions, weights) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertEqual(3, sess.run(update_op)) self.assertEqual(3, error.eval()) @@ -2866,7 +2866,7 @@ class MeanRelativeErrorTest(test.TestCase): error, update_op = metrics.mean_relative_error(labels, predictions, normalizer) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) # Run several updates. @@ -2891,7 +2891,7 @@ class MeanRelativeErrorTest(test.TestCase): error, update_op = metrics.mean_relative_error( labels, predictions, normalizer=labels) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertEqual(expected_error, sess.run(update_op)) self.assertEqual(expected_error, error.eval()) @@ -2907,7 +2907,7 @@ class MeanRelativeErrorTest(test.TestCase): error, update_op = metrics.mean_relative_error( labels, predictions, normalizer=array_ops.zeros_like(labels)) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertEqual(0.0, sess.run(update_op)) self.assertEqual(0.0, error.eval()) @@ -2945,7 +2945,7 @@ class MeanSquaredErrorTest(test.TestCase): labels = random_ops.random_normal((10, 3), seed=2) error, update_op = metrics.mean_squared_error(labels, predictions) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) # Run several updates. @@ -2963,7 +2963,7 @@ class MeanSquaredErrorTest(test.TestCase): error, update_op = metrics.mean_squared_error(labels, predictions) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertEqual(0, sess.run(update_op)) self.assertEqual(0, error.eval()) @@ -2976,7 +2976,7 @@ class MeanSquaredErrorTest(test.TestCase): error, update_op = metrics.mean_squared_error(labels, predictions) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertEqual(6, sess.run(update_op)) self.assertEqual(6, error.eval()) @@ -2990,13 +2990,13 @@ class MeanSquaredErrorTest(test.TestCase): error, update_op = metrics.mean_squared_error(labels, predictions, weights) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertEqual(13, sess.run(update_op)) self.assertEqual(13, error.eval()) def testMultipleBatchesOfSizeOne(self): - with self.test_session() as sess: + with self.cached_session() as sess: # Create the queue that populates the predictions. preds_queue = data_flow_ops.FIFOQueue( 2, dtypes=dtypes_lib.float32, shapes=(1, 3)) @@ -3020,7 +3020,7 @@ class MeanSquaredErrorTest(test.TestCase): self.assertAlmostEqual(208.0 / 6, error.eval(), 5) def testMetricsComputedConcurrently(self): - with self.test_session() as sess: + with self.cached_session() as sess: # Create the queue that populates one set of predictions. preds_queue0 = data_flow_ops.FIFOQueue( 2, dtypes=dtypes_lib.float32, shapes=(1, 3)) @@ -3063,7 +3063,7 @@ class MeanSquaredErrorTest(test.TestCase): self.assertAlmostEqual(79.0 / 6, mse1, 5) def testMultipleMetricsOnMultipleBatchesOfSizeOne(self): - with self.test_session() as sess: + with self.cached_session() as sess: # Create the queue that populates the predictions. preds_queue = data_flow_ops.FIFOQueue( 2, dtypes=dtypes_lib.float32, shapes=(1, 3)) @@ -3122,7 +3122,7 @@ class RootMeanSquaredErrorTest(test.TestCase): labels = random_ops.random_normal((10, 3), seed=2) error, update_op = metrics.root_mean_squared_error(labels, predictions) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) # Run several updates. @@ -3135,7 +3135,7 @@ class RootMeanSquaredErrorTest(test.TestCase): self.assertEqual(initial_error, error.eval()) def testSingleUpdateZeroError(self): - with self.test_session() as sess: + with self.cached_session() as sess: predictions = constant_op.constant( 0.0, shape=(1, 3), dtype=dtypes_lib.float32) labels = constant_op.constant(0.0, shape=(1, 3), dtype=dtypes_lib.float32) @@ -3148,7 +3148,7 @@ class RootMeanSquaredErrorTest(test.TestCase): self.assertEqual(0, rmse.eval()) def testSingleUpdateWithError(self): - with self.test_session() as sess: + with self.cached_session() as sess: predictions = constant_op.constant( [2, 4, 6], shape=(1, 3), dtype=dtypes_lib.float32) labels = constant_op.constant( @@ -3161,7 +3161,7 @@ class RootMeanSquaredErrorTest(test.TestCase): self.assertAlmostEqual(math.sqrt(6), rmse.eval(), 5) def testSingleUpdateWithErrorAndWeights(self): - with self.test_session() as sess: + with self.cached_session() as sess: predictions = constant_op.constant( [2, 4, 6, 8], shape=(1, 4), dtype=dtypes_lib.float32) labels = constant_op.constant( @@ -3220,7 +3220,7 @@ class MeanCosineDistanceTest(test.TestCase): labels = random_ops.random_normal((10, 3), seed=2) error, update_op = metrics.mean_cosine_distance(labels, predictions, dim=1) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) # Run several updates. @@ -3242,7 +3242,7 @@ class MeanCosineDistanceTest(test.TestCase): error, update_op = metrics.mean_cosine_distance(labels, predictions, dim=2) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertEqual(0, sess.run(update_op)) self.assertEqual(0, error.eval()) @@ -3258,7 +3258,7 @@ class MeanCosineDistanceTest(test.TestCase): error, update_op = metrics.mean_cosine_distance(labels, predictions, dim=2) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertAlmostEqual(1, sess.run(update_op), 5) self.assertAlmostEqual(1, error.eval(), 5) @@ -3279,7 +3279,7 @@ class MeanCosineDistanceTest(test.TestCase): np_labels, shape=(3, 1, 3), dtype=dtypes_lib.float32) error, update_op = metrics.mean_cosine_distance(labels, predictions, dim=2) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertAlmostEqual(1.0, sess.run(update_op), 5) self.assertAlmostEqual(1.0, error.eval(), 5) @@ -3298,7 +3298,7 @@ class MeanCosineDistanceTest(test.TestCase): error, update_op = metrics.mean_cosine_distance( labels, predictions, dim=2, weights=weights) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertEqual(0, sess.run(update_op)) self.assertEqual(0, error.eval()) @@ -3317,7 +3317,7 @@ class MeanCosineDistanceTest(test.TestCase): error, update_op = metrics.mean_cosine_distance( labels, predictions, dim=2, weights=weights) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertEqual(1.5, update_op.eval()) self.assertEqual(1.5, error.eval()) @@ -3352,7 +3352,7 @@ class PcntBelowThreshTest(test.TestCase): self.assertListEqual(ops.get_collection(my_collection_name), [update_op]) def testOneUpdate(self): - with self.test_session() as sess: + with self.cached_session() as sess: values = constant_op.constant( [2, 4, 6, 8], shape=(1, 4), dtype=dtypes_lib.float32) @@ -3369,7 +3369,7 @@ class PcntBelowThreshTest(test.TestCase): self.assertAlmostEqual(0.0, pcnt2, 5) def testSomePresentOneUpdate(self): - with self.test_session() as sess: + with self.cached_session() as sess: values = constant_op.constant( [2, 4, 6, 8], shape=(1, 4), dtype=dtypes_lib.float32) weights = constant_op.constant( @@ -3445,7 +3445,7 @@ class MeanIOUTest(test.TestCase): mean_iou, update_op = metrics.mean_iou( labels, predictions, num_classes=num_classes) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) # Run several updates. @@ -3459,7 +3459,7 @@ class MeanIOUTest(test.TestCase): def testMultipleUpdates(self): num_classes = 3 - with self.test_session() as sess: + with self.cached_session() as sess: # Create the queue that populates the predictions. preds_queue = data_flow_ops.FIFOQueue( 5, dtypes=dtypes_lib.int32, shapes=(1, 1)) @@ -3490,7 +3490,7 @@ class MeanIOUTest(test.TestCase): def testMultipleUpdatesWithWeights(self): num_classes = 2 - with self.test_session() as sess: + with self.cached_session() as sess: # Create the queue that populates the predictions. preds_queue = data_flow_ops.FIFOQueue( 6, dtypes=dtypes_lib.int32, shapes=(1, 1)) @@ -3538,7 +3538,7 @@ class MeanIOUTest(test.TestCase): # one class, and thus there is one row and one column with # zero entries in the confusion matrix. num_classes = 3 - with self.test_session() as sess: + with self.cached_session() as sess: # Create the queue that populates the predictions. # There is no prediction for class 2. preds_queue = data_flow_ops.FIFOQueue( @@ -3585,7 +3585,7 @@ class MeanIOUTest(test.TestCase): ], 0) num_classes = 2 - with self.test_session() as sess: + with self.cached_session() as sess: miou, update_op = metrics.mean_iou(labels, predictions, num_classes) sess.run(variables.local_variables_initializer()) confusion_matrix = update_op.eval() @@ -3597,7 +3597,7 @@ class MeanIOUTest(test.TestCase): predictions = array_ops.zeros([40]) labels = array_ops.zeros([40]) num_classes = 1 - with self.test_session() as sess: + with self.cached_session() as sess: miou, update_op = metrics.mean_iou(labels, predictions, num_classes) sess.run(variables.local_variables_initializer()) self.assertEqual(40, update_op.eval()[0]) @@ -3607,7 +3607,7 @@ class MeanIOUTest(test.TestCase): predictions = array_ops.zeros([40]) labels = array_ops.ones([40]) num_classes = 2 - with self.test_session() as sess: + with self.cached_session() as sess: miou, update_op = metrics.mean_iou(labels, predictions, num_classes) sess.run(variables.local_variables_initializer()) self.assertAllEqual([[0, 0], [40, 0]], update_op.eval()) @@ -3637,7 +3637,7 @@ class MeanIOUTest(test.TestCase): 0, shape=[1]) ], 0) - with self.test_session() as sess: + with self.cached_session() as sess: miou, update_op = metrics.mean_iou( labels, predictions, num_classes, weights=weights) sess.run(variables.local_variables_initializer()) @@ -3657,7 +3657,7 @@ class MeanIOUTest(test.TestCase): [[0, 0, 2, 1, 1, 1], [1, 1, 2, 0, 0, 0]]]) num_classes = 3 - with self.test_session() as sess: + with self.cached_session() as sess: miou, update_op = metrics.mean_iou(labels, predictions, num_classes) sess.run(variables.local_variables_initializer()) self.assertAllEqual([[7, 4, 3], [3, 5, 2], [0, 0, 0]], update_op.eval()) @@ -3669,7 +3669,7 @@ class MeanIOUTest(test.TestCase): labels = constant_op.constant([0]) predictions = constant_op.constant([0]) num_classes = 2 - with self.test_session() as sess: + with self.cached_session() as sess: miou, update_op = metrics.mean_iou(labels, predictions, num_classes) sess.run(variables.local_variables_initializer()) self.assertAllEqual([[1, 0], [0, 0]], update_op.eval()) @@ -3687,7 +3687,7 @@ class MeanIOUTest(test.TestCase): [[0, 0, 0, 1, 1, 1], [1, 1, 1, 0, 0, 0]]]) num_classes = 3 - with self.test_session() as sess: + with self.cached_session() as sess: miou, update_op = metrics.mean_iou(labels, predictions, num_classes) sess.run(variables.local_variables_initializer()) self.assertAllEqual([[9, 5, 0], [3, 7, 0], [0, 0, 0]], update_op.eval()) @@ -3751,7 +3751,7 @@ class MeanPerClassAccuracyTest(test.TestCase): mean_accuracy, update_op = metrics.mean_per_class_accuracy( labels, predictions, num_classes=num_classes) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) # Run several updates. @@ -3764,7 +3764,7 @@ class MeanPerClassAccuracyTest(test.TestCase): self.assertEqual(initial_mean_accuracy, mean_accuracy.eval()) num_classes = 3 - with self.test_session() as sess: + with self.cached_session() as sess: # Create the queue that populates the predictions. preds_queue = data_flow_ops.FIFOQueue( 5, dtypes=dtypes_lib.int32, shapes=(1, 1)) @@ -3796,7 +3796,7 @@ class MeanPerClassAccuracyTest(test.TestCase): def testMultipleUpdatesWithWeights(self): num_classes = 2 - with self.test_session() as sess: + with self.cached_session() as sess: # Create the queue that populates the predictions. preds_queue = data_flow_ops.FIFOQueue( 6, dtypes=dtypes_lib.int32, shapes=(1, 1)) @@ -3844,7 +3844,7 @@ class MeanPerClassAccuracyTest(test.TestCase): # one class, and thus there is one row and one column with # zero entries in the confusion matrix. num_classes = 3 - with self.test_session() as sess: + with self.cached_session() as sess: # Create the queue that populates the predictions. # There is no prediction for class 2. preds_queue = data_flow_ops.FIFOQueue( @@ -3880,7 +3880,7 @@ class MeanPerClassAccuracyTest(test.TestCase): predictions = array_ops.zeros([40]) labels = array_ops.zeros([40]) num_classes = 1 - with self.test_session() as sess: + with self.cached_session() as sess: mean_accuracy, update_op = metrics.mean_per_class_accuracy( labels, predictions, num_classes) sess.run(variables.local_variables_initializer()) @@ -3891,7 +3891,7 @@ class MeanPerClassAccuracyTest(test.TestCase): predictions = array_ops.zeros([40]) labels = array_ops.ones([40]) num_classes = 2 - with self.test_session() as sess: + with self.cached_session() as sess: mean_accuracy, update_op = metrics.mean_per_class_accuracy( labels, predictions, num_classes) sess.run(variables.local_variables_initializer()) @@ -3910,7 +3910,7 @@ class MeanPerClassAccuracyTest(test.TestCase): constant_op.constant(0, shape=[1]), constant_op.constant(1, shape=[8]), constant_op.constant(0, shape=[1]) ], 0) - with self.test_session() as sess: + with self.cached_session() as sess: mean_accuracy, update_op = metrics.mean_per_class_accuracy( labels, predictions, num_classes, weights=weights) sess.run(variables.local_variables_initializer()) @@ -3944,7 +3944,7 @@ class FalseNegativesTest(test.TestCase): tn, tn_update_op = metrics.false_negatives( labels=labels, predictions=predictions) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertAllClose(0., tn.eval()) self.assertAllClose(3., tn_update_op.eval()) @@ -3963,7 +3963,7 @@ class FalseNegativesTest(test.TestCase): tn, tn_update_op = metrics.false_negatives( labels=labels, predictions=predictions, weights=weights) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertAllClose(0., tn.eval()) self.assertAllClose(5., tn_update_op.eval()) @@ -3993,7 +3993,7 @@ class FalseNegativesAtThresholdsTest(test.TestCase): fn, fn_update_op = metrics.false_negatives_at_thresholds( predictions=predictions, labels=labels, thresholds=[0.15, 0.5, 0.85]) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertAllEqual((0, 0, 0), fn.eval()) self.assertAllEqual((0, 2, 3), fn_update_op.eval()) @@ -4012,7 +4012,7 @@ class FalseNegativesAtThresholdsTest(test.TestCase): weights=((3.0,), (5.0,), (7.0,)), thresholds=[0.15, 0.5, 0.85]) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertAllEqual((0.0, 0.0, 0.0), fn.eval()) self.assertAllEqual((0.0, 8.0, 11.0), fn_update_op.eval()) @@ -4043,7 +4043,7 @@ class FalsePositivesTest(test.TestCase): tn, tn_update_op = metrics.false_positives( labels=labels, predictions=predictions) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertAllClose(0., tn.eval()) self.assertAllClose(7., tn_update_op.eval()) @@ -4062,7 +4062,7 @@ class FalsePositivesTest(test.TestCase): tn, tn_update_op = metrics.false_positives( labels=labels, predictions=predictions, weights=weights) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertAllClose(0., tn.eval()) self.assertAllClose(14., tn_update_op.eval()) @@ -4092,7 +4092,7 @@ class FalsePositivesAtThresholdsTest(test.TestCase): fp, fp_update_op = metrics.false_positives_at_thresholds( predictions=predictions, labels=labels, thresholds=[0.15, 0.5, 0.85]) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertAllEqual((0, 0, 0), fp.eval()) self.assertAllEqual((7, 4, 2), fp_update_op.eval()) @@ -4113,7 +4113,7 @@ class FalsePositivesAtThresholdsTest(test.TestCase): (19.0, 23.0, 29.0, 31.0)), thresholds=[0.15, 0.5, 0.85]) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertAllEqual((0.0, 0.0, 0.0), fp.eval()) self.assertAllEqual((125.0, 42.0, 12.0), fp_update_op.eval()) @@ -4144,7 +4144,7 @@ class TrueNegativesTest(test.TestCase): tn, tn_update_op = metrics.true_negatives( labels=labels, predictions=predictions) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertAllClose(0., tn.eval()) self.assertAllClose(3., tn_update_op.eval()) @@ -4163,7 +4163,7 @@ class TrueNegativesTest(test.TestCase): tn, tn_update_op = metrics.true_negatives( labels=labels, predictions=predictions, weights=weights) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertAllClose(0., tn.eval()) self.assertAllClose(4., tn_update_op.eval()) @@ -4193,7 +4193,7 @@ class TrueNegativesAtThresholdsTest(test.TestCase): tn, tn_update_op = metrics.true_negatives_at_thresholds( predictions=predictions, labels=labels, thresholds=[0.15, 0.5, 0.85]) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertAllEqual((0, 0, 0), tn.eval()) self.assertAllEqual((2, 5, 7), tn_update_op.eval()) @@ -4212,7 +4212,7 @@ class TrueNegativesAtThresholdsTest(test.TestCase): weights=((0.0, 2.0, 3.0, 5.0),), thresholds=[0.15, 0.5, 0.85]) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertAllEqual((0.0, 0.0, 0.0), tn.eval()) self.assertAllEqual((5.0, 15.0, 23.0), tn_update_op.eval()) @@ -4243,7 +4243,7 @@ class TruePositivesTest(test.TestCase): tn, tn_update_op = metrics.true_positives( labels=labels, predictions=predictions) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertAllClose(0., tn.eval()) self.assertAllClose(7., tn_update_op.eval()) @@ -4262,7 +4262,7 @@ class TruePositivesTest(test.TestCase): tn, tn_update_op = metrics.true_positives( labels=labels, predictions=predictions, weights=weights) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertAllClose(0., tn.eval()) self.assertAllClose(12., tn_update_op.eval()) @@ -4292,7 +4292,7 @@ class TruePositivesAtThresholdsTest(test.TestCase): tp, tp_update_op = metrics.true_positives_at_thresholds( predictions=predictions, labels=labels, thresholds=[0.15, 0.5, 0.85]) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertAllEqual((0, 0, 0), tp.eval()) self.assertAllEqual((3, 1, 0), tp_update_op.eval()) @@ -4309,7 +4309,7 @@ class TruePositivesAtThresholdsTest(test.TestCase): predictions=predictions, labels=labels, weights=37.0, thresholds=[0.15, 0.5, 0.85]) - with self.test_session() as sess: + with self.cached_session() as sess: sess.run(variables.local_variables_initializer()) self.assertAllEqual((0.0, 0.0, 0.0), tp.eval()) self.assertAllEqual((111.0, 37.0, 0.0), tp_update_op.eval()) diff --git a/tensorflow/python/kernel_tests/numerics_test.py b/tensorflow/python/kernel_tests/numerics_test.py index 89ada8430e..6cc70f7c89 100644 --- a/tensorflow/python/kernel_tests/numerics_test.py +++ b/tensorflow/python/kernel_tests/numerics_test.py @@ -66,7 +66,7 @@ class VerifyTensorAllFiniteTest(test.TestCase): class NumericsTest(test.TestCase): def testInf(self): - with self.test_session(graph=ops.Graph()): + with self.session(graph=ops.Graph()): t1 = constant_op.constant(1.0) t2 = constant_op.constant(0.0) a = math_ops.div(t1, t2) @@ -76,7 +76,7 @@ class NumericsTest(test.TestCase): a.eval() def testNaN(self): - with self.test_session(graph=ops.Graph()): + with self.session(graph=ops.Graph()): t1 = constant_op.constant(0.0) t2 = constant_op.constant(0.0) a = math_ops.div(t1, t2) @@ -86,7 +86,7 @@ class NumericsTest(test.TestCase): a.eval() def testBoth(self): - with self.test_session(graph=ops.Graph()): + with self.session(graph=ops.Graph()): t1 = constant_op.constant([1.0, 0.0]) t2 = constant_op.constant([0.0, 0.0]) a = math_ops.div(t1, t2) @@ -96,7 +96,7 @@ class NumericsTest(test.TestCase): a.eval() def testPassThrough(self): - with self.test_session(graph=ops.Graph()): + with self.session(graph=ops.Graph()): t1 = constant_op.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3]) checked = array_ops.check_numerics(t1, message="pass through test") value = checked.eval() diff --git a/tensorflow/python/kernel_tests/pad_op_test.py b/tensorflow/python/kernel_tests/pad_op_test.py index 944de217a1..e415d7879e 100644 --- a/tensorflow/python/kernel_tests/pad_op_test.py +++ b/tensorflow/python/kernel_tests/pad_op_test.py @@ -188,7 +188,7 @@ class PadOpTest(test.TestCase): mode="SYMMETRIC").eval() def testInvalid(self): - with self.test_session(): + with self.cached_session(): x = [[1, 2, 3], [4, 5, 6]] with self.assertRaisesRegexp(ValueError, "Unknown padding mode"): array_ops.pad(x, [[1, 0], [2, 1]], mode="weird").eval() diff --git a/tensorflow/python/kernel_tests/padding_fifo_queue_test.py b/tensorflow/python/kernel_tests/padding_fifo_queue_test.py index d8c3f9823c..95f3dcceea 100644 --- a/tensorflow/python/kernel_tests/padding_fifo_queue_test.py +++ b/tensorflow/python/kernel_tests/padding_fifo_queue_test.py @@ -95,13 +95,13 @@ class PaddingFIFOQueueTest(test.TestCase): """, q.queue_ref.op.node_def) def testEnqueue(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.PaddingFIFOQueue(10, dtypes_lib.float32, ((),)) enqueue_op = q.enqueue((10.0,)) enqueue_op.run() def testEnqueueWithShape(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.PaddingFIFOQueue( 10, dtypes_lib.float32, shapes=((3, 2),)) enqueue_correct_op = q.enqueue(([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]],)) @@ -111,14 +111,14 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertEqual(1, q.size().eval()) def testEnqueueManyWithShape(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.PaddingFIFOQueue( 10, [dtypes_lib.int32, dtypes_lib.int32], shapes=[(), (2,)]) q.enqueue_many([[1, 2, 3, 4], [[1, 1], [2, 2], [3, 3], [4, 4]]]).run() self.assertEqual(4, q.size().eval()) def testParallelEnqueue(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue(10, dtypes_lib.float32, ((),)) elems = [10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0, 100.0] enqueue_ops = [q.enqueue((x,)) for x in elems] @@ -144,7 +144,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertItemsEqual(elems, results) def testParallelDequeue(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue(10, dtypes_lib.float32, ((),)) elems = [10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0, 100.0] enqueue_ops = [q.enqueue((x,)) for x in elems] @@ -168,7 +168,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertItemsEqual(elems, results) def testDequeue(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.PaddingFIFOQueue(10, dtypes_lib.float32, ((),)) elems = [10.0, 20.0, 30.0] enqueue_ops = [q.enqueue((x,)) for x in elems] @@ -182,7 +182,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertEqual([elems[i]], vals) def testEnqueueAndBlockingDequeue(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue(3, dtypes_lib.float32, ((),)) elems = [10.0, 20.0, 30.0] enqueue_ops = [q.enqueue((x,)) for x in elems] @@ -212,7 +212,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertEqual([elem], result) def testMultiEnqueueAndDequeue(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue(10, (dtypes_lib.int32, dtypes_lib.float32), ((), ())) @@ -230,12 +230,12 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertEqual([y], y_val) def testQueueSizeEmpty(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.PaddingFIFOQueue(10, dtypes_lib.float32, ((),)) self.assertEqual([0], q.size().eval()) def testQueueSizeAfterEnqueueAndDequeue(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.PaddingFIFOQueue(10, dtypes_lib.float32, ((),)) enqueue_op = q.enqueue((10.0,)) dequeued_t = q.dequeue() @@ -248,7 +248,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertEqual(0, size.eval()) def testEnqueueMany(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.PaddingFIFOQueue(10, dtypes_lib.float32, ((),)) elems = [10.0, 20.0, 30.0, 40.0] enqueue_op = q.enqueue_many((elems,)) @@ -261,7 +261,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertEqual([elems[i % 4]], vals) def testEmptyEnqueueMany(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.PaddingFIFOQueue(10, dtypes_lib.float32, ( (None, None),)) empty_t = constant_op.constant( @@ -274,7 +274,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertEqual([0], size_t.eval()) def testEmptyDequeueMany(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.PaddingFIFOQueue(10, dtypes_lib.float32, shapes=((),)) enqueue_op = q.enqueue((10.0,)) dequeued_t = q.dequeue_many(0) @@ -284,7 +284,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertEqual([], dequeued_t.eval().tolist()) def testEmptyDequeueManyWithDynamicShape(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.PaddingFIFOQueue( 10, dtypes_lib.float32, shapes=((None,),)) enqueue_op = q.enqueue(([10.0],)) @@ -295,7 +295,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertEqual([], dequeued_t.eval().tolist()) def testEmptyDequeueUpToWithDynamicShape(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.PaddingFIFOQueue( 10, dtypes_lib.float32, shapes=((None,),)) enqueue_op = q.enqueue(([10.0],)) @@ -306,7 +306,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertEqual([], dequeued_t.eval().tolist()) def testConstructPaddingFIFOQueueWithNoShape(self): - with self.test_session(): + with self.cached_session(): with self.assertRaisesRegexp( ValueError, r"When providing partial shapes, a list of shapes must be provided."): @@ -314,7 +314,7 @@ class PaddingFIFOQueueTest(test.TestCase): None).queue_ref.eval() def testMultiEnqueueMany(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue(10, (dtypes_lib.float32, dtypes_lib.int32), ((), (2,))) @@ -332,7 +332,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertAllEqual(int_elems[i % 4], int_val) def testMultiEnqueueManyWithPartiallyKnownShapes(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue( 10, (dtypes_lib.float32, dtypes_lib.int32), shapes=((), (None,))) float_elems = [10.0, 20.0, 30.0, 40.0] @@ -349,7 +349,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertAllEqual(int_elems[i % 4], int_val) def testDequeueMany(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.PaddingFIFOQueue(10, dtypes_lib.float32, ((),)) elems = [10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0, 100.0] enqueue_op = q.enqueue_many((elems,)) @@ -361,7 +361,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertAllEqual(elems[4:8], dequeued_t.eval()) def testDequeueUpToNoBlocking(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.PaddingFIFOQueue(10, dtypes_lib.float32, ((),)) elems = [10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0, 100.0] enqueue_op = q.enqueue_many((elems,)) @@ -373,7 +373,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertAllEqual(elems[4:8], dequeued_t.eval()) def testMultiDequeueMany(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue( 10, (dtypes_lib.float32, dtypes_lib.int32), shapes=((), (2,))) float_elems = [ @@ -404,7 +404,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertEqual(int_val.shape, dequeued_single_t[1].get_shape()) def testMultiDequeueManyWithPartiallyKnownShapes(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue( 10, (dtypes_lib.float32, dtypes_lib.int32), shapes=((), (None,))) float_elems = [ @@ -443,7 +443,7 @@ class PaddingFIFOQueueTest(test.TestCase): dequeued_single_t[1].get_shape())) def testMultiDequeueManyWithPartiallyKnownShapesAndVariableSizeInput(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue( 10, (dtypes_lib.string, dtypes_lib.int32), shapes=((None,), (1, None))) @@ -484,7 +484,7 @@ class PaddingFIFOQueueTest(test.TestCase): dequeued_single_t[1].get_shape())) def testMultiDequeueUpToPartiallyKnownShapesAndVariableInputNoBlocking(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue( 10, (dtypes_lib.string, dtypes_lib.int32), shapes=((None,), (1, None))) @@ -525,7 +525,7 @@ class PaddingFIFOQueueTest(test.TestCase): dequeued_single_t[1].get_shape())) def testHighDimension(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.PaddingFIFOQueue(10, dtypes_lib.int32, ((4, 4, 4, 4),)) elems = np.array([[[[[x] * 4] * 4] * 4] * 4 for x in range(10)], np.int32) enqueue_op = q.enqueue_many((elems,)) @@ -535,7 +535,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertAllEqual(dequeued_t.eval(), elems) def testPartiallyKnownHighDimension(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.PaddingFIFOQueue(10, dtypes_lib.int32, ( (4, None, 4, None),)) elems = np.array([[[[[x] * 4] * 4] * 4] * 4 for x in range(10)], np.int32) @@ -592,7 +592,7 @@ class PaddingFIFOQueueTest(test.TestCase): array_ops.placeholder(dtypes_lib.int32))) def testEnqueueWrongPartiallyKnownShapeAtRuntime(self): - with self.test_session() as sess: + with self.cached_session() as sess: # First dimension of second component is unknown, second # dimension must be 3. q = data_flow_ops.PaddingFIFOQueue(10, @@ -607,7 +607,7 @@ class PaddingFIFOQueueTest(test.TestCase): feed_dict={elems_bad: np.array([1] * 12).reshape((3, 4))}) def testEnqueueDequeueManyWrongPartiallyKnownShape(self): - with self.test_session() as sess: + with self.cached_session() as sess: # First dimension of second component is unknown, second # dimension must be 3. q = data_flow_ops.PaddingFIFOQueue(10, @@ -625,7 +625,7 @@ class PaddingFIFOQueueTest(test.TestCase): dequeued_t.eval() def testParallelEnqueueMany(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue(1000, dtypes_lib.float32, shapes=((),)) elems = [10.0 * x for x in range(100)] enqueue_op = q.enqueue_many((elems,)) @@ -644,7 +644,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertItemsEqual(dequeued_t.eval(), elems * 10) def testParallelDequeueMany(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue(1000, dtypes_lib.float32, shapes=((),)) elems = [10.0 * x for x in range(1000)] enqueue_op = q.enqueue_many((elems,)) @@ -666,7 +666,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertItemsEqual(elems, dequeued_elems) def testParallelDequeueUpTo(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue(1000, dtypes_lib.float32, shapes=((),)) elems = [10.0 * x for x in range(1000)] enqueue_op = q.enqueue_many((elems,)) @@ -690,7 +690,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertItemsEqual(elems, dequeued_elems) def testParallelEnqueueAndDequeue(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue(50, dtypes_lib.float32, shapes=((),)) initial_elements = [10.0] * 49 q.enqueue_many((initial_elements,)).run() @@ -723,7 +723,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertTrue(elem in (10.0, 20.0)) def testMixtureOfEnqueueAndEnqueueMany(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue(10, dtypes_lib.int32, shapes=((),)) enqueue_placeholder = array_ops.placeholder(dtypes_lib.int32, shape=()) enqueue_op = q.enqueue((enqueue_placeholder,)) @@ -759,7 +759,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertEqual(0, q.size().eval()) def testMixtureOfDequeueAndDequeueMany(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue(10, dtypes_lib.int32, shapes=((),)) enqueue_op = q.enqueue_many((np.arange(250, dtype=np.int32),)) dequeued_t = q.dequeue() @@ -793,7 +793,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertEqual(0, q.size().eval()) def testBlockingDequeueMany(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue(10, dtypes_lib.float32, ((),)) elems = [10.0, 20.0, 30.0, 40.0] enqueue_op = q.enqueue_many((elems,)) @@ -820,7 +820,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertAllEqual(elems, dequeued_elems) def testBlockingDequeueUpTo(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue(10, dtypes_lib.float32, ((),)) elems = [10.0, 20.0, 30.0, 40.0] enqueue_op = q.enqueue_many((elems,)) @@ -847,7 +847,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertAllEqual(elems, dequeued_elems) def testDequeueManyWithTensorParameter(self): - with self.test_session(): + with self.cached_session(): # Define a first queue that contains integer counts. dequeue_counts = [random.randint(1, 10) for _ in range(100)] count_q = data_flow_ops.PaddingFIFOQueue(100, dtypes_lib.int32, ((),)) @@ -872,7 +872,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertEqual(elems, dequeued_elems) def testDequeueFromClosedQueue(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.PaddingFIFOQueue(10, dtypes_lib.float32, ((),)) elems = [10.0, 20.0, 30.0, 40.0] enqueue_op = q.enqueue_many((elems,)) @@ -890,7 +890,7 @@ class PaddingFIFOQueueTest(test.TestCase): dequeued_t.eval() def testBlockingDequeueFromClosedQueue(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue(10, dtypes_lib.float32, ((),)) elems = [10.0, 20.0, 30.0, 40.0] enqueue_op = q.enqueue_many((elems,)) @@ -916,7 +916,7 @@ class PaddingFIFOQueueTest(test.TestCase): dequeue_thread.join() def testDequeueUpToFromClosedQueueReturnsRemainder(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue(10, dtypes_lib.float32, ((),)) elems = [10.0, 20.0, 30.0, 40.0] enqueue_op = q.enqueue_many((elems,)) @@ -938,7 +938,7 @@ class PaddingFIFOQueueTest(test.TestCase): dequeue_thread.join() def testBlockingDequeueFromClosedEmptyQueue(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue(10, dtypes_lib.float32, ((),)) close_op = q.close() dequeued_t = q.dequeue() @@ -958,7 +958,7 @@ class PaddingFIFOQueueTest(test.TestCase): dequeue_thread.join() def testBlockingDequeueManyFromClosedQueue(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue(10, dtypes_lib.float32, ((),)) elems = [10.0, 20.0, 30.0, 40.0] enqueue_op = q.enqueue_many((elems,)) @@ -983,7 +983,7 @@ class PaddingFIFOQueueTest(test.TestCase): dequeue_thread.join() def testBlockingDequeueManyButNotAllFromClosedQueue(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue(10, dtypes_lib.float32, ((),)) elems = [10.0, 20.0, 30.0, 40.0] enqueue_op = q.enqueue_many((elems,)) @@ -1008,7 +1008,7 @@ class PaddingFIFOQueueTest(test.TestCase): dequeue_thread.join() def testEnqueueManyLargerThanCapacityWithConcurrentDequeueMany(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue(4, dtypes_lib.float32, ((),)) elems = [10.0, 20.0, 30.0, 40.0] enqueue_op = q.enqueue_many((elems,)) @@ -1045,7 +1045,7 @@ class PaddingFIFOQueueTest(test.TestCase): close_thread.join() def testClosedBlockingDequeueManyRestoresPartialBatch(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue(4, (dtypes_lib.float32, dtypes_lib.float32), ((), ())) elems_a = [1.0, 2.0, 3.0] @@ -1078,7 +1078,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertEqual(0, q.size().eval()) def testBlockingDequeueManyFromClosedEmptyQueue(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue(10, dtypes_lib.float32, ((),)) close_op = q.close() dequeued_t = q.dequeue_many(4) @@ -1098,7 +1098,7 @@ class PaddingFIFOQueueTest(test.TestCase): dequeue_thread.join() def testBlockingDequeueUpToFromClosedEmptyQueue(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue(10, dtypes_lib.float32, ((),)) close_op = q.close() dequeued_t = q.dequeue_up_to(4) @@ -1118,7 +1118,7 @@ class PaddingFIFOQueueTest(test.TestCase): dequeue_thread.join() def testEnqueueToClosedQueue(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.PaddingFIFOQueue(10, dtypes_lib.float32, ((),)) enqueue_op = q.enqueue((10.0,)) close_op = q.close() @@ -1131,7 +1131,7 @@ class PaddingFIFOQueueTest(test.TestCase): enqueue_op.run() def testEnqueueManyToClosedQueue(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.PaddingFIFOQueue(10, dtypes_lib.float32, ((),)) elems = [10.0, 20.0, 30.0, 40.0] enqueue_op = q.enqueue_many((elems,)) @@ -1145,7 +1145,7 @@ class PaddingFIFOQueueTest(test.TestCase): enqueue_op.run() def testBlockingEnqueueToFullQueue(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue(4, dtypes_lib.float32, ((),)) elems = [10.0, 20.0, 30.0, 40.0] enqueue_op = q.enqueue_many((elems,)) @@ -1168,7 +1168,7 @@ class PaddingFIFOQueueTest(test.TestCase): thread.join() def testBlockingEnqueueManyToFullQueue(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue(4, dtypes_lib.float32, ((),)) elems = [10.0, 20.0, 30.0, 40.0] enqueue_op = q.enqueue_many((elems,)) @@ -1195,7 +1195,7 @@ class PaddingFIFOQueueTest(test.TestCase): thread.join() def testBlockingEnqueueBeforeClose(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue(4, dtypes_lib.float32, ((),)) elems = [10.0, 20.0, 30.0, 40.0] enqueue_op = q.enqueue_many((elems,)) @@ -1232,7 +1232,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertEqual(0, q.size().eval()) def testBlockingEnqueueManyBeforeClose(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue(4, dtypes_lib.float32, ((),)) elems = [10.0, 20.0, 30.0] enqueue_op = q.enqueue_many((elems,)) @@ -1265,7 +1265,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertEqual(elem, dequeued_t.eval()) def testDoesNotLoseValue(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.PaddingFIFOQueue(1, dtypes_lib.float32, ((),)) enqueue_op = q.enqueue((10.0,)) size_t = q.size() @@ -1275,7 +1275,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertEqual(size_t.eval(), [1]) def testSharedQueueSameSession(self): - with self.test_session(): + with self.cached_session(): q1 = data_flow_ops.PaddingFIFOQueue( 1, dtypes_lib.float32, ((),), shared_name="shared_queue") q1.enqueue((10.0,)).run() @@ -1305,7 +1305,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertEqual(q2_size_t.eval(), [0]) def testIncompatibleSharedQueueErrors(self): - with self.test_session(): + with self.cached_session(): q_a_1 = data_flow_ops.PaddingFIFOQueue( 10, dtypes_lib.float32, ((),), shared_name="q_a") q_a_2 = data_flow_ops.PaddingFIFOQueue( @@ -1356,7 +1356,7 @@ class PaddingFIFOQueueTest(test.TestCase): q_f_2.queue_ref.op.run() def testSelectQueue(self): - with self.test_session(): + with self.cached_session(): num_queues = 10 qlist = list() for _ in xrange(num_queues): @@ -1370,7 +1370,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertEqual(q.dequeue().eval(), 10.0) def testSelectQueueOutOfRange(self): - with self.test_session(): + with self.cached_session(): q1 = data_flow_ops.PaddingFIFOQueue(10, dtypes_lib.float32, ((),)) q2 = data_flow_ops.PaddingFIFOQueue(15, dtypes_lib.float32, ((),)) enq_q = data_flow_ops.PaddingFIFOQueue.from_list(3, [q1, q2]) @@ -1394,7 +1394,7 @@ class PaddingFIFOQueueTest(test.TestCase): sess.run(enqueue_many_op) def testResetOfBlockingOperation(self): - with self.test_session() as sess: + with self.cached_session() as sess: q_empty = data_flow_ops.PaddingFIFOQueue(5, dtypes_lib.float32, ((),)) dequeue_op = q_empty.dequeue() dequeue_many_op = q_empty.dequeue_many(1) @@ -1422,7 +1422,7 @@ class PaddingFIFOQueueTest(test.TestCase): t.join() def testBigEnqueueMany(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue(5, dtypes_lib.int32, ((),)) elem = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] enq = q.enqueue_many((elem,)) @@ -1467,7 +1467,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertAllEqual(elem, results) def testBigDequeueMany(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PaddingFIFOQueue(2, dtypes_lib.int32, ((),)) elem = np.arange(4, dtype=np.int32) enq_list = [q.enqueue((e,)) for e in elem] @@ -1493,7 +1493,7 @@ class PaddingFIFOQueueTest(test.TestCase): self.assertAllEqual(elem, results) def testDtypes(self): - with self.test_session() as sess: + with self.cached_session() as sess: dtypes = [ dtypes_lib.float32, dtypes_lib.float64, dtypes_lib.int32, dtypes_lib.uint8, dtypes_lib.int16, dtypes_lib.int8, dtypes_lib.int64, diff --git a/tensorflow/python/kernel_tests/parse_single_example_op_test.py b/tensorflow/python/kernel_tests/parse_single_example_op_test.py index bf4c89b368..a84895a287 100644 --- a/tensorflow/python/kernel_tests/parse_single_example_op_test.py +++ b/tensorflow/python/kernel_tests/parse_single_example_op_test.py @@ -89,7 +89,7 @@ def _compare_output_to_expected(tester, dict_tensors, expected_tensors, class ParseExampleTest(test.TestCase): def _test(self, kwargs, expected_values=None, expected_err=None): - with self.test_session() as sess: + with self.cached_session() as sess: if expected_err: with self.assertRaisesWithPredicateMatch(expected_err[0], expected_err[1]): @@ -844,7 +844,7 @@ class ParseExampleTest(test.TestCase): class ParseSingleExampleTest(test.TestCase): def _test(self, kwargs, expected_values=None, expected_err=None): - with self.test_session() as sess: + with self.cached_session() as sess: if expected_err: with self.assertRaisesWithPredicateMatch(expected_err[0], expected_err[1]): diff --git a/tensorflow/python/kernel_tests/parsing_ops_test.py b/tensorflow/python/kernel_tests/parsing_ops_test.py index 7dff4501cc..71d8b60d3c 100644 --- a/tensorflow/python/kernel_tests/parsing_ops_test.py +++ b/tensorflow/python/kernel_tests/parsing_ops_test.py @@ -89,7 +89,7 @@ def _compare_output_to_expected(tester, dict_tensors, expected_tensors, class ParseExampleTest(test.TestCase): def _test(self, kwargs, expected_values=None, expected_err=None): - with self.test_session() as sess: + with self.cached_session() as sess: if expected_err: with self.assertRaisesWithPredicateMatch(expected_err[0], expected_err[1]): @@ -937,7 +937,7 @@ class ParseExampleTest(test.TestCase): class ParseSingleExampleTest(test.TestCase): def _test(self, kwargs, expected_values=None, expected_err=None): - with self.test_session() as sess: + with self.cached_session() as sess: if expected_err: with self.assertRaisesWithPredicateMatch(expected_err[0], expected_err[1]): @@ -1054,7 +1054,7 @@ class ParseSequenceExampleTest(test.TestCase): expected_feat_list_values = expected_feat_list_values or {} expected_length_values = expected_length_values or {} - with self.test_session() as sess: + with self.cached_session() as sess: if expected_err: with self.assertRaisesWithPredicateMatch(expected_err[0], expected_err[1]): @@ -1606,7 +1606,7 @@ class ParseSequenceExampleTest(test.TestCase): class DecodeJSONExampleTest(test.TestCase): def _testRoundTrip(self, examples): - with self.test_session() as sess: + with self.cached_session() as sess: examples = np.array(examples, dtype=np.object) json_tensor = constant_op.constant( @@ -1696,7 +1696,7 @@ class DecodeJSONExampleTest(test.TestCase): ]) def testInvalidSyntax(self): - with self.test_session() as sess: + with self.cached_session() as sess: json_tensor = constant_op.constant(["{]"]) binary_tensor = parsing_ops.decode_json_example(json_tensor) with self.assertRaisesOpError("Error while parsing JSON"): @@ -1706,7 +1706,7 @@ class DecodeJSONExampleTest(test.TestCase): class ParseTensorOpTest(test.TestCase): def testToFloat32(self): - with self.test_session(): + with self.cached_session(): expected = np.random.rand(3, 4, 5).astype(np.float32) tensor_proto = tensor_util.make_tensor_proto(expected) @@ -1719,7 +1719,7 @@ class ParseTensorOpTest(test.TestCase): self.assertAllEqual(expected, result) def testToUint8(self): - with self.test_session(): + with self.cached_session(): expected = np.random.rand(3, 4, 5).astype(np.uint8) tensor_proto = tensor_util.make_tensor_proto(expected) @@ -1732,7 +1732,7 @@ class ParseTensorOpTest(test.TestCase): self.assertAllEqual(expected, result) def testTypeMismatch(self): - with self.test_session(): + with self.cached_session(): expected = np.random.rand(3, 4, 5).astype(np.uint8) tensor_proto = tensor_util.make_tensor_proto(expected) @@ -1745,7 +1745,7 @@ class ParseTensorOpTest(test.TestCase): tensor.eval(feed_dict={serialized: tensor_proto.SerializeToString()}) def testInvalidInput(self): - with self.test_session(): + with self.cached_session(): serialized = array_ops.placeholder(dtypes.string) tensor = parsing_ops.parse_tensor(serialized, dtypes.uint16) diff --git a/tensorflow/python/kernel_tests/partitioned_variables_test.py b/tensorflow/python/kernel_tests/partitioned_variables_test.py index 15d5702252..b34d30f5c0 100644 --- a/tensorflow/python/kernel_tests/partitioned_variables_test.py +++ b/tensorflow/python/kernel_tests/partitioned_variables_test.py @@ -39,7 +39,7 @@ from tensorflow.python.training import saver as saver_lib class PartitionerCreatorsTest(test.TestCase): def testFixedSizePartitioner(self): - with self.test_session(): + with self.cached_session(): partitioner = partitioned_variables.fixed_size_partitioner(5, axis=0) with variable_scope.variable_scope("root", partitioner=partitioner): v0 = variable_scope.get_variable( @@ -50,7 +50,7 @@ class PartitionerCreatorsTest(test.TestCase): self.assertAllEqual(v0_part, (5, 1)) def testFixedSizePartitionerInt64(self): - with self.test_session(): + with self.cached_session(): partitioner = partitioned_variables.fixed_size_partitioner(4, axis=0) with variable_scope.variable_scope("root", partitioner=partitioner): v0 = variable_scope.get_variable("v0", dtype=dtypes.int64, shape=[20]) @@ -58,7 +58,7 @@ class PartitionerCreatorsTest(test.TestCase): self.assertEqual(len(v0_list), 4) def testResourceFixedSizePartitioner(self): - with self.test_session(): + with self.cached_session(): partitioner = partitioned_variables.fixed_size_partitioner(5, axis=0) with variable_scope.variable_scope( "root", partitioner=partitioner, use_resource=True): @@ -88,7 +88,7 @@ class PartitionerCreatorsTest(test.TestCase): self.assertAllEqual(v0_part, expected_partitions) def testVariableAxisSizePartitioner(self): - with self.test_session(): + with self.cached_session(): # Create a partitioned variable of shape (4, 8, 16, 32) type float32 # Bytes per slice along the given axes: @@ -210,7 +210,7 @@ class PartitionerCreatorsTest(test.TestCase): self.assertAllEqual(v0_part, expected_partitions) def testMinMaxVariablePartitioner(self): - with self.test_session(): + with self.cached_session(): # Partitioning a variable of shape=[2048] with a minimum of 2K per slice. self._testMinMaxVariablePartitioner( max_partitions=100, @@ -323,7 +323,7 @@ class PartitionedVariablesTestCase(test.TestCase): self.assertEquals(expected_specs[i], slices[i]._save_slice_info.spec) def testVecConstantInit(self): - with self.test_session(): + with self.cached_session(): rnd_par = constant_op.constant([1, 2, 3, 4]) vs = partitioned_variables.create_partitioned_variables([4], [4], rnd_par) variables.global_variables_initializer().run() @@ -334,7 +334,7 @@ class PartitionedVariablesTestCase(test.TestCase): self._TestSaveSpec(vs, ["4 0,1", "4 1,1", "4 2,1", "4 3,1"]) def testConstantInit(self): - with self.test_session(): + with self.cached_session(): rnd_par = constant_op.constant([[1, 2, 3, 4], [5, 6, 7, 8]]) vs = partitioned_variables.create_partitioned_variables([2, 4], [1, 2], rnd_par) @@ -346,7 +346,7 @@ class PartitionedVariablesTestCase(test.TestCase): self._TestSaveSpec(vs, ["2 4 0,2:0,2", "2 4 0,2:2,2"]) def _testNameHelper(self, use_resource=False): - with self.test_session(): + with self.cached_session(): rnd_par = constant_op.constant([[1, 2, 3, 4], [5, 6, 7, 8]]) with variable_scope.variable_scope("hi", use_resource=use_resource): vs1 = partitioned_variables.create_partitioned_variables([2, 4], [1, 2], @@ -363,7 +363,7 @@ class PartitionedVariablesTestCase(test.TestCase): self.assertEqual(var2_name + "/part_0:0", vs2[0].name) self.assertEqual(var2_name + "/part_1:0", vs2[1].name) # Test same variable. - with self.test_session(): + with self.cached_session(): rnd_par = constant_op.constant([[1, 2, 3, 4], [5, 6, 7, 8]]) with variable_scope.variable_scope( "hola", use_resource=use_resource) as vs: @@ -383,7 +383,7 @@ class PartitionedVariablesTestCase(test.TestCase): self.assertEqual(var2_name + "/part_0:0", vs2[0].name) self.assertEqual(var2_name + "/part_1:0", vs2[1].name) # Test name_scope - with self.test_session(): + with self.cached_session(): rnd_par = constant_op.constant([[1, 2, 3, 4], [5, 6, 7, 8]]) with ops.name_scope("ola"): vs1 = partitioned_variables.create_partitioned_variables([2, 4], [1, 2], @@ -408,7 +408,7 @@ class PartitionedVariablesTestCase(test.TestCase): self._testNameHelper(use_resource=True) def testRandomInitValue(self): - with self.test_session(): + with self.cached_session(): rnd = variables.Variable(random_ops.random_uniform([200, 40])) vs = partitioned_variables.create_partitioned_variables( rnd.get_shape(), [1, 10], rnd.initialized_value()) @@ -425,7 +425,7 @@ class PartitionedVariablesTestCase(test.TestCase): ]) def testRandomInitUnevenPartitions(self): - with self.test_session(): + with self.cached_session(): rnd = variables.Variable( random_ops.random_uniform([20, 43], dtype=dtypes.float64)) var_lists = [ @@ -463,7 +463,7 @@ class PartitionedVariablesTestCase(test.TestCase): self._TestSaveSpec(vs, save_specs[i]) def testDegenerate(self): - with self.test_session(): + with self.cached_session(): rnd = variables.Variable(random_ops.random_uniform([10, 43])) vs = partitioned_variables.create_partitioned_variables( rnd.get_shape(), [1, 1], rnd.initialized_value()) @@ -474,7 +474,7 @@ class PartitionedVariablesTestCase(test.TestCase): self._TestSaveSpec(vs, ["10 43 0,10:0,43"]) def testSliceSizeOne(self): - with self.test_session(): + with self.cached_session(): rnd = variables.Variable(random_ops.random_uniform([10, 43])) vs = partitioned_variables.create_partitioned_variables( rnd.get_shape(), [10, 1], rnd.initialized_value()) @@ -492,7 +492,7 @@ class PartitionedVariablesTestCase(test.TestCase): self.assertAllClose([0., 1., 2., 3.], _IotaInitializer([4])) self.assertAllClose([[0., 1.], [0., 10.], [0., 100.], [0., 1000.]], _IotaInitializer([4, 2])) - with self.test_session(): + with self.cached_session(): vs = partitioned_variables.create_partitioned_variables([13, 5], [3, 1], _IotaInitializer) variables.global_variables_initializer().run() @@ -506,7 +506,7 @@ class PartitionedVariablesTestCase(test.TestCase): def testRandomInitializer(self): # Sanity check that the slices uses a different seed when using a random # initializer function. - with self.test_session(): + with self.cached_session(): var0, var1 = partitioned_variables.create_partitioned_variables( [20, 12], [1, 2], init_ops.random_uniform_initializer()) variables.global_variables_initializer().run() @@ -514,7 +514,7 @@ class PartitionedVariablesTestCase(test.TestCase): self.assertTrue(np.linalg.norm(val0 - val1) > 1e-6) # Negative test that proves that slices have the same values if # the random initializer uses a seed. - with self.test_session(): + with self.cached_session(): var0, var1 = partitioned_variables.create_partitioned_variables( [20, 12], [1, 2], init_ops.random_uniform_initializer(seed=201)) variables.global_variables_initializer().run() @@ -522,7 +522,7 @@ class PartitionedVariablesTestCase(test.TestCase): self.assertAllClose(val0, val1) def testSomeErrors(self): - with self.test_session(): + with self.cached_session(): rnd = variables.Variable(random_ops.random_uniform([10, 43])) with self.assertRaises(ValueError): partitioned_variables.create_partitioned_variables( @@ -547,7 +547,7 @@ class PartitionedVariablesTestCase(test.TestCase): [10, 43], [1, 50], rnd.initialized_value()) def testControlDepsNone(self): - with self.test_session() as session: + with self.cached_session() as session: c = constant_op.constant(1.0) with ops.control_dependencies([c]): # d get the control dependency. @@ -573,7 +573,7 @@ class PartitionedVariablesTestCase(test.TestCase): self.assertEqual([], op.control_inputs) def testConcat(self): - with self.test_session() as session: + with self.cached_session() as session: var_x = variable_scope.get_variable( "x", initializer=constant_op.constant([1., 2.]), diff --git a/tensorflow/python/kernel_tests/priority_queue_test.py b/tensorflow/python/kernel_tests/priority_queue_test.py index 3fb9c9c468..73a9c81638 100644 --- a/tensorflow/python/kernel_tests/priority_queue_test.py +++ b/tensorflow/python/kernel_tests/priority_queue_test.py @@ -36,7 +36,7 @@ from tensorflow.python.platform import test class PriorityQueueTest(test.TestCase): def testRoundTripInsertReadOnceSorts(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PriorityQueue(2000, (dtypes.string, dtypes.string), ( (), ())) elem = np.random.randint(-5, 5, size=100).astype(np.int64) @@ -67,7 +67,7 @@ class PriorityQueueTest(test.TestCase): self.assertEqual(missed, set()) def testRoundTripInsertMultiThreadedReadOnceSorts(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PriorityQueue(2000, (dtypes.string, dtypes.string), ( (), ())) elem = np.random.randint(-5, 5, size=100).astype(np.int64) @@ -113,7 +113,7 @@ class PriorityQueueTest(test.TestCase): self.assertEqual(missed, set()) def testRoundTripFillsCapacityMultiThreadedEnqueueAndDequeue(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PriorityQueue(10, (dtypes.int64), (())) num_threads = 40 @@ -163,7 +163,7 @@ class PriorityQueueTest(test.TestCase): self.assertAllEqual(sorted(dequeued), sorted(all_enqueued_values)) def testRoundTripInsertManyMultiThreadedReadManyMultithreadedSorts(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PriorityQueue(2000, (dtypes.int64), (())) num_threads = 40 @@ -219,7 +219,7 @@ class PriorityQueueTest(test.TestCase): self.assertAllEqual(set(dequeued), set(all_enqueued_values)) def testRoundTripInsertManyMultiThreadedReadOnceSorts(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PriorityQueue(2000, (dtypes.string, dtypes.string), ( (), ())) elem = np.random.randint(-5, 5, size=100).astype(np.int64) @@ -268,7 +268,7 @@ class PriorityQueueTest(test.TestCase): self.assertEqual(missed, set()) def testRoundTripInsertOnceReadOnceSorts(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.PriorityQueue(2000, (dtypes.string, dtypes.string), ( (), ())) elem = np.random.randint(-100, 100, size=1000).astype(np.int64) @@ -289,7 +289,7 @@ class PriorityQueueTest(test.TestCase): self.assertTrue((dv0, dv1) in allowed[e]) def testRoundTripInsertOnceReadManySorts(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.PriorityQueue(2000, (dtypes.int64), (())) elem = np.random.randint(-100, 100, size=1000).astype(np.int64) q.enqueue_many((elem, elem)).run() @@ -297,7 +297,7 @@ class PriorityQueueTest(test.TestCase): self.assertAllEqual(deq_values, sorted(elem)) def testRoundTripInsertOnceReadOnceLotsSorts(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.PriorityQueue(2000, (dtypes.int64), (())) elem = np.random.randint(-100, 100, size=1000).astype(np.int64) q.enqueue_many((elem, elem)).run() @@ -306,13 +306,13 @@ class PriorityQueueTest(test.TestCase): self.assertAllEqual(deq_values, sorted(elem)) def testInsertingNonInt64Fails(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.PriorityQueue(2000, (dtypes.string), (())) with self.assertRaises(TypeError): q.enqueue_many((["a", "b", "c"], ["a", "b", "c"])).run() def testInsertingNonScalarFails(self): - with self.test_session() as sess: + with self.cached_session() as sess: input_priority = array_ops.placeholder(dtypes.int64) input_other = array_ops.placeholder(dtypes.string) q = data_flow_ops.PriorityQueue(2000, (dtypes.string,), (())) diff --git a/tensorflow/python/kernel_tests/random/random_ops_test.py b/tensorflow/python/kernel_tests/random/random_ops_test.py index 0ef6a95cfc..d199a9d9dd 100644 --- a/tensorflow/python/kernel_tests/random/random_ops_test.py +++ b/tensorflow/python/kernel_tests/random/random_ops_test.py @@ -320,6 +320,15 @@ class RandomUniformTest(RandomOpTestCommon): error = np.abs(counts - mean) self.assertLess(error.max(), 5 * std) + # Check that minval = maxval is fine iff we're producing no numbers + def testUniformIntsDegenerate(self): + for dt in dtypes.int32, dtypes.int64: + def sample(n): + return self._Sampler(n, minv=0, maxv=0, dtype=dt, use_gpu=True)() + self.assertEqual(sample(0).shape, (10, 0)) + with self.assertRaisesOpError('Need minval < maxval, got 0 >= 0'): + sample(1) + # Checks that the CPU and GPU implementation returns the same results, # given the same random seed def testCPUGPUMatch(self): diff --git a/tensorflow/python/kernel_tests/reader_ops_test.py b/tensorflow/python/kernel_tests/reader_ops_test.py index 8e06e1abfb..8c84b2a49f 100644 --- a/tensorflow/python/kernel_tests/reader_ops_test.py +++ b/tensorflow/python/kernel_tests/reader_ops_test.py @@ -146,7 +146,7 @@ class IdentityReaderTest(test.TestCase): self.assertAllEqual(expected, v) def testOneEpoch(self): - with self.test_session() as sess: + with self.cached_session() as sess: reader = io_ops.IdentityReader("test_reader") work_completed = reader.num_work_units_completed() produced = reader.num_records_produced() @@ -180,7 +180,7 @@ class IdentityReaderTest(test.TestCase): self.assertAllEqual(0, queued_length.eval()) def testMultipleEpochs(self): - with self.test_session() as sess: + with self.cached_session() as sess: reader = io_ops.IdentityReader("test_reader") queue = data_flow_ops.FIFOQueue(99, [dtypes.string], shapes=()) enqueue = queue.enqueue_many([["DD", "EE"]]) @@ -201,7 +201,7 @@ class IdentityReaderTest(test.TestCase): sess.run([key, value]) def testSerializeRestore(self): - with self.test_session() as sess: + with self.cached_session() as sess: reader = io_ops.IdentityReader("test_reader") produced = reader.num_records_produced() queue = data_flow_ops.FIFOQueue(99, [dtypes.string], shapes=()) @@ -256,7 +256,7 @@ class IdentityReaderTest(test.TestCase): reader.restore_state(b"BOGUS" + state[5:]).run() def testReset(self): - with self.test_session() as sess: + with self.cached_session() as sess: reader = io_ops.IdentityReader("test_reader") work_completed = reader.num_work_units_completed() produced = reader.num_records_produced() @@ -307,7 +307,7 @@ class WholeFileReaderTest(test.TestCase): self.assertAllEqual(self._content[index], v) def testOneEpoch(self): - with self.test_session() as sess: + with self.cached_session() as sess: reader = io_ops.WholeFileReader("test_reader") queue = data_flow_ops.FIFOQueue(99, [dtypes.string], shapes=()) queue.enqueue_many([self._filenames]).run() @@ -323,7 +323,7 @@ class WholeFileReaderTest(test.TestCase): sess.run([key, value]) def testInfiniteEpochs(self): - with self.test_session() as sess: + with self.cached_session() as sess: reader = io_ops.WholeFileReader("test_reader") queue = data_flow_ops.FIFOQueue(99, [dtypes.string], shapes=()) enqueue = queue.enqueue_many([self._filenames]) @@ -366,7 +366,7 @@ class TextLineReaderTest(test.TestCase): return filenames def _testOneEpoch(self, files): - with self.test_session() as sess: + with self.cached_session() as sess: reader = io_ops.TextLineReader(name="test_reader") queue = data_flow_ops.FIFOQueue(99, [dtypes.string], shapes=()) key, value = reader.read(queue) @@ -391,7 +391,7 @@ class TextLineReaderTest(test.TestCase): def testSkipHeaderLines(self): files = self._CreateFiles() - with self.test_session() as sess: + with self.cached_session() as sess: reader = io_ops.TextLineReader(skip_header_lines=1, name="test_reader") queue = data_flow_ops.FIFOQueue(99, [dtypes.string], shapes=()) key, value = reader.read(queue) @@ -522,7 +522,7 @@ class FixedLengthRecordReaderTest(TFCompressionTestCase): # gap_bytes=hop_bytes-record_bytes def _TestOneEpoch(self, files, num_records, gap_bytes, encoding=None): hop_bytes = 0 if gap_bytes == 0 else self._record_bytes + gap_bytes - with self.test_session() as sess: + with self.cached_session() as sess: reader = io_ops.FixedLengthRecordReader( header_bytes=self._header_bytes, record_bytes=self._record_bytes, @@ -549,7 +549,7 @@ class FixedLengthRecordReaderTest(TFCompressionTestCase): files, num_overlapped_records, encoding=None): - with self.test_session() as sess: + with self.cached_session() as sess: reader = io_ops.FixedLengthRecordReader( header_bytes=self._header_bytes, record_bytes=self._record_bytes, @@ -621,7 +621,7 @@ class TFRecordReaderTest(TFCompressionTestCase): def testOneEpoch(self): files = self._CreateFiles() - with self.test_session() as sess: + with self.cached_session() as sess: reader = io_ops.TFRecordReader(name="test_reader") queue = data_flow_ops.FIFOQueue(99, [dtypes.string], shapes=()) key, value = reader.read(queue) @@ -640,7 +640,7 @@ class TFRecordReaderTest(TFCompressionTestCase): def testReadUpTo(self): files = self._CreateFiles() - with self.test_session() as sess: + with self.cached_session() as sess: reader = io_ops.TFRecordReader(name="test_reader") queue = data_flow_ops.FIFOQueue(99, [dtypes.string], shapes=()) batch_size = 3 @@ -670,7 +670,7 @@ class TFRecordReaderTest(TFCompressionTestCase): options = tf_record.TFRecordOptions(TFRecordCompressionType.ZLIB) files = self._CreateFiles(options) - with self.test_session() as sess: + with self.cached_session() as sess: reader = io_ops.TFRecordReader(name="test_reader", options=options) queue = data_flow_ops.FIFOQueue(99, [dtypes.string], shapes=()) key, value = reader.read(queue) @@ -687,7 +687,7 @@ class TFRecordReaderTest(TFCompressionTestCase): options = tf_record.TFRecordOptions(TFRecordCompressionType.GZIP) files = self._CreateFiles(options) - with self.test_session() as sess: + with self.cached_session() as sess: reader = io_ops.TFRecordReader(name="test_reader", options=options) queue = data_flow_ops.FIFOQueue(99, [dtypes.string], shapes=()) key, value = reader.read(queue) @@ -752,7 +752,7 @@ class LMDBReaderTest(test.TestCase): shutil.copy(path, self.db_path) def testReadFromFile(self): - with self.test_session() as sess: + with self.cached_session() as sess: reader = io_ops.LMDBReader(name="test_read_from_file") queue = data_flow_ops.FIFOQueue(99, [dtypes.string], shapes=()) key, value = reader.read(queue) @@ -770,7 +770,7 @@ class LMDBReaderTest(test.TestCase): k, v = sess.run([key, value]) def testReadFromSameFile(self): - with self.test_session() as sess: + with self.cached_session() as sess: reader1 = io_ops.LMDBReader(name="test_read_from_same_file1") reader2 = io_ops.LMDBReader(name="test_read_from_same_file2") filename_queue = input_lib.string_input_producer( @@ -789,7 +789,7 @@ class LMDBReaderTest(test.TestCase): coord.join(threads) def testReadFromFolder(self): - with self.test_session() as sess: + with self.cached_session() as sess: reader = io_ops.LMDBReader(name="test_read_from_folder") queue = data_flow_ops.FIFOQueue(99, [dtypes.string], shapes=()) key, value = reader.read(queue) @@ -807,7 +807,7 @@ class LMDBReaderTest(test.TestCase): k, v = sess.run([key, value]) def testReadFromFileRepeatedly(self): - with self.test_session() as sess: + with self.cached_session() as sess: reader = io_ops.LMDBReader(name="test_read_from_file_repeated") filename_queue = input_lib.string_input_producer( [self.db_path], num_epochs=None) diff --git a/tensorflow/python/kernel_tests/record_input_test.py b/tensorflow/python/kernel_tests/record_input_test.py index 068860d5d4..ebb9872f22 100644 --- a/tensorflow/python/kernel_tests/record_input_test.py +++ b/tensorflow/python/kernel_tests/record_input_test.py @@ -44,7 +44,7 @@ class RecordInputOpTest(test.TestCase): w.close() def testRecordInputSimple(self): - with self.test_session() as sess: + with self.cached_session() as sess: self.generateTestData("basic", 1, 1) yield_op = data_flow_ops.RecordInput( @@ -57,7 +57,7 @@ class RecordInputOpTest(test.TestCase): self.assertEqual(sess.run(yield_op), b"0000000000") def testRecordInputSimpleGzip(self): - with self.test_session() as sess: + with self.cached_session() as sess: self.generateTestData( "basic", 1, @@ -76,7 +76,7 @@ class RecordInputOpTest(test.TestCase): self.assertEqual(sess.run(yield_op), b"0000000000") def testRecordInputSimpleZlib(self): - with self.test_session() as sess: + with self.cached_session() as sess: self.generateTestData( "basic", 1, @@ -98,7 +98,7 @@ class RecordInputOpTest(test.TestCase): files = 100 records_per_file = 100 batches = 2 - with self.test_session() as sess: + with self.cached_session() as sess: self.generateTestData("basic", files, records_per_file) records = data_flow_ops.RecordInput( @@ -126,7 +126,7 @@ class RecordInputOpTest(test.TestCase): def testDoesNotDeadlock(self): # Iterate multiple times to cause deadlock if there is a chance it can occur for _ in range(30): - with self.test_session() as sess: + with self.cached_session() as sess: self.generateTestData("basic", 1, 1) records = data_flow_ops.RecordInput( @@ -141,7 +141,7 @@ class RecordInputOpTest(test.TestCase): sess.run(yield_op) def testEmptyGlob(self): - with self.test_session() as sess: + with self.cached_session() as sess: record_input = data_flow_ops.RecordInput(file_pattern="foo") yield_op = record_input.get_yield_op() sess.run(variables.global_variables_initializer()) @@ -152,7 +152,7 @@ class RecordInputOpTest(test.TestCase): files = 10 records_per_file = 10 batches = 2 - with self.test_session() as sess: + with self.cached_session() as sess: self.generateTestData("basic", files, records_per_file) records = data_flow_ops.RecordInput( diff --git a/tensorflow/python/kernel_tests/reduce_join_op_test.py b/tensorflow/python/kernel_tests/reduce_join_op_test.py index 663561ced7..3bb4986313 100644 --- a/tensorflow/python/kernel_tests/reduce_join_op_test.py +++ b/tensorflow/python/kernel_tests/reduce_join_op_test.py @@ -113,7 +113,7 @@ class ReduceJoinTest(UnicodeTestCase): keep_dims: Whether or not to retain reduced dimensions. separator: The separator to use for joining. """ - with self.test_session(): + with self.cached_session(): output = string_ops.reduce_join( inputs=input_array, axis=axis, @@ -136,7 +136,7 @@ class ReduceJoinTest(UnicodeTestCase): axis: The indices to reduce. separator: The separator to use when joining. """ - with self.test_session(): + with self.cached_session(): output = string_ops.reduce_join( inputs=input_array, axis=axis, keep_dims=False, separator=separator) output_keep_dims = string_ops.reduce_join( @@ -234,7 +234,7 @@ class ReduceJoinTest(UnicodeTestCase): input_array = [["a"], ["b"]] truth = ["ab"] truth_shape = None - with self.test_session(): + with self.cached_session(): placeholder = array_ops.placeholder(dtypes.string, name="placeholder") reduced = string_ops.reduce_join(placeholder, axis=0) output_array = reduced.eval(feed_dict={placeholder.name: input_array}) @@ -247,7 +247,7 @@ class ReduceJoinTest(UnicodeTestCase): truth_dim_zero = ["thisplease", "isdo", "anot", "testpanic"] truth_dim_one = ["thisisatest", "pleasedonotpanic"] truth_shape = None - with self.test_session(): + with self.cached_session(): placeholder = array_ops.placeholder(dtypes.int32, name="placeholder") reduced = string_ops.reduce_join(input_array, axis=placeholder) output_array_dim_zero = reduced.eval(feed_dict={placeholder.name: [0]}) @@ -298,7 +298,7 @@ class ReduceJoinTest(UnicodeTestCase): self._testMultipleReduceJoin(input_array, axis=permutation) def testInvalidReductionIndices(self): - with self.test_session(): + with self.cached_session(): with self.assertRaisesRegexp(ValueError, "Invalid reduction dim"): string_ops.reduce_join(inputs="", axis=0) with self.assertRaisesRegexp(ValueError, @@ -313,7 +313,7 @@ class ReduceJoinTest(UnicodeTestCase): string_ops.reduce_join(inputs=[[""]], axis=[0, 2]) def testZeroDims(self): - with self.test_session(): + with self.cached_session(): inputs = np.zeros([0, 1], dtype=str) # Reduction that drops the dim of size 0. @@ -326,7 +326,7 @@ class ReduceJoinTest(UnicodeTestCase): self.assertAllEqual([0], output_shape) def testInvalidArgsUnknownShape(self): - with self.test_session(): + with self.cached_session(): placeholder = array_ops.placeholder(dtypes.string, name="placeholder") index_too_high = string_ops.reduce_join(placeholder, axis=1) duplicate_index = string_ops.reduce_join(placeholder, axis=[-1, 1]) @@ -336,7 +336,7 @@ class ReduceJoinTest(UnicodeTestCase): duplicate_index.eval(feed_dict={placeholder.name: [[""]]}) def testInvalidArgsUnknownIndices(self): - with self.test_session(): + with self.cached_session(): placeholder = array_ops.placeholder(dtypes.int32, name="placeholder") reduced = string_ops.reduce_join(["test", "test2"], axis=placeholder) diff --git a/tensorflow/python/kernel_tests/reduction_ops_test.py b/tensorflow/python/kernel_tests/reduction_ops_test.py index ea78b58d88..248036a82a 100644 --- a/tensorflow/python/kernel_tests/reduction_ops_test.py +++ b/tensorflow/python/kernel_tests/reduction_ops_test.py @@ -61,7 +61,7 @@ class ReducedShapeTest(test.TestCase): self.assertAllEqual(output.eval(), result) def testSimple(self): - with self.test_session(): + with self.cached_session(): self._check([3], [], [3]) self._check([3], [0], [1]) self._check([5, 3], [], [5, 3]) @@ -71,7 +71,7 @@ class ReducedShapeTest(test.TestCase): def testZeros(self): """Check that reduced_shape does the right thing with zero dimensions.""" - with self.test_session(): + with self.cached_session(): self._check([0], [], [0]) self._check([0], [0], [1]) self._check([0, 3], [], [0, 3]) @@ -84,7 +84,7 @@ class ReducedShapeTest(test.TestCase): self._check([3, 0], [0, 1], [1, 1]) def testNegAxes(self): - with self.test_session(): + with self.cached_session(): self._check([10, 10, 10], [-1], [10, 10, 1]) self._check([10, 10, 10], [-1, 2], [10, 10, 1]) self._check([10, 10, 10], [-1, -1], [10, 10, 1]) @@ -95,7 +95,7 @@ class ReducedShapeTest(test.TestCase): class ReductionUnknownShape(test.TestCase): def testBasic(self): - with self.test_session(): + with self.cached_session(): for dtype, reductions in [(dtypes.float32, (math_ops.reduce_sum, math_ops.reduce_mean, math_ops.reduce_prod, math_ops.reduce_max, @@ -212,7 +212,7 @@ class SumReductionTest(BaseReductionTest): arr = np.ones([68000], dtype=np.float16) - with self.test_session(graph=ops.Graph(), use_gpu=True) as sess: + with self.session(graph=ops.Graph(), use_gpu=True) as sess: tf_arr = variables.Variable(arr) variables.global_variables_initializer().run() tf_mean = math_ops.reduce_mean(tf_arr, 0, False) @@ -235,7 +235,7 @@ class SumReductionTest(BaseReductionTest): col_sum = np.sum(arr, axis=0) row_sum = np.sum(arr, axis=1) - with self.test_session(graph=ops.Graph(), use_gpu=True) as sess: + with self.session(graph=ops.Graph(), use_gpu=True) as sess: tf_row_sum = self._tf_reduce(arr, 1, False) tf_col_sum = self._tf_reduce(arr, 0, False) tf_out_row, tf_out_col = sess.run([tf_row_sum, tf_col_sum]) @@ -249,7 +249,7 @@ class SumReductionTest(BaseReductionTest): sum_y = np.sum(arr, axis=1) sum_xz = np.sum(arr, axis=(0, 2)) - with self.test_session(graph=ops.Graph(), use_gpu=True) as sess: + with self.session(graph=ops.Graph(), use_gpu=True) as sess: tf_sum_xz = self._tf_reduce(arr, [0, 2], False) tf_sum_y = self._tf_reduce(arr, 1, False) tf_out_sum_xz, tf_out_sum_y = sess.run([tf_sum_xz, tf_sum_y]) @@ -617,7 +617,7 @@ class MinReductionTest(test.TestCase): def testGradient(self): s = [2, 3, 4, 2] x = np.arange(1.0, 49.0).reshape(s).astype(np.float64) - with self.test_session(): + with self.cached_session(): t = ops.convert_to_tensor(x) su = math_ops.reduce_min(t, [1, 2]) jacob_t, jacob_n = gradient_checker.compute_gradient( @@ -627,7 +627,7 @@ class MinReductionTest(test.TestCase): def testGradient2(self): s = [2, 3, 4, 2] x = np.arange(1.0, 49.0).reshape(s).astype(np.float64) - with self.test_session(): + with self.cached_session(): t = ops.convert_to_tensor(x) su = math_ops.reduce_min(t, [1]) jacob_t, jacob_n = gradient_checker.compute_gradient( @@ -637,7 +637,7 @@ class MinReductionTest(test.TestCase): def testGradient3(self): s = [2, 3, 4, 2] x = np.arange(1.0, 49.0).reshape(s).astype(np.float64) - with self.test_session(): + with self.cached_session(): t = ops.convert_to_tensor(x) su = math_ops.reduce_min(t, [2]) jacob_t, jacob_n = gradient_checker.compute_gradient( @@ -647,7 +647,7 @@ class MinReductionTest(test.TestCase): def testGradient4(self): s = [2, 3, 4, 2] x = np.arange(1.0, 49.0).reshape(s).astype(np.float64) - with self.test_session(): + with self.cached_session(): t = ops.convert_to_tensor(x) su = math_ops.reduce_min(t) jacob_t, jacob_n = gradient_checker.compute_gradient( @@ -655,7 +655,7 @@ class MinReductionTest(test.TestCase): self.assertAllClose(jacob_t, jacob_n, rtol=1e-8, atol=1e-8) def testEmptyGradients(self): - with self.test_session(): + with self.cached_session(): x = array_ops.zeros([0, 3]) y = math_ops.reduce_min(x, [1]) error = gradient_checker.compute_gradient_error(x, [0, 3], y, [0]) @@ -744,7 +744,7 @@ class MaxReductionTest(test.TestCase): def testGradient(self): s = [2, 3, 4, 2] x = np.arange(-49.0, -1.0).reshape(s).astype(np.float64) - with self.test_session(): + with self.cached_session(): t = ops.convert_to_tensor(x) su = math_ops.reduce_max(t, [1, 2]) jacob_t, jacob_n = gradient_checker.compute_gradient( @@ -754,7 +754,7 @@ class MaxReductionTest(test.TestCase): def testGradient2(self): s = [2, 3, 4, 2] x = np.arange(-49.0, -1.0).reshape(s).astype(np.float64) - with self.test_session(): + with self.cached_session(): t = ops.convert_to_tensor(x) su = math_ops.reduce_max(t, [1]) jacob_t, jacob_n = gradient_checker.compute_gradient( @@ -764,7 +764,7 @@ class MaxReductionTest(test.TestCase): def testGradient3(self): s = [2, 3, 4, 2] x = np.arange(-49.0, -1.0).reshape(s).astype(np.float64) - with self.test_session(): + with self.cached_session(): t = ops.convert_to_tensor(x) su = math_ops.reduce_max(t, [2]) jacob_t, jacob_n = gradient_checker.compute_gradient( @@ -774,7 +774,7 @@ class MaxReductionTest(test.TestCase): def testGradient4(self): s = [2, 3, 4, 2] x = np.arange(-49.0, -1.0).reshape(s).astype(np.float64) - with self.test_session(): + with self.cached_session(): t = ops.convert_to_tensor(x) su = math_ops.reduce_max(t) jacob_t, jacob_n = gradient_checker.compute_gradient( @@ -782,7 +782,7 @@ class MaxReductionTest(test.TestCase): self.assertAllClose(jacob_t, jacob_n, rtol=1e-8, atol=1e-8) def testEmptyGradients(self): - with self.test_session(): + with self.cached_session(): x = array_ops.zeros([0, 3]) y = math_ops.reduce_max(x, [1]) error = gradient_checker.compute_gradient_error(x, [0, 3], y, [0]) @@ -960,7 +960,7 @@ class CountNonzeroReductionTest(test.TestCase): def testStringReduce(self): # Test case for GitHub issue 18712 - with self.test_session() as sess: + with self.cached_session() as sess: v = math_ops.count_nonzero(constant_op.constant(["test"])) self.assertAllClose(sess.run(v), 1) diff --git a/tensorflow/python/kernel_tests/reduction_ops_test_big.py b/tensorflow/python/kernel_tests/reduction_ops_test_big.py index d70360775a..1e8524f72a 100644 --- a/tensorflow/python/kernel_tests/reduction_ops_test_big.py +++ b/tensorflow/python/kernel_tests/reduction_ops_test_big.py @@ -63,7 +63,7 @@ class BigReductionTest(BaseReductionTest): row_sum = np.ones([size_x], dtype=np.float32) * size_y full_sum = np.ones([], dtype=np.float32) * size_x * size_y - with self.test_session(graph=ops.Graph(), use_gpu=True) as sess: + with self.session(graph=ops.Graph(), use_gpu=True) as sess: tf_row_sum = self._tf_reduce_sum(arr, 1, False) tf_col_sum = self._tf_reduce_sum(arr, 0, False) tf_full_sum = self._tf_reduce_sum(arr, [0, 1], False) @@ -81,7 +81,7 @@ class BigReductionTest(BaseReductionTest): sum_y = np.ones([size_x, size_z], dtype=np.float32) sum_xz = np.ones([size_y], dtype=np.float32) - with self.test_session(graph=ops.Graph(), use_gpu=True) as sess: + with self.session(graph=ops.Graph(), use_gpu=True) as sess: tf_sum_xz = self._tf_reduce_mean(arr, [0, 2], False) tf_sum_y = self._tf_reduce_mean(arr, 1, False) tf_out_sum_xz, tf_out_sum_y = sess.run([tf_sum_xz, tf_sum_y]) @@ -106,7 +106,7 @@ class BigReductionTest(BaseReductionTest): row_max = np.max(arr, axis=1) full_max = np.max(col_max) - with self.test_session(graph=ops.Graph(), use_gpu=True) as sess: + with self.session(graph=ops.Graph(), use_gpu=True) as sess: tf_row_max = self._tf_reduce_max(arr, 1, False) tf_col_max = self._tf_reduce_max(arr, 0, False) tf_full_max = self._tf_reduce_max(arr, [0, 1], False) @@ -125,7 +125,7 @@ class BigReductionTest(BaseReductionTest): sum_y = np.max(arr, axis=1) sum_xz = np.max(arr, axis=(0, 2)) - with self.test_session(graph=ops.Graph(), use_gpu=True) as sess: + with self.session(graph=ops.Graph(), use_gpu=True) as sess: tf_sum_xz = self._tf_reduce_max(arr, [0, 2], False) tf_sum_y = self._tf_reduce_max(arr, 1, False) tf_out_sum_xz, tf_out_sum_y = sess.run([tf_sum_xz, tf_sum_y]) @@ -149,7 +149,7 @@ class BigReductionTest(BaseReductionTest): row_sum = np.ones([size_x], dtype=np.bool) full_sum = np.ones([1], dtype=np.bool).reshape([]) - with self.test_session(graph=ops.Graph(), use_gpu=True) as sess: + with self.session(graph=ops.Graph(), use_gpu=True) as sess: tf_row_sum = self._tf_reduce_all(arr, 1, False) tf_col_sum = self._tf_reduce_all(arr, 0, False) tf_full_sum = self._tf_reduce_all(arr, [0, 1], False) @@ -167,7 +167,7 @@ class BigReductionTest(BaseReductionTest): sum_y = np.ones([size_x, size_z], dtype=np.bool) sum_xz = np.ones([size_y], dtype=np.bool) - with self.test_session(graph=ops.Graph(), use_gpu=True) as sess: + with self.session(graph=ops.Graph(), use_gpu=True) as sess: tf_sum_xz = self._tf_reduce_all(arr, [0, 2], False) tf_sum_y = self._tf_reduce_all(arr, 1, False) tf_out_sum_xz, tf_out_sum_y = sess.run([tf_sum_xz, tf_sum_y]) diff --git a/tensorflow/python/kernel_tests/regex_full_match_op_test.py b/tensorflow/python/kernel_tests/regex_full_match_op_test.py index 7bd8c3ca27..98746e7d9b 100644 --- a/tensorflow/python/kernel_tests/regex_full_match_op_test.py +++ b/tensorflow/python/kernel_tests/regex_full_match_op_test.py @@ -35,28 +35,28 @@ class RegexFullMatchOpVariantsTest(test.TestCase, parameterized.TestCase): def testRegexFullMatch(self, op): values = ["abaaba", "abcdabcde"] - with self.test_session(): + with self.cached_session(): input_tensor = constant_op.constant(values, dtypes.string) matched = op(input_tensor, "a.*a").eval() self.assertAllEqual([True, False], matched) def testRegexFullMatchTwoDims(self, op): values = [["abaaba", "abcdabcde"], ["acdcba", "ebcda"]] - with self.test_session(): + with self.cached_session(): input_tensor = constant_op.constant(values, dtypes.string) matched = op(input_tensor, "a.*a").eval() self.assertAllEqual([[True, False], [True, False]], matched) def testEmptyMatch(self, op): values = ["abc", "1"] - with self.test_session(): + with self.cached_session(): input_tensor = constant_op.constant(values, dtypes.string) matched = op(input_tensor, "").eval() self.assertAllEqual([False, False], matched) def testInvalidPattern(self, op): values = ["abc", "1"] - with self.test_session(): + with self.cached_session(): input_tensor = constant_op.constant(values, dtypes.string) invalid_pattern = "A[" matched = op(input_tensor, invalid_pattern) @@ -68,7 +68,7 @@ class RegexFullMatchOpTest(test.TestCase): def testRegexFullMatchDelegation(self): with compat.forward_compatibility_horizon(2018, 11, 1): - with self.test_session(): + with self.cached_session(): input_tensor = constant_op.constant("foo", dtypes.string) pattern = "[a-z]" op = string_ops.regex_full_match(input_tensor, pattern) @@ -80,7 +80,7 @@ class RegexFullMatchOpTest(test.TestCase): def testStaticRegexFullMatchDelegation(self): with compat.forward_compatibility_horizon(2018, 11, 20): - with self.test_session(): + with self.cached_session(): input_tensor = constant_op.constant("foo", dtypes.string) pattern = "[a-z]*" op = string_ops.regex_full_match(input_tensor, pattern) diff --git a/tensorflow/python/kernel_tests/regex_replace_op_test.py b/tensorflow/python/kernel_tests/regex_replace_op_test.py index f0e84b8fca..d9b7ed28d2 100644 --- a/tensorflow/python/kernel_tests/regex_replace_op_test.py +++ b/tensorflow/python/kernel_tests/regex_replace_op_test.py @@ -20,7 +20,6 @@ from __future__ import print_function from absl.testing import parameterized -from tensorflow.python.compat import compat from tensorflow.python.framework import constant_op from tensorflow.python.framework import dtypes from tensorflow.python.ops import gen_string_ops @@ -34,7 +33,7 @@ from tensorflow.python.platform import test class RegexReplaceOpVariantsTest(test.TestCase, parameterized.TestCase): def testForwarding(self, op): - with self.test_session(): + with self.cached_session(): # Generate an input that is uniquely consumed by the regex op. # This exercises code paths which are optimized for this case # (e.g., using forwarding). @@ -48,7 +47,7 @@ class RegexReplaceOpVariantsTest(test.TestCase, parameterized.TestCase): def testRemovePrefix(self, op): values = ["a:foo", "a:bar", "a:foo", "b:baz", "b:qux", "ca:b"] - with self.test_session(): + with self.cached_session(): input_vector = constant_op.constant(values, dtypes.string) stripped = op(input_vector, "^(a:|b:)", "", replace_global=False).eval() self.assertAllEqual([b"foo", b"bar", b"foo", b"baz", b"qux", b"ca:b"], @@ -56,21 +55,21 @@ class RegexReplaceOpVariantsTest(test.TestCase, parameterized.TestCase): def testRegexReplace(self, op): values = ["aba\naba", "abcdabcde"] - with self.test_session(): + with self.cached_session(): input_vector = constant_op.constant(values, dtypes.string) stripped = op(input_vector, "a.*a", "(\\0)").eval() self.assertAllEqual([b"(aba)\n(aba)", b"(abcda)bcde"], stripped) def testEmptyMatch(self, op): values = ["abc", "1"] - with self.test_session(): + with self.cached_session(): input_vector = constant_op.constant(values, dtypes.string) stripped = op(input_vector, "", "x").eval() self.assertAllEqual([b"xaxbxcx", b"x1x"], stripped) def testInvalidPattern(self, op): values = ["abc", "1"] - with self.test_session(): + with self.cached_session(): input_vector = constant_op.constant(values, dtypes.string) invalid_pattern = "A[" replace = op(input_vector, invalid_pattern, "x") @@ -79,7 +78,7 @@ class RegexReplaceOpVariantsTest(test.TestCase, parameterized.TestCase): def testGlobal(self, op): values = ["ababababab", "abcabcabc", ""] - with self.test_session(): + with self.cached_session(): input_vector = constant_op.constant(values, dtypes.string) stripped = op(input_vector, "ab", "abc", True).eval() self.assertAllEqual([b"abcabcabcabcabc", b"abccabccabcc", b""], stripped) @@ -100,22 +99,20 @@ class RegexReplaceTest(test.TestCase, parameterized.TestCase): (as_tensor, as_string), (as_tensor, as_tensor)) def testRegexReplaceDelegation(self, pattern_fn, rewrite_fn): - with compat.forward_compatibility_horizon(2018, 10, 11): - with self.test_session(): - input_vector = constant_op.constant("foo", dtypes.string) - pattern = pattern_fn("[a-z]") - replace = rewrite_fn(".") - op = string_ops.regex_replace(input_vector, pattern, replace) - self.assertTrue(op.name.startswith("RegexReplace")) + with self.cached_session(): + input_vector = constant_op.constant("foo", dtypes.string) + pattern = pattern_fn("[a-z]") + replace = rewrite_fn(".") + op = string_ops.regex_replace(input_vector, pattern, replace) + self.assertTrue(op.name.startswith("RegexReplace")) def testStaticRegexReplaceDelegation(self): - with compat.forward_compatibility_horizon(2018, 10, 11): - with self.test_session(): - input_vector = constant_op.constant("foo", dtypes.string) - pattern = "[a-z]" - replace = "." - op = string_ops.regex_replace(input_vector, pattern, replace) - self.assertTrue(op.name.startswith("StaticRegexReplace")) + with self.cached_session(): + input_vector = constant_op.constant("foo", dtypes.string) + pattern = "[a-z]" + replace = "." + op = string_ops.regex_replace(input_vector, pattern, replace) + self.assertTrue(op.name.startswith("StaticRegexReplace")) if __name__ == "__main__": test.main() diff --git a/tensorflow/python/kernel_tests/relu_op_test.py b/tensorflow/python/kernel_tests/relu_op_test.py index d97a1613b9..b26e944af8 100644 --- a/tensorflow/python/kernel_tests/relu_op_test.py +++ b/tensorflow/python/kernel_tests/relu_op_test.py @@ -105,7 +105,7 @@ class ReluTest(test.TestCase): # The gradient test for ReLU is a bit tricky as the derivative is not well # defined at around zero and we want to avoid that in terms of input values. def testGradientFloat32(self): - with self.test_session(): + with self.cached_session(): x = constant_op.constant( [-0.9, -0.7, -0.5, -0.3, -0.1, 0.1, 0.3, 0.5, 0.7, 0.9], shape=[2, 5], @@ -150,7 +150,7 @@ class ReluTest(test.TestCase): self.assertAllClose(dx_f32_v, dx_f16_v, atol=3e-4) def testGradientFloat64(self): - with self.test_session(): + with self.cached_session(): x = constant_op.constant( [-0.9, -0.7, -0.5, -0.3, -0.1, 0.1, 0.3, 0.5, 0.7, 0.9], shape=[2, 5], @@ -167,7 +167,7 @@ class ReluTest(test.TestCase): self.assertLess(err, 1e-10) def testGradGradFloat32(self): - with self.test_session(): + with self.cached_session(): x = constant_op.constant( [-0.9, -0.7, -0.5, -0.3, -0.1, 0.1, 0.3, 0.5, 0.7, 0.9], shape=[2, 5], @@ -184,7 +184,7 @@ class ReluTest(test.TestCase): self.assertLess(err, 1e-4) def testGradGradFloat64(self): - with self.test_session(): + with self.cached_session(): x = constant_op.constant( [-0.9, -0.7, -0.5, -0.3, -0.1, 0.1, 0.3, 0.5, 0.7, 0.9], shape=[2, 5], @@ -202,7 +202,7 @@ class ReluTest(test.TestCase): self.assertLess(err, 1e-10) def testGradientScalar(self): - with self.test_session() as sess: + with self.cached_session() as sess: x = variables.Variable(100.) y = nn_ops.relu(x) loss = y**2 @@ -250,7 +250,7 @@ class Relu6Test(test.TestCase): # not well defined at around zero and six and we want to avoid that # in terms of input values. def testGradientFloat32(self): - with self.test_session(): + with self.cached_session(): x = constant_op.constant( [-0.9, -0.7, -0.5, -0.3, -0.1, 6.1, 6.3, 6.5, 6.7, 6.9], shape=[2, 5], @@ -266,7 +266,7 @@ class Relu6Test(test.TestCase): self.assertLess(err, 1e-4) def testGradientFloat64(self): - with self.test_session(): + with self.cached_session(): x = constant_op.constant( [-0.9, -0.7, -0.5, -0.3, -0.1, 6.1, 6.3, 6.5, 6.7, 6.9], shape=[2, 5], @@ -430,7 +430,7 @@ class EluTest(test.TestCase): use_gpu=True) def testGradientFloat32(self): - with self.test_session(): + with self.cached_session(): x_val = [[-0.9, -0.7, -0.5, -0.3, -0.1], [0.1, 0.3, 0.5, 0.7, 0.9]] x = constant_op.constant(x_val, name="x") y = nn_ops.elu(x, name="elu") @@ -441,7 +441,7 @@ class EluTest(test.TestCase): self.assertLess(err, 1e-4) def testGradientFloat64(self): - with self.test_session(): + with self.cached_session(): x_val = [[-0.9, -0.7, -0.5, -0.3, -0.1], [0.1, 0.3, 0.5, 0.7, 0.9]] x = constant_op.constant(x_val, dtype=dtypes.float64, name="x") y = nn_ops.elu(x, name="elu") @@ -452,7 +452,7 @@ class EluTest(test.TestCase): self.assertLess(err, 1e-6) def testGradGrad(self): - with self.test_session(): + with self.cached_session(): x = array_ops.placeholder(dtype=dtypes.float32) elu = nn_ops.elu(x) g, = gradients_impl.gradients(elu, x) @@ -463,7 +463,7 @@ class EluTest(test.TestCase): self.assertLess(err, 1e-4) def testGradGradFloat32(self): - with self.test_session(): + with self.cached_session(): x = constant_op.constant( [-0.9, -0.7, -0.5, -0.3, -0.1, 0.1, 0.3, 0.5, 0.7, 0.9], shape=[2, 5], @@ -480,7 +480,7 @@ class EluTest(test.TestCase): self.assertLess(err, 1e-4) def testGradGradFloat64(self): - with self.test_session(): + with self.cached_session(): x = constant_op.constant( [-0.9, -0.7, -0.5, -0.3, -0.1, 0.1, 0.3, 0.5, 0.7, 0.9], shape=[2, 5], @@ -532,7 +532,7 @@ class SeluTest(test.TestCase): use_gpu=True) def testGradientFloat32(self): - with self.test_session(): + with self.cached_session(): x_val = [[-0.9, -0.7, -0.5, -0.3, -0.1], [0.1, 0.3, 0.5, 0.7, 0.9]] x = constant_op.constant(x_val, name="x") y = nn_ops.selu(x, name="selu") @@ -543,7 +543,7 @@ class SeluTest(test.TestCase): self.assertLess(err, 1e-4) def testGradientFloat64(self): - with self.test_session(): + with self.cached_session(): x_val = [[-0.9, -0.7, -0.5, -0.3, -0.1], [0.1, 0.3, 0.5, 0.7, 0.9]] x = constant_op.constant(x_val, dtype=dtypes.float64, name="x") y = nn_ops.selu(x, name="selu") @@ -554,7 +554,7 @@ class SeluTest(test.TestCase): self.assertLess(err, 1e-6) def testGradGradFloat32(self): - with self.test_session(): + with self.cached_session(): x = constant_op.constant( [-0.9, -0.7, -0.5, -0.3, -0.1, 0.1, 0.3, 0.5, 0.7, 0.9], shape=[2, 5], @@ -571,7 +571,7 @@ class SeluTest(test.TestCase): self.assertLess(err, 1e-4) def testGradGradFloat64(self): - with self.test_session(): + with self.cached_session(): x = constant_op.constant( [-0.9, -0.7, -0.5, -0.3, -0.1, 0.1, 0.3, 0.5, 0.7, 0.9], shape=[2, 5], @@ -620,7 +620,7 @@ class CreluTest(test.TestCase): use_gpu=True) def testNumbersWithAxis0(self): - with self.test_session(): + with self.cached_session(): crelu = nn_ops.crelu( np.array([[-9, 7, -5, 3, -1], [1, -3, 5, -7, 9]]), axis=0) tf_relu = crelu.eval() @@ -629,7 +629,7 @@ class CreluTest(test.TestCase): self.assertAllEqual(np_crelu, tf_relu) def testNumbersWithAxis1(self): - with self.test_session(): + with self.cached_session(): crelu = nn_ops.crelu( np.array([[-9, 7, -5, 3, -1], [1, -3, 5, -7, 9]]), axis=1) tf_relu = crelu.eval() diff --git a/tensorflow/python/kernel_tests/reshape_op_test.py b/tensorflow/python/kernel_tests/reshape_op_test.py index ef9b439230..ca3ff1d1df 100644 --- a/tensorflow/python/kernel_tests/reshape_op_test.py +++ b/tensorflow/python/kernel_tests/reshape_op_test.py @@ -94,7 +94,7 @@ class ReshapeTest(test.TestCase): def testFloatReshapeGradThreeDimensions(self): x = np.arange(1., 25.).reshape([2, 3, 4]).astype(np.float32) s = list(np.shape(x)) - with self.test_session(): + with self.cached_session(): input_tensor = constant_op.constant(x) reshape_out = array_ops.reshape(input_tensor, [1, 8, 3]) err = gradient_checker.compute_gradient_error( diff --git a/tensorflow/python/kernel_tests/reverse_sequence_op_test.py b/tensorflow/python/kernel_tests/reverse_sequence_op_test.py index 9beb615b2c..8fc71e0c57 100644 --- a/tensorflow/python/kernel_tests/reverse_sequence_op_test.py +++ b/tensorflow/python/kernel_tests/reverse_sequence_op_test.py @@ -120,7 +120,7 @@ class ReverseSequenceTest(test.TestCase): batch_axis = 2 seq_lengths = np.asarray([3, 0, 4], dtype=np.int64) - with self.test_session(): + with self.cached_session(): input_t = constant_op.constant(x, shape=x.shape) seq_lengths_t = constant_op.constant(seq_lengths, shape=seq_lengths.shape) reverse_sequence_out = array_ops.reverse_sequence( @@ -171,7 +171,7 @@ class ReverseSequenceTest(test.TestCase): seq_axis=0, batch_axis=3) - with self.test_session(): + with self.cached_session(): inputs = array_ops.placeholder(dtypes.float32, shape=(32, 2, 3)) seq_lengths = array_ops.placeholder(dtypes.int64, shape=(32,)) output = array_ops.reverse_sequence( diff --git a/tensorflow/python/kernel_tests/rnn_test.py b/tensorflow/python/kernel_tests/rnn_test.py index a28cdc3b26..05ad9f6336 100644 --- a/tensorflow/python/kernel_tests/rnn_test.py +++ b/tensorflow/python/kernel_tests/rnn_test.py @@ -516,7 +516,7 @@ class RNNTest(test.TestCase): fix_weights_generator.build((None, input_shape)) weights = fix_weights_generator.get_weights() - with self.test_session(graph=ops_lib.Graph()) as sess: + with self.session(graph=ops_lib.Graph()) as sess: inputs = array_ops.placeholder( dtypes.float32, shape=(None, timestep, input_shape)) cell = keras.layers.SimpleRNNCell(output_shape) @@ -524,7 +524,7 @@ class RNNTest(test.TestCase): cell, inputs, dtype=dtypes.float32) cell.set_weights(weights) [tf_out, tf_state] = sess.run([tf_out, tf_state], {inputs: x_train}) - with self.test_session(graph=ops_lib.Graph()) as sess: + with self.session(graph=ops_lib.Graph()) as sess: k_input = keras.Input(shape=(timestep, input_shape), dtype=dtypes.float32) cell = keras.layers.SimpleRNNCell(output_shape) @@ -536,7 +536,7 @@ class RNNTest(test.TestCase): self.assertAllClose(tf_state, k_state) def testBasicLSTMCellInterchangeWithLSTMCell(self): - with self.test_session(graph=ops_lib.Graph()) as sess: + with self.session(graph=ops_lib.Graph()) as sess: basic_cell = rnn_cell_impl.BasicLSTMCell(1) basic_cell(array_ops.ones([1, 1]), state=basic_cell.get_initial_state(inputs=None, @@ -548,7 +548,7 @@ class RNNTest(test.TestCase): prefix = os.path.join(self.get_temp_dir(), "ckpt") save_path = save.save(sess, prefix) - with self.test_session(graph=ops_lib.Graph()) as sess: + with self.session(graph=ops_lib.Graph()) as sess: lstm_cell = rnn_cell_impl.LSTMCell(1, name="basic_lstm_cell") lstm_cell(array_ops.ones([1, 1]), state=lstm_cell.get_initial_state(inputs=None, diff --git a/tensorflow/python/kernel_tests/scalar_test.py b/tensorflow/python/kernel_tests/scalar_test.py index 287919bab7..d15f2c7b50 100644 --- a/tensorflow/python/kernel_tests/scalar_test.py +++ b/tensorflow/python/kernel_tests/scalar_test.py @@ -53,7 +53,7 @@ class ScalarTest(test.TestCase): for version in strict + lenient: with ops.Graph().as_default() as g: test_util.set_producer_version(g, version) - with self.test_session(graph=g) as sess: + with self.session(graph=g) as sess: feed = {} xs = placeholders(args, feed) x = op(*xs) diff --git a/tensorflow/python/kernel_tests/scatter_nd_ops_test.py b/tensorflow/python/kernel_tests/scatter_nd_ops_test.py index f2f3023469..86e063cb36 100644 --- a/tensorflow/python/kernel_tests/scatter_nd_ops_test.py +++ b/tensorflow/python/kernel_tests/scatter_nd_ops_test.py @@ -294,7 +294,7 @@ class StatefulScatterNdTest(test.TestCase): self.assertAllEqual(scatter_update.get_shape().as_list(), shape) expected_result = np.zeros([2, 2], dtype=np.int32) - with self.test_session(): + with self.cached_session(): ref.initializer.run() self.assertAllEqual(expected_result, scatter_update.eval()) @@ -409,7 +409,7 @@ class ScatterNdTest(test.TestCase): expected = np.array([b"", b"one", b"", b"three", b"four", b"", b"", b"seven"]) scatter = self.scatter_nd(indices, updates, shape=(8,)) - with self.test_session() as sess: + with self.cached_session() as sess: result = sess.run(scatter) self.assertAllEqual(expected, result) @@ -420,7 +420,7 @@ class ScatterNdTest(test.TestCase): dtype=dtypes.string) expected = np.array([b"", b"", b"", b"bb", b"a", b"", b"", b"c"]) scatter = self.scatter_nd(indices, updates, shape=(8,)) - with self.test_session() as sess: + with self.cached_session() as sess: result = sess.run(scatter) self.assertAllEqual(expected, result) @@ -432,7 +432,7 @@ class ScatterNdTest(test.TestCase): expected = [np.array([b"", b"", b"", b"bc", b"a", b"", b"", b"d"]), np.array([b"", b"", b"", b"cb", b"a", b"", b"", b"d"])] scatter = self.scatter_nd(indices, updates, shape=(8,)) - with self.test_session() as sess: + with self.cached_session() as sess: result = sess.run(scatter) self.assertTrue(np.array_equal(result, expected[0]) or np.array_equal(result, expected[1])) @@ -451,7 +451,7 @@ class ScatterNdTest(test.TestCase): scatter = self.scatter_nd(indices, updates, shape) self.assertAllEqual(scatter.get_shape().as_list(), shape) expected_result = np.zeros([2, 2], dtype=np.int32) - with self.test_session(): + with self.cached_session(): self.assertAllEqual(expected_result, scatter.eval()) def testUndefinedIndicesShape(self): @@ -486,7 +486,7 @@ class ScatterNdTest(test.TestCase): updates = array_ops.placeholder(dtypes.int32, shape=None) shape = constant_op.constant([0, 3, 2], dtypes.int32) - with self.test_session(): + with self.cached_session(): with self.assertRaisesOpError( "Indices and updates specified for empty output"): self.scatter_nd(indices, updates, shape).eval(feed_dict={ @@ -500,7 +500,7 @@ class ScatterNdTest(test.TestCase): shape = constant_op.constant([0], dtypes.int32) scatter = self.scatter_nd(indices, updates, shape) - with self.test_session(): + with self.cached_session(): self.assertEqual(scatter.eval().size, 0) def testRank3InvalidShape1(self): @@ -531,7 +531,7 @@ class ScatterNdTest(test.TestCase): [outputs], [updates, input_], [grad_vals]) expected_updates_grad = np.array([1, 4], dtype=np.float64) expected_input_grad = np.array([[1, 2], [3, 4]], dtype=np.float64) - with self.test_session(): + with self.cached_session(): self.assertAllEqual(expected_updates_grad, updates_grad.eval()) if self.non_aliasing_add_test: self.assertAllEqual(expected_input_grad, input_grad.eval()) @@ -548,7 +548,7 @@ class ScatterNdTest(test.TestCase): [outputs], [updates, input_], [grad_vals]) expected_updates_grad = np.array([[1, 2], [3, 4]], dtype=np.float64) expected_input_grad = np.array([[3, 4], [1, 2]], dtype=np.float64) - with self.test_session(): + with self.cached_session(): self.assertAllEqual(expected_updates_grad, updates_grad.eval()) if self.non_aliasing_add_test: self.assertAllEqual(expected_input_grad, input_grad.eval()) @@ -570,7 +570,7 @@ class ScatterNdTest(test.TestCase): [[[3, 4], [5, 6]], [[1, 2], [7, 8]]], dtype=np.float64) expected_input_grad = np.array( [[[1, 2], [3, 4]], [[5, 6], [7, 8]]], dtype=np.float64) - with self.test_session(): + with self.cached_session(): self.assertAllEqual(expected_updates_grad, updates_grad.eval()) if self.non_aliasing_add_test: self.assertAllEqual(expected_input_grad, input_grad.eval()) @@ -607,7 +607,7 @@ class ScatterNdTest(test.TestCase): [[[[1, 2], [3, 4]]]], [[[[5, 6], [7, 8]]]] ]]], dtype=np.float64) - with self.test_session(): + with self.cached_session(): self.assertAllEqual(expected_updates_grad, updates_grad.eval()) if self.non_aliasing_add_test: self.assertAllEqual(expected_input_grad, input_grad.eval()) @@ -616,33 +616,33 @@ class ScatterNdTest(test.TestCase): indices = array_ops.zeros([100000, 1], dtypes.int32) values = np.random.randn(100000) shape = [1] - with self.test_session(): + with self.cached_session(): val = self.scatter_nd(indices, values, shape).eval() self.assertAllClose([np.sum(values)], val) def testSmokeScatterNdBatch2DSliceDim2(self): - with self.test_session(): + with self.cached_session(): indices = array_ops.zeros([3, 5, 2], dtype=dtypes.int32) values = array_ops.zeros([3, 5, 7]) shape = [4, 6, 7] self.scatter_nd(indices, values, shape).eval() def testSmokeScatterNdBatch1DSliceDim2(self): - with self.test_session(): + with self.cached_session(): indices = array_ops.zeros([0, 2], dtype=dtypes.int32) values = array_ops.zeros([0, 7]) shape = [4, 6, 7] self.scatter_nd(indices, values, shape).eval() def testSmokeScatterNdBatch1DSliceDim3ShapeRank7(self): - with self.test_session(): + with self.cached_session(): indices = array_ops.zeros([1, 3], dtype=dtypes.int32) values = array_ops.zeros([1, 6, 7, 8, 9]) shape = [3, 4, 5, 6, 7, 8, 9] self.scatter_nd(indices, values, shape).eval() def testSmokeScatterNdBatch2DSliceDim3ShapeRank7(self): - with self.test_session(): + with self.cached_session(): indices = array_ops.zeros([1, 2, 3], dtype=dtypes.int32) values = array_ops.zeros([1, 2, 6, 7, 8, 9]) shape = [3, 4, 5, 6, 7, 8, 9] diff --git a/tensorflow/python/kernel_tests/segment_reduction_ops_test.py b/tensorflow/python/kernel_tests/segment_reduction_ops_test.py index a82855dfeb..2931877c11 100644 --- a/tensorflow/python/kernel_tests/segment_reduction_ops_test.py +++ b/tensorflow/python/kernel_tests/segment_reduction_ops_test.py @@ -177,7 +177,7 @@ class SegmentReductionOpTest(SegmentReductionHelper): def testSegmentIdsInvalid1(self): shape = [4, 4] - with self.test_session(): + with self.cached_session(): tf_x, _ = self._input(shape) indices = [-1, -1, 0, 0] s = math_ops.segment_sum(data=tf_x, segment_ids=indices) @@ -188,7 +188,7 @@ class SegmentReductionOpTest(SegmentReductionHelper): def testSegmentIdsInvalid2(self): shape = [4, 4] - with self.test_session(): + with self.cached_session(): tf_x, _ = self._input(shape) indices = [0, 1, 0, 1] s = math_ops.segment_sum(data=tf_x, segment_ids=indices) @@ -197,7 +197,7 @@ class SegmentReductionOpTest(SegmentReductionHelper): def testSegmentIdsInvalid3(self): shape = [4, 4] - with self.test_session(): + with self.cached_session(): tf_x, _ = self._input(shape) indices = [0, 1, 2, 0] s = math_ops.segment_sum(data=tf_x, segment_ids=indices) @@ -233,7 +233,7 @@ class SegmentReductionOpTest(SegmentReductionHelper): math_ops.segment_sum, math_ops.segment_mean, math_ops.segment_min, math_ops.segment_max ]: - with self.test_session(): + with self.cached_session(): tf_x, np_x = self._input(shape, dtype=dtypes_lib.float64) s = tf_op(data=tf_x, segment_ids=indices) jacob_t, jacob_n = gradient_checker.compute_gradient( @@ -300,7 +300,7 @@ class UnsortedSegmentTest(SegmentReductionHelper): tf_ans = s.eval() if dtype is dtypes_lib.bfloat16: tf_ans = tf_ans.astype(np.float32) - self.assertAllClose(np_ans, tf_ans) + self.assertAllCloseAccordingToType(np_ans, tf_ans) self.assertShapeEqual(np_ans, s) def testNumSegmentsTypes(self): @@ -736,7 +736,7 @@ class SparseSegmentReductionOpTest(SparseSegmentReductionHelper): segment_indices = [0, 1, 2, 2] num_indices = len(segment_indices) for tf_op in [math_ops.sparse_segment_sum, math_ops.sparse_segment_mean]: - with self.test_session(): + with self.cached_session(): tf_indices, _, tf_x, np_x = self._sparse_input( shape, num_indices, dtype=dtypes_lib.float64) s = tf_op(data=tf_x, indices=tf_indices, segment_ids=segment_indices) @@ -758,7 +758,7 @@ class SparseSegmentReductionOpTest(SparseSegmentReductionHelper): math_ops.sparse_segment_sum_with_num_segments, math_ops.sparse_segment_mean_with_num_segments, ]: - with self.test_session(): + with self.cached_session(): tf_indices, _, tf_x, np_x = self._sparse_input( shape, num_indices, dtype=dtypes_lib.float64) s = tf_op( diff --git a/tensorflow/python/kernel_tests/session_ops_test.py b/tensorflow/python/kernel_tests/session_ops_test.py index 678016b13d..03e1ae852f 100644 --- a/tensorflow/python/kernel_tests/session_ops_test.py +++ b/tensorflow/python/kernel_tests/session_ops_test.py @@ -31,7 +31,7 @@ from tensorflow.python.platform import test class SessionOpsTest(test.TestCase): def testHandleBasic(self): - with self.test_session() as sess: + with self.cached_session() as sess: # Return a handle. a = constant_op.constant(10) b = constant_op.constant(5) @@ -45,7 +45,7 @@ class SessionOpsTest(test.TestCase): self.assertEqual(500, sess.run(y, feed_dict={f: h.handle})) def testHandleEval(self): - with self.test_session() as sess: + with self.cached_session() as sess: # Return a handle. a = constant_op.constant(10) b = constant_op.constant(5) @@ -57,7 +57,7 @@ class SessionOpsTest(test.TestCase): self.assertEqual(50, h.eval()) def testHandleAndValue(self): - with self.test_session() as sess: + with self.cached_session() as sess: # Return a handle and a value. a = constant_op.constant(10) b = constant_op.constant(5) @@ -70,7 +70,7 @@ class SessionOpsTest(test.TestCase): self.assertEqual(500, v) def testHandleCond(self): - with self.test_session() as sess: + with self.cached_session() as sess: # Return a handle and a value a = constant_op.constant(10) b = constant_op.constant(5) @@ -90,7 +90,7 @@ class SessionOpsTest(test.TestCase): self.assertEqual(5000, result) def testHandleForLoop(self): - with self.test_session() as sess: + with self.cached_session() as sess: # Initialize a handle. a = constant_op.constant(0) h = session_ops.get_session_handle(a) @@ -107,7 +107,7 @@ class SessionOpsTest(test.TestCase): self.assertEqual(100, h.eval()) def testHandleWhileLoop(self): - with self.test_session() as sess: + with self.cached_session() as sess: # Initialize a handle. a = constant_op.constant(0) h = session_ops.get_session_handle(a) @@ -127,7 +127,7 @@ class SessionOpsTest(test.TestCase): self.assertEqual(101, h.eval()) def testHandleMover(self): - with self.test_session() as sess: + with self.cached_session() as sess: # Return a handle. a = constant_op.constant(10) b = constant_op.constant(5) @@ -148,7 +148,7 @@ class SessionOpsTest(test.TestCase): self.assertEqual(100, sess.run(y, feed_dict={f: h.handle})) def testHandleDelete(self): - with self.test_session() as sess: + with self.cached_session() as sess: # Return a handle. a = constant_op.constant(10) b = constant_op.constant(5) @@ -157,7 +157,7 @@ class SessionOpsTest(test.TestCase): sess.run(h).delete() def testHandleDeleteRaw(self): - with self.test_session() as sess: + with self.cached_session() as sess: # Return a handle. a = constant_op.constant(10) b = constant_op.constant(5) @@ -171,7 +171,7 @@ class SessionOpsTest(test.TestCase): sess.run(x, feed_dict={f: raw_h}) def testMultiDevices(self): - with self.test_session() as sess: + with self.cached_session() as sess: with ops.device(test.gpu_device_name()): a = constant_op.constant(1.0) a_handle = sess.run(session_ops.get_session_handle(a)) @@ -189,7 +189,7 @@ class SessionOpsTest(test.TestCase): self.assertEqual(3.0, c_handle.eval()) def testHandleGC(self): - with self.test_session() as sess: + with self.cached_session() as sess: # initial values live on CPU with ops.device("/cpu:0"): one = constant_op.constant(1, dtype=dtypes.float32) @@ -213,7 +213,7 @@ class SessionOpsTest(test.TestCase): add_h2: x_handle.handle}) def testHandlePlacement(self): - with self.test_session() as sess: + with self.cached_session() as sess: a = constant_op.constant(1.0) a_handle_op = session_ops.get_session_handle(a) b = constant_op.constant(2.0) @@ -233,7 +233,7 @@ class SessionOpsTest(test.TestCase): self.assertEqual(3.0, c_handle.eval()) def testFeedOneHandleDirectly(self): - with self.test_session() as sess: + with self.cached_session() as sess: a = constant_op.constant(10.0) b = constant_op.constant(5.0) c = math_ops.multiply(a, b) @@ -244,7 +244,7 @@ class SessionOpsTest(test.TestCase): self.assertAllClose(2500.0, sess.run(d, feed_dict={c: h_c})) def testDirectHandleFeedOverlappingWithFetches(self): - with self.test_session() as sess: + with self.cached_session() as sess: a = constant_op.constant(10.0) b = constant_op.constant(5.0) c = math_ops.multiply(a, b) @@ -270,7 +270,7 @@ class SessionOpsTest(test.TestCase): self.assertAllClose(50.0, d_val) def testFeedTwoHandlesDirectly(self): - with self.test_session() as sess: + with self.cached_session() as sess: a = constant_op.constant(10.0) b = constant_op.constant(5.0) c = math_ops.multiply(a, b) @@ -284,7 +284,7 @@ class SessionOpsTest(test.TestCase): self.assertAllClose(-48.0, sess.run(e, feed_dict={c: h_d, d: h_c})) def testFeedHandleToVariableDirectly(self): - with self.test_session() as sess: + with self.cached_session() as sess: a = variables.Variable(12.0) inc_a = state_ops.assign_add(a, 2.0) b = math_ops.add(a, 5.0) diff --git a/tensorflow/python/kernel_tests/sets_test.py b/tensorflow/python/kernel_tests/sets_test.py index 52b723802f..8335e9c139 100644 --- a/tensorflow/python/kernel_tests/sets_test.py +++ b/tensorflow/python/kernel_tests/sets_test.py @@ -158,7 +158,7 @@ class SetOpsTest(test_util.TensorFlowTestCase): for op in ops: self.assertEqual(None, op.get_shape().dims) self.assertEqual(dtypes.int32, op.dtype) - with self.test_session() as sess: + with self.cached_session() as sess: results = sess.run(ops) self.assertAllEqual(results[0], results[1]) return results[0] @@ -477,7 +477,7 @@ class SetOpsTest(test_util.TensorFlowTestCase): dynamic_values_shape_ops = [] static_indices_shape = None static_values_shape = None - with self.test_session() as sess: + with self.cached_session() as sess: for op in ops: if static_indices_shape is None: static_indices_shape = op.indices.get_shape() @@ -533,7 +533,7 @@ class SetOpsTest(test_util.TensorFlowTestCase): def _set_intersection_count(self, a, b): op = sets.set_size(sets.set_intersection(a, b)) - with self.test_session() as sess: + with self.cached_session() as sess: return sess.run(op) def test_set_difference_multirow_2d(self): @@ -971,7 +971,7 @@ class SetOpsTest(test_util.TensorFlowTestCase): def _set_difference_count(self, a, b, aminusb=True): op = sets.set_size(sets.set_difference(a, b, aminusb)) - with self.test_session() as sess: + with self.cached_session() as sess: return sess.run(op) def test_set_union_multirow_2d(self): @@ -1220,7 +1220,7 @@ class SetOpsTest(test_util.TensorFlowTestCase): def _set_union_count(self, a, b): op = sets.set_size(sets.set_union(a, b)) - with self.test_session() as sess: + with self.cached_session() as sess: return sess.run(op) def _assert_set_operation(self, expected_indices, expected_values, diff --git a/tensorflow/python/kernel_tests/shape_ops_test.py b/tensorflow/python/kernel_tests/shape_ops_test.py index 34e34d9d1b..0304dc3875 100644 --- a/tensorflow/python/kernel_tests/shape_ops_test.py +++ b/tensorflow/python/kernel_tests/shape_ops_test.py @@ -158,7 +158,7 @@ class ShapeOpsTest(test.TestCase): # Disabled because it takes too long to run, but manually verified # as passing at time of writing. def _test64BitOutput(self): - with self.test_session(): + with self.cached_session(): inp = array_ops.zeros([2**31]) num_elements = array_ops.size_internal( inp, optimize=False, out_type=dtypes.int64) @@ -166,7 +166,7 @@ class ShapeOpsTest(test.TestCase): # Too large for tf.int32 output. with self.assertRaises(errors_impl.InvalidArgumentError): - with self.test_session(): + with self.cached_session(): inp = array_ops.zeros([2**31]) num_elements = array_ops.size_internal( inp, optimize=False, out_type=dtypes.int32) @@ -228,7 +228,7 @@ class ShapeOpsTest(test.TestCase): self._compareExpandDimsAll(choice([2, 3, 5]), -4) def testExpandDimsErrors(self): - with self.test_session(): + with self.cached_session(): self.assertRaises(ValueError, array_ops.expand_dims, np.zeros([2, 3, 5]), -5) self.assertRaises(ValueError, array_ops.expand_dims, @@ -239,7 +239,7 @@ class ShapeOpsTest(test.TestCase): [False, True, True], 4) def testExpandDimsGradient(self): - with self.test_session(): + with self.cached_session(): inp = constant_op.constant( np.random.rand(4, 2).astype("f"), dtype=dtypes.float32) squeezed = array_ops.expand_dims(inp, 1) @@ -249,7 +249,7 @@ class ShapeOpsTest(test.TestCase): self.assertLess(err, 1e-3) def testExpandDimsScalar(self): - with self.test_session(): + with self.cached_session(): inp = constant_op.constant(7) self.assertAllEqual([7], array_ops.expand_dims(inp, 0).eval()) self.assertAllEqual([7], array_ops.expand_dims(inp, -1).eval()) @@ -375,7 +375,7 @@ class ShapeOpsTest(test.TestCase): np.zeros([1, 2, 1]), [2, 3]) def testSqueezeGradient(self): - with self.test_session(): + with self.cached_session(): inp = np.random.rand(4, 2).astype("f") a = array_ops.reshape(inp, [4, 1, 2]) squeezed = array_ops.squeeze(a, []) @@ -385,7 +385,7 @@ class ShapeOpsTest(test.TestCase): self.assertLess(err, 1e-3) def testSqueezeGradientWithSqueezeDims(self): - with self.test_session(): + with self.cached_session(): inp = np.random.rand(4, 2).astype("f") a = array_ops.reshape(inp, [4, 1, 2, 1]) squeezed = array_ops.squeeze(a, [1]) @@ -395,7 +395,7 @@ class ShapeOpsTest(test.TestCase): self.assertLess(err, 1e-3) def testSqueezeWithUnknownShape(self): - with self.test_session(): + with self.cached_session(): a = array_ops.placeholder(dtypes.float32, shape=[2, None]) squeezed = array_ops.squeeze(a, [1]) @@ -433,7 +433,7 @@ class TileTest(test.TestCase): self.assertTrue((result == np.tile(inp, (1, 4))).all()) def testIdentityTileAndGrad(self): - with self.test_session(): + with self.cached_session(): inp = np.random.rand(4, 1).astype(np.float32) a = constant_op.constant(inp) tiled = array_ops.tile(a, [1, 1]) @@ -443,7 +443,7 @@ class TileTest(test.TestCase): self.assertTrue((result == np.tile(inp, (1, 1))).all()) def testEmpty(self): - with self.test_session(): + with self.cached_session(): inp = np.random.rand(2, 3).astype(np.float32) a = constant_op.constant(inp) tiled = array_ops.tile(a, [5, 0]) @@ -453,7 +453,7 @@ class TileTest(test.TestCase): def testUnknownInputShape(self): """Importing can call _TileShape without shape of <multiples> known.""" - with self.test_session(): + with self.cached_session(): inp = array_ops.placeholder(dtypes.float32) # unknown shape multiples = constant_op.constant([1, 2, 3, 4], dtype=np.int32) tiled = array_ops.tile(inp, multiples) @@ -503,7 +503,7 @@ class TileTest(test.TestCase): self.assertAllEqual(result, np.tile(inp, (1, 4))) def testInvalidDim(self): - with self.test_session(): + with self.cached_session(): inp = np.random.rand(4, 1).astype("f") a = constant_op.constant( [float(x) for x in inp.ravel(order="C")], @@ -546,7 +546,7 @@ class TileTest(test.TestCase): self._RunAndVerifyResult(10, use_gpu=True) def testGradientSimpleReduction(self): - with self.test_session(): + with self.cached_session(): inp = np.random.rand(4, 1).astype("f") a = constant_op.constant( [float(x) for x in inp.flatten()], shape=[4, 1], dtype=dtypes.float32) @@ -561,7 +561,7 @@ class TileTest(test.TestCase): self.assertAllClose(np.sum(grad_inp, axis=1).reshape(4, 1), result, 1e-3) def testGradientStridedReduction(self): - with self.test_session(): + with self.cached_session(): inp = np.random.rand(4, 2).astype("f") a = constant_op.constant( [float(x) for x in inp.flatten()], shape=[4, 2], dtype=dtypes.float32) @@ -634,7 +634,7 @@ class TileTest(test.TestCase): self._RunAndVerifyGradientResult([2, 1, 3, 3, 2], [1, 3, 3, 1, 2]) def testGradientStridedReductionGC(self): - with self.test_session(): + with self.cached_session(): inp = np.random.rand(4, 2).astype("f") a = constant_op.constant( [float(x) for x in inp.flatten()], shape=[4, 2], dtype=dtypes.float32) @@ -647,7 +647,7 @@ class TileTest(test.TestCase): dtype=dtypes.float32) outputs = array_ops.gather(array_ops.tile(inputs, [3]), [1, 5, 9, 3, 7, 2, 2, 2]) - with self.test_session(): + with self.cached_session(): error = gradient_checker.compute_gradient_error( inputs, inputs.get_shape().as_list(), outputs, outputs.get_shape().as_list()) @@ -659,7 +659,7 @@ class TileTest(test.TestCase): inputs = array_ops.reshape(inputs, [-1, 1, 1]) outputs = array_ops.gather(array_ops.tile(inputs, [3, 4, 2]), [1, 5, 9, 3, 7, 2, 2, 2]) - with self.test_session(): + with self.cached_session(): error = gradient_checker.compute_gradient_error( inputs, inputs.get_shape().as_list(), outputs, outputs.get_shape().as_list()) diff --git a/tensorflow/python/kernel_tests/slice_op_test.py b/tensorflow/python/kernel_tests/slice_op_test.py index 40d384c623..c08d3222b3 100644 --- a/tensorflow/python/kernel_tests/slice_op_test.py +++ b/tensorflow/python/kernel_tests/slice_op_test.py @@ -107,7 +107,7 @@ class SliceTest(test.TestCase): def testScalarInput(self): input_val = 0 - with self.test_session() as sess: + with self.cached_session() as sess: # Test with constant input; shape inference fails. with self.assertRaisesWithPredicateMatch(ValueError, "out of range"): constant_op.constant(input_val)[:].get_shape() @@ -121,7 +121,7 @@ class SliceTest(test.TestCase): def testInvalidIndex(self): input_val = [1, 2] - with self.test_session() as sess: + with self.cached_session() as sess: # Test with constant input; shape inference fails. with self.assertRaisesWithPredicateMatch(ValueError, "out of range"): constant_op.constant(input_val)[1:, 1:].get_shape() diff --git a/tensorflow/python/kernel_tests/softmax_op_test.py b/tensorflow/python/kernel_tests/softmax_op_test.py index fbf1adba9b..89f4697e5c 100644 --- a/tensorflow/python/kernel_tests/softmax_op_test.py +++ b/tensorflow/python/kernel_tests/softmax_op_test.py @@ -22,7 +22,6 @@ import unittest import numpy as np -from tensorflow.python.compat import compat from tensorflow.python.framework import dtypes from tensorflow.python.framework import errors_impl from tensorflow.python.ops import array_ops @@ -163,10 +162,9 @@ class SoftmaxTest(test.TestCase): self._testOverflow(use_gpu=False) def test1DTensorAsInputNoReshape(self): - with compat.forward_compatibility_horizon(2018, 8, 27): - self._testSoftmax( - np.array([3., 2., 3., 9.]).astype(np.float64), use_gpu=False) - self._testOverflow(use_gpu=False) + self._testSoftmax( + np.array([3., 2., 3., 9.]).astype(np.float64), use_gpu=False) + self._testOverflow(use_gpu=False) def test3DTensorAsInput(self): self._testSoftmax( @@ -177,13 +175,12 @@ class SoftmaxTest(test.TestCase): self._testOverflow(use_gpu=False) def test3DTensorAsInputNoReshape(self): - with compat.forward_compatibility_horizon(2018, 8, 27): - self._testSoftmax( - np.array([[[1., 1., 1., 1.], [1., 2., 3., 4.]], - [[2., 3., 4., 5.], [6., 7., 8., 9.]], - [[5., 4., 3., 2.], [1., 2., 3., 4.]]]).astype(np.float32), - use_gpu=False) - self._testOverflow(use_gpu=False) + self._testSoftmax( + np.array([[[1., 1., 1., 1.], [1., 2., 3., 4.]], + [[2., 3., 4., 5.], [6., 7., 8., 9.]], + [[5., 4., 3., 2.], [1., 2., 3., 4.]]]).astype(np.float32), + use_gpu=False) + self._testOverflow(use_gpu=False) def testAlongFirstDimension(self): self._testSoftmax( @@ -210,7 +207,7 @@ class SoftmaxTest(test.TestCase): self.assertEqual([3, 2, 4], op.get_shape()) def testEmptyInput(self): - with self.test_session(): + with self.cached_session(): x = array_ops.placeholder(dtypes.float32, shape=[0, 3]) self.assertEqual(0, array_ops.size(x).eval()) # reshape would raise if logits is empty @@ -218,7 +215,7 @@ class SoftmaxTest(test.TestCase): nn_ops.softmax(x, axis=0).eval() def testDimTooLarge(self): - with self.test_session(): + with self.cached_session(): # Use placeholder to make sure we get runtime error instead of shape # inference error. dim = array_ops.placeholder_with_default(100, shape=[]) diff --git a/tensorflow/python/kernel_tests/softplus_op_test.py b/tensorflow/python/kernel_tests/softplus_op_test.py index c0269db9ae..e8dc272637 100644 --- a/tensorflow/python/kernel_tests/softplus_op_test.py +++ b/tensorflow/python/kernel_tests/softplus_op_test.py @@ -21,7 +21,6 @@ from __future__ import print_function import numpy as np from tensorflow.python.framework import constant_op -from tensorflow.python.framework import errors from tensorflow.python.ops import gradient_checker from tensorflow.python.ops import gradients_impl from tensorflow.python.ops import nn_ops @@ -72,7 +71,7 @@ class SoftplusTest(test.TestCase): use_gpu=True) def testGradient(self): - with self.test_session(): + with self.cached_session(): x = constant_op.constant( [-0.9, -0.7, -0.5, -0.3, -0.1, 0.1, 0.3, 0.5, 0.7, 0.9], shape=[2, 5], @@ -88,7 +87,7 @@ class SoftplusTest(test.TestCase): self.assertLess(err, 1e-4) def testGradGrad(self): - with self.test_session(): + with self.cached_session(): x = constant_op.constant( [-0.9, -0.7, -0.5, -0.3, -0.1, 0.1, 0.3, 0.5, 0.7, 0.9], shape=[2, 5], @@ -105,7 +104,7 @@ class SoftplusTest(test.TestCase): self.assertLess(err, 5e-5) def testGradGradGrad(self): - with self.test_session(): + with self.cached_session(): x = constant_op.constant( [-0.9, -0.7, -0.5, -0.3, -0.1, 0.1, 0.3, 0.5, 0.7, 0.9], shape=[2, 5], @@ -123,10 +122,10 @@ class SoftplusTest(test.TestCase): self.assertLess(err, 5e-5) def testNoInts(self): - with self.test_session(): + with self.cached_session(): with self.assertRaisesRegexp( - errors.InvalidArgumentError, - "No OpKernel was registered to support Op 'Softplus'"): + TypeError, + "'features' has DataType int32 not in list of allowed values"): nn_ops.softplus(constant_op.constant(7)).eval() diff --git a/tensorflow/python/kernel_tests/softsign_op_test.py b/tensorflow/python/kernel_tests/softsign_op_test.py index a5247ce08d..1b4db9fa46 100644 --- a/tensorflow/python/kernel_tests/softsign_op_test.py +++ b/tensorflow/python/kernel_tests/softsign_op_test.py @@ -21,7 +21,6 @@ from __future__ import print_function import numpy as np from tensorflow.python.framework import constant_op -from tensorflow.python.framework import errors from tensorflow.python.ops import gradient_checker from tensorflow.python.ops import nn_ops import tensorflow.python.ops.nn_grad # pylint: disable=unused-import @@ -51,7 +50,7 @@ class SoftsignTest(test.TestCase): use_gpu=True) def testGradient(self): - with self.test_session(): + with self.cached_session(): x = constant_op.constant( [-0.9, -0.7, -0.5, -0.3, -0.1, 0.1, 0.3, 0.5, 0.7, 0.9], shape=[2, 5], @@ -67,10 +66,10 @@ class SoftsignTest(test.TestCase): self.assertLess(err, 1e-4) def testNoInts(self): - with self.test_session(): + with self.cached_session(): with self.assertRaisesRegexp( - errors.InvalidArgumentError, - "No OpKernel was registered to support Op 'Softsign'"): + TypeError, + "'features' has DataType int32 not in list of allowed values"): nn_ops.softsign(constant_op.constant(7)).eval() diff --git a/tensorflow/python/kernel_tests/spacetobatch_op_test.py b/tensorflow/python/kernel_tests/spacetobatch_op_test.py index 2a9232b6ae..e267c05915 100644 --- a/tensorflow/python/kernel_tests/spacetobatch_op_test.py +++ b/tensorflow/python/kernel_tests/spacetobatch_op_test.py @@ -551,7 +551,7 @@ class SpaceToBatchNDGradientTest(test.TestCase): def _checkGrad(self, x, block_shape, paddings): block_shape = np.array(block_shape) paddings = np.array(paddings).reshape((len(block_shape), 2)) - with self.test_session(): + with self.cached_session(): tf_x = ops.convert_to_tensor(x) tf_y = array_ops.space_to_batch_nd(tf_x, block_shape, paddings) epsilon = 1e-5 @@ -638,7 +638,7 @@ class RequiredSpaceToBatchPaddingsTest(test.TestCase): t_paddings, t_crops = array_ops.required_space_to_batch_paddings( input_shape_placeholder, block_shape_placeholder, base_paddings_placeholder) - with self.test_session(): + with self.cached_session(): paddings_result = t_paddings.eval(assignments) crops_result = t_crops.eval(assignments) self.assertAllEqual(paddings_result, paddings_const) diff --git a/tensorflow/python/kernel_tests/sparse_conditional_accumulator_test.py b/tensorflow/python/kernel_tests/sparse_conditional_accumulator_test.py index 3bb5e899fe..a824d5c826 100644 --- a/tensorflow/python/kernel_tests/sparse_conditional_accumulator_test.py +++ b/tensorflow/python/kernel_tests/sparse_conditional_accumulator_test.py @@ -99,20 +99,20 @@ class IndexedSlicesConditionalAccumulatorTest(test.TestCase): """, q.accumulator_ref.op.node_def) def testAccumulatorSizeEmpty(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.SparseConditionalAccumulator( dtypes_lib.float32, name="Q") self.assertEqual(q.num_accumulated().eval(), 0) def testAccumulatorSetGlobalStep(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.SparseConditionalAccumulator( dtypes_lib.float32, name="Q", shape=tensor_shape.TensorShape([1])) set_global_step_op = q.set_global_step(1) set_global_step_op.run() def testAccumulatorApplyGradFloat32(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.SparseConditionalAccumulator( dtypes_lib.float32, name="Q", shape=tensor_shape.TensorShape([3, 3])) accum_op = q.apply_indexed_slices_grad( @@ -123,7 +123,7 @@ class IndexedSlicesConditionalAccumulatorTest(test.TestCase): self.assertEqual(q.num_accumulated().eval(), 1) def testDtypes(self): - with self.test_session() as sess: + with self.cached_session() as sess: dtypes = [dtypes_lib.float16, dtypes_lib.float32, dtypes_lib.float64] for i in range(len(dtypes)): @@ -145,7 +145,7 @@ class IndexedSlicesConditionalAccumulatorTest(test.TestCase): self._assertEqual_nparray(sum_elems / len(elems), result, sess) def testAccumulatorMultipleAccumulators(self): - with self.test_session() as sess: + with self.cached_session() as sess: q_f32_0 = data_flow_ops.SparseConditionalAccumulator( dtypes_lib.float32, name="Q", shape=tensor_shape.TensorShape([2, 2])) q_f32_1 = data_flow_ops.SparseConditionalAccumulator( @@ -175,7 +175,7 @@ class IndexedSlicesConditionalAccumulatorTest(test.TestCase): self._assertEqual_indexedslices(expected_tensors[i], result) def testAccumulatorTakeGradMean(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.SparseConditionalAccumulator( dtypes_lib.float32, name="Q", shape=()) @@ -195,7 +195,7 @@ class IndexedSlicesConditionalAccumulatorTest(test.TestCase): self.assertAllEqual([-1, 2], val.dense_shape) def testAccumulatorTakeGradSum(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.SparseConditionalAccumulator( dtypes_lib.float32, name="Q", shape=(), reduction_type="SUM") @@ -220,7 +220,7 @@ class IndexedSlicesConditionalAccumulatorTest(test.TestCase): dtypes_lib.float32, name="Q", shape=(), reduction_type="Invalid") def testAccumulatorRepeatedTakeGrad(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.SparseConditionalAccumulator( dtypes_lib.float32, name="Q", shape=()) @@ -258,7 +258,7 @@ class IndexedSlicesConditionalAccumulatorTest(test.TestCase): self.assertAllEqual(val.dense_shape, [-1, 2]) def testParallelApplyGradMean(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.SparseConditionalAccumulator( dtypes_lib.float32, name="Q", shape=tensor_shape.TensorShape([2, 2])) elems = [10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0, 100.0] @@ -289,7 +289,7 @@ class IndexedSlicesConditionalAccumulatorTest(test.TestCase): val, sess) def testParallelApplyGradSum(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.SparseConditionalAccumulator( dtypes_lib.float32, name="Q", @@ -323,7 +323,7 @@ class IndexedSlicesConditionalAccumulatorTest(test.TestCase): val, sess) def testParallelTakeGrad(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.SparseConditionalAccumulator( dtypes_lib.float32, name="Q", shape=tensor_shape.TensorShape([2, 2])) elems = [e + 1 for e in range(10)] @@ -362,7 +362,7 @@ class IndexedSlicesConditionalAccumulatorTest(test.TestCase): np.array([[0, 0], [elems[i], 0]]), results[i], sess) def testAccumulatorApplyAndBlockingTake(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.SparseConditionalAccumulator( dtypes_lib.float32, name="Q", shape=tensor_shape.TensorShape([2, 2])) @@ -397,7 +397,7 @@ class IndexedSlicesConditionalAccumulatorTest(test.TestCase): sess.run(takeg_op) def testAccumulatorCancel(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.SparseConditionalAccumulator( dtypes_lib.float32, name="Q", @@ -416,7 +416,7 @@ class IndexedSlicesConditionalAccumulatorTest(test.TestCase): takeg_thread.join() def testNonVectorIndices(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.SparseConditionalAccumulator( dtypes_lib.float32, name="Q", shape=tensor_shape.TensorShape([3, 3])) @@ -428,7 +428,7 @@ class IndexedSlicesConditionalAccumulatorTest(test.TestCase): grad_values=np.array([1, 2]).astype(np.float32)).run() def testZeroDimensionValues(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.SparseConditionalAccumulator( dtypes_lib.float32, name="Q", shape=tensor_shape.TensorShape([3, 3])) @@ -438,7 +438,7 @@ class IndexedSlicesConditionalAccumulatorTest(test.TestCase): grad_indices=[0], grad_values=np.array(1).astype(np.float32)).run() def testWrongNonEmptyInputValues(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.SparseConditionalAccumulator( dtypes_lib.float32, name="Q", shape=tensor_shape.TensorShape([3, 3])) @@ -449,7 +449,7 @@ class IndexedSlicesConditionalAccumulatorTest(test.TestCase): grad_values=np.array([[0, 1, 1]]).astype(np.float32)).run() def testDynamicNonVectorIndices(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.SparseConditionalAccumulator( dtypes_lib.float32, name="Q", shape=tensor_shape.TensorShape([3, 3])) @@ -468,7 +468,7 @@ class IndexedSlicesConditionalAccumulatorTest(test.TestCase): }) def testDynamicWrongNonEmptyInputValues(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.SparseConditionalAccumulator( dtypes_lib.float32, name="Q", shape=tensor_shape.TensorShape([3, 3])) @@ -486,7 +486,7 @@ class IndexedSlicesConditionalAccumulatorTest(test.TestCase): }) def testEmptyShapeApply(self): - with self.test_session(): + with self.cached_session(): q = data_flow_ops.SparseConditionalAccumulator( dtypes_lib.float32, name="Q", shape=tensor_shape.TensorShape([])) @@ -511,7 +511,7 @@ class IndexedSlicesConditionalAccumulatorTest(test.TestCase): q.apply_grad(grad_indices=[0], grad_values=[1.0]).run() def testValidateShape(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.SparseConditionalAccumulator( dtypes_lib.float32, name="Q", shape=[2, 2, None]) @@ -606,7 +606,7 @@ class IndexedSlicesConditionalAccumulatorTest(test.TestCase): local_step=1).run() def testReturnShape(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.SparseConditionalAccumulator( dtypes_lib.float32, name="Q", shape=[2, None]) @@ -631,7 +631,7 @@ class IndexedSlicesConditionalAccumulatorTest(test.TestCase): self.assertAllEqual(val.dense_shape, [-1, 2, 2, 3]) def testApplyGradtInt32IndicesAndShape(self): - with self.test_session() as sess: + with self.cached_session() as sess: q = data_flow_ops.SparseConditionalAccumulator( dtypes_lib.float32, name="Q", shape=tensor_shape.TensorShape([3, 3])) accum_op = q.apply_grad( diff --git a/tensorflow/python/kernel_tests/sparse_cross_op_test.py b/tensorflow/python/kernel_tests/sparse_cross_op_test.py index ca7898d466..6e0714da70 100644 --- a/tensorflow/python/kernel_tests/sparse_cross_op_test.py +++ b/tensorflow/python/kernel_tests/sparse_cross_op_test.py @@ -42,7 +42,7 @@ class SparseCrossOpTest(test.TestCase): 'batch2-FC1-F1_X_batch2-FC2-F1', 'batch2-FC1-F1_X_batch2-FC2-F2', 'batch2-FC1-F2_X_batch2-FC2-F1', 'batch2-FC1-F2_X_batch2-FC2-F2' ]]) - with self.test_session() as sess: + with self.cached_session() as sess: self._assert_sparse_tensor_equals(expected_out, sess.run(op)) def test_dense(self): @@ -62,7 +62,7 @@ class SparseCrossOpTest(test.TestCase): 'batch2-FC1-F1_X_batch2-FC2-F1', 'batch2-FC1-F1_X_batch2-FC2-F2', 'batch2-FC1-F2_X_batch2-FC2-F1', 'batch2-FC1-F2_X_batch2-FC2-F2' ]]) - with self.test_session() as sess: + with self.cached_session() as sess: self._assert_sparse_tensor_equals(expected_out, sess.run(op)) def test_integer_mixed_string_sparse(self): @@ -76,7 +76,7 @@ class SparseCrossOpTest(test.TestCase): '333_X_batch2-FC2-F1', '333_X_batch2-FC2-F2', '55555_X_batch2-FC2-F1', '55555_X_batch2-FC2-F2' ]]) - with self.test_session() as sess: + with self.cached_session() as sess: self._assert_sparse_tensor_equals(expected_out, sess.run(op)) def test_integer_mixed_string_dense(self): @@ -94,7 +94,7 @@ class SparseCrossOpTest(test.TestCase): '55555_X_batch2-FC2-F1', '55555_X_batch2-FC2-F2', '999999_X_batch2-FC2-F1', '999999_X_batch2-FC2-F2' ]]) - with self.test_session() as sess: + with self.cached_session() as sess: self._assert_sparse_tensor_equals(expected_out, sess.run(op)) def test_sparse_cross_dense(self): @@ -111,7 +111,7 @@ class SparseCrossOpTest(test.TestCase): 'batch2-FC1-F1_X_batch2-FC2-F1', 'batch2-FC1-F1_X_batch2-FC2-F2', 'batch2-FC1-F2_X_batch2-FC2-F1', 'batch2-FC1-F2_X_batch2-FC2-F2' ]]) - with self.test_session() as sess: + with self.cached_session() as sess: self._assert_sparse_tensor_equals(expected_out, sess.run(op)) def test_integer_sparse_input(self): @@ -127,7 +127,7 @@ class SparseCrossOpTest(test.TestCase): '333_X_batch2-FC2-F1', '333_X_batch2-FC2-F2', '5555_X_batch2-FC2-F1', '5555_X_batch2-FC2-F2' ]]) - with self.test_session() as sess: + with self.cached_session() as sess: self._assert_sparse_tensor_equals(expected_out, sess.run(op)) def test_permutation_3x3x3(self): @@ -169,7 +169,7 @@ class SparseCrossOpTest(test.TestCase): 'batch1-FC1-F3_X_batch1-FC2-F3_X_batch1-FC3-F2', 'batch1-FC1-F3_X_batch1-FC2-F3_X_batch1-FC3-F3' ]]) - with self.test_session() as sess: + with self.cached_session() as sess: self._assert_sparse_tensor_equals(expected_out, sess.run(op)) def test_permutation_3x1x2(self): @@ -188,7 +188,7 @@ class SparseCrossOpTest(test.TestCase): 'batch1-FC1-F3_X_batch1-FC2-F1_X_batch1-FC3-F1', 'batch1-FC1-F3_X_batch1-FC2-F1_X_batch1-FC3-F2' ]]) - with self.test_session() as sess: + with self.cached_session() as sess: self._assert_sparse_tensor_equals(expected_out, sess.run(op)) def test_large_batch(self): @@ -221,7 +221,7 @@ class SparseCrossOpTest(test.TestCase): ]) expected_out = self._sparse_tensor(col_out) - with self.test_session() as sess: + with self.cached_session() as sess: self._assert_sparse_tensor_equals(expected_out, sess.run(op)) def test_one_column_empty(self): @@ -234,7 +234,7 @@ class SparseCrossOpTest(test.TestCase): self._sparse_tensor([], 1), self._sparse_tensor([['batch1-FC3-F1', 'batch1-FC3-F2']]) ]) - with self.test_session() as sess: + with self.cached_session() as sess: self._assert_sparse_tensor_empty(sess.run(op)) def test_some_columns_empty(self): @@ -253,7 +253,7 @@ class SparseCrossOpTest(test.TestCase): 'batch1-FC1-F2_X_batch1-FC2-F1_X_batch1-FC3-F1', 'batch1-FC1-F2_X_batch1-FC2-F1_X_batch1-FC3-F2' ]], 2) - with self.test_session() as sess: + with self.cached_session() as sess: self._assert_sparse_tensor_equals(expected_out, sess.run(op)) def test_all_columns_empty(self): @@ -266,7 +266,7 @@ class SparseCrossOpTest(test.TestCase): self._sparse_tensor([]), self._sparse_tensor([]) ]) - with self.test_session() as sess: + with self.cached_session() as sess: self._assert_sparse_tensor_empty(sess.run(op)) def test_hashed_zero_bucket_no_hash_key(self): @@ -277,7 +277,7 @@ class SparseCrossOpTest(test.TestCase): ]) # Check actual hashed output to prevent unintentional hashing changes. expected_out = self._sparse_tensor([[1971693436396284976]]) - with self.test_session() as sess: + with self.cached_session() as sess: self._assert_sparse_tensor_equals(expected_out, sess.run(op)) def test_hashed_zero_bucket(self): @@ -290,7 +290,7 @@ class SparseCrossOpTest(test.TestCase): hash_key=sparse_ops._DEFAULT_HASH_KEY + 1) # Check actual hashed output to prevent unintentional hashing changes. expected_out = self._sparse_tensor([[4847552627144134031]]) - with self.test_session() as sess: + with self.cached_session() as sess: self._assert_sparse_tensor_equals(expected_out, sess.run(op)) # TODO(sibyl-Aix6ihai): Add benchmark to compare Hashed vs Non-hashed. @@ -304,7 +304,7 @@ class SparseCrossOpTest(test.TestCase): num_buckets=100) # Check actual hashed output to prevent unintentional hashing changes. expected_out = self._sparse_tensor([[83]]) - with self.test_session() as sess: + with self.cached_session() as sess: self._assert_sparse_tensor_equals(expected_out, sess.run(op)) def test_hashed_output(self): @@ -318,7 +318,7 @@ class SparseCrossOpTest(test.TestCase): hash_key=sparse_ops._DEFAULT_HASH_KEY + 1) # Check actual hashed output to prevent unintentional hashing changes. expected_out = self._sparse_tensor([[31]]) - with self.test_session() as sess: + with self.cached_session() as sess: self._assert_sparse_tensor_equals(expected_out, sess.run(op)) def test_hashed__has_no_collision(self): @@ -344,7 +344,7 @@ class SparseCrossOpTest(test.TestCase): self._sparse_tensor([['batch1-FC3-F1', 'batch1-FC3-F2']]) ], num_buckets=1000) - with self.test_session() as sess: + with self.cached_session() as sess: out = sess.run(op) self.assertEqual(6, len(out.values)) self.assertAllEqual([[0, i] for i in range(6)], out.indices) diff --git a/tensorflow/python/kernel_tests/sparse_matmul_op_test.py b/tensorflow/python/kernel_tests/sparse_matmul_op_test.py index f50e39d6d5..90009fc33e 100644 --- a/tensorflow/python/kernel_tests/sparse_matmul_op_test.py +++ b/tensorflow/python/kernel_tests/sparse_matmul_op_test.py @@ -130,7 +130,7 @@ class MatMulGradientTest(test.TestCase): def _testGradients(self, tr_a, tr_b, sp_a, sp_b, a_dtype, b_dtype, delta, name): - with self.test_session(): + with self.cached_session(): a = constant_op.constant( RandMatrix( 3, 2, tr_a, round_bfloat=True), dtype=dtypes.float32) diff --git a/tensorflow/python/kernel_tests/sparse_ops_test.py b/tensorflow/python/kernel_tests/sparse_ops_test.py index fc39de150e..79efee3f5b 100644 --- a/tensorflow/python/kernel_tests/sparse_ops_test.py +++ b/tensorflow/python/kernel_tests/sparse_ops_test.py @@ -628,7 +628,7 @@ class SparseReduceTest(test_util.TensorFlowTestCase): else: np_ans = np.max(np_ans, axis=ra, keepdims=keep_dims) - with self.test_session(): + with self.cached_session(): if do_sum: tf_dense_ans = sparse_ops.sparse_reduce_sum(sp_t, reduction_axes, keep_dims) diff --git a/tensorflow/python/kernel_tests/sparse_tensors_map_ops_test.py b/tensorflow/python/kernel_tests/sparse_tensors_map_ops_test.py index 96793d5af3..31e84341ae 100644 --- a/tensorflow/python/kernel_tests/sparse_tensors_map_ops_test.py +++ b/tensorflow/python/kernel_tests/sparse_tensors_map_ops_test.py @@ -76,7 +76,7 @@ class SparseTensorsMapTest(test.TestCase): return sparse_tensor_lib.SparseTensorValue(ind, val, shape) def testAddTakeMany(self): - with self.test_session(graph=ops.Graph(), use_gpu=False) as sess: + with self.session(graph=ops.Graph(), use_gpu=False) as sess: sp_input0 = self._SparseTensorValue_5x6(np.arange(6)) sp_input1 = self._SparseTensorValue_3x4(np.arange(6)) handle0 = add_sparse_to_tensors_map(sp_input0, shared_name="a") diff --git a/tensorflow/python/kernel_tests/sparse_to_dense_op_py_test.py b/tensorflow/python/kernel_tests/sparse_to_dense_op_py_test.py index 87a4eb9c7b..c71746cc99 100644 --- a/tensorflow/python/kernel_tests/sparse_to_dense_op_py_test.py +++ b/tensorflow/python/kernel_tests/sparse_to_dense_op_py_test.py @@ -81,7 +81,7 @@ class SparseToDenseTest(test.TestCase): self.assertAllClose(np_ans, tf_ans) def testZeroDefault(self): - with self.test_session(): + with self.cached_session(): x = sparse_ops.sparse_to_dense(2, [4], 7).eval() self.assertAllEqual(x, [0, 0, 7, 0]) @@ -94,12 +94,12 @@ class SparseToDenseTest(test.TestCase): self.assertAllClose(np_ans, tf_ans) def testBadShape(self): - with self.test_session(): + with self.cached_session(): with self.assertRaisesWithPredicateMatch(ValueError, "must be rank 1"): _SparseToDense([1, 3], [[5], [3]], 1, -1) def testBadValue(self): - with self.test_session(): + with self.cached_session(): dense = _SparseToDense([1, 3], [5], [[5], [3]], -1) with self.assertRaisesOpError( r"sparse_values has incorrect shape \[2,1\], " @@ -107,20 +107,20 @@ class SparseToDenseTest(test.TestCase): dense.eval() def testBadNumValues(self): - with self.test_session(): + with self.cached_session(): dense = _SparseToDense([1, 3], [5], [1, 2, 3], -1) with self.assertRaisesOpError( r"sparse_values has incorrect shape \[3\], should be \[\] or \[2\]"): dense.eval() def testBadDefault(self): - with self.test_session(): + with self.cached_session(): dense = _SparseToDense([1, 3], [5], [1, 2], [0]) with self.assertRaisesOpError("default_value should be a scalar"): dense.eval() def testOutOfBoundsIndicesWithWithoutValidation(self): - with self.test_session(): + with self.cached_session(): dense = _SparseToDense( sparse_indices=[[1], [10]], output_size=[5], @@ -140,7 +140,7 @@ class SparseToDenseTest(test.TestCase): dense_without_validation.eval() def testRepeatingIndicesWithWithoutValidation(self): - with self.test_session(): + with self.cached_session(): dense = _SparseToDense( sparse_indices=[[1], [1]], output_size=[5], @@ -158,7 +158,7 @@ class SparseToDenseTest(test.TestCase): dense_without_validation.eval() def testUnsortedIndicesWithWithoutValidation(self): - with self.test_session(): + with self.cached_session(): dense = _SparseToDense( sparse_indices=[[2], [1]], output_size=[5], diff --git a/tensorflow/python/kernel_tests/sparsemask_op_test.py b/tensorflow/python/kernel_tests/sparsemask_op_test.py index cf6c9494ae..6f5dd45b61 100644 --- a/tensorflow/python/kernel_tests/sparsemask_op_test.py +++ b/tensorflow/python/kernel_tests/sparsemask_op_test.py @@ -34,7 +34,7 @@ class SparseMaskTest(test.TestCase): out_values = values[1:, :] out_indices = np.array([2, 3, 4], dtype=np.int32) - with self.test_session() as sess: + with self.cached_session() as sess: values_tensor = ops.convert_to_tensor(values) indices_tensor = ops.convert_to_tensor(indices) mask_indices_tensor = ops.convert_to_tensor(mask_indices) diff --git a/tensorflow/python/kernel_tests/string_format_op_test.py b/tensorflow/python/kernel_tests/string_format_op_test.py new file mode 100644 index 0000000000..74a5072bab --- /dev/null +++ b/tensorflow/python/kernel_tests/string_format_op_test.py @@ -0,0 +1,384 @@ +# Copyright 2015 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== +"""Tests for tensorflow.kernels.logging_ops.""" + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + + +from tensorflow.python.eager import context +from tensorflow.python.framework import constant_op +from tensorflow.python.framework import test_util +from tensorflow.python.ops import array_ops +from tensorflow.python.ops import math_ops +from tensorflow.python.ops import string_ops +from tensorflow.python.ops import variables +from tensorflow.python.platform import test +from tensorflow.python.util import compat + + +class StringFormatOpTest(test.TestCase): + + @test_util.run_in_graph_and_eager_modes() + def testFormatOneTensorOneDim(self): + with self.cached_session(): + tensor = math_ops.range(10) + format_output = string_ops.string_format("{}", tensor) + out = self.evaluate(format_output) + expected = "[0 1 2 ... 7 8 9]" + self.assertEqual(compat.as_text(out), expected) + + with self.cached_session(): + tensor = math_ops.range(10) + format_output = string_ops.string_format("{}", [tensor]) + out = self.evaluate(format_output) + expected = "[0 1 2 ... 7 8 9]" + self.assertEqual(compat.as_text(out), expected) + + @test_util.run_in_graph_and_eager_modes() + def testFormatOneVariableScalar(self): + with self.cached_session(): + var = variables.Variable(3.34) + format_output = string_ops.string_format("{}", [var]) + if not context.executing_eagerly(): + variables.global_variables_initializer().run() + out = self.evaluate(format_output) + expected = "3.34" + self.assertEqual(compat.as_text(out), expected) + + @test_util.run_in_graph_and_eager_modes() + def testFormatOneVariableOneDim(self): + with self.cached_session(): + var = variables.Variable(math_ops.range(10)) + format_output = string_ops.string_format("{}", [var]) + if not context.executing_eagerly(): + variables.global_variables_initializer().run() + out = self.evaluate(format_output) + expected = "[0 1 2 ... 7 8 9]" + self.assertEqual(compat.as_text(out), expected) + + @test_util.run_in_graph_and_eager_modes() + def testFormatTwoVariablesWithAssignAdd(self): + with self.cached_session(): + var_one = variables.Variable(2.14) + plus_one = var_one.assign_add(1.0) + var_two = variables.Variable(math_ops.range(10)) + format_output = string_ops.string_format("{}, {}", [var_one, var_two]) + if not context.executing_eagerly(): + variables.global_variables_initializer().run() + self.evaluate(plus_one) + out = self.evaluate(format_output) + expected = "3.14, [0 1 2 ... 7 8 9]" + self.assertEqual(compat.as_text(out), expected) + + @test_util.run_in_graph_and_eager_modes() + def testFormatOneTensorOneDimFloat(self): + with self.cached_session(): + tensor = constant_op.constant([0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7]) + format_output = string_ops.string_format("{}", tensor) + out = self.evaluate(format_output) + expected = "[0 0.1 0.2 ... 0.5 0.6 0.7]" + self.assertEqual(compat.as_text(out), expected) + + @test_util.run_in_graph_and_eager_modes() + def testFormatOneTensorOneDimMatchesSummarize(self): + with self.cached_session(): + tensor = math_ops.range(6) + format_output = string_ops.string_format("{}", tensor, summarize=3) + out = self.evaluate(format_output) + expected = "[0 1 2 3 4 5]" + self.assertEqual(compat.as_text(out), expected) + + @test_util.run_in_graph_and_eager_modes() + def testFormatOneTensorOneDimVarySummarize(self): + with self.cached_session(): + tensor = math_ops.range(6) + format_output = string_ops.string_format("{}", tensor, summarize=-1) + out = self.evaluate(format_output) + expected = "[0 1 2 3 4 5]" + self.assertEqual(compat.as_text(out), expected) + + with self.cached_session(): + tensor = math_ops.range(6) + format_output = string_ops.string_format("{}", tensor, summarize=1) + out = self.evaluate(format_output) + expected = "[0 ... 5]" + self.assertEqual(compat.as_text(out), expected) + + with self.cached_session(): + tensor = math_ops.range(6) + format_output = string_ops.string_format("{}", tensor, summarize=2) + out = self.evaluate(format_output) + expected = "[0 1 ... 4 5]" + self.assertEqual(compat.as_text(out), expected) + + with self.cached_session(): + tensor = math_ops.range(6) + format_output = string_ops.string_format("{}", tensor, summarize=10) + out = self.evaluate(format_output) + expected = "[0 1 2 3 4 5]" + self.assertEqual(compat.as_text(out), expected) + + @test_util.run_in_graph_and_eager_modes() + def testFormatOneTensorOneDimAlmostSummarize(self): + with self.cached_session(): + tensor = math_ops.range(5) + format_output = string_ops.string_format("{}", tensor, summarize=3) + out = self.evaluate(format_output) + expected = "[0 1 2 3 4]" + self.assertEqual(compat.as_text(out), expected) + + @test_util.run_in_graph_and_eager_modes() + def testFormatOneTensorTwoDimLessThanSummarize(self): + with self.cached_session(): + tensor = array_ops.reshape(math_ops.range(4), [2, 2]) + format_output = string_ops.string_format("{}", tensor, summarize=3) + out = self.evaluate(format_output) + expected = ("[[0 1]\n" + " [2 3]]") + self.assertEqual(compat.as_text(out), expected) + + @test_util.run_in_graph_and_eager_modes() + def testFormatOneTensorTwoDim(self): + with self.cached_session(): + tensor = array_ops.reshape(math_ops.range(100), [10, 10]) + format_output = string_ops.string_format("{}", tensor) + out = self.evaluate(format_output) + expected = ("[[0 1 2 ... 7 8 9]\n" + " [10 11 12 ... 17 18 19]\n" + " [20 21 22 ... 27 28 29]\n" + " ...\n" + " [70 71 72 ... 77 78 79]\n" + " [80 81 82 ... 87 88 89]\n" + " [90 91 92 ... 97 98 99]]") + self.assertEqual(compat.as_text(out), expected) + + @test_util.run_in_graph_and_eager_modes() + def testFormatOneTensorTwoDimSummarizeTwo(self): + with self.cached_session(): + tensor = array_ops.reshape(math_ops.range(100), [10, 10]) + format_output = string_ops.string_format("{}", tensor, summarize=2) + out = self.evaluate(format_output) + expected = ("[[0 1 ... 8 9]\n" + " [10 11 ... 18 19]\n" + " ...\n" + " [80 81 ... 88 89]\n" + " [90 91 ... 98 99]]") + self.assertEqual(compat.as_text(out), expected) + + @test_util.run_in_graph_and_eager_modes() + def testFormatOneTensorThreeDim(self): + with self.cached_session(): + tensor = array_ops.reshape(math_ops.range(1000), [10, 10, 10]) + format_output = string_ops.string_format("{}", tensor) + out = self.evaluate(format_output) + expected = ("[[[0 1 2 ... 7 8 9]\n" + " [10 11 12 ... 17 18 19]\n" + " [20 21 22 ... 27 28 29]\n" + " ...\n" + " [70 71 72 ... 77 78 79]\n" + " [80 81 82 ... 87 88 89]\n" + " [90 91 92 ... 97 98 99]]\n" + "\n" + " [[100 101 102 ... 107 108 109]\n" + " [110 111 112 ... 117 118 119]\n" + " [120 121 122 ... 127 128 129]\n" + " ...\n [170 171 172 ... 177 178 179]\n" + " [180 181 182 ... 187 188 189]\n" + " [190 191 192 ... 197 198 199]]\n" + "\n" + " [[200 201 202 ... 207 208 209]\n" + " [210 211 212 ... 217 218 219]\n" + " [220 221 222 ... 227 228 229]\n" + " ...\n" + " [270 271 272 ... 277 278 279]\n" + " [280 281 282 ... 287 288 289]\n" + " [290 291 292 ... 297 298 299]]\n" + "\n" + " ...\n" + "\n" + " [[700 701 702 ... 707 708 709]\n" + " [710 711 712 ... 717 718 719]\n" + " [720 721 722 ... 727 728 729]\n" + " ...\n" + " [770 771 772 ... 777 778 779]\n" + " [780 781 782 ... 787 788 789]\n" + " [790 791 792 ... 797 798 799]]\n" + "\n" + " [[800 801 802 ... 807 808 809]\n" + " [810 811 812 ... 817 818 819]\n" + " [820 821 822 ... 827 828 829]\n" + " ...\n" + " [870 871 872 ... 877 878 879]\n" + " [880 881 882 ... 887 888 889]\n" + " [890 891 892 ... 897 898 899]]\n" + "\n" + " [[900 901 902 ... 907 908 909]\n" + " [910 911 912 ... 917 918 919]\n" + " [920 921 922 ... 927 928 929]\n" + " ...\n" + " [970 971 972 ... 977 978 979]\n" + " [980 981 982 ... 987 988 989]\n" + " [990 991 992 ... 997 998 999]]]") + self.assertEqual(compat.as_text(out), expected) + + @test_util.run_in_graph_and_eager_modes() + def testFormatOneTensorTemplatePrefix(self): + with self.cached_session(): + tensor = array_ops.reshape(math_ops.range(100), [10, 10]) + format_output = string_ops.string_format("tensor summary: {}", tensor) + out = self.evaluate(format_output) + expected = ("tensor summary: [[0 1 2 ... 7 8 9]\n" + " [10 11 12 ... 17 18 19]\n" + " [20 21 22 ... 27 28 29]\n" + " ...\n" + " [70 71 72 ... 77 78 79]\n" + " [80 81 82 ... 87 88 89]\n" + " [90 91 92 ... 97 98 99]]") + self.assertEqual(compat.as_text(out), expected) + + @test_util.run_in_graph_and_eager_modes() + def testFormatOneTensorTemplatePrefixAndSuffix(self): + with self.cached_session(): + tensor = array_ops.reshape(math_ops.range(100), [10, 10]) + format_output = string_ops.string_format("tensor summary: {}, suffix", + tensor) + out = self.evaluate(format_output) + expected = ("tensor summary: [[0 1 2 ... 7 8 9]\n" + " [10 11 12 ... 17 18 19]\n" + " [20 21 22 ... 27 28 29]\n" + " ...\n" + " [70 71 72 ... 77 78 79]\n" + " [80 81 82 ... 87 88 89]\n" + " [90 91 92 ... 97 98 99]], suffix") + self.assertEqual(compat.as_text(out), expected) + + @test_util.run_in_graph_and_eager_modes() + def testFormatOneTensorTemplateSuffix(self): + with self.cached_session(): + tensor = array_ops.reshape(math_ops.range(100), [10, 10]) + format_output = string_ops.string_format("{}, suffix", tensor) + out = self.evaluate(format_output) + expected = ("[[0 1 2 ... 7 8 9]\n" + " [10 11 12 ... 17 18 19]\n" + " [20 21 22 ... 27 28 29]\n" + " ...\n" + " [70 71 72 ... 77 78 79]\n" + " [80 81 82 ... 87 88 89]\n" + " [90 91 92 ... 97 98 99]], suffix") + self.assertEqual(compat.as_text(out), expected) + + @test_util.run_in_graph_and_eager_modes() + def testFormatNoTensor(self): + with self.cached_session(): + format_output = string_ops.string_format("No tensor.", ()) + out = self.evaluate(format_output) + expected = "No tensor." + self.assertEqual(compat.as_text(out), expected) + + @test_util.run_in_graph_and_eager_modes() + def testFormatMultiTensor(self): + with self.cached_session(): + tensor_one = array_ops.reshape(math_ops.range(100), [10, 10]) + tensor_two = tensor_one * 10 + format_output = string_ops.string_format("One: {},\nTwo: {}", + (tensor_one, tensor_two)) + out = self.evaluate(format_output) + expected = ("One: [[0 1 2 ... 7 8 9]\n" + " [10 11 12 ... 17 18 19]\n" + " [20 21 22 ... 27 28 29]\n" + " ...\n" + " [70 71 72 ... 77 78 79]\n" + " [80 81 82 ... 87 88 89]\n" + " [90 91 92 ... 97 98 99]],\n" + "Two: [[0 10 20 ... 70 80 90]\n" + " [100 110 120 ... 170 180 190]\n" + " [200 210 220 ... 270 280 290]\n" + " ...\n" + " [700 710 720 ... 770 780 790]\n" + " [800 810 820 ... 870 880 890]\n" + " [900 910 920 ... 970 980 990]]") + self.assertEqual(compat.as_text(out), expected) + + @test_util.run_in_graph_and_eager_modes() + def testFormatSummarizeOne(self): + with self.cached_session(): + tensor = array_ops.reshape(math_ops.range(100), [10, 10]) + format_output = string_ops.string_format("tensor summary: {}", tensor, + summarize=1) + out = self.evaluate(format_output) + expected = ("tensor summary: [[0 ... 9]\n" + " ...\n" + " [90 ... 99]]") + self.assertEqual(compat.as_text(out), expected) + + @test_util.run_in_graph_and_eager_modes() + def testFormatSummarizeTwo(self): + with self.cached_session(): + tensor = array_ops.reshape(math_ops.range(100), [10, 10]) + format_output = string_ops.string_format("tensor summary: {}", tensor, + summarize=2) + out = self.evaluate(format_output) + expected = ("tensor summary: [[0 1 ... 8 9]\n" + " [10 11 ... 18 19]\n" + " ...\n" + " [80 81 ... 88 89]\n" + " [90 91 ... 98 99]]") + self.assertEqual(compat.as_text(out), expected) + + @test_util.run_in_graph_and_eager_modes() + def testFormatPlaceholder(self): + with self.cached_session(): + tensor = array_ops.reshape(math_ops.range(100), [10, 10]) + format_output = string_ops.string_format("tensor summary: %t%", tensor, + placeholder="%t%") + out = self.evaluate(format_output) + expected = ("tensor summary: [[0 1 2 ... 7 8 9]\n" + " [10 11 12 ... 17 18 19]\n" + " [20 21 22 ... 27 28 29]\n" + " ...\n" + " [70 71 72 ... 77 78 79]\n" + " [80 81 82 ... 87 88 89]\n" + " [90 91 92 ... 97 98 99]]") + self.assertEqual(compat.as_text(out), expected) + + @test_util.run_in_graph_and_eager_modes() + def testTensorCountMustMatchPlaceholderCount(self): + with self.cached_session(): + with self.assertRaisesRegexp( + ValueError, r"2 placeholder\(s\) in template does not match 1 " + r"tensor\(s\) provided as input"): + tensor = math_ops.range(10) + format_output = string_ops.string_format("{} {}", tensor) + self.evaluate(format_output) + with self.cached_session(): + with self.assertRaisesRegexp( + ValueError, r"2 placeholder\(s\) in template does not match 1 " + r"tensor\(s\) provided as input"): + tensor = math_ops.range(10) + format_output = string_ops.string_format("{} {}", [tensor]) + self.evaluate(format_output) + with self.cached_session(): + with self.assertRaisesRegexp( + ValueError, r"1 placeholder\(s\) in template does not match 2 " + r"tensor\(s\) provided as input"): + tensor = math_ops.range(10) + format_output = string_ops.string_format("{}", (tensor, tensor)) + self.evaluate(format_output) + + +if __name__ == "__main__": + test.main() diff --git a/tensorflow/python/kernel_tests/string_join_op_test.py b/tensorflow/python/kernel_tests/string_join_op_test.py index ce19333654..e4371ab5b9 100644 --- a/tensorflow/python/kernel_tests/string_join_op_test.py +++ b/tensorflow/python/kernel_tests/string_join_op_test.py @@ -28,7 +28,7 @@ class StringJoinOpTest(test.TestCase): input1 = "a" input2 = [["b"], ["c"]] - with self.test_session(): + with self.cached_session(): output = string_ops.string_join([input0, input1]) self.assertAllEqual(output.eval(), [b"aa", b"ba"]) diff --git a/tensorflow/python/kernel_tests/string_length_op_test.py b/tensorflow/python/kernel_tests/string_length_op_test.py index 075a3204ad..4afe3ad3f4 100644 --- a/tensorflow/python/kernel_tests/string_length_op_test.py +++ b/tensorflow/python/kernel_tests/string_length_op_test.py @@ -27,11 +27,38 @@ class StringLengthOpTest(test.TestCase): def testStringLength(self): strings = [[["1", "12"], ["123", "1234"], ["12345", "123456"]]] - with self.test_session() as sess: + with self.cached_session() as sess: lengths = string_ops.string_length(strings) values = sess.run(lengths) self.assertAllEqual(values, [[[1, 2], [3, 4], [5, 6]]]) + def testUnit(self): + unicode_strings = [u"H\xc3llo", u"\U0001f604"] + utf8_strings = [s.encode("utf-8") for s in unicode_strings] + expected_utf8_byte_lengths = [6, 4] + expected_utf8_char_lengths = [5, 1] + + with self.test_session() as sess: + utf8_byte_lengths = string_ops.string_length(utf8_strings, unit="BYTE") + utf8_char_lengths = string_ops.string_length( + utf8_strings, unit="UTF8_CHAR") + self.assertAllEqual( + sess.run(utf8_byte_lengths), expected_utf8_byte_lengths) + self.assertAllEqual( + sess.run(utf8_char_lengths), expected_utf8_char_lengths) + with self.assertRaisesRegexp( + ValueError, "Attr 'unit' of 'StringLength' Op passed string 'XYZ' " + 'not in: "BYTE", "UTF8_CHAR"'): + string_ops.string_length(utf8_strings, unit="XYZ") + + def testLegacyPositionalName(self): + # Code that predates the 'unit' parameter may have used a positional + # argument for the 'name' parameter. Check that we don't break such code. + strings = [[["1", "12"], ["123", "1234"], ["12345", "123456"]]] + lengths = string_ops.string_length(strings, "some_name") + with self.test_session(): + self.assertAllEqual(lengths.eval(), [[[1, 2], [3, 4], [5, 6]]]) + if __name__ == "__main__": test.main() diff --git a/tensorflow/python/kernel_tests/string_split_op_test.py b/tensorflow/python/kernel_tests/string_split_op_test.py index b6a0f45adc..b968e885ed 100644 --- a/tensorflow/python/kernel_tests/string_split_op_test.py +++ b/tensorflow/python/kernel_tests/string_split_op_test.py @@ -32,7 +32,7 @@ class StringSplitOpTest(test.TestCase): def testStringSplit(self): strings = ["pigs on the wing", "animals"] - with self.test_session() as sess: + with self.cached_session() as sess: tokens = string_ops.string_split(strings) indices, values, shape = sess.run(tokens) self.assertAllEqual(indices, [[0, 0], [0, 1], [0, 2], [0, 3], [1, 0]]) @@ -42,7 +42,7 @@ class StringSplitOpTest(test.TestCase): def testStringSplitEmptyDelimiter(self): strings = ["hello", "hola", b"\xF0\x9F\x98\x8E"] # Last string is U+1F60E - with self.test_session() as sess: + with self.cached_session() as sess: tokens = string_ops.string_split(strings, delimiter="") indices, values, shape = sess.run(tokens) self.assertAllEqual(indices, [[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], @@ -60,7 +60,7 @@ class StringSplitOpTest(test.TestCase): def testStringSplitEmptyToken(self): strings = ["", " a", "b ", " c", " ", " d ", " e", "f ", " g ", " "] - with self.test_session() as sess: + with self.cached_session() as sess: tokens = string_ops.string_split(strings) indices, values, shape = sess.run(tokens) self.assertAllEqual( @@ -72,7 +72,7 @@ class StringSplitOpTest(test.TestCase): def testStringSplitOnSetEmptyToken(self): strings = ["", " a", "b ", " c", " ", " d ", ". e", "f .", " .g. ", " ."] - with self.test_session() as sess: + with self.cached_session() as sess: tokens = string_ops.string_split(strings, delimiter=" .") indices, values, shape = sess.run(tokens) self.assertAllEqual( @@ -84,7 +84,7 @@ class StringSplitOpTest(test.TestCase): def testStringSplitWithDelimiter(self): strings = ["hello|world", "hello world"] - with self.test_session() as sess: + with self.cached_session() as sess: self.assertRaises( ValueError, string_ops.string_split, strings, delimiter=["|", ""]) @@ -106,7 +106,7 @@ class StringSplitOpTest(test.TestCase): def testStringSplitWithDelimiterTensor(self): strings = ["hello|world", "hello world"] - with self.test_session() as sess: + with self.cached_session() as sess: delimiter = array_ops.placeholder(dtypes.string) tokens = string_ops.string_split(strings, delimiter=delimiter) @@ -124,7 +124,7 @@ class StringSplitOpTest(test.TestCase): def testStringSplitWithDelimitersTensor(self): strings = ["hello.cruel,world", "hello cruel world"] - with self.test_session() as sess: + with self.cached_session() as sess: delimiter = array_ops.placeholder(dtypes.string) tokens = string_ops.string_split(strings, delimiter=delimiter) @@ -143,7 +143,7 @@ class StringSplitOpTest(test.TestCase): def testStringSplitWithNoSkipEmpty(self): strings = ["#a", "b#", "#c#"] - with self.test_session() as sess: + with self.cached_session() as sess: tokens = string_ops.string_split(strings, "#", skip_empty=False) indices, values, shape = sess.run(tokens) self.assertAllEqual(indices, [[0, 0], [0, 1], @@ -152,7 +152,7 @@ class StringSplitOpTest(test.TestCase): self.assertAllEqual(values, [b"", b"a", b"b", b"", b"", b"c", b""]) self.assertAllEqual(shape, [3, 3]) - with self.test_session() as sess: + with self.cached_session() as sess: tokens = string_ops.string_split(strings, "#") indices, values, shape = sess.run(tokens) self.assertAllEqual(values, [b"a", b"b", b"c"]) @@ -165,7 +165,7 @@ class StringSplitV2OpTest(test.TestCase): def testSplitV2(self): strings = ["pigs on the wing", "animals"] - with self.test_session() as sess: + with self.cached_session() as sess: tokens = string_ops.string_split_v2(strings) indices, values, shape = sess.run(tokens) self.assertAllEqual(indices, [[0, 0], [0, 1], [0, 2], [0, 3], [1, 0]]) @@ -180,7 +180,7 @@ class StringSplitV2OpTest(test.TestCase): # ['', '', '4', '5', '', '6', ''] strings = ["1<>2<>3", "<><>4<>5<><>6<>"] - with self.test_session() as sess: + with self.cached_session() as sess: tokens = string_ops.string_split_v2(strings, sep="<>") indices, values, shape = sess.run(tokens) self.assertAllEqual( @@ -198,7 +198,7 @@ class StringSplitV2OpTest(test.TestCase): # ['1', '2', '', '3', ''] strings = ["1,2,3", "4,5,,6,"] - with self.test_session() as sess: + with self.cached_session() as sess: tokens = string_ops.string_split_v2(strings, sep=',') indices, values, shape = sess.run(tokens) self.assertAllEqual(indices, [[0, 0], [0, 1], [0, 2], @@ -215,7 +215,7 @@ class StringSplitV2OpTest(test.TestCase): #['1', '2', '3'] strings = ["1 2 3", " 4 5 6 "] - with self.test_session() as sess: + with self.cached_session() as sess: tokens = string_ops.string_split_v2(strings) indices, values, shape = sess.run(tokens) self.assertAllEqual(indices, [[0, 0], [0, 1], [0, 2], @@ -231,7 +231,7 @@ class StringSplitV2OpTest(test.TestCase): # ['4', '5,,6,'] strings = ["1,2,3", "4,5,,6,"] - with self.test_session() as sess: + with self.cached_session() as sess: tokens = string_ops.string_split_v2(strings, sep=',', maxsplit=1) indices, values, shape = sess.run(tokens) self.assertAllEqual(indices, [[0, 0], [0, 1], @@ -247,7 +247,7 @@ class StringSplitV2OpTest(test.TestCase): # ['4', '5 6 '] strings = ["1 2 3", " 4 5 6 "] - with self.test_session() as sess: + with self.cached_session() as sess: tokens = string_ops.string_split_v2(strings, maxsplit=1) indices, values, shape = sess.run(tokens) self.assertAllEqual(indices, [[0, 0], [0, 1], diff --git a/tensorflow/python/kernel_tests/string_strip_op_test.py b/tensorflow/python/kernel_tests/string_strip_op_test.py index 30fd477ff4..a96b71490e 100644 --- a/tensorflow/python/kernel_tests/string_strip_op_test.py +++ b/tensorflow/python/kernel_tests/string_strip_op_test.py @@ -28,7 +28,7 @@ class StringStripOpTest(test.TestCase): def test_string_strip(self): strings = ["pigs on the wing", "animals"] - with self.test_session() as sess: + with self.cached_session() as sess: output = string_ops.string_strip(strings) output = sess.run(output) self.assertAllEqual(output, [b"pigs on the wing", b"animals"]) @@ -37,7 +37,7 @@ class StringStripOpTest(test.TestCase): strings = [["pigs on the wing", "animals"], [" hello ", "\n\tworld \r \n"]] - with self.test_session() as sess: + with self.cached_session() as sess: output = string_ops.string_strip(strings) output = sess.run(output) self.assertAllEqual(output, [[b"pigs on the wing", b"animals"], @@ -46,7 +46,7 @@ class StringStripOpTest(test.TestCase): def test_string_strip_with_empty_strings(self): strings = [" hello ", "", "world ", " \t \r \n "] - with self.test_session() as sess: + with self.cached_session() as sess: output = string_ops.string_strip(strings) output = sess.run(output) self.assertAllEqual(output, [b"hello", b"", b"world", b""]) diff --git a/tensorflow/python/kernel_tests/string_to_hash_bucket_op_test.py b/tensorflow/python/kernel_tests/string_to_hash_bucket_op_test.py index 2c6064e64b..9cb0c9d18f 100644 --- a/tensorflow/python/kernel_tests/string_to_hash_bucket_op_test.py +++ b/tensorflow/python/kernel_tests/string_to_hash_bucket_op_test.py @@ -27,7 +27,7 @@ from tensorflow.python.platform import test class StringToHashBucketOpTest(test.TestCase): def testStringToOneHashBucketFast(self): - with self.test_session(): + with self.cached_session(): input_string = array_ops.placeholder(dtypes.string) output = string_ops.string_to_hash_bucket_fast(input_string, 1) result = output.eval(feed_dict={input_string: ['a', 'b', 'c']}) @@ -35,7 +35,7 @@ class StringToHashBucketOpTest(test.TestCase): self.assertAllEqual([0, 0, 0], result) def testStringToHashBucketsFast(self): - with self.test_session(): + with self.cached_session(): input_string = array_ops.placeholder(dtypes.string) output = string_ops.string_to_hash_bucket_fast(input_string, 10) result = output.eval(feed_dict={input_string: ['a', 'b', 'c', 'd']}) @@ -47,7 +47,7 @@ class StringToHashBucketOpTest(test.TestCase): self.assertAllEqual([9, 2, 2, 5], result) def testStringToOneHashBucketLegacyHash(self): - with self.test_session(): + with self.cached_session(): input_string = array_ops.placeholder(dtypes.string) output = string_ops.string_to_hash_bucket(input_string, 1) result = output.eval(feed_dict={input_string: ['a', 'b', 'c']}) @@ -55,7 +55,7 @@ class StringToHashBucketOpTest(test.TestCase): self.assertAllEqual([0, 0, 0], result) def testStringToHashBucketsLegacyHash(self): - with self.test_session(): + with self.cached_session(): input_string = array_ops.placeholder(dtypes.string) output = string_ops.string_to_hash_bucket(input_string, 10) result = output.eval(feed_dict={input_string: ['a', 'b', 'c']}) @@ -66,14 +66,14 @@ class StringToHashBucketOpTest(test.TestCase): self.assertAllEqual([8, 0, 7], result) def testStringToOneHashBucketStrongOneHashBucket(self): - with self.test_session(): + with self.cached_session(): input_string = constant_op.constant(['a', 'b', 'c']) output = string_ops.string_to_hash_bucket_strong( input_string, 1, key=[123, 345]) self.assertAllEqual([0, 0, 0], output.eval()) def testStringToHashBucketsStrong(self): - with self.test_session(): + with self.cached_session(): input_string = constant_op.constant(['a', 'b', 'c']) output = string_ops.string_to_hash_bucket_strong( input_string, 10, key=[98765, 132]) @@ -84,7 +84,7 @@ class StringToHashBucketOpTest(test.TestCase): self.assertAllEqual([4, 2, 8], output.eval()) def testStringToHashBucketsStrongInvalidKey(self): - with self.test_session(): + with self.cached_session(): input_string = constant_op.constant(['a', 'b', 'c']) with self.assertRaisesOpError('Key must have 2 elements'): string_ops.string_to_hash_bucket_strong( diff --git a/tensorflow/python/kernel_tests/string_to_number_op_test.py b/tensorflow/python/kernel_tests/string_to_number_op_test.py index cc4c21b66c..99ee25e125 100644 --- a/tensorflow/python/kernel_tests/string_to_number_op_test.py +++ b/tensorflow/python/kernel_tests/string_to_number_op_test.py @@ -29,7 +29,7 @@ _ERROR_MESSAGE = "StringToNumberOp could not correctly convert string: " class StringToNumberOpTest(test.TestCase): def _test(self, tf_type, good_pairs, bad_pairs): - with self.test_session(): + with self.cached_session(): # Build a small testing graph. input_string = array_ops.placeholder(dtypes.string) output = parsing_ops.string_to_number( diff --git a/tensorflow/python/kernel_tests/substr_op_test.py b/tensorflow/python/kernel_tests/substr_op_test.py index 73ac71e1f5..cd3fe14883 100644 --- a/tensorflow/python/kernel_tests/substr_op_test.py +++ b/tensorflow/python/kernel_tests/substr_op_test.py @@ -18,6 +18,7 @@ from __future__ import absolute_import from __future__ import division from __future__ import print_function +from absl.testing import parameterized import numpy as np from tensorflow.python.framework import errors_impl @@ -25,7 +26,7 @@ from tensorflow.python.ops import string_ops from tensorflow.python.platform import test -class SubstrOpTest(test.TestCase): +class SubstrOpTest(test.TestCase, parameterized.TestCase): def _testScalarString(self, dtype): test_string = b"Hello" @@ -34,18 +35,40 @@ class SubstrOpTest(test.TestCase): expected_value = b"ell" substr_op = string_ops.substr(test_string, position, length) - with self.test_session(): + with self.cached_session(): substr = substr_op.eval() self.assertAllEqual(substr, expected_value) - # position is equal to the length of string. + # Negative position. + test_string = b"Hello" + position = np.array(-4, dtype) + length = np.array(3, dtype) + expected_value = b"ell" + + substr_op = string_ops.substr(test_string, position, length) + with self.cached_session(): + substr = substr_op.eval() + self.assertAllEqual(substr, expected_value) + + # Position is equal to the length of string. test_string = b"" position = np.array(0, dtype) length = np.array(2, dtype) expected_value = b"" substr_op = string_ops.substr(test_string, position, length) - with self.test_session(): + with self.cached_session(): + substr = substr_op.eval() + self.assertAllEqual(substr, expected_value) + + # Negative position magnitude is equal to the length of string. + test_string = b"yo" + position = np.array(-2, dtype) + length = np.array(1, dtype) + expected_value = b"y" + + substr_op = string_ops.substr(test_string, position, length) + with self.cached_session(): substr = substr_op.eval() self.assertAllEqual(substr, expected_value) @@ -56,7 +79,18 @@ class SubstrOpTest(test.TestCase): expected_value = [b"ell", b"orl"] substr_op = string_ops.substr(test_string, position, length) - with self.test_session(): + with self.cached_session(): + substr = substr_op.eval() + self.assertAllEqual(substr, expected_value) + + # Negative position. + test_string = [b"Hello", b"World"] + position = np.array(-4, dtype) + length = np.array(3, dtype) + expected_value = [b"ell", b"orl"] + + substr_op = string_ops.substr(test_string, position, length) + with self.cached_session(): substr = substr_op.eval() self.assertAllEqual(substr, expected_value) @@ -70,7 +104,21 @@ class SubstrOpTest(test.TestCase): [b"ixte", b"even", b"ight"]] substr_op = string_ops.substr(test_string, position, length) - with self.test_session(): + with self.cached_session(): + substr = substr_op.eval() + self.assertAllEqual(substr, expected_value) + + # Negative position + test_string = [[b"ten", b"eleven", b"twelve"], + [b"thirteen", b"fourteen", b"fifteen"], + [b"sixteen", b"seventeen", b"eighteen"]] + position = np.array(-2, dtype) + length = np.array(2, dtype) + expected_value = [[b"en", b"en", b"ve"], [b"en", b"en", b"en"], + [b"en", b"en", b"en"]] + + substr_op = string_ops.substr(test_string, position, length) + with self.cached_session(): substr = substr_op.eval() self.assertAllEqual(substr, expected_value) @@ -78,13 +126,13 @@ class SubstrOpTest(test.TestCase): test_string = [[b"ten", b"eleven", b"twelve"], [b"thirteen", b"fourteen", b"fifteen"], [b"sixteen", b"seventeen", b"eighteen"]] - position = np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3]], dtype) - length = np.array([[2, 3, 4], [4, 3, 2], [5, 5, 5]], dtype) - expected_value = [[b"en", b"eve", b"lve"], [b"hirt", b"urt", b"te"], - [b"ixtee", b"vente", b"hteen"]] + position = np.array([[1, -4, 3], [1, 2, -4], [-5, 2, 3]], dtype) + length = np.array([[2, 2, 4], [4, 3, 2], [5, 5, 5]], dtype) + expected_value = [[b"en", b"ev", b"lve"], [b"hirt", b"urt", b"te"], + [b"xteen", b"vente", b"hteen"]] substr_op = string_ops.substr(test_string, position, length) - with self.test_session(): + with self.cached_session(): substr = substr_op.eval() self.assertAllEqual(substr, expected_value) @@ -94,33 +142,33 @@ class SubstrOpTest(test.TestCase): [b"thirteen", b"fourteen", b"fifteen"], [b"sixteen", b"seventeen", b"eighteen"], [b"nineteen", b"twenty", b"twentyone"]] - position = np.array([1, 2, 3], dtype) + position = np.array([1, -4, 3], dtype) length = np.array([1, 2, 3], dtype) - expected_value = [[b"e", b"ev", b"lve"], [b"h", b"ur", b"tee"], - [b"i", b"ve", b"hte"], [b"i", b"en", b"nty"]] + expected_value = [[b"e", b"ev", b"lve"], [b"h", b"te", b"tee"], + [b"i", b"te", b"hte"], [b"i", b"en", b"nty"]] substr_op = string_ops.substr(test_string, position, length) - with self.test_session(): + with self.cached_session(): substr = substr_op.eval() self.assertAllEqual(substr, expected_value) # Broadcast input string onto pos/len test_string = [b"thirteen", b"fourteen", b"fifteen"] - position = np.array([[1, 2, 3], [3, 2, 1], [5, 5, 5]], dtype) + position = np.array([[1, -2, 3], [-3, 2, 1], [5, 5, -5]], dtype) length = np.array([[3, 2, 1], [1, 2, 3], [2, 2, 2]], dtype) - expected_value = [[b"hir", b"ur", b"t"], [b"r", b"ur", b"ift"], - [b"ee", b"ee", b"en"]] + expected_value = [[b"hir", b"en", b"t"], [b"e", b"ur", b"ift"], + [b"ee", b"ee", b"ft"]] substr_op = string_ops.substr(test_string, position, length) - with self.test_session(): + with self.cached_session(): substr = substr_op.eval() self.assertAllEqual(substr, expected_value) # Test 1D broadcast test_string = b"thirteen" - position = np.array([1, 5, 7], dtype) + position = np.array([1, -5, 7], dtype) length = np.array([3, 2, 1], dtype) - expected_value = [b"hir", b"ee", b"n"] + expected_value = [b"hir", b"rt", b"n"] substr_op = string_ops.substr(test_string, position, length) - with self.test_session(): + with self.cached_session(): substr = substr_op.eval() self.assertAllEqual(substr, expected_value) @@ -128,10 +176,8 @@ class SubstrOpTest(test.TestCase): test_string = [[b"ten", b"eleven", b"twelve"], [b"thirteen", b"fourteen", b"fifteen"], [b"sixteen", b"seventeen", b"eighteen"]] - position = np.array([1, 2, 3, 4], dtype) + position = np.array([1, 2, -3, 4], dtype) length = np.array([1, 2, 3, 4], dtype) - expected_value = [[b"e", b"ev", b"lve"], [b"h", b"ur", b"tee"], - [b"i", b"ve", b"hte"]] with self.assertRaises(ValueError): substr_op = string_ops.substr(test_string, position, length) @@ -141,7 +187,16 @@ class SubstrOpTest(test.TestCase): position = np.array(7, dtype) length = np.array(3, dtype) substr_op = string_ops.substr(test_string, position, length) - with self.test_session(): + with self.cached_session(): + with self.assertRaises(errors_impl.InvalidArgumentError): + substr = substr_op.eval() + + # Scalar/Scalar (with negative) + test_string = b"Hello" + position = np.array(-7, dtype) + length = np.array(3, dtype) + substr_op = string_ops.substr(test_string, position, length) + with self.cached_session(): with self.assertRaises(errors_impl.InvalidArgumentError): substr = substr_op.eval() @@ -150,16 +205,16 @@ class SubstrOpTest(test.TestCase): position = np.array(4, dtype) length = np.array(1, dtype) substr_op = string_ops.substr(test_string, position, length) - with self.test_session(): + with self.cached_session(): with self.assertRaises(errors_impl.InvalidArgumentError): substr = substr_op.eval() - # Negative pos - test_string = b"Hello" - position = np.array(-1, dtype) - length = np.array(3, dtype) + # Vector/Scalar (with negative) + test_string = [b"good", b"good", b"bad", b"good"] + position = np.array(-4, dtype) + length = np.array(1, dtype) substr_op = string_ops.substr(test_string, position, length) - with self.test_session(): + with self.cached_session(): with self.assertRaises(errors_impl.InvalidArgumentError): substr = substr_op.eval() @@ -169,7 +224,17 @@ class SubstrOpTest(test.TestCase): position = np.array([[1, 2, 3], [1, 2, 4], [1, 2, 3]], dtype) length = np.array([[3, 2, 1], [1, 2, 3], [2, 2, 2]], dtype) substr_op = string_ops.substr(test_string, position, length) - with self.test_session(): + with self.cached_session(): + with self.assertRaises(errors_impl.InvalidArgumentError): + substr = substr_op.eval() + + # Matrix/Matrix (with negative) + test_string = [[b"good", b"good", b"good"], [b"good", b"good", b"bad"], + [b"good", b"good", b"good"]] + position = np.array([[1, 2, -3], [1, 2, -4], [1, 2, -3]], dtype) + length = np.array([[3, 2, 1], [1, 2, 3], [2, 2, 2]], dtype) + substr_op = string_ops.substr(test_string, position, length) + with self.cached_session(): with self.assertRaises(errors_impl.InvalidArgumentError): substr = substr_op.eval() @@ -178,7 +243,16 @@ class SubstrOpTest(test.TestCase): position = np.array([1, 2, 4], dtype) length = np.array([1, 2, 3], dtype) substr_op = string_ops.substr(test_string, position, length) - with self.test_session(): + with self.cached_session(): + with self.assertRaises(errors_impl.InvalidArgumentError): + substr = substr_op.eval() + + # Broadcast (with negative) + test_string = [[b"good", b"good", b"good"], [b"good", b"good", b"bad"]] + position = np.array([-1, -2, -4], dtype) + length = np.array([1, 2, 3], dtype) + substr_op = string_ops.substr(test_string, position, length) + with self.cached_session(): with self.assertRaises(errors_impl.InvalidArgumentError): substr = substr_op.eval() @@ -198,7 +272,18 @@ class SubstrOpTest(test.TestCase): with self.assertRaises(ValueError): substr_op = string_ops.substr(test_string, position, length) - def _testAll(self, dtype): + # Negative position. + test_string = [[b"ten", b"eleven", b"twelve"], + [b"thirteen", b"fourteen", b"fifteen"], + [b"sixteen", b"seventeen", b"eighteen"]] + position = np.array([[-1, -2, -3]], dtype) + length = np.array([1, 2, 3], dtype) + # Should fail: position/length have different rank + with self.assertRaises(ValueError): + substr_op = string_ops.substr(test_string, position, length) + + @parameterized.parameters(np.int32, np.int64) + def testAll(self, dtype): self._testScalarString(dtype) self._testVectorStrings(dtype) self._testMatrixStrings(dtype) @@ -208,14 +293,8 @@ class SubstrOpTest(test.TestCase): self._testOutOfRangeError(dtype) self._testMismatchPosLenShapes(dtype) - def testInt32(self): - self._testAll(np.int32) - - def testInt64(self): - self._testAll(np.int64) - def testWrongDtype(self): - with self.test_session(): + with self.cached_session(): with self.assertRaises(TypeError): string_ops.substr(b"test", 3.0, 1) with self.assertRaises(TypeError): diff --git a/tensorflow/python/kernel_tests/summary_audio_op_test.py b/tensorflow/python/kernel_tests/summary_audio_op_test.py index eaae671192..e59a2ceef7 100644 --- a/tensorflow/python/kernel_tests/summary_audio_op_test.py +++ b/tensorflow/python/kernel_tests/summary_audio_op_test.py @@ -50,7 +50,7 @@ class SummaryAudioOpTest(test.TestCase): def testAudioSummary(self): np.random.seed(7) for channels in (1, 2, 5, 8): - with self.test_session(graph=ops.Graph()) as sess: + with self.session(graph=ops.Graph()) as sess: num_frames = 7 shape = (4, num_frames, channels) # Generate random audio in the range [-1.0, 1.0). diff --git a/tensorflow/python/kernel_tests/summary_image_op_test.py b/tensorflow/python/kernel_tests/summary_image_op_test.py index 4718827e88..b650e10404 100644 --- a/tensorflow/python/kernel_tests/summary_image_op_test.py +++ b/tensorflow/python/kernel_tests/summary_image_op_test.py @@ -52,7 +52,7 @@ class SummaryImageOpTest(test.TestCase): def testImageSummary(self): for depth in (1, 3, 4): for positive in False, True: - with self.test_session(graph=ops.Graph()) as sess: + with self.session(graph=ops.Graph()) as sess: shape = (4, 5, 7) + (depth,) bad_color = [255, 0, 0, 255][:depth] # Build a mostly random image with one nan @@ -87,7 +87,7 @@ class SummaryImageOpTest(test.TestCase): def testImageSummaryUint8(self): np.random.seed(7) for depth in (1, 3, 4): - with self.test_session(graph=ops.Graph()) as sess: + with self.session(graph=ops.Graph()) as sess: shape = (4, 5, 7) + (depth,) # Build a random uint8 image diff --git a/tensorflow/python/kernel_tests/summary_ops_test.py b/tensorflow/python/kernel_tests/summary_ops_test.py index 2da7107f61..0c500120b0 100644 --- a/tensorflow/python/kernel_tests/summary_ops_test.py +++ b/tensorflow/python/kernel_tests/summary_ops_test.py @@ -34,7 +34,7 @@ class SummaryOpsTest(test.TestCase): return summ def testScalarSummary(self): - with self.test_session() as sess: + with self.cached_session() as sess: const = constant_op.constant([10.0, 20.0]) summ = logging_ops.scalar_summary(["c1", "c2"], const, name="mysumm") value = sess.run(summ) @@ -45,7 +45,7 @@ class SummaryOpsTest(test.TestCase): """, self._AsSummary(value)) def testScalarSummaryDefaultName(self): - with self.test_session() as sess: + with self.cached_session() as sess: const = constant_op.constant([10.0, 20.0]) summ = logging_ops.scalar_summary(["c1", "c2"], const) value = sess.run(summ) @@ -56,7 +56,7 @@ class SummaryOpsTest(test.TestCase): """, self._AsSummary(value)) def testMergeSummary(self): - with self.test_session() as sess: + with self.cached_session() as sess: const = constant_op.constant(10.0) summ1 = summary.histogram("h", const) summ2 = logging_ops.scalar_summary("c", const) diff --git a/tensorflow/python/kernel_tests/summary_tensor_op_test.py b/tensorflow/python/kernel_tests/summary_tensor_op_test.py index d534aadb79..0f4643393a 100644 --- a/tensorflow/python/kernel_tests/summary_tensor_op_test.py +++ b/tensorflow/python/kernel_tests/summary_tensor_op_test.py @@ -42,7 +42,7 @@ class SummaryOpsTest(test.TestCase): self.assertTrue(np.array_equal(actual, expected)) def testTags(self): - with self.test_session() as sess: + with self.cached_session() as sess: c = constant_op.constant(1) s1 = summary_ops.tensor_summary("s1", c) with ops.name_scope("foo"): @@ -65,7 +65,7 @@ class SummaryOpsTest(test.TestCase): self.assertEqual(v4.tag, "foo/zod/TensorSummary") def testScalarSummary(self): - with self.test_session() as sess: + with self.cached_session() as sess: const = constant_op.constant(10.0) summ = summary_ops.tensor_summary("foo", const) result = sess.run(summ) @@ -76,7 +76,7 @@ class SummaryOpsTest(test.TestCase): def testStringSummary(self): s = six.b("foobar") - with self.test_session() as sess: + with self.cached_session() as sess: const = constant_op.constant(s) summ = summary_ops.tensor_summary("foo", const) result = sess.run(summ) @@ -86,7 +86,7 @@ class SummaryOpsTest(test.TestCase): self._AssertNumpyEq(n, s) def testManyScalarSummary(self): - with self.test_session() as sess: + with self.cached_session() as sess: const = array_ops.ones([5, 5, 5]) summ = summary_ops.tensor_summary("foo", const) result = sess.run(summ) @@ -96,7 +96,7 @@ class SummaryOpsTest(test.TestCase): def testManyStringSummary(self): strings = [[six.b("foo bar"), six.b("baz")], [six.b("zoink"), six.b("zod")]] - with self.test_session() as sess: + with self.cached_session() as sess: const = constant_op.constant(strings) summ = summary_ops.tensor_summary("foo", const) result = sess.run(summ) @@ -106,7 +106,7 @@ class SummaryOpsTest(test.TestCase): def testManyBools(self): bools = [True, True, True, False, False, False] - with self.test_session() as sess: + with self.cached_session() as sess: const = constant_op.constant(bools) summ = summary_ops.tensor_summary("foo", const) result = sess.run(summ) @@ -116,7 +116,7 @@ class SummaryOpsTest(test.TestCase): self._AssertNumpyEq(n, bools) def testSummaryDescriptionAndDisplayName(self): - with self.test_session() as sess: + with self.cached_session() as sess: def get_description(summary_op): summ_str = sess.run(summary_op) diff --git a/tensorflow/python/kernel_tests/tensor_array_ops_test.py b/tensorflow/python/kernel_tests/tensor_array_ops_test.py index 6de6fbe767..0ad2063558 100644 --- a/tensorflow/python/kernel_tests/tensor_array_ops_test.py +++ b/tensorflow/python/kernel_tests/tensor_array_ops_test.py @@ -1504,6 +1504,19 @@ class TensorArrayTest(test.TestCase): vdx, vdy = sess.run([dx, dy]) self.assertAllClose(vdx, vdy) + def testTensorArrayInt64GPU(self): + if not test.is_gpu_available(): + return + with self.test_session(use_gpu=True, force_gpu=True) as sess: + value = array_ops.placeholder(dtypes.int64) + ta = tensor_array_ops.TensorArray(dtype=dtypes.int64, size=2) + ta = ta.scatter([0, 1], value) + r0 = ta.read(0) + r1 = ta.read(1) + v0, v1 = sess.run([r0, r1], feed_dict={value: [-3, 100]}) + self.assertAllEqual(v0, -3) + self.assertAllEqual(v1, 100) + if __name__ == "__main__": test.main() diff --git a/tensorflow/python/kernel_tests/tensordot_op_test.py b/tensorflow/python/kernel_tests/tensordot_op_test.py index 8ad29afd0a..d8d76440f1 100644 --- a/tensorflow/python/kernel_tests/tensordot_op_test.py +++ b/tensorflow/python/kernel_tests/tensordot_op_test.py @@ -48,7 +48,7 @@ class TensordotTest(test_lib.TestCase): with self.assertRaises(ValueError): math_ops.tensordot(a, b, (a_axes, b_axes)) # Invalid dynamic shapes. - with self.test_session() as sess: + with self.cached_session() as sess: with self.assertRaisesRegexp(errors_impl.InvalidArgumentError, "Matrix size-incompatible"): a_ph = array_ops.placeholder(dtypes.float32) @@ -80,7 +80,7 @@ class TensordotTest(test_lib.TestCase): output = math_ops.tensordot(a_ph, b_ph, axes_ph) # Note: We don't support scalar Tensor values for axes. for axes_value in 1, [1], [0, 1], [[1]], [[0, 1]], [[0], [7]]: - with self.test_session() as sess: + with self.cached_session() as sess: with self.assertRaises(errors_impl.InvalidArgumentError): _ = sess.run( [output], feed_dict={ @@ -92,7 +92,7 @@ class TensordotTest(test_lib.TestCase): # Test case for 11950 def test_valid_axis(self): for axes_value in [1, 2], [[1], [2]], [[], []], 0: - with self.test_session() as sess: + with self.cached_session(): np_a = np.ones((3, 3)) np_b = np.array([2, 3, 1])[None, None] np_ans = np.tensordot(np_a, np_b, axes_value) diff --git a/tensorflow/python/kernel_tests/transpose_op_test.py b/tensorflow/python/kernel_tests/transpose_op_test.py index 290200ce45..f42800226e 100644 --- a/tensorflow/python/kernel_tests/transpose_op_test.py +++ b/tensorflow/python/kernel_tests/transpose_op_test.py @@ -451,13 +451,13 @@ class TransposeTest(test.TestCase): array_ops.transpose(array_ops.placeholder(dtypes.int32)).get_shape()) def testNullTensor(self): - with self.test_session(): + with self.cached_session(): x = constant_op.constant([], dtype=dtypes.float32, shape=[1, 4, 0]) xt = array_ops.transpose(x, [0, 2, 1]).eval() self.assertAllEqual(xt.shape, (1, 0, 4)) def _testError(self, x, p, err): - with self.test_session(): + with self.cached_session(): with self.assertRaisesOpError(err): array_ops.transpose(x, p).eval() diff --git a/tensorflow/python/kernel_tests/unique_op_test.py b/tensorflow/python/kernel_tests/unique_op_test.py index bbc040dc13..316570e13e 100644 --- a/tensorflow/python/kernel_tests/unique_op_test.py +++ b/tensorflow/python/kernel_tests/unique_op_test.py @@ -30,7 +30,7 @@ class UniqueTest(test.TestCase): def testInt32(self): x = np.random.randint(2, high=10, size=7000) - with self.test_session() as sess: + with self.cached_session() as sess: y, idx = array_ops.unique(x) tf_y, tf_idx = sess.run([y, idx]) @@ -41,7 +41,7 @@ class UniqueTest(test.TestCase): def testInt32OutIdxInt64(self): x = np.random.randint(2, high=10, size=7000) - with self.test_session() as sess: + with self.cached_session() as sess: y, idx = array_ops.unique(x, out_idx=dtypes.int64) tf_y, tf_idx = sess.run([y, idx]) @@ -53,7 +53,7 @@ class UniqueTest(test.TestCase): def testString(self): indx = np.random.randint(65, high=122, size=7000) x = [chr(i) for i in indx] - with self.test_session() as sess: + with self.cached_session() as sess: y, idx = array_ops.unique(x) tf_y, tf_idx = sess.run([y, idx]) @@ -65,7 +65,7 @@ class UniqueTest(test.TestCase): def testInt32Axis(self): for dtype in [np.int32, np.int64]: x = np.array([[1, 0, 0], [1, 0, 0], [2, 0, 0]]) - with self.test_session() as sess: + with self.cached_session() as sess: y0, idx0 = gen_array_ops.unique_v2(x, axis=np.array([0], dtype)) tf_y0, tf_idx0 = sess.run([y0, idx0]) y1, idx1 = gen_array_ops.unique_v2(x, axis=np.array([1], dtype)) @@ -79,7 +79,7 @@ class UniqueTest(test.TestCase): # This test is only temporary, once V2 is used # by default, the axis will be wrapped to allow `axis=None`. x = np.random.randint(2, high=10, size=7000) - with self.test_session() as sess: + with self.cached_session() as sess: y, idx = gen_array_ops.unique_v2(x, axis=np.array([], np.int32)) tf_y, tf_idx = sess.run([y, idx]) @@ -93,7 +93,7 @@ class UniqueWithCountsTest(test.TestCase): def testInt32(self): x = np.random.randint(2, high=10, size=7000) - with self.test_session() as sess: + with self.cached_session() as sess: y, idx, count = array_ops.unique_with_counts(x) tf_y, tf_idx, tf_count = sess.run([y, idx, count]) @@ -106,7 +106,7 @@ class UniqueWithCountsTest(test.TestCase): def testInt32OutIdxInt64(self): x = np.random.randint(2, high=10, size=7000) - with self.test_session() as sess: + with self.cached_session() as sess: y, idx, count = array_ops.unique_with_counts(x, out_idx=dtypes.int64) tf_y, tf_idx, tf_count = sess.run([y, idx, count]) @@ -121,7 +121,7 @@ class UniqueWithCountsTest(test.TestCase): indx = np.random.randint(65, high=122, size=7000) x = [chr(i) for i in indx] - with self.test_session() as sess: + with self.cached_session() as sess: y, idx, count = array_ops.unique_with_counts(x) tf_y, tf_idx, tf_count = sess.run([y, idx, count]) @@ -136,7 +136,7 @@ class UniqueWithCountsTest(test.TestCase): def testInt32Axis(self): for dtype in [np.int32, np.int64]: x = np.array([[1, 0, 0], [1, 0, 0], [2, 0, 0]]) - with self.test_session() as sess: + with self.cached_session() as sess: y0, idx0, count0 = gen_array_ops.unique_with_counts_v2( x, axis=np.array([0], dtype)) tf_y0, tf_idx0, tf_count0 = sess.run([y0, idx0, count0]) @@ -154,7 +154,7 @@ class UniqueWithCountsTest(test.TestCase): # This test is only temporary, once V2 is used # by default, the axis will be wrapped to allow `axis=None`. x = np.random.randint(2, high=10, size=7000) - with self.test_session() as sess: + with self.cached_session() as sess: y, idx, count = gen_array_ops.unique_with_counts_v2( x, axis=np.array([], np.int32)) tf_y, tf_idx, tf_count = sess.run([y, idx, count]) diff --git a/tensorflow/python/kernel_tests/unstack_op_test.py b/tensorflow/python/kernel_tests/unstack_op_test.py index 1ee6e0866a..b373c419b6 100644 --- a/tensorflow/python/kernel_tests/unstack_op_test.py +++ b/tensorflow/python/kernel_tests/unstack_op_test.py @@ -99,7 +99,7 @@ class UnstackOpTest(test.TestCase): self.assertLess(err, 1e-6) def testInferNum(self): - with self.test_session(): + with self.cached_session(): for shape in (2,), (3,), (2, 3), (3, 2), (4, 3, 2): x = array_ops.placeholder(np.float32, shape=shape) cs = array_ops.unstack(x) @@ -131,13 +131,13 @@ class UnstackOpTest(test.TestCase): for j in range(-i, i): expected = np_split_squeeze(a, j) - with self.test_session() as sess: + with self.cached_session() as sess: actual_unstack = sess.run(array_ops.unstack(a, axis=j)) self.assertAllEqual(expected, actual_unstack) def testAxis0Default(self): - with self.test_session() as sess: + with self.cached_session() as sess: a = constant_op.constant([[1, 2, 3], [4, 5, 6]], name='a') unstacked = sess.run(array_ops.unstack(a)) @@ -156,7 +156,7 @@ class UnstackOpTest(test.TestCase): array_ops.unstack(a, axis=-3) def testZeroLengthDim(self): - with self.test_session(): + with self.cached_session(): x = array_ops.zeros(shape=(0, 1, 2)) y = array_ops.unstack(x, axis=1)[0].eval() self.assertEqual(y.shape, (0, 2)) diff --git a/tensorflow/python/kernel_tests/variable_ops_test.py b/tensorflow/python/kernel_tests/variable_ops_test.py index cf369c0718..3d2f8b6155 100644 --- a/tensorflow/python/kernel_tests/variable_ops_test.py +++ b/tensorflow/python/kernel_tests/variable_ops_test.py @@ -118,7 +118,7 @@ class VariableOpTest(test.TestCase): self.assertEqual(tensor_shape.unknown_shape(), assigned.get_shape()) def testAssignNoShape(self): - with self.test_session(): + with self.cached_session(): value = self._NewShapelessTensor() var = state_ops.variable_op([1, 2], dtypes.float32, set_shape=False) self.assertEqual(tensor_shape.unknown_shape(), var.get_shape()) @@ -126,7 +126,7 @@ class VariableOpTest(test.TestCase): state_ops.assign(var, value).get_shape()) def testAssignNoShapeNoValidateShape(self): - with self.test_session(): + with self.cached_session(): value = self._NewShapelessTensor() var = state_ops.variable_op([1, 2], dtypes.float32, set_shape=False) self.assertEqual(tensor_shape.unknown_shape(), var.get_shape()) diff --git a/tensorflow/python/kernel_tests/variable_scope_test.py b/tensorflow/python/kernel_tests/variable_scope_test.py index d57b79cb90..401e1ae102 100644 --- a/tensorflow/python/kernel_tests/variable_scope_test.py +++ b/tensorflow/python/kernel_tests/variable_scope_test.py @@ -113,7 +113,7 @@ class VariableScopeTest(test.TestCase): self.assertEqual(w.constraint, constraint) def testStringDefaultInitializer(self): - with self.test_session(): + with self.cached_session(): v = variable_scope.get_variable("string", shape=[], dtype=dtypes.string) variables_lib.global_variables_initializer().run() self.assertAllEqual(compat.as_bytes(v.eval()), b"") @@ -263,7 +263,7 @@ class VariableScopeTest(test.TestCase): # TODO(alive): support variable partitioning/caching in eager mode. def testVarScopeCachingDevice(self): - with self.test_session(): + with self.cached_session(): caching_device = "/job:moo" with variable_scope.variable_scope("tower"): with variable_scope.variable_scope( @@ -367,7 +367,7 @@ class VariableScopeTest(test.TestCase): variable_scope.get_variable("s", initializer=init, dtype=dtypes.float64) def testControlDeps(self): - with self.test_session() as sess: + with self.cached_session() as sess: v0 = variable_scope.get_variable( "v0", [1], initializer=init_ops.constant_initializer(0)) with ops.control_dependencies([v0.value()]): @@ -403,7 +403,7 @@ class VariableScopeTest(test.TestCase): variable_scope._DEFAULT_USE_RESOURCE = old def testControlFlow(self): - with self.test_session() as sess: + with self.cached_session() as sess: v0 = variable_scope.get_variable( "v0", [], initializer=init_ops.constant_initializer(0)) var_dict = {} @@ -513,7 +513,7 @@ class VariableScopeTest(test.TestCase): self.assertEqual(sc2, "testVarScopeNameScope3/scope2/") def testVarScopeOriginalNameScope(self): - with self.test_session(): + with self.cached_session(): with ops.name_scope("scope1"): with variable_scope.variable_scope("tower") as tower: self.assertEqual(tower.original_name_scope, "scope1/tower/") @@ -536,7 +536,7 @@ class VariableScopeTest(test.TestCase): self.assertEqual(sc3, "scope1/tower/bar_1/") def testVarScopeObjectReuse(self): - with self.test_session(): + with self.cached_session(): vs = None with variable_scope.variable_scope("jump", reuse=True) as scope: vs = scope @@ -563,7 +563,7 @@ class VariableScopeTest(test.TestCase): self.assertFalse(jump_no_reuse.reuse) def testVarScopeGetOrCreateReuse(self): - with self.test_session(): + with self.cached_session(): def test_value(value): x = constant_op.constant(value) @@ -582,7 +582,7 @@ class VariableScopeTest(test.TestCase): test_value(17.) def testVarOpScope(self): - with self.test_session(): + with self.cached_session(): with ops.name_scope("testVarOpScope1"): with variable_scope.variable_scope("tower", "default", []): self.assertEqual( @@ -608,7 +608,7 @@ class VariableScopeTest(test.TestCase): self.assertEqual(sc2, "testVarOpScope2/default_1/testVarOpScope2/") def testVarOpScopeUniqueNamesInterleavedSubstringScopes(self): - with self.test_session(): + with self.cached_session(): with variable_scope.variable_scope(None, "defaultScope1"): with variable_scope.variable_scope(None, "layer"): self.assertEqual( @@ -631,7 +631,7 @@ class VariableScopeTest(test.TestCase): "defaultScope1_2/layer/w:0") def testVarOpScopeUniqueNamesWithJump(self): - with self.test_session(): + with self.cached_session(): with variable_scope.variable_scope("default") as default: with variable_scope.variable_scope(None, "layer"): self.assertEqual( @@ -647,7 +647,7 @@ class VariableScopeTest(test.TestCase): variable_scope.get_variable("w", []).name, "default/layer_2/w:0") def testVarOpScopeReuse(self): - with self.test_session(): + with self.cached_session(): with variable_scope.variable_scope("outer") as outer: with variable_scope.variable_scope("tower", "default", []): self.assertEqual( @@ -673,7 +673,7 @@ class VariableScopeTest(test.TestCase): self.assertEqual(sc2, "outer_1/default/scope2/") def testVarScopeGetVar(self): - with self.test_session(): + with self.cached_session(): with variable_scope.variable_scope("root"): with variable_scope.variable_scope("towerA") as tower_a: va = variable_scope.get_variable("v", [1]) @@ -719,7 +719,7 @@ class VariableScopeTest(test.TestCase): self.assertEqual("dtype" in str(exc.exception), True) def testVarScopeOuterScope(self): - with self.test_session(): + with self.cached_session(): with variable_scope.variable_scope("outer") as outer: pass with variable_scope.variable_scope(outer): @@ -743,7 +743,7 @@ class VariableScopeTest(test.TestCase): self.assertEqual(sc2, "outer_2/default/scope2/") def testVarScopeNestedOuterScope(self): - with self.test_session(): + with self.cached_session(): with variable_scope.variable_scope("outer") as outer: with variable_scope.variable_scope(outer): self.assertEqual( @@ -768,7 +768,7 @@ class VariableScopeTest(test.TestCase): self.assertEqual(sc2, "outer/default_1/scope2/") def testVarOpScopeReuseParam(self): - with self.test_session(): + with self.cached_session(): with variable_scope.variable_scope("outer") as outer: with variable_scope.variable_scope("tower", "default", []): self.assertEqual( @@ -795,14 +795,14 @@ class VariableScopeTest(test.TestCase): self.assertEqual(sc2, "outer_1/default/scope2/") def testVarOpScopeReuseError(self): - with self.test_session(): + with self.cached_session(): with self.assertRaises(ValueError): with variable_scope.variable_scope(None, "default", reuse=True): self.assertEqual( variable_scope.get_variable("w", []).name, "outer/tower/w:0") def testVarOpScopeOuterScope(self): - with self.test_session(): + with self.cached_session(): with variable_scope.variable_scope("outer") as outer: pass with variable_scope.variable_scope(outer, "default", []): @@ -827,7 +827,7 @@ class VariableScopeTest(test.TestCase): self.assertEqual(sc2, "outer_2/default/scope2/") def testVarOpScopeNestedOuterScope(self): - with self.test_session(): + with self.cached_session(): with variable_scope.variable_scope("outer") as outer: with variable_scope.variable_scope(outer, "default", []): self.assertEqual( @@ -851,7 +851,7 @@ class VariableScopeTest(test.TestCase): self.assertEqual(sc2, "outer_1/default/scope2/") def testBasicWhenAuxiliaryNameScopeIsFalse(self): - with self.test_session(): + with self.cached_session(): with variable_scope.variable_scope( "scope", auxiliary_name_scope=False) as scope: self.assertEqual(scope.original_name_scope, "") @@ -886,7 +886,7 @@ class VariableScopeTest(test.TestCase): constant_op.constant([], name="c").name, "outer/inner/c:0") def testCreatedByDefaultNameWhenAuxiliaryNameScopeIsFalse(self): - with self.test_session(): + with self.cached_session(): with variable_scope.variable_scope( None, default_name="default", auxiliary_name_scope=False) as scope: self.assertEqual(scope.original_name_scope, "") @@ -910,7 +910,7 @@ class VariableScopeTest(test.TestCase): constant_op.constant([], name="c").name, "outer/default/c:0") def testReenterRootScopeWhenAuxiliaryNameScopeIsFalse(self): - with self.test_session(): + with self.cached_session(): root_scope = variable_scope.get_variable_scope() with variable_scope.variable_scope( root_scope, auxiliary_name_scope=False) as scope: @@ -927,7 +927,7 @@ class VariableScopeTest(test.TestCase): constant_op.constant([], name="c1").name, "outer/c1:0") def testAuxiliaryNameScopeIsInvalid(self): - with self.test_session(): + with self.cached_session(): with self.assertRaisesRegexp(TypeError, "auxiliary_name_scope"): with variable_scope.variable_scope( None, default_name="scope", auxiliary_name_scope="invalid"): @@ -947,7 +947,7 @@ class VariableScopeTest(test.TestCase): def testReuseScopeWithoutNameScopeCollision(self): # Github issue: #13429 - with self.test_session(): + with self.cached_session(): with variable_scope.variable_scope("outer"): with variable_scope.variable_scope("inner") as inner: pass @@ -1021,7 +1021,7 @@ class VariableScopeTest(test.TestCase): self.assertEqual(varname_type[1], ("y", dtypes.int64)) def testGetCollection(self): - with self.test_session(): + with self.cached_session(): _ = variable_scope.get_variable("testGetCollection_a", []) _ = variable_scope.get_variable( "testGetCollection_b", [], trainable=False) @@ -1075,7 +1075,7 @@ class VariableScopeTest(test.TestCase): ]) def testGetTrainableVariablesWithGetVariable(self): - with self.test_session(): + with self.cached_session(): _ = variable_scope.get_variable("testGetTrainableVariables_a", []) with variable_scope.variable_scope( "testGetTrainableVariables_foo") as scope: @@ -1111,7 +1111,7 @@ class VariableScopeTest(test.TestCase): trainable=True) def testGetTrainableVariablesWithVariable(self): - with self.test_session(): + with self.cached_session(): _ = variable_scope.variable(1.0, name="testGetTrainableVariables_a") with variable_scope.variable_scope( "testGetTrainableVariables_foo") as scope: @@ -1150,7 +1150,7 @@ class VariableScopeTest(test.TestCase): trainable=True) def testGetGlobalVariables(self): - with self.test_session(): + with self.cached_session(): _ = variable_scope.get_variable("testGetGlobalVariables_a", []) with variable_scope.variable_scope("testGetGlobalVariables_foo") as scope: _ = variable_scope.get_variable("testGetGlobalVariables_b", []) @@ -1160,7 +1160,7 @@ class VariableScopeTest(test.TestCase): "testGetGlobalVariables_b:0"]) def testGetLocalVariables(self): - with self.test_session(): + with self.cached_session(): _ = variable_scope.get_variable( "a", [], collections=[ops.GraphKeys.LOCAL_VARIABLES]) with variable_scope.variable_scope("foo") as scope: @@ -1396,7 +1396,7 @@ class VariableScopeWithCustomGetterTest(test.TestCase): self.assertEqual("scope/v/0:0", true_vars[0].name) self.assertEqual("scope/v/1:0", true_vars[1].name) self.assertEqual("custom_getter/add:0", v.name) - with self.test_session() as sess: + with self.cached_session() as sess: variables_lib.global_variables_initializer().run() np_vars, np_v = sess.run([true_vars, v]) self.assertAllClose(np_v, sum(np_vars)) @@ -1436,7 +1436,7 @@ class VariableScopeWithCustomGetterTest(test.TestCase): self.assertEqual(template % (1, 1, 0), true_vars[6].name) self.assertEqual(template % (1, 1, 1), true_vars[7].name) - with self.test_session() as sess: + with self.cached_session() as sess: variables_lib.global_variables_initializer().run() np_vars, np_v = sess.run([true_vars, v]) # take products of sums of products diff --git a/tensorflow/python/kernel_tests/variables_test.py b/tensorflow/python/kernel_tests/variables_test.py index 2b9c62ad6f..2e7975667c 100644 --- a/tensorflow/python/kernel_tests/variables_test.py +++ b/tensorflow/python/kernel_tests/variables_test.py @@ -42,7 +42,7 @@ from tensorflow.python.util import compat class VariablesTestCase(test.TestCase): def testInitialization(self): - with self.test_session(): + with self.cached_session(): var0 = variables.Variable(0.0) self.assertEqual("Variable:0", var0.name) self.assertEqual("Variable", var0._shared_name) @@ -69,7 +69,7 @@ class VariablesTestCase(test.TestCase): self.assertAllClose(1.1, var1.eval()) def testInitializationOrder(self): - with self.test_session(): + with self.cached_session(): rnd = variables.Variable(random_ops.random_uniform([3, 6]), name="rnd") self.assertEqual("rnd:0", rnd.name) self.assertEqual([3, 6], rnd.get_shape()) @@ -106,7 +106,7 @@ class VariablesTestCase(test.TestCase): pass def testAssignments(self): - with self.test_session(): + with self.cached_session(): var = variables.Variable(0.0) plus_one = var.assign_add(1.0) minus_one = var.assign_sub(2.0) @@ -142,7 +142,7 @@ class VariablesTestCase(test.TestCase): self.assertAllClose(4.0, var.eval()) def testZeroSizeStringAssign(self): - with self.test_session() as sess: + with self.cached_session() as sess: array = variables.Variable( initial_value=array_ops.zeros((0,), dtype=dtypes.string), name="foo", @@ -154,7 +154,7 @@ class VariablesTestCase(test.TestCase): self.assertEqual([], list(sess.run(copy_op))) def _countUpToTest(self, dtype): - with self.test_session(): + with self.cached_session(): zero = constant_op.constant(0, dtype=dtype) var = variables.Variable(zero) count_up_to = var.count_up_to(3) @@ -186,7 +186,7 @@ class VariablesTestCase(test.TestCase): self._countUpToTest(dtypes.int64) def testControlDepsNone(self): - with self.test_session(): + with self.cached_session(): c = constant_op.constant(1.0) with ops.control_dependencies([c]): # d get the control dep. @@ -199,7 +199,7 @@ class VariablesTestCase(test.TestCase): self.assertEqual([], var_x._ref().op.control_inputs) # pylint: disable=protected-access def testControlFlow(self): - with self.test_session() as sess: + with self.cached_session() as sess: v0 = variables.Variable(0, name="v0") var_dict = {} @@ -248,7 +248,7 @@ class VariablesTestCase(test.TestCase): control_flow_ops.while_loop(cond, body, [0, 0]) def testUseVariableAsTensor(self): - with self.test_session(): + with self.cached_session(): var_x = variables.Variable(2.0) var_y = variables.Variable(3.0) variables.global_variables_initializer().run() @@ -257,7 +257,7 @@ class VariablesTestCase(test.TestCase): self.assertAllClose(5.0, math_ops.add(var_x, var_y).eval()) def testZeroSizeVarSameAsConst(self): - with self.test_session(): + with self.cached_session(): zero_size_var = variables.Variable(array_ops.zeros([0, 2])) zero_size_const = array_ops.ones([2, 0]) variable_mul = math_ops.matmul(zero_size_const, zero_size_var) @@ -269,7 +269,7 @@ class VariablesTestCase(test.TestCase): self.assertAllClose([[0., 0.], [0., 0.]], variable_output) def testCachingDevice(self): - with self.test_session(): + with self.cached_session(): var = variables.Variable(2.0) self.assertEqual(var.device, var.value().device) self.assertEqual(var.device, var.initialized_value().device) @@ -279,7 +279,7 @@ class VariablesTestCase(test.TestCase): self.assertTrue(var_cached.value().device.startswith("/job:foo")) def testCollections(self): - with self.test_session(): + with self.cached_session(): var_x = variables.Variable(2.0) var_y = variables.Variable(2.0, trainable=False) var_z = variables.Variable(2.0, trainable=True) @@ -294,7 +294,7 @@ class VariablesTestCase(test.TestCase): self.assertEqual([var_x, var_z, var_t], variables.trainable_variables()) def testCollectionsWithScope(self): - with self.test_session(): + with self.cached_session(): with ops.name_scope("scope_1"): var_x = variables.Variable(2.0) with ops.name_scope("scope_2"): @@ -309,7 +309,7 @@ class VariablesTestCase(test.TestCase): self.assertEqual([var_y], variables.trainable_variables("scope_2")) def testOperators(self): - with self.test_session(): + with self.cached_session(): var_f = variables.Variable([2.0]) add = var_f + 0.0 radd = 1.0 + var_f @@ -382,13 +382,13 @@ class VariablesTestCase(test.TestCase): self.assertAllClose([[20.0, 30.0], [40.0, 60.0]], rmatmul.eval()) def testSession(self): - with self.test_session() as sess: + with self.cached_session() as sess: var = variables.Variable([1, 12]) variables.global_variables_initializer().run() self.assertAllClose([1, 12], sess.run(var)) def testDevicePlacement(self): - with self.test_session() as sess: + with self.cached_session() as sess: with ops.device("/cpu:0"): var = variables.Variable([1, 12]) init_value = var.initialized_value() @@ -408,7 +408,7 @@ class VariablesTestCase(test.TestCase): def testInitializerFunction(self): value = [[-42], [133.7]] shape = [2, 1] - with self.test_session(): + with self.cached_session(): initializer = lambda: constant_op.constant(value) v1 = variables.Variable(initializer, dtype=dtypes.float32) @@ -443,7 +443,7 @@ class VariablesTestCase(test.TestCase): constraint=constraint) def testNoRefDataRace(self): - with self.test_session(): + with self.cached_session(): a = variables.Variable([1, 2, 3], dtype=dtypes.float32) b = variables.Variable(a.initialized_value() + 2) c = variables.Variable(b.initialized_value() + 2) @@ -453,7 +453,7 @@ class VariablesTestCase(test.TestCase): self.assertAllEqual(c.eval(), [5, 6, 7]) def testInitializerFunctionDevicePlacement(self): - with self.test_session(): + with self.cached_session(): initializer = lambda: constant_op.constant(42.0) with ops.device("/cpu:100"): v1 = variables.Variable(initializer, dtype=dtypes.float32, name="v1") @@ -471,11 +471,11 @@ class VariablesTestCase(test.TestCase): self.assertEqual(expected_group_v2, i.op.colocation_groups()) def testVariableDefInitializedInstances(self): - with ops.Graph().as_default(), self.test_session() as sess: + with ops.Graph().as_default(), self.cached_session() as sess: v_def = variables.Variable( initial_value=constant_op.constant(3.0)).to_proto() - with ops.Graph().as_default(), self.test_session() as sess: + with ops.Graph().as_default(), self.cached_session() as sess: # v describes a VariableDef-based variable without an initial value. v = variables.Variable(variable_def=v_def) self.assertEqual(3.0, sess.run(v.initialized_value())) @@ -486,7 +486,7 @@ class VariablesTestCase(test.TestCase): self.assertEqual(1.0, v.initialized_value().eval()) v_def.ClearField("initial_value_name") - with ops.Graph().as_default(), self.test_session() as sess: + with ops.Graph().as_default(), self.cached_session() as sess: # Restoring a legacy VariableDef proto that does not have # initial_value_name set should still work. v = variables.Variable(variable_def=v_def) @@ -514,7 +514,7 @@ class VariablesTestCase(test.TestCase): .trainable) def testLoad(self): - with self.test_session(): + with self.cached_session(): var = variables.Variable(np.zeros((5, 5), np.float32)) variables.global_variables_initializer().run() var.load(np.ones((5, 5), np.float32)) @@ -540,12 +540,12 @@ class VariablesTestCase(test.TestCase): class IsInitializedTest(test.TestCase): def testNoVars(self): - with ops.Graph().as_default(), self.test_session() as sess: + with ops.Graph().as_default(), self.cached_session() as sess: uninited = variables.report_uninitialized_variables() self.assertEqual(0, sess.run(uninited).size) def testAssertVariablesInitialized(self): - with ops.Graph().as_default(), self.test_session() as sess: + with ops.Graph().as_default(), self.cached_session() as sess: v = variables.Variable([1, 2], name="v") w = variables.Variable([3, 4], name="w") _ = v, w @@ -555,7 +555,7 @@ class IsInitializedTest(test.TestCase): self.assertEqual(0, sess.run(uninited).size) def testVariableList(self): - with ops.Graph().as_default(), self.test_session() as sess: + with ops.Graph().as_default(), self.cached_session() as sess: v = variables.Variable([1, 2], name="v") w = variables.Variable([3, 4], name="w") uninited = variables.report_uninitialized_variables() @@ -566,14 +566,14 @@ class IsInitializedTest(test.TestCase): self.assertEqual(0, sess.run(uninited).size) def testZeroSizeVarInitialized(self): - with ops.Graph().as_default(), self.test_session() as sess: + with ops.Graph().as_default(), self.cached_session() as sess: v = variables.Variable(array_ops.zeros([0, 2]), name="v") uninited = variables.report_uninitialized_variables() v.initializer.run() # not strictly necessary self.assertEqual(0, sess.run(uninited).size) def testTrainingWithZeroSizeVar(self): - with ops.Graph().as_default(), self.test_session() as sess: + with ops.Graph().as_default(), self.cached_session() as sess: a = variables.Variable(array_ops.zeros([0, 2])) b = variables.Variable(array_ops.ones([2, 2])) objective = math_ops.reduce_sum(b + math_ops.matmul( @@ -592,7 +592,7 @@ class ObsoleteIsInitializedTest(test.TestCase): self.assertEqual(None, variables.assert_variables_initialized()) def testVariables(self): - with ops.Graph().as_default(), self.test_session() as sess: + with ops.Graph().as_default(), self.cached_session() as sess: v = variables.Variable([1, 2]) w = variables.Variable([3, 4]) _ = v, w @@ -603,7 +603,7 @@ class ObsoleteIsInitializedTest(test.TestCase): sess.run(inited) def testVariableList(self): - with ops.Graph().as_default(), self.test_session() as sess: + with ops.Graph().as_default(), self.cached_session() as sess: v = variables.Variable([1, 2]) w = variables.Variable([3, 4]) inited = variables.assert_variables_initialized([v]) diff --git a/tensorflow/python/kernel_tests/weights_broadcast_test.py b/tensorflow/python/kernel_tests/weights_broadcast_test.py index eda2856e0b..85f9abc69f 100644 --- a/tensorflow/python/kernel_tests/weights_broadcast_test.py +++ b/tensorflow/python/kernel_tests/weights_broadcast_test.py @@ -44,7 +44,7 @@ class AssertBroadcastableTest(test.TestCase): values_placeholder = array_ops.placeholder(dtypes_lib.float32) dynamic_op = weights_broadcast_ops.assert_broadcastable( weights=weights_placeholder, values=values_placeholder) - with self.test_session(): + with self.cached_session(): static_op.run() dynamic_op.run(feed_dict={ weights_placeholder: weights, @@ -100,7 +100,7 @@ class AssertBroadcastableTest(test.TestCase): values_placeholder = array_ops.placeholder(dtypes_lib.float32) dynamic_op = weights_broadcast_ops.assert_broadcastable( weights=weights_placeholder, values=values_placeholder) - with self.test_session(): + with self.cached_session(): with self.assertRaisesRegexp(errors_impl.OpError, error_msg): dynamic_op.run(feed_dict={ weights_placeholder: weights, @@ -157,7 +157,7 @@ class BroadcastWeightsTest(test.TestCase): values_placeholder = array_ops.placeholder(dtypes_lib.float32) dynamic_op = weights_broadcast_ops.broadcast_weights( weights=weights_placeholder, values=values_placeholder) - with self.test_session(): + with self.cached_session(): self.assertAllEqual(expected, static_op.eval()) self.assertAllEqual(expected, dynamic_op.eval(feed_dict={ weights_placeholder: weights, @@ -227,7 +227,7 @@ class BroadcastWeightsTest(test.TestCase): values_placeholder = array_ops.placeholder(dtypes_lib.float32) dynamic_op = weights_broadcast_ops.broadcast_weights( weights=weights_placeholder, values=values_placeholder) - with self.test_session(): + with self.cached_session(): with self.assertRaisesRegexp(errors_impl.OpError, error_msg): dynamic_op.eval(feed_dict={ weights_placeholder: weights, diff --git a/tensorflow/python/kernel_tests/while_v2_test.py b/tensorflow/python/kernel_tests/while_v2_test.py new file mode 100644 index 0000000000..3a070544e8 --- /dev/null +++ b/tensorflow/python/kernel_tests/while_v2_test.py @@ -0,0 +1,276 @@ +# Copyright 2018 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== +"""Tests for while_v2.""" + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +from absl.testing import parameterized + +from tensorflow.core.protobuf import rewriter_config_pb2 +from tensorflow.python.framework import constant_op +from tensorflow.python.framework import dtypes +from tensorflow.python.framework import meta_graph +from tensorflow.python.framework import ops +from tensorflow.python.grappler import tf_optimizer +from tensorflow.python.ops import array_ops +from tensorflow.python.ops import gradients_impl +from tensorflow.python.ops import list_ops +from tensorflow.python.ops import while_v2 +from tensorflow.python.ops.control_flow_ops import while_loop as while_loop_v1 +from tensorflow.python.ops.while_v2 import while_loop as while_loop_v2 +from tensorflow.python.platform import test + + +class WhileV2Test(test.TestCase, parameterized.TestCase): + + def testSingleLoopVar(self): + x = constant_op.constant(2.) + ret = while_loop_v2(lambda v: v < 8., lambda v: v * v, [x]) + grad = gradients_impl.gradients(ret, [x]) + with self.cached_session() as sess: + self.assertEqual(sess.run(ret), 16.) + self.assertSequenceEqual(sess.run(grad), [32.]) + + def testMultipleLoopVarsBasic(self): + x = constant_op.constant(5.) + y = constant_op.constant(3.) + + # x = 5. + # y = 3. + # while x < 45.: + # x = x * y + ret = while_loop_v2(lambda v, _: v < 45., lambda v, w: (v * w, w), [x, y]) + # ret = [x*y^2, y] + + # Note: This is simply d_ret[0]/d_x since d_ret[1]/d_x is 0. + grad = gradients_impl.gradients(ret, [x]) # [2*x*y] + with self.cached_session() as sess: + self.assertSequenceEqual(sess.run(ret), [45., 3.]) + self.assertSequenceEqual(sess.run(grad), [9.]) + + def testMultipleLoopVars(self): + x = constant_op.constant(5.) + y = constant_op.constant(3.) + + # x = 5. + # y = 3. + # while x < 45.: + # x = x * y + # y = x + y + ret = while_loop_v2(lambda v, _: v < 45., lambda v, w: (v * w, v + w), + [x, y]) + # ret = [y*x**2 + x*y**2, x*y + x + y] + + gradx_0 = gradients_impl.gradients(ret[0], [x]) # [2*x*y + y**2] + gradx_1 = gradients_impl.gradients(ret[1], [x]) # [y + 1] + gradx_2 = gradients_impl.gradients(ret, [x]) # [2*x*y + y**2 + 2*y + 1] + grady_0 = gradients_impl.gradients(ret[0], [y]) # [2*x*y + x**2] + grady_1 = gradients_impl.gradients(ret[1], [y]) # [x + 1] + grady_2 = gradients_impl.gradients(ret, [y]) # [2*x*y + x**2 + x + 1] + with self.cached_session() as sess: + self.assertSequenceEqual(sess.run(ret), [120., 23.]) + self.assertSequenceEqual(sess.run(gradx_0), [39.]) + self.assertSequenceEqual(sess.run(gradx_1), [4.]) + self.assertSequenceEqual(sess.run(gradx_2), [43.]) + self.assertSequenceEqual(sess.run(grady_0), [55.]) + self.assertSequenceEqual(sess.run(grady_1), [6.]) + self.assertSequenceEqual(sess.run(grady_2), [61.]) + + def testMultipleWhileLoops(self): + x = constant_op.constant(2.) + ret1 = while_loop_v2(lambda v: v < 4., lambda v: v * v, [x]) # x**2 + ret2 = while_loop_v2(lambda v: v < 16., lambda v: v * v, ret1) # x**4 + grad = gradients_impl.gradients(ret2, [x]) # 4x**3 + grad_grad = gradients_impl.gradients(grad, [x]) # 12x**2 + with self.cached_session() as sess: + self.assertSequenceEqual(sess.run(grad), [32.]) + self.assertSequenceEqual(sess.run(grad_grad), [48.]) + + def testDoubleDerivative(self): + x = constant_op.constant(2.) + ret = while_loop_v2(lambda v: v < 8., lambda v: v**2, [x]) # x**4 + grad = gradients_impl.gradients(ret, [x]) # 4x**3 + grad_grad = gradients_impl.gradients(grad, [x]) # 12x**2 + with self.cached_session() as sess: + self.assertEqual(sess.run(ret), 16.) + self.assertSequenceEqual(sess.run(grad), [32.]) + self.assertSequenceEqual(sess.run(grad_grad), [48.]) + + def testPruning(self): + x = constant_op.constant(1) + + tensor_list = list_ops.empty_tensor_list( + element_dtype=x.dtype, element_shape=x.shape) + + def Cond(x, tl): + del tl # Unused for Cond. + return x < 5 + + def Body(x, tl): + return x + 1, list_ops.tensor_list_push_back(tl, x) + + outputs = while_loop_v1(Cond, Body, [x, tensor_list]) + + train_op = ops.get_collection_ref(ops.GraphKeys.TRAIN_OP) + train_op.append(outputs[0]) + + def GetOptimizedGraph(): + mg = meta_graph.create_meta_graph_def(graph=ops.get_default_graph()) + rewriter_config = rewriter_config_pb2.RewriterConfig( + constant_folding=rewriter_config_pb2.RewriterConfig.OFF, + memory_optimization=rewriter_config_pb2.RewriterConfig.MANUAL) + return tf_optimizer.OptimizeGraph(rewriter_config, mg) + + g = GetOptimizedGraph() + self.assertEqual(len([n for n in g.node if n.op == "Enter"]), 1) + + stack = list_ops.tensor_list_stack(outputs[1], element_dtype=x.dtype) + train_op.append(stack) + g = GetOptimizedGraph() + self.assertEqual(len([n for n in g.node if n.op == "Enter"]), 2) + + def testCaptureExternalTensorInCond(self): + x = constant_op.constant(2.) + y = constant_op.constant(1.) + ret = while_loop_v2(lambda v: v + y < 9., lambda v: v * 3., [x]) + grad = gradients_impl.gradients(ret, [x]) + with self.cached_session() as sess: + self.assertEqual(sess.run(ret), 18.) + self.assertSequenceEqual(sess.run(grad), [9.]) + + def testCaptureExternalTensorInBody(self): + x = constant_op.constant(2.) + y = constant_op.constant(3.) + ret = while_loop_v2(lambda v: v < 8., lambda v: v * y, [x]) + grad = gradients_impl.gradients(ret, [x]) + with self.cached_session() as sess: + self.assertEqual(sess.run(ret), 18.) + self.assertSequenceEqual(sess.run(grad), [9.]) + + def testLoopWithTensorListPushBack(self): + x = constant_op.constant(2.) + + tensor_list = list_ops.empty_tensor_list( + element_dtype=dtypes.float32, element_shape=ScalarShape()) + + def Cond(x, tl): + del tl # Unused for Cond. + return x < 5. + + def Body(x, tl): + tl = list_ops.tensor_list_push_back(tl, x) + tl = list_ops.tensor_list_push_back(tl, constant_op.constant(100.)) + return x**2., tl + + ret = while_loop_v2(Cond, Body, [x, tensor_list]) + grad = gradients_impl.gradients(ret[0], x) + with self.cached_session() as sess: + self.assertEqual(sess.run(ret[0]), 16.) + self.assertSequenceEqual(sess.run(grad), [32.]) + + def testDuplicateAccumulator(self): + x = constant_op.constant(2.) + + tensor_list = list_ops.empty_tensor_list( + element_dtype=dtypes.float32, element_shape=ScalarShape()) + + def Cond(x, tl): + del tl # Unused for Cond. + return x < 5. + + def Body(x, tl): + # There is an accumulator in the loop already so we should not add + # another. + tl = list_ops.tensor_list_push_back(tl, x) + return x**2., tl + + ret = while_loop_v2(Cond, Body, [x, tensor_list]) + + for op in ops.get_default_graph().get_operations(): + if op.type == "While": + while_op = op + + body_graph = while_v2._get_body_graph(while_op) + # body_graph.inputs: [counter_arg, x_arg, tl_arg, *accumulators] + x_input_t = body_graph.inputs[1] + accumulator_count = len( + [c for c in x_input_t.consumers() if c.type == "TensorListPushBack"]) + self.assertEqual(accumulator_count, 1) + + grad = gradients_impl.gradients(ret[0], x) + with self.cached_session() as sess: + self.assertEqual(sess.run(ret[0]), 16.) + self.assertSequenceEqual(sess.run(grad), [32.]) + + @parameterized.named_parameters( + ("UnknownShape", None), + ("PartiallyDefinedShape", [None, 2]), + ("FullyDefinedShape", [1, 2]), + ) + def testTensorListOutputElementShape(self, shape): + + def MatchShape(actual_tensor_shape): + # Compare the shapes, treating None dimensions as equal. We do not + # directly check actual_tensor_shape and tf.TensorShape(shape) for + # equality because tf.Dimension.__eq__ returns None if either dimension is + # None. + if shape is None: + self.assertIsNone(actual_tensor_shape.dims) + else: + self.assertListEqual(actual_tensor_shape.as_list(), shape) + + def GetAccumulatorForInputAtIndex(while_op, idx): + body_graph = while_v2._get_body_graph(while_op) + y_input_t = body_graph.inputs[idx] + push_back_node = [c for c in y_input_t.consumers() + if c.type == "TensorListPushBack"][0] + output_idx = body_graph.outputs.index(push_back_node.outputs[0]) + return while_op.outputs[output_idx] + + x = constant_op.constant(2.) + y = array_ops.placeholder(dtype=dtypes.float32, shape=shape) + + # Forward pass. + ret = while_loop_v2(lambda v, u: v < 8., lambda v, u: (v * v, u), [x, y]) + while_op = ret[0].op + # Get the TensorList output of While op containing the accumulated values + # of y. + # while_op.inputs: [counter_arg, x_arg, y_arg, *accumulators] + output = GetAccumulatorForInputAtIndex(while_op, 2) + _, val = list_ops.tensor_list_pop_back(output, + element_dtype=dtypes.float32) + MatchShape(val.shape) + + # Gradient pass. + grad = gradients_impl.gradients(ret[1], y) + grad_while_op = grad[0].op + # Get the TensorList output of gradient While op containing the accumulated + # values of grad_y. + # grad_while_op.inputs: + # [counter_arg, total_iters_arg, grad_x_arg, grad_y_arg, *other_args] + grad_output = GetAccumulatorForInputAtIndex(grad_while_op, 4) + _, val = list_ops.tensor_list_pop_back(grad_output, + element_dtype=dtypes.float32) + MatchShape(val.shape) + + +def ScalarShape(): + return ops.convert_to_tensor([], dtype=dtypes.int32) + + +if __name__ == "__main__": + test.main() diff --git a/tensorflow/python/kernel_tests/xent_op_test.py b/tensorflow/python/kernel_tests/xent_op_test.py index 60c726d54c..729885169e 100644 --- a/tensorflow/python/kernel_tests/xent_op_test.py +++ b/tensorflow/python/kernel_tests/xent_op_test.py @@ -153,13 +153,13 @@ class XentTest(test.TestCase): self.assertAllCloseAccordingToType(np_backprop, tf_backprop) def testShapeMismatch(self): - with self.test_session(): + with self.cached_session(): with self.assertRaises(ValueError): gen_nn_ops.softmax_cross_entropy_with_logits( [[0., 1.], [2., 3.]], [[0., 1., 0.], [1., 0., 0.]]) def testNotMatrix(self): - with self.test_session(): + with self.cached_session(): with self.assertRaises(ValueError): gen_nn_ops.softmax_cross_entropy_with_logits([0., 1., 2., 3.], [0., 1., 0., 1.]) @@ -180,7 +180,7 @@ class XentTest(test.TestCase): np.array([[0., 0., 0., 1.], [0., .5, .5, 0.]]).astype(np.float64)) def testGradient(self): - with self.test_session() as sess: + with self.cached_session() as sess: l = constant_op.constant( [0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.5], shape=[3, 4], @@ -207,7 +207,7 @@ class XentTest(test.TestCase): self.assertLess(err, 5e-8) def testGradientLabelWithV2(self): - with self.test_session(): + with self.cached_session(): l = constant_op.constant( [0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.5], shape=[3, 4], @@ -225,7 +225,7 @@ class XentTest(test.TestCase): self.assertLess(err, 5e-8) def testSecondGradient(self): - with self.test_session() as sess: + with self.cached_session() as sess: l = constant_op.constant( [ 0.0, 0.0, 1.0 / 3, 0.0, 1.0 / 3, 0.0, 0.0, 0.0, 0.0, 0.5 / 3, 0.0, |