TensorFlow胆汁分解



通过阅读TensorFlow文档,我发现有一种方法可以计算方形矩阵的Cholesky分解。然而,通常当我想使用Cholesky分解时,我这样做是为了求解直接矩阵反演可能不稳定的线性系统。

因此,我正在寻找一种类似于在Scipy中实现的方法。有人知道TensorFlow中是否存在这种情况,或者是否有办法将其合并?

user19..8:如果你想让事情"大部分"保持在tensorflow中,那么目前的方法是做你和Berci在评论中讨论的事情:运行tensorflow图,直到你需要求解线性系统的点,然后用feed_dict反馈结果。在伪代码中:

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)

然后运行整个过程,执行:

_, _, 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)

这里的关键是将中间结果隐藏在tf.Variables中,以便以后可以继续计算。

(我不保证你从tf.cholesky中得到的内容是直接提供给scipy的正确格式,也不保证你不应该在前面的步骤中提取矩阵并将其提供给sciy,但整个工作流程应该对你有效)。

请注意,如果你正在进行大量的多核或GPU操作,然后必须在将矩阵输出到scipy时进行串行化,这将造成性能瓶颈,但这也可能很好,这在很大程度上取决于你的设置。

更新(2017/04/23)

TensorFlow现在有许多线性代数运算。例如,签出tf.cholesky_solve、tf.matrix_solve_ls、tf.matrix_solve、tf.qr、tf.svd等。当然,下面的原始答案也可能有帮助。

原件矩阵_逆能满足你的需要吗?它使用Cholesky或LU分解,具体取决于输入。例如,

>>> 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.]]

最新更新