【问题标题】:How to slice and summarize tensor matrix如何对张量矩阵进行切片和汇总
【发布时间】:2020-10-01 17:19:52
【问题描述】:

如何对张量矩阵进行切片和求和。我有一个这样的矩阵:

[[0.   0.   0.   0.12 0.75 0.13 0.   0.   0.  ]
 [0.   0.   0.   0.06 0.89 0.05 0.   0.   0.  ]
 [0.   0.   0.   0.3  0.39 0.31 0.   0.   0.  ]
 [0.   0.   0.   0.18 0.63 0.19 0.   0.   0.  ]
 [0.   0.   0.   0.   0.   0.   0.09 0.03 0.89]
 [0.   0.   0.   0.14 0.7  0.16 0.   0.   0.  ]
 [0.   0.   0.   0.   0.   0.   0.12 0.02 0.86]
 [0.   0.   0.   0.   0.   0.   0.1  0.02 0.88]
 [0.   0.   0.   0.03 0.93 0.04 0.   0.   0.  ]
 [ 0.06 0.89 0.05 0.   0.   0.  0.   0.   0.  ]], shape=(10, 9), dtype=float64)

因此,我想删除所有零并得到这样的矩阵:

[[0.12 0.75 0.13]
 [0.06 0.89 0.05]
 [0.3  0.39 0.31]
 [0.18 0.63 0.19]
 [0.09 0.03 0.89]
 [0.14 0.7  0.16]
 [0.12 0.02 0.86]
 [0.1  0.02 0.88]
 [0.03 0.93 0.04]
 [0.06 0.89 0.05]], shape=(10, 3), dtype=float64)

我想我应该将输入矩阵分成三部分 shape=(10,3) 并总结它们 tf.math.add(tf.math.add(tf.slice(x, [0, 0], [- 1, 3]), tf.slice(x, [0, 3], [-1, 3])), tf.slice(x, [0, 6], [-1, 3] 谢谢你的建议

【问题讨论】:

    标签: python tensorflow tensor


    【解决方案1】:

    试试这个:

    import tensorflow as tf
    
    x = [[0., 0., 0., 0.12, 0.75, 0.13, 0., 0., 0.],
         [0., 0., 0., 0.06, 0.89, 0.05, 0., 0., 0.],
         [0., 0., 0., 0.3, 0.39, 0.31, 0., 0., 0.],
         [0., 0., 0., 0.18, 0.63, 0.19, 0., 0., 0.],
         [0., 0., 0., 0., 0., 0., 0.09, 0.03, 0.89],
         [0., 0., 0., 0.14, 0.7, 0.16, 0., 0., 0.],
         [0., 0., 0., 0., 0., 0., 0.12, 0.02, 0.86],
         [0., 0., 0., 0., 0., 0., 0.1, 0.02, 0.88],
         [0., 0., 0., 0.03, 0.93, 0.04, 0., 0., 0.],
         [0.06, 0.89, 0.05, 0., 0., 0., 0., 0., 0.]]
    
    tf.reshape(tf.gather_nd(x, tf.where(tf.not_equal(x, 0))), (-1, 3))
    
    <tf.Tensor: shape=(10, 3), dtype=float32, numpy=
    array([[0.12, 0.75, 0.13],
           [0.06, 0.89, 0.05],
           [0.3 , 0.39, 0.31],
           [0.18, 0.63, 0.19],
           [0.09, 0.03, 0.89],
           [0.14, 0.7 , 0.16],
           [0.12, 0.02, 0.86],
           [0.1 , 0.02, 0.88],
           [0.03, 0.93, 0.04],
           [0.06, 0.89, 0.05]], dtype=float32)>
    

    【讨论】:

    • 如果矩阵的其他部分为0,例如0.12、0.、0.13,您将得到错误
    【解决方案2】:

    TensorFlow 2.x

    import tensorflow as tf
    
    x = tf.convert_to_tensor(
        [[0., 0., 0., 0.12, 0.75, 0.13, 0., 0., 0.],
         [0., 0., 0., 0.06, 0.89, 0.05, 0., 0., 0.],
         [0., 0., 0., 0.3, 0.39, 0.31, 0., 0., 0.],
         [0., 0., 0., 0.18, 0.63, 0.19, 0., 0., 0.],
         [0., 0., 0., 0., 0., 0., 0.09, 0.03, 0.89],
         [0., 0., 0., 0.14, 0.7, 0.16, 0., 0., 0.],
         [0., 0., 0., 0., 0., 0., 0.12, 0.02, 0.86],
         [0., 0., 0., 0., 0., 0., 0.1, 0.02, 0.88],
         [0., 0., 0., 0.03, 0.93, 0.04, 0., 0., 0.],
         [0.06, 0.89, 0.05, 0., 0., 0., 0., 0., 0.]]
         )
    
    
    res = tf.reshape(x[(x!=0)], (-1,3))
    

    如果每行有可变数量的正数。

    x_ragged = tf.RaggedTensor.from_tensor(x)
    mask_ragged = tf.RaggedTensor.from_tensor(x!=0)
    
    res = tf.ragged.boolean_mask(x_ragged, mask_ragged)
    

    PS:第二种解决方案会导致数字的微小差异(例如,0.12 变为 0.199999)(可能是数据类型问题)。您可能可以通过强制使用 f32 或 f64 数据类型来解决此问题。我没有调查过。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-11
      • 1970-01-01
      • 1970-01-01
      • 2018-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多