Have a look at @ptrblck’s answer:

  
    
    
    Advanced indexing with torch.topk 
  
  
    Would this work: 
x = torch.randn(3, 10, 10)
idx = torch.topk(x, k=2, dim=0)[1]
x.scatter_(0, idx, 100)
print(x)
  


you can get the topk values and indices using torch.topk() API.