According to this link, this may be the fastest way.

  
      github.com/pytorch/pytorch
  
  
    
  
	  
  

  
    
      Implement `numpy.random.choice` equivalent 1
    

    
      
        opened 
        
          
        
        Feb 8, 2019
      
      


      
        
          
          LeviViana
        
      
    
  



  🚀 Feature
Implement numpy.random.choice equivalent.
Motivation
In some cases, it is useful to get random samples from a torch Tensor efficiently. For instance, during...



    feature
    module: numpy
    topic: bootcamp
    triaged


  
  
    
    
  
  


import torch

bs = 8
a = torch.zeros(bs, 1)
b = torch.ones(bs, 1)
c = torch.cat([a, b], dim=-1)
choices_flat = c.view(-1)
# index = torch.randint(choices_flat.numel(), (bs,))
# or if replace = False
index = torch.randperm(choices_flat.numel())[:bs]
select = choices_flat[index]

print(select)