I’m sure I am missing something obvious, so perhaps one of you can get me past this current idiocy.

I have binary (or close to binary actually a float) image data (batch, channel, x, y) and I want to add noise to the input… with the catch that it still has to remain between 0 and 1. So I am imagining simply if a pixel is 1 then minus the noise, and if the pixel is 0 then add the noise. But I do not know how to do that efficiently. without going through some slow loops. Any ideas??

```
class noiseLayer(nn.Module):
def __init__(self, x_in, y_in, noisePercentage):
super(noiseLayer, self).__init__()
self.n_p = noisePercentage
self.x_in = x_in
self.y_in = y_in
def forward(self, x, v):
noise_tensor = torch.rand((self.x_in, self.y_in))*(self.n_p)
if v = 1 print ("noise_tensor", noise_tensor)
x = x + noise_tensor
if v = 1 print ("after adding noise", x)
# doesn't work because I get values over 1.
return x
```