aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/python/kernel_tests/reverse_sequence_op_test.py
diff options
context:
space:
mode:
authorGravatar Justine Tunney <jart@google.com>2016-12-14 16:30:24 -0800
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2016-12-14 16:43:13 -0800
commit5866e065bc95c1d7de8a27413b368016941889a6 (patch)
tree55b7db600e38b3a799ab39053cd99e61204f840b /tensorflow/python/kernel_tests/reverse_sequence_op_test.py
parent38a664cd961762e64899187a31a1b86cbe5a992e (diff)
Remove hourglass imports from kernel_tests
Change: 142080137
Diffstat (limited to 'tensorflow/python/kernel_tests/reverse_sequence_op_test.py')
-rw-r--r--tensorflow/python/kernel_tests/reverse_sequence_op_test.py107
1 files changed, 56 insertions, 51 deletions
diff --git a/tensorflow/python/kernel_tests/reverse_sequence_op_test.py b/tensorflow/python/kernel_tests/reverse_sequence_op_test.py
index ca6b198fa8..9beb615b2c 100644
--- a/tensorflow/python/kernel_tests/reverse_sequence_op_test.py
+++ b/tensorflow/python/kernel_tests/reverse_sequence_op_test.py
@@ -12,18 +12,23 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
-
"""Tests for tensorflow.ops.reverse_sequence_op."""
+
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
from six.moves import xrange # pylint: disable=redefined-builtin
-import tensorflow as tf
+from tensorflow.python.framework import constant_op
+from tensorflow.python.framework import dtypes
+from tensorflow.python.ops import array_ops
+from tensorflow.python.ops import gradient_checker
+from tensorflow.python.platform import test
-class ReverseSequenceTest(tf.test.TestCase):
+
+class ReverseSequenceTest(test.TestCase):
def _testReverseSequence(self,
x,
@@ -34,7 +39,7 @@ class ReverseSequenceTest(tf.test.TestCase):
use_gpu=False,
expected_err_re=None):
with self.test_session(use_gpu=use_gpu):
- ans = tf.reverse_sequence(
+ ans = array_ops.reverse_sequence(
x, batch_axis=batch_axis, seq_axis=seq_axis, seq_lengths=seq_lengths)
if expected_err_re is None:
tf_ans = ans.eval()
@@ -57,10 +62,10 @@ class ReverseSequenceTest(tf.test.TestCase):
False, expected_err_re)
def _testBasic(self, dtype, len_dtype=np.int64):
- x = np.asarray([
- [[1, 2, 3, 4], [5, 6, 7, 8]],
- [[9, 10, 11, 12], [13, 14, 15, 16]],
- [[17, 18, 19, 20], [21, 22, 23, 24]]], dtype=dtype)
+ x = np.asarray(
+ [[[1, 2, 3, 4], [5, 6, 7, 8]], [[9, 10, 11, 12], [13, 14, 15, 16]],
+ [[17, 18, 19, 20], [21, 22, 23, 24]]],
+ dtype=dtype)
x = x.reshape(3, 2, 4, 1, 1)
x = x.transpose([2, 1, 0, 3, 4]) # permute axes 0 <=> 2
@@ -68,9 +73,11 @@ class ReverseSequenceTest(tf.test.TestCase):
seq_lengths = np.asarray([3, 0, 4], dtype=len_dtype)
truth_orig = np.asarray(
- [[[3, 2, 1, 4], [7, 6, 5, 8]], # reverse 0:3
- [[9, 10, 11, 12], [13, 14, 15, 16]], # reverse none
- [[20, 19, 18, 17], [24, 23, 22, 21]]], # reverse 0:4 (all)
+ [
+ [[3, 2, 1, 4], [7, 6, 5, 8]], # reverse 0:3
+ [[9, 10, 11, 12], [13, 14, 15, 16]], # reverse none
+ [[20, 19, 18, 17], [24, 23, 22, 21]]
+ ], # reverse 0:4 (all)
dtype=dtype)
truth_orig = truth_orig.reshape(3, 2, 4, 1, 1)
truth = truth_orig.transpose([2, 1, 0, 3, 4]) # permute axes 0 <=> 2
@@ -101,10 +108,10 @@ class ReverseSequenceTest(tf.test.TestCase):
self._testBasic(np.complex128)
def testFloatReverseSequenceGrad(self):
- x = np.asarray([
- [[1, 2, 3, 4], [5, 6, 7, 8]],
- [[9, 10, 11, 12], [13, 14, 15, 16]],
- [[17, 18, 19, 20], [21, 22, 23, 24]]], dtype=np.float)
+ x = np.asarray(
+ [[[1, 2, 3, 4], [5, 6, 7, 8]], [[9, 10, 11, 12], [13, 14, 15, 16]],
+ [[17, 18, 19, 20], [21, 22, 23, 24]]],
+ dtype=np.float)
x = x.reshape(3, 2, 4, 1, 1)
x = x.transpose([2, 1, 0, 3, 4]) # transpose axes 0 <=> 2
@@ -114,70 +121,68 @@ class ReverseSequenceTest(tf.test.TestCase):
seq_lengths = np.asarray([3, 0, 4], dtype=np.int64)
with self.test_session():
- input_t = tf.constant(x, shape=x.shape)
- seq_lengths_t = tf.constant(seq_lengths, shape=seq_lengths.shape)
- reverse_sequence_out = tf.reverse_sequence(
+ 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(
input_t,
batch_axis=batch_axis,
seq_axis=seq_axis,
seq_lengths=seq_lengths_t)
- err = tf.test.compute_gradient_error(input_t,
- x.shape,
- reverse_sequence_out,
- x.shape,
- x_init_value=x)
+ err = gradient_checker.compute_gradient_error(
+ input_t, x.shape, reverse_sequence_out, x.shape, x_init_value=x)
print("ReverseSequence gradient error = %g" % err)
self.assertLess(err, 1e-8)
def testShapeFunctionEdgeCases(self):
- t = tf.reverse_sequence(
- tf.placeholder(
- tf.float32, shape=None),
- seq_lengths=tf.placeholder(
- tf.int64, shape=(32,)),
+ t = array_ops.reverse_sequence(
+ array_ops.placeholder(
+ dtypes.float32, shape=None),
+ seq_lengths=array_ops.placeholder(
+ dtypes.int64, shape=(32,)),
batch_axis=0,
seq_axis=1)
self.assertIs(t.get_shape().ndims, None)
# Batch size mismatched between input and seq_lengths.
with self.assertRaises(ValueError):
- tf.reverse_sequence(
- tf.placeholder(
- tf.float32, shape=(32, 2, 3)),
- seq_lengths=tf.placeholder(
- tf.int64, shape=(33,)),
+ array_ops.reverse_sequence(
+ array_ops.placeholder(
+ dtypes.float32, shape=(32, 2, 3)),
+ seq_lengths=array_ops.placeholder(
+ dtypes.int64, shape=(33,)),
seq_axis=3)
# seq_axis out of bounds.
with self.assertRaisesRegexp(ValueError, "seq_dim must be < input rank"):
- tf.reverse_sequence(
- tf.placeholder(
- tf.float32, shape=(32, 2, 3)),
- seq_lengths=tf.placeholder(
- tf.int64, shape=(32,)),
+ array_ops.reverse_sequence(
+ array_ops.placeholder(
+ dtypes.float32, shape=(32, 2, 3)),
+ seq_lengths=array_ops.placeholder(
+ dtypes.int64, shape=(32,)),
seq_axis=3)
# batch_axis out of bounds.
- with self.assertRaisesRegexp(
- ValueError, "batch_dim must be < input rank"):
- tf.reverse_sequence(
- tf.placeholder(
- tf.float32, shape=(32, 2, 3)),
- seq_lengths=tf.placeholder(
- tf.int64, shape=(32,)),
+ with self.assertRaisesRegexp(ValueError, "batch_dim must be < input rank"):
+ array_ops.reverse_sequence(
+ array_ops.placeholder(
+ dtypes.float32, shape=(32, 2, 3)),
+ seq_lengths=array_ops.placeholder(
+ dtypes.int64, shape=(32,)),
seq_axis=0,
batch_axis=3)
with self.test_session():
- inputs = tf.placeholder(tf.float32, shape=(32, 2, 3))
- seq_lengths = tf.placeholder(tf.int64, shape=(32,))
- output = tf.reverse_sequence(
+ inputs = array_ops.placeholder(dtypes.float32, shape=(32, 2, 3))
+ seq_lengths = array_ops.placeholder(dtypes.int64, shape=(32,))
+ output = array_ops.reverse_sequence(
inputs, seq_lengths=seq_lengths,
seq_axis=0) # batch_axis default is 0
with self.assertRaisesOpError("batch_dim == seq_dim"):
- output.eval(feed_dict={inputs: np.random.rand(32, 2, 3),
- seq_lengths: xrange(32)})
+ output.eval(feed_dict={
+ inputs: np.random.rand(32, 2, 3),
+ seq_lengths: xrange(32)
+ })
if __name__ == "__main__":
- tf.test.main()
+ test.main()