user1799323
user1799323

Reputation: 659

Tensorflow: creating a diagonal matrix with input on the sub/superdiagonals

I have the following code:

import tensorflow as tf

N = 10
X = tf.ones([N,], dtype=tf.float64)
D = tf.linalg.diag(X, k=1, num_rows=N+1, num_cols=N+1)

print(D)

which, based on the TF2 documentation, I expect to return an 11x11 tensor with X inserted on the first superdiagonal (even without the optional num_rows and num_cols arguments). However, the result is

tf.Tensor(
[[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]], shape=(10, 10), dtype=float64)

Is there something obvious that I am missing?

Upvotes: 1

Views: 495

Answers (1)

thushv89
thushv89

Reputation: 11333

I can tell you why this doesn't work, but I don't know what the fix is. Probably raise a github issue.

If you look at this line in the array_ops.py. It does a compatibility check tf.compat.forward_compatible to see if the compatibility window has expired. Which returns False (For both TF 2.0.0 and 2.1.0rc0) . Due to this reason it executes

return gen_array_ops.matrix_diag(diagonal=diagonal, name=name)

which you can see that none of k, num_rows, num_cols are used while calling. So the method is currently completely oblivious to these parameters if that tf.compat.forward_compatible check fails.

Upvotes: 1

Related Questions