aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/python/kernel_tests/linalg_grad_test.py
blob: 50e5328c3e8195ce5d207da47cd0d3bf31cc9a66 (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
"""Tests for tensorflow.ops.linalg_grad."""

import tensorflow.python.platform

import numpy as np
import tensorflow as tf

from tensorflow.python.kernel_tests import gradient_checker as gc


class MatrixInverseGradientTest(tf.test.TestCase):
  pass  # Filled in below

def _GetMatrixInverseGradientTest(dtype, shape):
  def Test(self):
    with self.test_session():
      np.random.seed(1)
      m = np.random.uniform(low=1.0, high=100.0, size=np.prod(shape)).reshape(
          shape).astype(dtype)
      a = tf.constant(m)
      epsilon = np.finfo(dtype).eps
      # Optimal stepsize for central difference is O(epsilon^{1/3}).
      delta = epsilon ** (1.0 / 3.0)
      tol = 1e-3

      if len(shape) == 2:
        ainv = tf.matrix_inverse(a)
      else:
        ainv = tf.batch_matrix_inverse(a)

      theoretical, numerical = gc.ComputeGradient(a, shape, ainv, shape,
                                                  delta=delta)
      self.assertAllClose(theoretical, numerical, atol=tol, rtol=tol)
  return Test


if __name__ == "__main__":
  # TODO(rmlarsen,irving): Reenable float32 once tolerances are fixed
  # The test used to loop over (np.float, np.double), both of which are float64.
  for dtype in np.float64,:
    for size in 2, 3, 5, 10:
      # We skip the rank 4, size 10 case: it is slow and conceptually covered
      # by the other cases.
      for extra in [(), (2,), (3,)] + [(3, 2)] * (size < 10):
        shape = extra + (size, size)
        name = '%s_%s' % (dtype.__name__, '_'.join(map(str, shape)))
        setattr(MatrixInverseGradientTest, 'testMatrixInverseGradient_' + name,
                _GetMatrixInverseGradientTest(dtype, shape))
  tf.test.main()