Remove column from tensor?

What is the proper way to remove a column from a tensor?

What I’m searching is the proper way to do the following (removing the 3):

t = torch.tensor([0,1,2,3,4,5])
t = torch.cat( ( t[:3], t[4:] ) )

Thanks in advance.

1 Like

Does boolean indexing work with torch tensors? Haven’t tried it myself yet.

a = np.arange(9, -1, -1)     # a = array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
b = a[np.arange(len(a))!=3]  # b = array([9, 8, 7, 5, 4, 3, 2, 1, 0])

Hi,

Did anyone figure an efficient way to pop up (delete) an entire column from a torch tensor? Do we have a function in PyTorch to do that? just like we have numpy.delete. Thanks!

HI @dev-team ,

Can somebody help out here ?

1 Like

Okay, so here is a less memory efficient way to delete last n columns. I have only tested it on a 2-D tensor.

>>> def delete_last_n_columns(a, n):
...     n_rows = a.size()[0]
...     n_cols = a.size()[1]
...     assert(n<n_cols)
...     first_cols = n_cols - n
...     mask = torch.arange(0,first_cols)
...     b = torch.index_select(a,1,mask) # Retain first few columns; delete last_n columns
...     return b
... 
>>> a.size()
torch.Size([10, 536])
>>> b = delete_last_n_columns(a,512)
>>> b.size()
torch.Size([10, 24])