【问题标题】:Tensorflow creat matrx from its elements and elements' indexTensorflow 从其元素和元素的索引创建矩阵
【发布时间】:2018-05-08 12:42:00
【问题描述】:

比如说,对于一个 N×N 矩阵,只有 m 个元素是非零的。如果我们已经有了 m 个非零元素及其所有索引 (i,j),如何在 Tensorflow 中构建矩阵?

【问题讨论】:

    标签: python matrix tensorflow tensor


    【解决方案1】:

    使用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)
    

    【讨论】:

      猜你喜欢
      • 2021-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-09
      • 1970-01-01
      • 1970-01-01
      • 2016-06-04
      • 2020-02-18
      相关资源
      最近更新 更多