我确信这里有一些逻辑,但我不知道一般的模式是什么。如果我将tf.contrib.distributions.fill_triangular应用于length (n+1)n/2列表,那么这些条目将以什么模式填充结果矩阵的上/下三角形?
为了说明这一点,下面是由fill_triangular填充的5x5矩阵
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))给出
[[ 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,人们可能会期望类似于np.triu_indices(5)的行为,例如:
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)这给了我们
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.]])tf.contrib.distributions.fill_triangular(list(range(int((n+1)*n/2))))的一般填充模式是什么
发布于 2019-09-24 07:51:14
正如tf官方文档中提到的,fill_triangular以顺时针螺旋的方式填充元素。
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]] <----<-----<-------<--------|https://stackoverflow.com/questions/58071102
复制相似问题