diff options
author | 2016-08-05 16:34:48 -0800 | |
---|---|---|
committer | 2016-08-05 17:47:29 -0700 | |
commit | e3d37a62a209e8ebade236c1f4e17ac770e88eee (patch) | |
tree | d3773cd8a01bd68d8190c0b92ca1b3c6b40759d7 | |
parent | 382859a11fb658d3a87f80a167f1003512eb57c3 (diff) |
Remove more uses of use_gpu from tensorflow tests.
Change: 129501616
-rw-r--r-- | tensorflow/python/kernel_tests/atrous_conv2d_test.py | 88 | ||||
-rw-r--r-- | tensorflow/python/kernel_tests/lrn_op_test.py | 55 | ||||
-rw-r--r-- | tensorflow/python/kernel_tests/numerics_test.py | 95 | ||||
-rw-r--r-- | tensorflow/python/kernel_tests/slice_op_test.py | 163 | ||||
-rw-r--r-- | tensorflow/python/kernel_tests/unpack_op_test.py | 63 | ||||
-rw-r--r-- | tensorflow/python/ops/image_grad_test.py | 113 | ||||
-rw-r--r-- | tensorflow/python/ops/math_grad_test.py | 48 |
7 files changed, 282 insertions, 343 deletions
diff --git a/tensorflow/python/kernel_tests/atrous_conv2d_test.py b/tensorflow/python/kernel_tests/atrous_conv2d_test.py index 2d19b8222d..7ca5a093c6 100644 --- a/tensorflow/python/kernel_tests/atrous_conv2d_test.py +++ b/tensorflow/python/kernel_tests/atrous_conv2d_test.py @@ -51,29 +51,28 @@ class AtrousConv2DTest(tf.test.TestCase): return filters_up def testAtrousConv2DForward(self): - for use_gpu in [True, False]: - with self.test_session(use_gpu=use_gpu): - # Input: [batch, height, width, input_depth] - height = 9 - for width in [9, 10]: # Test both odd and even width. - x_shape = [2, height, width, 2] - x = np.arange(np.prod(x_shape), dtype=np.float32).reshape(x_shape) - - # Filter: [kernel_height, kernel_width, input_depth, output_depth] - for kernel_height in range(1, 4): - for kernel_width in range(1, 4): - f_shape = [kernel_height, kernel_width, 2, 2] - f = np.arange(np.prod(f_shape), dtype=np.float32).reshape(f_shape) - - for rate in range(1, 4): - f_up = self._upsample_filters(f, rate) - - for padding in ["SAME", "VALID"]: - y1 = tf.nn.atrous_conv2d(x, f, rate, padding=padding) - y2 = tf.nn.conv2d(x, f_up, strides=[1, 1, 1, 1], - padding=padding) - self.assertAllClose(y1.eval(), y2.eval(), rtol=1e-2, - atol=1e-2) + with self.test_session(): + # Input: [batch, height, width, input_depth] + height = 9 + for width in [9, 10]: # Test both odd and even width. + x_shape = [2, height, width, 2] + x = np.arange(np.prod(x_shape), dtype=np.float32).reshape(x_shape) + + # Filter: [kernel_height, kernel_width, input_depth, output_depth] + for kernel_height in range(1, 4): + for kernel_width in range(1, 4): + f_shape = [kernel_height, kernel_width, 2, 2] + f = np.arange(np.prod(f_shape), dtype=np.float32).reshape(f_shape) + + for rate in range(1, 4): + f_up = self._upsample_filters(f, rate) + + for padding in ["SAME", "VALID"]: + y1 = tf.nn.atrous_conv2d(x, f, rate, padding=padding) + y2 = tf.nn.conv2d(x, f_up, strides=[1, 1, 1, 1], + padding=padding) + self.assertAllClose(y1.eval(), y2.eval(), rtol=1e-2, + atol=1e-2) def testAtrousSequence(self): """Tests optimization of sequence of atrous convolutions. @@ -128,28 +127,27 @@ class AtrousConv2DTest(tf.test.TestCase): self.assertAllClose(y1.eval(), y2.eval(), rtol=1e-2, atol=1e-2) def testGradient(self): - for use_gpu in [True, False]: - with self.test_session(use_gpu=use_gpu): - # Input: [batch, height, width, input_depth] - x_shape = [2, 5, 6, 2] - # Filter: [kernel_height, kernel_width, input_depth, output_depth] - f_shape = [3, 3, 2, 2] - # Output: [batch, height, width, output_depth] - y_shape = [2, 5, 6, 2] - - np.random.seed(1) # Make it reproducible. - x_val = np.random.random_sample(x_shape).astype(np.float32) - f_val = np.random.random_sample(f_shape).astype(np.float32) - x = tf.constant(x_val, name="x", dtype=tf.float32) - f = tf.constant(f_val, name="f", dtype=tf.float32) - - for rate in range(1, 4): - output = tf.nn.atrous_conv2d(x, f, rate=rate, padding="SAME") - err = tf.test.compute_gradient_error( - [x, f], [x_shape, f_shape], output, y_shape) - print("atrous_conv2d gradient err = %g " % err) - err_tolerance = 1e-3 - self.assertLess(err, err_tolerance) + with self.test_session(): + # Input: [batch, height, width, input_depth] + x_shape = [2, 5, 6, 2] + # Filter: [kernel_height, kernel_width, input_depth, output_depth] + f_shape = [3, 3, 2, 2] + # Output: [batch, height, width, output_depth] + y_shape = [2, 5, 6, 2] + + np.random.seed(1) # Make it reproducible. + x_val = np.random.random_sample(x_shape).astype(np.float32) + f_val = np.random.random_sample(f_shape).astype(np.float32) + x = tf.constant(x_val, name="x", dtype=tf.float32) + f = tf.constant(f_val, name="f", dtype=tf.float32) + + for rate in range(1, 4): + output = tf.nn.atrous_conv2d(x, f, rate=rate, padding="SAME") + err = tf.test.compute_gradient_error( + [x, f], [x_shape, f_shape], output, y_shape) + print("atrous_conv2d gradient err = %g " % err) + err_tolerance = 1e-3 + self.assertLess(err, err_tolerance) if __name__ == "__main__": diff --git a/tensorflow/python/kernel_tests/lrn_op_test.py b/tensorflow/python/kernel_tests/lrn_op_test.py index 3d72e8ae35..b759c627d4 100644 --- a/tensorflow/python/kernel_tests/lrn_op_test.py +++ b/tensorflow/python/kernel_tests/lrn_op_test.py @@ -45,8 +45,8 @@ class LRNOpTest(tf.test.TestCase): np.power(bias + alpha * np.sum(patch * patch), beta)) return output - def _RunAndVerify(self, dtype, use_gpu): - with self.test_session(use_gpu=use_gpu): + def _RunAndVerify(self, dtype): + with self.test_session(): # random shape shape = np.random.randint(1, 16, size=4) # Make depth at least 2 to make it meaningful @@ -77,30 +77,28 @@ class LRNOpTest(tf.test.TestCase): self.assertShapeEqual(expected, lrn_t) def testCompute(self): - for use_gpu in (True, False): - for _ in range(2): - self._RunAndVerify(tf.float32, use_gpu) - # Enable when LRN supports tf.float16 on GPU. - if not use_gpu: - self._RunAndVerify(tf.float16, use_gpu) + for _ in range(2): + self._RunAndVerify(tf.float32) + # Enable when LRN supports tf.float16 on GPU. + if not tf.test.is_gpu_available(): + self._RunAndVerify(tf.float16) def testGradientsZeroInput(self): - for use_gpu in (True, False): - with self.test_session(use_gpu=use_gpu): - shape = [4, 4, 4, 4] - p = tf.placeholder(tf.float32, shape=shape) - inp_array = np.zeros(shape).astype("f") - lrn_op = tf.nn.local_response_normalization(p, 2, 1.0, 0.0, - 1.0, name="lrn") - grad = tf.gradients([lrn_op], [p])[0] - params = {p: inp_array} - r = grad.eval(feed_dict=params) - expected = np.ones(shape).astype("f") - self.assertAllClose(r, expected) - self.assertShapeEqual(expected, grad) + with self.test_session(): + shape = [4, 4, 4, 4] + p = tf.placeholder(tf.float32, shape=shape) + inp_array = np.zeros(shape).astype("f") + lrn_op = tf.nn.local_response_normalization(p, 2, 1.0, 0.0, + 1.0, name="lrn") + grad = tf.gradients([lrn_op], [p])[0] + params = {p: inp_array} + r = grad.eval(feed_dict=params) + expected = np.ones(shape).astype("f") + self.assertAllClose(r, expected) + self.assertShapeEqual(expected, grad) - def _RunAndVerifyGradients(self, dtype, use_gpu): - with self.test_session(use_gpu=use_gpu): + def _RunAndVerifyGradients(self, dtype): + with self.test_session(): # random shape shape = np.random.randint(1, 5, size=4) # Make depth at least 2 to make it meaningful @@ -133,12 +131,11 @@ class LRNOpTest(tf.test.TestCase): self.assertLess(err, 1.0) def testGradients(self): - for use_gpu in (True, False): - for _ in range(2): - self._RunAndVerifyGradients(tf.float32, use_gpu) - # Enable when LRN supports tf.float16 on GPU. - if not use_gpu: - self._RunAndVerifyGradients(tf.float16, use_gpu) + for _ in range(2): + self._RunAndVerifyGradients(tf.float32) + # Enable when LRN supports tf.float16 on GPU. + if not tf.test.is_gpu_available(): + self._RunAndVerifyGradients(tf.float16) if __name__ == "__main__": diff --git a/tensorflow/python/kernel_tests/numerics_test.py b/tensorflow/python/kernel_tests/numerics_test.py index 95b989b978..9abd25cb56 100644 --- a/tensorflow/python/kernel_tests/numerics_test.py +++ b/tensorflow/python/kernel_tests/numerics_test.py @@ -29,11 +29,10 @@ class VerifyTensorAllFiniteTest(tf.test.TestCase): def testVerifyTensorAllFiniteSucceeds(self): x_shape = [5, 4] x = np.random.random_sample(x_shape).astype(np.float32) - for use_gpu in [False, True]: - with self.test_session(use_gpu=use_gpu): - t = tf.constant(x, shape=x_shape, dtype=tf.float32) - t_verified = tf.verify_tensor_all_finite(t, "Input is not a number.") - self.assertAllClose(x, t_verified.eval()) + with self.test_session(): + t = tf.constant(x, shape=x_shape, dtype=tf.float32) + t_verified = tf.verify_tensor_all_finite(t, "Input is not a number.") + self.assertAllClose(x, t_verified.eval()) def testVerifyTensorAllFiniteFails(self): x_shape = [5, 4] @@ -42,66 +41,60 @@ class VerifyTensorAllFiniteTest(tf.test.TestCase): # Test NaN. x[0] = np.nan - for use_gpu in [False, True]: - with self.test_session(use_gpu=use_gpu): - with self.assertRaisesOpError(my_msg): - t = tf.constant(x, shape=x_shape, dtype=tf.float32) - t_verified = tf.verify_tensor_all_finite(t, my_msg) - t_verified.eval() + with self.test_session(): + with self.assertRaisesOpError(my_msg): + t = tf.constant(x, shape=x_shape, dtype=tf.float32) + t_verified = tf.verify_tensor_all_finite(t, my_msg) + t_verified.eval() # Test Inf. x[0] = np.inf - for use_gpu in [False, True]: - with self.test_session(use_gpu=use_gpu): - with self.assertRaisesOpError(my_msg): - t = tf.constant(x, shape=x_shape, dtype=tf.float32) - t_verified = tf.verify_tensor_all_finite(t, my_msg) - t_verified.eval() + with self.test_session(): + with self.assertRaisesOpError(my_msg): + t = tf.constant(x, shape=x_shape, dtype=tf.float32) + t_verified = tf.verify_tensor_all_finite(t, my_msg) + t_verified.eval() class NumericsTest(tf.test.TestCase): def testInf(self): - for use_gpu in [True, False]: - with self.test_session(use_gpu=use_gpu, graph=tf.Graph()): - t1 = tf.constant(1.0) - t2 = tf.constant(0.0) - a = tf.div(t1, t2) - check = tf.add_check_numerics_ops() - a = control_flow_ops.with_dependencies([check], a) - with self.assertRaisesOpError("Inf"): - a.eval() + with self.test_session(graph=tf.Graph()): + t1 = tf.constant(1.0) + t2 = tf.constant(0.0) + a = tf.div(t1, t2) + check = tf.add_check_numerics_ops() + a = control_flow_ops.with_dependencies([check], a) + with self.assertRaisesOpError("Inf"): + a.eval() def testNaN(self): - for use_gpu in [True, False]: - with self.test_session(use_gpu=use_gpu, graph=tf.Graph()): - t1 = tf.constant(0.0) - t2 = tf.constant(0.0) - a = tf.div(t1, t2) - check = tf.add_check_numerics_ops() - a = control_flow_ops.with_dependencies([check], a) - with self.assertRaisesOpError("NaN"): - a.eval() + with self.test_session(graph=tf.Graph()): + t1 = tf.constant(0.0) + t2 = tf.constant(0.0) + a = tf.div(t1, t2) + check = tf.add_check_numerics_ops() + a = control_flow_ops.with_dependencies([check], a) + with self.assertRaisesOpError("NaN"): + a.eval() def testBoth(self): - for use_gpu in [True, False]: - with self.test_session(use_gpu=use_gpu, graph=tf.Graph()): - t1 = tf.constant([1.0, 0.0]) - t2 = tf.constant([0.0, 0.0]) - a = tf.div(t1, t2) - check = tf.add_check_numerics_ops() - a = control_flow_ops.with_dependencies([check], a) - with self.assertRaisesOpError("Inf and NaN"): - a.eval() + with self.test_session(graph=tf.Graph()): + t1 = tf.constant([1.0, 0.0]) + t2 = tf.constant([0.0, 0.0]) + a = tf.div(t1, t2) + check = tf.add_check_numerics_ops() + a = control_flow_ops.with_dependencies([check], a) + with self.assertRaisesOpError("Inf and NaN"): + a.eval() def testPassThrough(self): - for use_gpu in [True, False]: - with self.test_session(use_gpu=use_gpu, graph=tf.Graph()): - t1 = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3]) - checked = tf.check_numerics(t1, message="pass through test") - value = checked.eval() - self.assertAllEqual(np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]), value) - self.assertEqual([2, 3], checked.get_shape()) + with self.test_session(graph=tf.Graph()): + t1 = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3]) + checked = tf.check_numerics(t1, message="pass through test") + value = checked.eval() + self.assertAllEqual(np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]), value) + self.assertEqual([2, 3], checked.get_shape()) if __name__ == "__main__": diff --git a/tensorflow/python/kernel_tests/slice_op_test.py b/tensorflow/python/kernel_tests/slice_op_test.py index 4add9756ce..b2429f1fcc 100644 --- a/tensorflow/python/kernel_tests/slice_op_test.py +++ b/tensorflow/python/kernel_tests/slice_op_test.py @@ -25,106 +25,85 @@ import tensorflow as tf class SliceTest(tf.test.TestCase): - def _testEmpty(self, use_gpu): + def testEmpty(self): inp = np.random.rand(4, 4).astype("f") for k in xrange(4): - with self.test_session(use_gpu=use_gpu): + with self.test_session(): a = tf.constant(inp, shape=[4, 4], dtype=tf.float32) slice_t = a[2, k:k] slice_val = slice_t.eval() self.assertAllEqual(slice_val, inp[2, k:k]) - def testEmptyAll(self): - self._testEmpty(use_gpu=False) - self._testEmpty(use_gpu=True) - - def _testInt32(self, use_gpu): + def testInt32(self): inp = np.random.rand(4, 4).astype("i") for k in xrange(4): - with self.test_session(use_gpu=use_gpu): + with self.test_session(): a = tf.constant(inp, shape=[4, 4], dtype=tf.int32) slice_t = a[2, k:k] slice_val = slice_t.eval() self.assertAllEqual(slice_val, inp[2, k:k]) - def testInt32(self): - self._testEmpty(use_gpu=False) - self._testEmpty(use_gpu=True) - - def _testSelectAll(self, use_gpu): - with self.test_session(use_gpu=use_gpu): - inp = np.random.rand(4, 4, 4, 4).astype("f") - a = tf.constant(inp, shape=[4, 4, 4, 4], - dtype=tf.float32) - - slice_explicit_t = tf.slice(a, [0, 0, 0, 0], [-1, -1, -1, -1]) - slice_implicit_t = a[:, :, :, :] - - self.assertAllEqual(inp, slice_explicit_t.eval()) - self.assertAllEqual(inp, slice_implicit_t.eval()) - self.assertEqual(inp.shape, slice_explicit_t.get_shape()) - self.assertEqual(inp.shape, slice_implicit_t.get_shape()) - def testSelectAll(self): for _ in range(10): - self._testSelectAll(use_gpu=False) - self._testSelectAll(use_gpu=True) + with self.test_session(): + inp = np.random.rand(4, 4, 4, 4).astype("f") + a = tf.constant(inp, shape=[4, 4, 4, 4], + dtype=tf.float32) - def _testSingleDimension(self, use_gpu): - with self.test_session(use_gpu=use_gpu): - inp = np.random.rand(10).astype("f") - a = tf.constant(inp, shape=[10], dtype=tf.float32) + slice_explicit_t = tf.slice(a, [0, 0, 0, 0], [-1, -1, -1, -1]) + slice_implicit_t = a[:, :, :, :] - hi = np.random.randint(0, 9) - scalar_t = a[hi] - scalar_val = scalar_t.eval() - self.assertAllEqual(scalar_val, inp[hi]) - - if hi > 0: - lo = np.random.randint(0, hi) - else: - lo = 0 - slice_t = a[lo:hi] - slice_val = slice_t.eval() - self.assertAllEqual(slice_val, inp[lo:hi]) + self.assertAllEqual(inp, slice_explicit_t.eval()) + self.assertAllEqual(inp, slice_implicit_t.eval()) + self.assertEqual(inp.shape, slice_explicit_t.get_shape()) + self.assertEqual(inp.shape, slice_implicit_t.get_shape()) def testSingleDimension(self): for _ in range(10): - self._testSingleDimension(use_gpu=False) - self._testSingleDimension(use_gpu=True) + with self.test_session(): + inp = np.random.rand(10).astype("f") + a = tf.constant(inp, shape=[10], dtype=tf.float32) + + hi = np.random.randint(0, 9) + scalar_t = a[hi] + scalar_val = scalar_t.eval() + self.assertAllEqual(scalar_val, inp[hi]) + + if hi > 0: + lo = np.random.randint(0, hi) + else: + lo = 0 + slice_t = a[lo:hi] + slice_val = slice_t.eval() + self.assertAllEqual(slice_val, inp[lo:hi]) - def _testSliceMatrixDim0(self, x, begin, size, use_gpu): - with self.test_session(use_gpu=use_gpu): + def _testSliceMatrixDim0(self, x, begin, size): + with self.test_session(): tf_ans = tf.slice(x, [begin, 0], [size, x.shape[1]]).eval() np_ans = x[begin:begin+size, :] self.assertAllEqual(tf_ans, np_ans) def testSliceMatrixDim0(self): - for use_gpu in [False, True]: - x = np.random.rand(8, 4).astype("f") - self._testSliceMatrixDim0(x, 1, 2, use_gpu) - self._testSliceMatrixDim0(x, 3, 3, use_gpu) - y = np.random.rand(8, 7).astype("f") # 7 * sizeof(float) is not aligned - self._testSliceMatrixDim0(y, 1, 2, use_gpu) - self._testSliceMatrixDim0(y, 3, 3, use_gpu) - - def _testIndexAndSlice(self, use_gpu): - with self.test_session(use_gpu=use_gpu): - inp = np.random.rand(4, 4).astype("f") - a = tf.constant(inp, shape=[4, 4], dtype=tf.float32) - - x, y = np.random.randint(0, 3, size=2).tolist() - slice_t = a[x, 0:y] - slice_val = slice_t.eval() - self.assertAllEqual(slice_val, inp[x, 0:y]) + x = np.random.rand(8, 4).astype("f") + self._testSliceMatrixDim0(x, 1, 2) + self._testSliceMatrixDim0(x, 3, 3) + y = np.random.rand(8, 7).astype("f") # 7 * sizeof(float) is not aligned + self._testSliceMatrixDim0(y, 1, 2) + self._testSliceMatrixDim0(y, 3, 3) def testSingleElementAll(self): for _ in range(10): - self._testIndexAndSlice(use_gpu=False) - self._testIndexAndSlice(use_gpu=True) + with self.test_session(): + inp = np.random.rand(4, 4).astype("f") + a = tf.constant(inp, shape=[4, 4], dtype=tf.float32) - def _testSimple(self, use_gpu): - with self.test_session(use_gpu=use_gpu) as sess: + x, y = np.random.randint(0, 3, size=2).tolist() + slice_t = a[x, 0:y] + slice_val = slice_t.eval() + self.assertAllEqual(slice_val, inp[x, 0:y]) + + def testSimple(self): + with self.test_session() as sess: inp = np.random.rand(4, 4).astype("f") a = tf.constant([float(x) for x in inp.ravel(order="C")], shape=[4, 4], dtype=tf.float32) @@ -136,12 +115,8 @@ class SliceTest(tf.test.TestCase): self.assertEqual(slice_val.shape, slice_t.get_shape()) self.assertEqual(slice2_val.shape, slice2_t.get_shape()) - def testSimpleAll(self): - self._testSimple(use_gpu=False) - self._testSimple(use_gpu=True) - - def _testComplex(self, use_gpu): - with self.test_session(use_gpu=use_gpu): + def testComplex(self): + with self.test_session(): inp = np.random.rand(4, 10, 10, 4).astype("f") a = tf.constant(inp, dtype=tf.float32) @@ -154,16 +129,11 @@ class SliceTest(tf.test.TestCase): slice_t = a[:, x, y:z, :] self.assertAllEqual(slice_t.eval(), inp[:, x, y:z, :]) - def testComplex(self): - for _ in range(10): - self._testComplex(use_gpu=False) - self._testComplex(use_gpu=True) - - def _RunAndVerifyResult(self, use_gpu): + def testRandom(self): # Random dims of rank 6 input_shape = np.random.randint(0, 20, size=6) inp = np.random.rand(*input_shape).astype("f") - with self.test_session(use_gpu=use_gpu) as sess: + with self.test_session() as sess: a = tf.constant([float(x) for x in inp.ravel(order="C")], shape=input_shape, dtype=tf.float32) indices = [0 if x == 0 else np.random.randint(x) for x in input_shape] @@ -190,13 +160,8 @@ class SliceTest(tf.test.TestCase): self.assertEqual(expected_val.shape, slice_t.get_shape()) self.assertEqual(expected_val.shape, slice2_t.get_shape()) - def testRandom(self): - for _ in range(10): - self._RunAndVerifyResult(use_gpu=False) - self._RunAndVerifyResult(use_gpu=True) - - def _testGradientSlice(self, input_shape, slice_begin, slice_size, use_gpu): - with self.test_session(use_gpu=use_gpu): + def _testGradientSlice(self, input_shape, slice_begin, slice_size): + with self.test_session(): num_inputs = np.prod(input_shape) num_grads = np.prod(slice_size) inp = np.random.rand(num_inputs).astype("f").reshape(input_shape) @@ -218,32 +183,28 @@ class SliceTest(tf.test.TestCase): self.assertAllClose(np_ans, result) - def _testGradientVariableSize(self, use_gpu): - with self.test_session(use_gpu=use_gpu): + def _testGradientVariableSize(self): + with self.test_session(): inp = tf.constant([1.0, 2.0, 3.0], name="in") out = tf.slice(inp, [1], [-1]) grad_actual = tf.gradients(out, inp)[0].eval() self.assertAllClose([0., 1., 1.], grad_actual) - def _testGradientsSimple(self, use_gpu): + def testGradientsAll(self): # Slice the middle square out of a 4x4 input - self._testGradientSlice([4, 4], [1, 1], [2, 2], use_gpu) + self._testGradientSlice([4, 4], [1, 1], [2, 2]) # Slice the upper left square out of a 4x4 input - self._testGradientSlice([4, 4], [0, 0], [2, 2], use_gpu) + self._testGradientSlice([4, 4], [0, 0], [2, 2]) # Slice a non-square input starting from (2,1) - self._testGradientSlice([4, 4], [2, 1], [1, 2], use_gpu) + self._testGradientSlice([4, 4], [2, 1], [1, 2]) # Slice a 3D tensor - self._testGradientSlice([3, 3, 3], [0, 1, 0], [2, 1, 1], use_gpu) + self._testGradientSlice([3, 3, 3], [0, 1, 0], [2, 1, 1]) # Use -1 as a slice dimension. - self._testGradientVariableSize(use_gpu) - - def testGradientsAll(self): - self._testGradientsSimple(use_gpu=False) - self._testGradientsSimple(use_gpu=True) + self._testGradientVariableSize() def testNotIterable(self): # NOTE(mrry): If we register __getitem__ as an overloaded diff --git a/tensorflow/python/kernel_tests/unpack_op_test.py b/tensorflow/python/kernel_tests/unpack_op_test.py index 75a0cf3c37..f4bc4955f4 100644 --- a/tensorflow/python/kernel_tests/unpack_op_test.py +++ b/tensorflow/python/kernel_tests/unpack_op_test.py @@ -35,43 +35,40 @@ class UnpackOpTest(tf.test.TestCase): def testSimple(self): np.random.seed(7) - for use_gpu in False, True: - with self.test_session(use_gpu=use_gpu): - for shape in (2,), (3,), (2, 3), (3, 2), (4, 3, 2): - data = np.random.randn(*shape) - # Convert data to a single tensorflow tensor - x = tf.constant(data) - # Unpack into a list of tensors - cs = tf.unpack(x, num=shape[0]) - self.assertEqual(type(cs), list) - self.assertEqual(len(cs), shape[0]) - cs = [c.eval() for c in cs] - self.assertAllEqual(cs, data) - - def testGradientsAxis0(self): - for use_gpu in False, True: + with self.test_session(): for shape in (2,), (3,), (2, 3), (3, 2), (4, 3, 2): data = np.random.randn(*shape) - shapes = [shape[1:]] * shape[0] - for i in xrange(shape[0]): - with self.test_session(use_gpu=use_gpu): - x = tf.constant(data) - cs = tf.unpack(x, num=shape[0]) - err = tf.test.compute_gradient_error(x, shape, cs[i], shapes[i]) - self.assertLess(err, 1e-6) + # Convert data to a single tensorflow tensor + x = tf.constant(data) + # Unpack into a list of tensors + cs = tf.unpack(x, num=shape[0]) + self.assertEqual(type(cs), list) + self.assertEqual(len(cs), shape[0]) + cs = [c.eval() for c in cs] + self.assertAllEqual(cs, data) + + def testGradientsAxis0(self): + for shape in (2,), (3,), (2, 3), (3, 2), (4, 3, 2): + data = np.random.randn(*shape) + shapes = [shape[1:]] * shape[0] + for i in xrange(shape[0]): + with self.test_session(): + x = tf.constant(data) + cs = tf.unpack(x, num=shape[0]) + err = tf.test.compute_gradient_error(x, shape, cs[i], shapes[i]) + self.assertLess(err, 1e-6) def testGradientsAxis1(self): - for use_gpu in False, True: - for shape in (2, 3), (3, 2), (4, 3, 2): - data = np.random.randn(*shape) - out_shape = list(shape) - del out_shape[1] - for i in xrange(shape[1]): - with self.test_session(use_gpu=use_gpu): - x = tf.constant(data) - cs = tf.unpack(x, num=shape[1], axis=1) - err = tf.test.compute_gradient_error(x, shape, cs[i], out_shape) - self.assertLess(err, 1e-6) + for shape in (2, 3), (3, 2), (4, 3, 2): + data = np.random.randn(*shape) + out_shape = list(shape) + del out_shape[1] + for i in xrange(shape[1]): + with self.test_session(): + x = tf.constant(data) + cs = tf.unpack(x, num=shape[1], axis=1) + err = tf.test.compute_gradient_error(x, shape, cs[i], out_shape) + self.assertLess(err, 1e-6) def testInferNum(self): with self.test_session(): diff --git a/tensorflow/python/ops/image_grad_test.py b/tensorflow/python/ops/image_grad_test.py index ccbae0f157..2a5fc4f310 100644 --- a/tensorflow/python/ops/image_grad_test.py +++ b/tensorflow/python/ops/image_grad_test.py @@ -33,15 +33,14 @@ class ResizeNearestNeighborOpTest(tf.test.TestCase): for nptype in self.TYPES: x = np.arange(0, 4).reshape(in_shape).astype(nptype) - for use_gpu in [False, True]: - with self.test_session(use_gpu=use_gpu) as sess: - input_tensor = tf.constant(x, shape=in_shape) - resize_out = tf.image.resize_nearest_neighbor(input_tensor, - out_shape[1:3]) - self.assertEqual(out_shape, list(resize_out.get_shape())) + with self.test_session() as sess: + input_tensor = tf.constant(x, shape=in_shape) + resize_out = tf.image.resize_nearest_neighbor(input_tensor, + out_shape[1:3]) + self.assertEqual(out_shape, list(resize_out.get_shape())) - resize_out = sess.run(resize_out) - self.assertEqual(out_shape, list(resize_out.shape)) + resize_out = sess.run(resize_out) + self.assertEqual(out_shape, list(resize_out.shape)) def testGradFromResizeToLargerInBothDims(self): in_shape = [1, 2, 3, 1] @@ -50,17 +49,16 @@ class ResizeNearestNeighborOpTest(tf.test.TestCase): for nptype in self.TYPES: x = np.arange(0, 6).reshape(in_shape).astype(nptype) - for use_gpu in [False, True]: - with self.test_session(use_gpu=use_gpu): - input_tensor = tf.constant(x, shape=in_shape) - resize_out = tf.image.resize_nearest_neighbor(input_tensor, - out_shape[1:3]) - err = tf.test.compute_gradient_error(input_tensor, - in_shape, - resize_out, - out_shape, - x_init_value=x) - self.assertLess(err, 1e-3) + with self.test_session(): + input_tensor = tf.constant(x, shape=in_shape) + resize_out = tf.image.resize_nearest_neighbor(input_tensor, + out_shape[1:3]) + err = tf.test.compute_gradient_error(input_tensor, + in_shape, + resize_out, + out_shape, + x_init_value=x) + self.assertLess(err, 1e-3) def testGradFromResizeToSmallerInBothDims(self): in_shape = [1, 4, 6, 1] @@ -69,17 +67,16 @@ class ResizeNearestNeighborOpTest(tf.test.TestCase): for nptype in self.TYPES: x = np.arange(0, 24).reshape(in_shape).astype(nptype) - for use_gpu in [False, True]: - with self.test_session(use_gpu=use_gpu): - input_tensor = tf.constant(x, shape=in_shape) - resize_out = tf.image.resize_nearest_neighbor(input_tensor, - out_shape[1:3]) - err = tf.test.compute_gradient_error(input_tensor, - in_shape, - resize_out, - out_shape, - x_init_value=x) - self.assertLess(err, 1e-3) + with self.test_session(): + input_tensor = tf.constant(x, shape=in_shape) + resize_out = tf.image.resize_nearest_neighbor(input_tensor, + out_shape[1:3]) + err = tf.test.compute_gradient_error(input_tensor, + in_shape, + resize_out, + out_shape, + x_init_value=x) + self.assertLess(err, 1e-3) def testCompareGpuVsCpu(self): in_shape = [1, 4, 6, 3] @@ -195,16 +192,15 @@ class CropAndResizeOpTest(tf.test.TestCase): boxes = np.array([[0, 0, 1, 1], [.1, .2, .7, .8]], dtype=np.float32) box_ind = np.array([0, 1], dtype=np.int32) - for use_gpu in [False, True]: - with self.test_session(use_gpu=use_gpu) as sess: - crops = tf.image.crop_and_resize( - tf.constant(image, shape=image_shape), - tf.constant(boxes, shape=[num_boxes, 4]), - tf.constant(box_ind, shape=[num_boxes]), - tf.constant(crop_size, shape=[2])) - self.assertEqual(crops_shape, list(crops.get_shape())) - crops = sess.run(crops) - self.assertEqual(crops_shape, list(crops.shape)) + with self.test_session() as sess: + crops = tf.image.crop_and_resize( + tf.constant(image, shape=image_shape), + tf.constant(boxes, shape=[num_boxes, 4]), + tf.constant(box_ind, shape=[num_boxes]), + tf.constant(crop_size, shape=[2])) + self.assertEqual(crops_shape, list(crops.get_shape())) + crops = sess.run(crops) + self.assertEqual(crops_shape, list(crops.shape)) def _randomUniformAvoidAnchors(self, low, high, anchors, radius, num_samples): """Generate samples that are far enough from a set of anchor points. @@ -281,25 +277,24 @@ class CropAndResizeOpTest(tf.test.TestCase): boxes = np.array(boxes, dtype=np.float32) box_ind = np.arange(batch, dtype=np.int32) - for use_gpu in [False, True]: - with self.test_session(use_gpu=use_gpu): - image_tensor = tf.constant(image, shape=image_shape) - boxes_tensor = tf.constant(boxes, shape=[num_boxes, 4]) - box_ind_tensor = tf.constant(box_ind, shape=[num_boxes]) - crops = tf.image.crop_and_resize( - image_tensor, - boxes_tensor, - box_ind_tensor, - tf.constant(crop_size, shape=[2])) - - err = tf.test.compute_gradient_error( - [image_tensor, boxes_tensor], [image_shape, boxes_shape], - crops, - crops_shape, - delta=delta, - x_init_value=[image, boxes]) - - self.assertLess(err, 2e-3) + with self.test_session(): + image_tensor = tf.constant(image, shape=image_shape) + boxes_tensor = tf.constant(boxes, shape=[num_boxes, 4]) + box_ind_tensor = tf.constant(box_ind, shape=[num_boxes]) + crops = tf.image.crop_and_resize( + image_tensor, + boxes_tensor, + box_ind_tensor, + tf.constant(crop_size, shape=[2])) + + err = tf.test.compute_gradient_error( + [image_tensor, boxes_tensor], [image_shape, boxes_shape], + crops, + crops_shape, + delta=delta, + x_init_value=[image, boxes]) + + self.assertLess(err, 2e-3) if __name__ == "__main__": diff --git a/tensorflow/python/ops/math_grad_test.py b/tensorflow/python/ops/math_grad_test.py index 6886432e28..861ff64224 100644 --- a/tensorflow/python/ops/math_grad_test.py +++ b/tensorflow/python/ops/math_grad_test.py @@ -33,23 +33,22 @@ class SquaredDifferenceOpTest(tf.test.TestCase): l = np.random.randn(*left_shape) r = np.random.randn(*right_shape) - for use_gpu in [True, False]: - with self.test_session(use_gpu=use_gpu): - left_tensor = tf.constant(l, shape=left_shape) - right_tensor = tf.constant(r, shape=right_shape) - output = tf.squared_difference(left_tensor, right_tensor) - left_err = tf.test.compute_gradient_error(left_tensor, - left_shape, - output, - output_shape, - x_init_value=l) - right_err = tf.test.compute_gradient_error(right_tensor, - right_shape, - output, - output_shape, - x_init_value=r) - self.assertLess(left_err, 1e-10) - self.assertLess(right_err, 1e-10) + with self.test_session(): + left_tensor = tf.constant(l, shape=left_shape) + right_tensor = tf.constant(r, shape=right_shape) + output = tf.squared_difference(left_tensor, right_tensor) + left_err = tf.test.compute_gradient_error(left_tensor, + left_shape, + output, + output_shape, + x_init_value=l) + right_err = tf.test.compute_gradient_error(right_tensor, + right_shape, + output, + output_shape, + x_init_value=r) + self.assertLess(left_err, 1e-10) + self.assertLess(right_err, 1e-10) def testGrad(self): self._testGrad([1, 2, 3, 2], [3, 2]) @@ -72,14 +71,13 @@ class AbsOpTest(tf.test.TestCase): value = tf.convert_to_tensor(self._biasedRandN(shape, bias=bias), dtype=dtype) - for use_gpu in [True, False]: - with self.test_session(use_gpu=use_gpu): - if dtype in (tf.complex64, tf.complex128): - output = tf.complex_abs(value) - else: - output = tf.abs(value) - error = tf.test.compute_gradient_error( - value, shape, output, output.get_shape().as_list()) + with self.test_session(): + if dtype in (tf.complex64, tf.complex128): + output = tf.complex_abs(value) + else: + output = tf.abs(value) + error = tf.test.compute_gradient_error( + value, shape, output, output.get_shape().as_list()) self.assertLess(error, max_error) def testComplexAbs(self): |