提问人:P.Carlino 提问时间:8/28/2023 更新时间:9/5/2023 访问量:23
TensorFlow 中的同步逆矩阵计算,用于正交匹配追踪
Simultaneous Inverse Matrix Computations in TensorFlow for Orthogonal Matching Pursuit
问:
我目前正在创建一个可以同时在不同补丁上执行的正交匹配追求 (OMP) 版本,利用 TensorFlow 的强大功能通过静态图编译进行优化。
我为以下算法主循环的单个步骤提供了伪代码:
support = tf.TensorArray(dtype=tf.int64, size=1, dynamic_size=True)
# For each element in patches, compute the projection on the dictionary using only TensorFlow API
dot_products = tf.abs(tf.matmul(A, patches, transpose_a=True))
max_index = tf.argmax(dot_products, axis=1)
support = support.write(i, max_index + 1)
support = support.write(i + 1, max_index + 1)
idx = support.stack()
non_zero_rows = tf.reduce_all(tf.not_equal(idx, 0), axis=1)
idx = tf.boolean_mask(idx, non_zero_rows) - 1
A_tilde = tf.gather(A, idx, axis=1)
m, n = A.shape
selected_atoms = tf.matmul(A_tilde, A_tilde, transpose_a=True)
在获得 selected_atoms 后,这是一个由 n 个大小为 nxm 的矩阵组成的 3D 张量,我需要求解最小二乘问题 为此,我需要计算 的逆函数。有没有一种方法使用 TensorFlow 的 API 来同时计算 l 个逆矩阵并将它们存储在形状为 lxnxm 的 3D 张量中?我将不胜感激任何指导。|patches - selected_atoms * x_coeff| ** 2
selected_atoms.T @ selected_atoms
谢谢。
答:
0赞
Juan Carlos Ramirez
9/5/2023
#1
要获得伪逆,可以使用 pinv 函数,它会做你想要的(对于最小二乘法,伪逆是明确定义的,而反函数可能不是)。
import tensorflow as tf
x_batch = tf.constant([[[1., 0.4, 0.5],
[0.4, 0.2, 0.25],
[0.5, 0.25, 0.35]],
[[0.5, 0, 0],
[0, 0.5, 0],
[0, 0, 0.5]]])
tf.linalg.pinv(x_batch)
函数调用返回:
tf.Tensor: shape=(2, 3, 3), dtype=float32, numpy=
array([[[ 4.9999995e+00, -9.9999933e+00, -4.2915344e-06],
[-9.9999905e+00, 6.6666672e+01, -3.3333347e+01],
[-6.1988831e-06, -3.3333344e+01, 2.6666681e+01]],
[[ 2.0000000e+00, 0.0000000e+00, 0.0000000e+00],
[ 0.0000000e+00, 2.0000000e+00, 0.0000000e+00],
[ 0.0000000e+00, 0.0000000e+00, 2.0000000e+00]]], dtype=float32)
如果你只想要回归的系数,你不需要存储pseudo_inverse,但 pinv*patches(可能会节省大量内存)请参阅:http://www.seas.ucla.edu/~vandenbe/133A/lectures/ls.pdf
评论