def testConcurrentExecutesWithoutError(self): all_ops = [] with self.session(use_gpu=True) as sess: for compute_v_ in True, False: matrix1 = random_ops.random_normal([5, 5], seed=42) matrix2 = random_ops.random_normal([5, 5], seed=42) if compute_v_: e1, v1 = linalg_ops.eig(matrix1) e2, v2 = linalg_ops.eig(matrix2) all_ops += [e1, v1, e2, v2] else: e1 = linalg_ops.eigvals(matrix1) e2 = linalg_ops.eigvals(matrix2) all_ops += [e1, e2] val = self.evaluate(all_ops) self.assertAllEqual(val[0], val[2]) # The algorithm is slightly different for compute_v being True and False, # so require approximate equality only here. self.assertAllClose(val[2], val[4]) self.assertAllEqual(val[4], val[5]) self.assertAllEqual(val[1], val[3])
def Test(self): np.random.seed(1) n = shape_[-1] batch_shape = shape_[:-2] np_dtype = dtype_.as_numpy_dtype def RandomInput(): # Most matrices are diagonalizable a = np.random.uniform(low=-1.0, high=1.0, size=n * n).reshape([n, n]).astype(np_dtype) if dtype_.is_complex: a += 1j * np.random.uniform(low=-1.0, high=1.0, size=n * n).reshape([n, n]).astype(np_dtype) a = np.tile(a, batch_shape + (1, 1)) return a if dtype_ in (dtypes_lib.float32, dtypes_lib.complex64): atol = 1e-4 else: atol = 1e-12 a = RandomInput() np_e, np_v = np.linalg.eig(a) with self.session(): if compute_v_: tf_e, tf_v = linalg_ops.eig(constant_op.constant(a)) # Check that V*diag(E)*V^(-1) is close to A. a_ev = math_ops.matmul( math_ops.matmul(tf_v, array_ops.matrix_diag(tf_e)), linalg_ops.matrix_inverse(tf_v)) self.assertAllClose(self.evaluate(a_ev), a, atol=atol) # Compare to numpy.linalg.eig. CompareEigenDecompositions(self, np_e, np_v, self.evaluate(tf_e), self.evaluate(tf_v), atol) else: tf_e = linalg_ops.eigvals(constant_op.constant(a)) self.assertAllClose(SortEigenValues(np_e), SortEigenValues(self.evaluate(tf_e)), atol=atol)