zii
zii

Reputation: 35

Creating a tridiagonal 2-D tensor in Tensorflow

Given three 1-D tensors (i.e. vectors) in tensorflow, is there a clever (efficient) way to make a tri-diagonal matrix by putting one vector on the sub-diagonal, another on the diagonal, and finally one on the super-diagonal.

tf.diag makes it easy to create a diagonal matrix out of one of the vectors.

Can you also give an example of how to do it with a tf.while_loop.

Upvotes: 1

Views: 328

Answers (1)

javidcf
javidcf

Reputation: 59711

This is one way to do it with tf.scatter_nd:

import tensorflow as tf

def tridiagonal(diag, sub, sup):
    n = tf.shape(diag)[0]
    r = tf.range(n)
    ii = tf.concat([r, r[1:], r[:-1]], axis=0)
    jj = tf.concat([r, r[:-1], r[1:]], axis=0)
    idx = tf.stack([ii, jj], axis=1)
    values = tf.concat([diag, sub, sup], axis=0)
    return tf.scatter_nd(idx, values, [n, n])

diag = tf.placeholder(tf.int32, [None])
sub = tf.placeholder(tf.int32, [None])
sup = tf.placeholder(tf.int32, [None])
tri = tridiagonal(diag, sub, sup)
with tf.Session() as sess:
    print(sess.run(tri, feed_dict={diag: [0, 1, 2, 3],
                                   sub: [4, 5, 6],
                                   sup: [7, 8, 9]}))

Output:

[[0 7 0 0]
 [4 1 8 0]
 [0 5 2 9]
 [0 0 6 3]]

Upvotes: 1

Related Questions