【发布时间】:2018-05-08 12:42:00
【问题描述】:
比如说,对于一个 N×N 矩阵,只有 m 个元素是非零的。如果我们已经有了 m 个非零元素及其所有索引 (i,j),如何在 Tensorflow 中构建矩阵?
【问题讨论】:
标签: python matrix tensorflow tensor
比如说,对于一个 N×N 矩阵,只有 m 个元素是非零的。如果我们已经有了 m 个非零元素及其所有索引 (i,j),如何在 Tensorflow 中构建矩阵?
【问题讨论】:
标签: python matrix tensorflow tensor
使用tf.SparseTensor,如果需要,使用tf.sparse_tensor_to_dense。例如:
import tensorflow as tf
values = [1, 2, 3, 4]
indices = [[0, 1], [1, 0], [1, 2], [2, 1]]
st = tf.SparseTensor(indices, values, dense_shape=[3, 3])
dt = tf.sparse_tensor_to_dense(st)
with tf.Session() as sess:
result = sess.run(dt)
print(result)
【讨论】: