I have a PyTorch tensor with 0 and non 0 values, and I want to copy the non zero values in a new tensor such that it is as compact as possible. ie its shape is [num_rows,max([number of non0 values in each row])]. What is the a vectorised way to do this without putting a for loop? The following image should illustrate(black = non zero values):
Asked
Active
Viewed 287 times
1
-
2This works - [`Python: Justifying NumPy array`](https://stackoverflow.com/questions/44558215/python-justifying-numpy-array)? – Divakar Oct 01 '20 at 19:01
-
Please explain exactly what output you expect.The output you gave in the illustration isn't the most compact representation of the input's non-zero values. In fact, there are much less white(assuming they refer to the non-zero pixels) pixels in the output than in the input. – Gil Pinsky Oct 01 '20 at 20:12
-
Okay, I updated the convention : (black = non zero values) – Amogh Oct 01 '20 at 22:24