title: Efficient way of selectively replacing vectors from a tensor in pytorch
body: <p>Given a batch of text sequences, the same is converted into a tensor with each word represented using word embeddings or vectors (of 300 dimensions). I need to selectively replace vectors for certain specific words with a new set of embeddings. Further, this replacement will occur only for not all occurrences of the specific word, but only randomly. Currently, I have the following code to achieve this. It traverses through every word using 2 for loops, check if the word is in a specified list, <code>splIndices</code>. Then it checks if the word needs to be replaced or not, based on T or F value in <code>selected_</code>.</p>
  
  <p>But could this be done in a more efficient manner?</p>
  
  <p>The below code may not be an MWE, but I have tried to simplify the code by removing the specifics, so as to focus on the problem. Please ignore the semantics or purpose of the code as it may not have been appropriately represented in this snippet. The question is about improving performance.</p>
  
  <pre class="lang-py prettyprint-override"><code>
  splIndices = [45, 62, 2983, 456, 762]  # vocabulary indices which needs to be replaced
  splFreqs = 2000  # assuming the words in splIndices occurs 2000 times
  selected_ = Torch.Tensor(2000).uniform_(0, 1) &gt; 0.2  # Tensor with 20% of the entries True
  replIndexCtr = 0  # counter for selected_
  
  # Dictionary with vectors to be replaced. This is a dummy function.
  # Original function depends on some property of the word
  diffVector = {45: Torch.Tensor(300).uniform_(0, 1), ...... 762: Torch.Tensor(300).uniform_(0, 1) } 
  
  embeding = nn.Embedding.from_pretrained(embedding_matrix, freeze=False)
  tempVals = x  # shape [32, 41] - batch of 32 sequences with 41 words each
  x = embeding(x) # shape [32, 41, 300] - the sequence now has replaced vocab indices with embeddings
  
  # iterate through batch for sequences
  for i, item in enumerate(x):
      # iterate sequences for words
      for j, stuff in enumerate(item):
          if tempVals[i][j].item() in splIndices: 
              if self.selected_[replIndexCtr] == True:                   
                  x[i,j] = diffVector[tempVals[i][j].item()]
                  replIndexCtr += 1
  
  
  </code></pre>