张量流中具有相关变量的相关矩阵

一个OK

我需要张量流中的k x k对称矩阵,其中对角线是常数1而非对角线条目是可变的。

即:

k = 3
a = tf.Variable(1.0*tf.ones([k*(k-1)/2, 1]))

然后:

Rho =
[[1, a[0], a[1]],
[[a[0], 1, a[2]],
[[a[1], a[2], 1]]

有一些简单明了tensorflow实现从让我kaRho求解相关矩阵很常见,因此我怀疑应该在tf中直接实现该矩阵。

谢谢!

一个提案

按照以上对逻辑k=3,这将是足够获得严格-上三角矩阵Ua然后Rho将简单地是:

U = tf.[strictly_upper_triangular](a)
Rho = tf.eye(k) + U + tf.transpose(U)

有这样的功能[strictly_upper_triangular]吗?

Jdehesa

这是一种可能性:

import tensorflow as tf
import numpy as np

k = 5
# Using a constant instead now for testing
#a = tf.Variable(1.0 * tf.ones([(k * (k - 1)) // 2, 1]))
a = 10 * tf.range((k * (k - 1)) // 2, dtype=tf.float32) + 10
# Make indices and mask
idx = np.zeros((k, k), dtype=int)
mask = np.zeros((k, k), dtype=bool)
triu_idx = np.triu_indices(k, 1)
idx[triu_idx] = np.arange((k * (k - 1)) // 2)
mask[triu_idx] = True
# Make upper triangular matrix
u = tf.where(mask, tf.gather(a, idx), tf.zeros((k, k), dtype=a.dtype))
# Final matrix
Rho = tf.eye(k, dtype=u.dtype) + u + tf.transpose(u)
print(sess.run(Rho))

输出:

[[   1.   10.   20.   30.   40.]
 [  10.    1.   50.   60.   70.]
 [  20.   50.    1.   80.   90.]
 [  30.   60.   80.    1.  100.]
 [  40.   70.   90.  100.    1.]]

缺点是k必须在图形构造时知道,因为索引和掩码矩阵是用NumPy构造的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章