Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the diagonal of a matrix in TensorFlow

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.

like image 735
theaNO Avatar asked Nov 13 '15 19:11

theaNO


3 Answers

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)

like image 138
lhlmgr Avatar answered Nov 09 '22 09:11

lhlmgr


Currently it is possible to extract diagonal elements with tf.diag_part. Here is their example:

"""
'input' is [[1, 0, 0, 0],
            [0, 2, 0, 0],
            [0, 0, 3, 0],
            [0, 0, 0, 4]]
"""

tf.diag_part(input) ==> [1, 2, 3, 4]

Old answer (when diag_part) was not available (still relevant if you want to achieve something that is not available now):

After looking though the math operations and tensor transformations, it does not look like such operation exists. Even if you can extract this data with matrix multiplications it would not be efficient (get diagonal is O(n)).

You have three approaches, starting with easy to hard.

  1. Evaluate the tensor, extract diagonal with numpy, build a variable with TF
  2. Use tf.pack in a way Anurag suggested (also extract the value 3 using tf.shape
  3. Write your own op in C++, rebuild TF and use it natively.
like image 25
Salvador Dali Avatar answered Nov 09 '22 10:11

Salvador Dali


Use the tf.diag_part()

with tf.Session() as sess:
    x = tf.ones(shape=[3, 3])
    x_diag = tf.diag_part(x)
    print(sess.run(x_diag ))
like image 3
Shaohan Huang Avatar answered Nov 09 '22 09:11

Shaohan Huang