Filter tensors whose elements are zeros

I have a tensor with the following dimensionality:

torch.Size([30, 5, 90])

Some of the tensors in the second dimension are just 90 zeros. For example:

mytensor[0][0]

tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])

How can I drop those (n, 90) tensors whose all elements are zeros? I want to do this because in the forward propagation, even though those tensors are zeros the bias is added and the output is never zero. I would appreciate any help on this.

I am not sure about the efficnt way. But one way to do this is create an empty tensor, Iterate the current tensor then stack the elements only which you want to the empty tenor using ```torch.stack``

This code snippet should work:

# Setup
x = torch.randn(30, 5, 90)
x[:, 0] = 0.
x[:, 3] = 0.

# Filter
idx = (x != 0.).all(2).all(0)
y = x[:, idx]
print(y.shape)
> torch.Size([30, 3, 90])
1 Like