aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/python/kernel_tests/transpose_op_test.py
blob: 2786eaf37b92d76fd0c9d7f81441aabf8ec062d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""Functional tests for Transpose op."""
import itertools
import tensorflow.python.platform

import numpy as np
import tensorflow as tf

from tensorflow.python.kernel_tests.gradient_checker import ComputeGradient


class TransposeTest(tf.test.TestCase):

  def _np_transpose(self, x, perm):
    ret = np.copy(x)
    ret = ret.transpose(perm)
    return ret

  def _compareCpu(self, x, p):
    np_ans = self._np_transpose(x, p)
    with self.test_session(use_gpu=False):
      inx = tf.convert_to_tensor(x)
      y = tf.transpose(inx, p)
      tf_ans = y.eval()
      self.assertAllEqual(np_ans, tf_ans)
      self.assertShapeEqual(np_ans, y)

      jacob_t = None
      # Gradient check on CPU.
      xs = list(np.shape(x))
      ys = list(np.shape(tf_ans))
      if x.dtype == np.float32:
        jacob_t, jacob_n = ComputeGradient(inx, xs, y, ys, x, 1e-2)
        self.assertAllClose(jacob_t, jacob_n, 1e-3, 1e-3)
      elif x.dtype == np.float64:
        jacob_t, jacob_n = ComputeGradient(inx, xs, y, ys, x, 1e-2)
        self.assertAllClose(jacob_t, jacob_n, 1e-6, 1e-6)

      return tf_ans, jacob_t

  def _compareGpu(self, x, p):
    np_ans = self._np_transpose(x, p)
    with self.test_session(use_gpu=True):
      inx = tf.convert_to_tensor(x)
      y = tf.transpose(inx, p)
      tf_ans = y.eval()
      self.assertAllEqual(np_ans, tf_ans)
      self.assertShapeEqual(np_ans, y)

      jacob_t = None
      # Gradient check on GPU.
      xs = list(np.shape(x))
      ys = list(np.shape(tf_ans))
      if x.dtype == np.float32:
        jacob_t, jacob_n = ComputeGradient(inx, xs, y, ys, x, 1e-2)
        self.assertAllClose(jacob_t, jacob_n, 1e-3, 1e-3)
      elif x.dtype == np.float64:
        jacob_t, jacob_n = ComputeGradient(inx, xs, y, ys, x, 1e-2)
        self.assertAllClose(jacob_t, jacob_n, 1e-6, 1e-6)

      return tf_ans, jacob_t

  def _compare(self, x, use_gpu=False):
    n = np.ndim(x)
    # generate all permutations of [0, 1, ... n-1] in random order.
    all_perm = np.random.permutation(
        [p for p in itertools.permutations(range(n))]).astype(np.int32)
    for p in all_perm[0:2]:
      self._compareCpu(x, p)
      if use_gpu:
        self._compareGpu(x, p)

  def _compare_cpu_gpu(self, x):
    n = np.ndim(x)
    # generate all permutation of [0, 1, ... n-1] in random order.
    all_perm = np.random.permutation(
        [p for p in itertools.permutations(range(n))]).astype(np.int32)
    for p in all_perm[0:2]:
      tf_a_cpu, tf_g_cpu = self._compareCpu(x, p)
      tf_a_gpu, tf_g_gpu = self._compareGpu(x, p)
      assert tf_g_cpu is not None
      assert tf_g_gpu is not None
      if x.dtype == np.float32:
        self.assertAllClose(tf_a_cpu, tf_a_gpu, 1e-3, 1e-3)
        self.assertAllClose(tf_g_cpu, tf_g_gpu, 1e-3, 1e-3)
      elif x.dtype == np.float64:
        self.assertAllClose(tf_a_cpu, tf_a_gpu, 1e-6, 1e-6)
        self.assertAllClose(tf_g_cpu, tf_g_gpu, 1e-6, 1e-6)

  def _testCpu(self, x):
    self._compare(x, use_gpu=False)

  def test1D(self):
    self._compareCpu(np.arange(0., 2), [0])

  def testNop(self):
    self._compareCpu(np.arange(0, 6).reshape([3, 2]).astype(np.float32), [0, 1])

  def testSimple(self):
    self._compareCpu(np.arange(0, 8).reshape([2, 4]).astype(np.float32),
                     np.array([1, 0]).astype(np.int32))

  def testFloat(self):
    self._compare_cpu_gpu(np.arange(0, 21).reshape([3, 7]).astype(np.float32))
    self._compare_cpu_gpu(
        np.arange(0, 210).reshape([2, 3, 5, 7]).astype(np.float32))

  def testDouble(self):
    self._compare_cpu_gpu(np.arange(0, 21).reshape([3, 7]).astype(np.float64))
    self._compare_cpu_gpu(
        np.arange(0, 210).reshape([2, 3, 5, 7]).astype(np.float64))

  def testSComplex(self):
    self._testCpu(np.complex(1, 2) * np.arange(0, 21).reshape(
        [3, 7]).astype(np.complex64))
    self._testCpu(np.complex(1, 2) * np.arange(0, 210).reshape(
        [2, 3, 5, 7]).astype(np.complex64))

  def testInt8(self):
    self._testCpu(np.arange(0, 21).reshape([3, 7]).astype(np.int8))
    self._testCpu(np.arange(0, 210).reshape([2, 3, 5, 7]).astype(np.int8))

  def testInt16(self):
    self._testCpu(np.arange(0, 21).reshape([3, 7]).astype(np.int16))
    self._testCpu(np.arange(0, 210).reshape([2, 3, 5, 7]).astype(np.int16))

  def testInt32(self):
    self._testCpu(np.arange(0, 21).reshape([3, 7]).astype(np.int32))
    self._testCpu(np.arange(0, 210).reshape([2, 3, 5, 7]).astype(np.int32))

  def testInt64(self):
    self._testCpu(np.arange(0, 21).reshape([3, 7]).astype(np.int64))
    self._testCpu(np.arange(0, 210).reshape([2, 3, 5, 7]).astype(np.int64))

  def testTranspose2DAuto(self):
    x_np = [[1, 2, 3], [4, 5, 6]]
    for use_gpu in [False, True]:
      with self.test_session(use_gpu=use_gpu):
        x_tf = tf.transpose(x_np).eval()
        self.assertAllEqual(x_tf, [[1, 4], [2, 5], [3, 6]])

  def testTransposeShapes(self):
    self.assertEqual([], tf.transpose(
        tf.placeholder(tf.int32, shape=[])).get_shape().dims)
    self.assertEqual([100], tf.transpose(
        tf.placeholder(tf.int32, shape=[100])).get_shape().dims)
    self.assertEqual([37, 100], tf.transpose(
        tf.placeholder(tf.int32, shape=[100, 37])).get_shape().dims)
    self.assertEqual([100, 37], tf.transpose(
        tf.placeholder(tf.int32, shape=[100, 37]), [0, 1]).get_shape().dims)
    self.assertEqual([15, 37, 100], tf.transpose(
        tf.placeholder(tf.int32, shape=[100, 37, 15])).get_shape().dims)
    self.assertEqual([15, 100, 37], tf.transpose(
        tf.placeholder(tf.int32,
                       shape=[100, 37, 15]), [2, 0, 1]).get_shape().dims)
    self.assertEqual(tf.TensorShape(None), tf.transpose(
        tf.placeholder(tf.int32)).get_shape())

  def _testError(self, x, p, err):
    with self.test_session():
      with self.assertRaisesOpError(err):
        tf.transpose(x, p).eval()

  def testError(self):
    with self.assertRaises(ValueError):
      tf.transpose(np.arange(0., 30).reshape([2, 3, 5]), [[0, 1], [2, 3]])
    self._testError(np.arange(0., 2 ** 10).reshape([2] * 10),
                    range(10),
                    "not implemented")
    with self.assertRaises(IndexError):
      tf.transpose(np.arange(0., 30).reshape([2, 3, 5]), [0, 1, 3])
    self._testError(np.arange(0., 30).reshape([2, 3, 5]),
                    [0, 1, 1],
                    "2 is missing")

if __name__ == "__main__":
  tf.test.main()