Tensorflow:创建张量A,使A [i] [i - 1] = A [i - 1] [j] = 1

时间:2016-12-16 15:31:14

标签: python tensorflow

我必须在张量流中创建一个二维张量A,这样A [i] [i - 1] = A [i - 1] [j] = 1,其他元素等于零。

这个张量的形状应该等于另一个张量的形状,这就是为什么我不能用numpy数组创建它。

请问你能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

size = 100
indices = []
for i in xrange(size-1):
  indices.append([i, i+1])
  indices.append([i+1, i])

dense = tf.sparse_to_dense(indices, [size, size], [1]*2*(size-1))
sess = tf.Session()
sess.run(dense)