我需要在 Tensorflow 中求解 A(y)x = b 形式的方程,其中 A 是一个大的稀疏带矩阵,也是其他一些张量(如 y)的函数。当然,解 x 也将是张量 y 的函数。求解 x 后,我想获取 x 相对于 y 的梯度。
我考虑了两个选择: 1. 使用稀疏外部库来有效地反转 A,例如scipy.sparse. 为此,我需要将张量转换为 numpy 数组,然后再转换回张量。这种方法的问题是我无法将渐变磁带与外部库(例如scipy.sparse. 2. 使用与梯度带配合使用的 Tensorflow 矩阵求逆。对于大型矩阵来说,这非常慢,因为它没有利用张量的稀疏性。我无法在 Tensorflow 中找到稀疏反转实现。
我需要的一个简单的小例子:
y = tf.constant(3.14)
A = my_sparse_tensor(shape=(1000, 1000)) # Arbitrary function that returns a sparse tensor
b = tf.ones(shape=(1000, 1))
with tf.GradientTape() as g:
g.watch(y)
A = A * y
x = tf.matmul(sparse_invert(A), b)
dx_dy = g.gradient(x, y)
Run Code Online (Sandbox Code Playgroud)
当然,A 对 y 的依赖性比本例复杂得多。在 Tensorflow …