在pytorch中沿轴计算非零元素
Count nonzeros element along an axis in pytorch
我有一个(32x750)
张量
tensor([[ 0.0000, 0.0000, 0.0000, ..., 0.0000, 0.0000, 0.0043],
[ 0.0000, 0.0000, 0.0000, ..., 0.0000, 0.0000, 0.0043],
[ 0.0000, 0.0044, 0.0000, ..., 0.0044, 0.0000, 0.0000],
...,
[ 0.0059, 0.0000, 0.0059, ..., 0.0059, 0.0000, 0.0000],
[ 0.0059, 0.0000, 0.0059, ..., 0.0059, 0.0000, 0.0000],
[ 0.0000, 0.0000, 0.0000, ..., 0.0000, 0.0056, 0.0000]], device='cuda:0')
我想获取每行中非零元素的数量。类似的东西 [12 47 0 5 .... 8 7 50]
这个 discussion and 没有解决我的问题,关注 1-D tensor.
的非零元素的数量
谢谢
我有一个(32x750)
张量
tensor([[ 0.0000, 0.0000, 0.0000, ..., 0.0000, 0.0000, 0.0043],
[ 0.0000, 0.0000, 0.0000, ..., 0.0000, 0.0000, 0.0043],
[ 0.0000, 0.0044, 0.0000, ..., 0.0044, 0.0000, 0.0000],
...,
[ 0.0059, 0.0000, 0.0059, ..., 0.0059, 0.0000, 0.0000],
[ 0.0059, 0.0000, 0.0059, ..., 0.0059, 0.0000, 0.0000],
[ 0.0000, 0.0000, 0.0000, ..., 0.0000, 0.0056, 0.0000]], device='cuda:0')
我想获取每行中非零元素的数量。类似的东西 [12 47 0 5 .... 8 7 50]
这个 discussion and 1-D tensor.
谢谢