Reputation: 5551
I'm sure that there is some logic here, but I can't figure out what the general pattern is. If I apply tf.contrib.distributions.fill_triangular
to a length (n+1)n/2
list, then in what pattern do the entries fill the upper/lower triangle of the resulting matrix?
To illustrate, here is a 5x5 matrix filled by fill_triangular
:
import tensorflow as tf
foo = tf.contrib.distributions.fill_triangular(
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15], upper=True
)
with tf.Session('') as sesh:
print(sesh.run(foo))
gives
[[ 1 2 3 4 5]
[ 0 7 8 9 10]
[ 0 0 13 14 15]
[ 0 0 0 12 11]
[ 0 0 0 0 6]]
OTOH, one might expect a similar behavior to np.triu_indices(5)
, e.g.:
import numpy as np
bar = np.zeros((5,5))
bar[np.triu_indices(5)] = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
print(bar)
which gives
array([[ 1., 2., 3., 4., 5.],
[ 0., 6., 7., 8., 9.],
[ 0., 0., 10., 11., 12.],
[ 0., 0., 0., 13., 14.],
[ 0., 0., 0., 0., 15.]])
What is the general fill pattern for tf.contrib.distributions.fill_triangular(list(range(int((n+1)*n/2))))
?
Upvotes: 0
Views: 363
Reputation: 4475
As the tf official doc mentioned, fill_triangular fills the elements in a clockwise spiral.
import tensorflow as tf
import numpy as np
foo = tf.contrib.distributions.fill_triangular(
np.arange(1,22), upper=True
)
with tf.Session('') as sesh:
print(sesh.run(foo))
#[[ 1->2->3->4-> 5 -> 6] >---->----->------->--------|
# [ 0 8->9->10->11-> 12] >------->------>-------| |
# [ 0 0 15->16->17-> 18] | >--->--->-----| | |
# [ 0 0 0 21<-20<- 19] | | <---<---<---| | |
# [ 0 0 0 0 14<- 13] | <-----<------<-------| |
# [ 0 0 0 0 0 7]] <----<-----<-------<--------|
Upvotes: 1