在 TensorFlow 中获取矩阵的对角线 [英] Get the diagonal of a matrix in TensorFlow

查看:76
本文介绍了在 TensorFlow 中获取矩阵的对角线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 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 中获取矩阵的对角线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆