What is an efficient way to replace specific columns in tensor

I need to apply a mask on specific columns of a tensor. I could not find any helping function for that, so i am creating a subset and then applying mask on the subset. Now i need to merge the subset into oringal tensor but i can not find some efficient way.

import torch
X=torch.rand(10,9) 
tensorsize = X.size()
indices = torch.tensor([0,3,7,5,4])  #sorting them will make the process faster?
candidateCF=torch.index_select(X,1,indices)
mask=torch.FloatTensor(candidateCF.size()).uniform_() >= 0.3
output=candidateCF.mul(mask)
############
X[indices]= output    #is there any way to replace output subset into X on specified indices,  

For loop is too time-consuming. currently my code takes 12 seconds for one epoch… :frowning:

If column-wise or row-wise replacements are required you can do:

X[:, indices] = output # for column replacements
X[indices, :] = output # for row replacements

Does this serves your purpose?

1 Like

That’s what I was looking for. Thank you.