def _compareBatchSelfAdjointEigRank2(self, x, use_gpu=False): with self.test_session() as sess: tf_eig = tf.batch_self_adjoint_eig(tf.constant(x)) tf_out = sess.run([tf_eig])[0] dlist = x.shape d = dlist[-2] self.assertEqual(len(tf_eig.get_shape()), 2) self.assertEqual([d+1, d], tf_eig.get_shape().dims[-2:]) self._testEigs(x, d, tf_out)
def testWrongDimensions(self): # The input to self_adjoint_eig should be 2-dimensional tensor. scalar = tf.constant(1.) with self.assertRaises(ValueError): tf.self_adjoint_eig(scalar) vector = tf.constant([1., 2.]) with self.assertRaises(ValueError): tf.self_adjoint_eig(vector) tensor = tf.constant([[[1., 2.], [3., 4.]], [[1., 2.], [3., 4.]]]) with self.assertRaises(ValueError): tf.self_adjoint_eig(tensor) # The input to batch_batch_self_adjoint_eig should be a tensor of # at least rank 2. scalar = tf.constant(1.) with self.assertRaises(ValueError): tf.batch_self_adjoint_eig(scalar) vector = tf.constant([1., 2.]) with self.assertRaises(ValueError): tf.batch_self_adjoint_eig(vector)
def _compareBatchSelfAdjointEigRank3(self, x, use_gpu=False): with self.test_session() as sess: tf_eig = tf.batch_self_adjoint_eig(tf.constant(x)) tf_out = sess.run([tf_eig])[0] dlist = x.shape d = dlist[-2] self.assertEqual([d+1, d], tf_eig.get_shape().dims[-2:]) # not testing the values. self.assertEqual(dlist[0], tf_eig.get_shape().dims[0]) for i in xrange(dlist[0]): self._testEigs(x[i], d, tf_out[i])
# tf.cholesky x = np.random.rand(10, 10) z_cholesky = tf.cholesky(x) # tf.batch_cholesky batch_x = np.random.rand(10, 5, 5) z_batch_cholesky = tf.batch_cholesky(x) # tf.self_adjoint_eig x = np.random.rand(10, 8) z_self_adjoint_eig = tf.self_adjoint_eig(x) # tf.batch_self_adjoint_eig batch_x = np.random.rand(10, 8, 5) z_batch_self_adjoint_eig = tf.batch_self_adjoint_eig(batch_x) with tf.Session() as sess: print "tf.diag" print sess.run(z_diag) print "tf.transpose" print sess.run(z_transpose) print "tf.matmul" print sess.run(z_matmul) print "tf.batch_matmul" print sess.run(z_batch_matmul) print "tf.matrix_determinant"
def test_BatchSelfAdjointEig(self): t = tf.batch_self_adjoint_eig(np.array(3 * [3, 2, 2, 1]).reshape(3, 2, 2).astype("float32")) self.check(t, ndigits=4, stats=True)