Solved by simenthys in post #2 
              
              
                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?