【问题标题】:Create new tensor based on indexes from another tensor and allocate with it's values根据另一个张量的索引创建新张量并分配它的值
【发布时间】:2018-11-16 15:52:17
【问题描述】:

我有一个张量t 看起来像这样[[0, 1, 1.5], [1, 1, 7.3], [2, 0, 2.3]],我需要创建形状为 (3, 3, 1) 的新张量 t1 其中t1[t[:, :1], t[:, 1:2]](第一列中的元素用作第一个坐标,元素从第二列作为第二个坐标)分配有来自t 的第三列的元素。喜欢这个t1 = [[[0.0], [1.5], [0.0]], [[0.0], [7.3], [0.0]], [[2.3], [0.0], [0.0]]]。如何在没有循环的 TensorFlow(或 Numpy)中使用矩阵运算来做到这一点?

【问题讨论】:

    标签: python numpy tensorflow numpy-ndarray


    【解决方案1】:

    您可以为此使用tf.sparse_to_dense

    tf.sparse_to_dense(tf.to_int32(t[:, :2]), [3, 3], t[:, 2])[..., None]
    

    【讨论】:

      【解决方案2】:

      怎么样:

      import numpy as np
      
      t = np.asarray([[0,1,1.5],[1,1,7.3],[2,0,2.3]])
      
      t1 = np.zeros([3,3,1])
      t1[t[:,0].astype(int), t[:,1].astype(int),0] = t[:,2]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-15
        • 1970-01-01
        • 1970-01-01
        • 2016-06-20
        • 2018-03-13
        • 2018-07-02
        相关资源
        最近更新 更多