Hi,
The problem is that you have a non-floating point Tensor that you set by hand to requires_grad_() again.
You will need to only use differentiable ops here.
To keep the values between 0/1 I would recommend the softmax function that will make sure you get non-zero gradients everywhere (but you won’t be able to reach 0 or 1).
Or you can use clamp. But then the gradients for the values <0 and >1 will be 0.