Hi Guys,
I would like to remove zero values of a tensor and “join” the non-zero values in each row of a tensor in format [B, C, H, W]. A naive way would to do out_x = x[x!=0], this approach is bad because would destruct the Tensor dimensions. For resume, I would like to transform an input tensor like this:
in_x = torch.tensor([[[[0.,   0.,   2.,  20., 250.,   0.,   0.,   0.,   0.,   0., 250.,
                      20.,   2.,   0.,   0.,   0.],
                      [0.,   0.,   2.,  20.,   0.,   0.,  20., 250.,   0.,   0., 250.,
                      20.,  20.,   2.,   0.,   0.],
                      [0.,   0.,   0.,   0.,   0.,   0.,   2.,  20., 250.,   0., 250.,
                      20.,  20.,   2.,   0.,   0.],
                      [0.,   2.,  20.,   0.,  20., 250.,   0., 250.,  20.,   0.,  20.,
                       2.,   0.,   0.,   0.,   0.]]]])

In an output tensor like this:
out_x = torch.tensor([[[[  2.,  20., 250., 250.,  20.,   2.,   0.,   0.,   0.,   0.,   0.,
                          0.,   0.,   0.,   0.,   0.],
                       [  2.,  20.,  20., 250., 250., 20.,  20.,   2.,    0.,   0.,   0.,
                          0.,   0.,   0.,   0.,   0.],
                       [  2.,  20., 250., 250.,  20., 20.,   2.,   0.,   0.,   0.,   0.,
                          0.,   0.,   0.,   0.,   0.],
                       [  2.,  20.,  20., 250., 250.,  20.,  20.,   2., 0.,   0.,   0.,
                          0.,   0.,   0.,   0.,   0.]]]])

Note that both Tensors have the same shape: (1,1,4,16)
The solution must not contain For Loops or anything else that degrades performance.
Thanks.