Clip values of Variable image

I’m using pytorch autograd to update pixels of an image. I’ve noticed that after a while, the pixels start going out of range (only values in (0,1) make sense). What is the correct way to keep the pixel values in the desired range?

It seems the following snippet is working as it should:

optimizer.step()
optimizer.zero_grad()
x.data.clamp_(0,1)

Will this have some unwanted side effects?

afaik, that snippet should work fine.

1 Like