Remove Tensor elements based on a list


Does anyone know how to remove elements of a tensor based on a condition on a separate tensor?.

tensor.shape = torch.Size([10, 80, 300]                        # (batch, max_lenght, embedding_dim)
actual_lenghts = tensor([ 7, 1, 0, 14, 26, 17, 6, 0, 10, 4]))

In the given case, i would like to remove the samples with sequence lenght of 0, so i can pass the batch through an LSTM. The output should look like this:

final_tensor.shape = torch.Size([8,80,300])
actual_lenghts = ([7,1,14,26,17,6,0,10,4])


Try the following:

tensor[(actual_lengths != 0).nonzero().squeeze()]

You would have to convert the list of actual lengths to a tensor first, because otherwise there would be no member functions like nonzero() or squeeze()