Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TensorFlow cholesky decomposition

From reading the TensorFlow documentation I see that there is a method for computing the Cholesky decomposition of a square matrix. However, usually when I want to use Cholesky decomposition, I do it for the purposes of solving a linear system where direct matrix inversion might be unstable.

Therefore, I am looking for a method similar to the one implemented in Scipy. Does anyone know if this exists in TensorFlow or if there is a way it could be incorporated?

like image 307
user1936768 Avatar asked Nov 14 '15 14:11

user1936768


People also ask

What does Cholesky decomposition do?

Cholesky decomposition or factorization is a powerful numerical optimization technique that is widely used in linear algebra. It decomposes an Hermitian, positive definite matrix into a lower triangular and its conjugate component. These can later be used for optimally performing algebraic operations.

How do you prove Cholesky decomposition?

A square matrix is said to have a Cholesky decomposition if it can be written as the product of a lower triangular matrix and its transpose (conjugate transpose in the complex case); the lower triangular matrix is required to have strictly positive real entries on its main diagonal.

Where do I find Cholesky factor?

The Cholesky decomposition of a Hermitian positive-definite matrix A is a decomposition of the form A = [L][L]T, where L is a lower triangular matrix with real and positive diagonal entries, and LT denotes the conjugate transpose of L.


2 Answers

user19..8: The way to do this for now if you want to keep things "mostly" in tensorflow would be to do what you and Berci were discussing in the comments: Run the tensorflow graph until the point where you need to solve the linear system, and then feed the results back in with a feed_dict. In pseudocode:

saved_tensor1 = tf.Variable(...)
saved_tensor2 = tf.Variable(...)

start_of_model...
tensor1, tensor2 = various stuff...
do_save_tensor1 = saved_tensor1.assign(tensor1)
do_save_tensor2 = saved_tensor2.assign(tensor2)
your_cholesky = tf.cholesky(your_other_tensor, ...)

## THIS IS THE SPLIT POINT
# Second half of your model starts here
solved_system = tf.placeholder(...)  # You'll feed this in with feed_dict
final_answer = do_something_with(saved_tensor1, saved_tensor2, solved_system)

Then to run the whole thing, do:

_, _, cho = tf.run([do_save_tensor1, do_save_tensor2, your_cholesky])
solution = ... solve your linear system with scipy ...
feed_dict = {solved_system: solution}
answer = tf.run(final_answer, feed_dict=feed_dict)

The key here is stashing your intermediate results in tf.Variables so that you can resume the computation afterwards.

(I'm not promising that what you get out of tf.cholesky is in the right format to feed directly to scipy, or that you shouldn't just pull out the matrix in an earlier step and feed it to scipy, but this overall workflow should work for you).

Note that this will create a performance bottleneck if you're doing heavily multicore or GPU operations and then have to serialize on spitting the matrix out to scipy, but it might also be just fine - depends a lot on your setting.

like image 88
dga Avatar answered Jan 02 '23 12:01

dga


Update (2017/04/23)

TensorFlow now has many linear algebra operations. For instance, checkout tf.cholesky_solve, tf.matrix_solve_ls, tf.matrix_solve, tf.qr, tf.svd, etc. Of course, the original answer below may be helpful as well.

Original Does matrix_inverse do what you need? It uses Cholesky or LU Decomposition, depending on the input. For example,

>>> import tensorflow as tf
>>> x = [[1.,1.],[-2.,3.],[1.,-1.]]
>>> y = [[-1.],[-8.],[3.]]
>>> a = tf.matrix_inverse(tf.matmul(x, x, transpose_a=True))
>>> b = tf.matmul(tf.matmul(a, x, transpose_b=True), y)
>>> with tf.Session():
...   print b.eval()
... 
[[ 1.]
 [-2.]]
like image 28
rhaertel80 Avatar answered Jan 02 '23 11:01

rhaertel80