aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/python/kernel_tests/slice_op_test.py
diff options
context:
space:
mode:
authorGravatar Gunhan Gulsoy <gunan@google.com>2016-08-05 16:34:48 -0800
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2016-08-05 17:47:29 -0700
commite3d37a62a209e8ebade236c1f4e17ac770e88eee (patch)
treed3773cd8a01bd68d8190c0b92ca1b3c6b40759d7 /tensorflow/python/kernel_tests/slice_op_test.py
parent382859a11fb658d3a87f80a167f1003512eb57c3 (diff)
Remove more uses of use_gpu from tensorflow tests.
Change: 129501616
Diffstat (limited to 'tensorflow/python/kernel_tests/slice_op_test.py')
-rw-r--r--tensorflow/python/kernel_tests/slice_op_test.py163
1 files changed, 62 insertions, 101 deletions
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