问题描述
有没有办法在 TensorFlow 中提取方阵的对角线?也就是说,对于这样的矩阵:
[[0, 1, 2],[3, 4, 5],[6, 7, 8]]我想获取元素:[0, 4, 8]
在 numpy 中,这通过 np 非常简单.诊断:
在 TensorFlow 中,有一个 diag 函数,但它只是形成了一个对角线上的参数中指定元素的新矩阵,这不是我想要的.
我可以想象如何通过跨步实现这一点……但我没有看到 TensorFlow 中的张量跨步.
使用 tensorflow 0.8 可以使用 tf.diag_part()
提取对角线元素(参见 文档)
更新
对于 tensorflow >= r1.12 其 tf.linalg.tensor_diag_part
(参见 文档)
Is there a way to extract the diagonal of a square matrix in TensorFlow? That is, for a matrix like this:
[
[0, 1, 2],
[3, 4, 5],
[6, 7, 8]
]
I want to fetch the elements: [0, 4, 8]
In numpy, this is pretty straight-forward via np.diag:
In TensorFlow, there is a diag function, but it only forms a new matrix with the elements specified in the argument on the diagonal, which is not what I want.
I could imagine how this could be done via striding... but I don't see striding for tensors in TensorFlow.
with tensorflow 0.8 its possible to extract the diagonal elements with tf.diag_part()
(see documentation)
UPDATE
for tensorflow >= r1.12 its tf.linalg.tensor_diag_part
(see documentation)
这篇关于在 TensorFlow 中获取矩阵的对角线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!