【发布时间】: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