What about
tensor[tensor==4] = 16

Assuming you want to replace for example each 4 with a 16.
Edit: you could also do this with a tensor, but you would need to do it like tensor[tensor==4] = sixteens_tensor.clone() if you want to do it in autograd on a leaf variable. Also the sixteens_tensor must contain exactly as many values as there are 4s in tensor