Это один из способов сделать это с 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]}))
Вывод:
[[0 7 0 0]
[4 1 8 0]
[0 5 2 9]
[0 0 6 3]]