我有同样的问题,我为它创建了一个自定义操作。只要您不想将梯度计算为 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
该解决方案基于我在https://gist.github.com/harpone/3453185b41d8d985356cbe5e57d67342找到的代码
至少对我来说,解决方案非常快。如果您发现错误(例如在梯度计算中),请告诉我