Memory Tensorflow中的稀疏矩阵三角解?
在Tensorflow中是否有一个用稀疏三角矩阵求解Ax=b的实现?(对应于tf.矩阵_三角形_solve()) 好的,如果我们有一个,例如,作为一个具有稀疏矩阵表示的下三角矩阵,我们需要用tf.sparse_to_dense()将它转换为稠密矩阵 但是,如果A具有非常大的维度(例如16416x16416)和非常稀疏的条目(例如0.018%(大约45216个非零),则它将占用大量内存 我认为,如果我们能够利用稀疏矩阵解算器,例如在Tensorflow中,对于带状结构的矩阵,这将非常有帮助 对不起,如果我的问题不相关。 例如,如果对此有任何解决方案,我将非常感谢您的帮助Memory Tensorflow中的稀疏矩阵三角解?,memory,tensorflow,sparse-matrix,linear-equation,Memory,Tensorflow,Sparse Matrix,Linear Equation,在Tensorflow中是否有一个用稀疏三角矩阵求解Ax=b的实现?(对应于tf.矩阵_三角形_solve()) 好的,如果我们有一个,例如,作为一个具有稀疏矩阵表示的下三角矩阵,我们需要用tf.sparse_to_dense()将它转换为稠密矩阵 但是,如果A具有非常大的维度(例如16416x16416)和非常稀疏的条目(例如0.018%(大约45216个非零),则它将占用大量内存 我认为,如果我们能够利用稀疏矩阵解算器,例如在Tensorflow中,对于带状结构的矩阵,这将非常有帮助 对不起
谢谢。TF里有。因此,您目前唯一的方法(正如您所确定的)是我也有同样的问题,我为它创建了一个自定义op。只要您不想将渐变计算到A中,并且A保持不变,那么以下代码应该会有所帮助:
import tensorflow as tf
import numpy as np
from scipy.sparse import linalg as sla
import scipy
lu = sla.splu(A)
# Define custom py_func which takes also a grad op as argument:
def py_func(func, inp, Tout, stateful=True, name=None, grad=None):
rnd_name = 'PyFuncGrad' + str(np.random.randint(0, 1E+8))
tf.RegisterGradient(rnd_name)(grad)
g = tf.get_default_graph()
with g.gradient_override_map({"PyFunc": rnd_name}):
return tf.py_func(func, inp, Tout, stateful=stateful, name=name)
def sparse_solve(x, lu, dtype=tf.float64, name=None):
with tf.name_scope(name, 'SparseSolve', [x]) as name:
solve_x = py_func(lu.solve,
[x],
[dtype],
name=name,
grad=_SparseSolveGrad(dtype, lu))
return solve_x[0]
class _SparseSolveGrad:
def __init__(self, dtype, lu):
self.dtype = dtype
self.lu = lu
def __call__(self, op, grad):
x = op.inputs[0]
y = tf.conj(tf.py_func(self.lu.solve, [tf.conj(grad)], self.dtype))
return y
该解决方案基于我在上找到的代码
至少对我来说,解决方案非常快。如果发现错误(例如在梯度计算中),请告诉我。是的,正确。如果我们能用稀疏张量做得更多,那将非常好。当然会很好,但现在不可能了