Python 如何对张量矩阵进行切片和汇总

Python 如何对张量矩阵进行切片和汇总,python,tensorflow,tensor,Python,Tensorflow,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

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

[[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)
因此,我想删除所有零,得到如下矩阵:

[[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]), 切片(x,[0,6],-1,3] Thx给你的建议

试试这个:

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

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数据类型来解决此问题。我尚未对此进行调查。

如果矩阵的其他部分中的值为0,例如0.12、0、0.13,您将得到错误