In the first lines of forward you are re-wrapping your predictions and targets in new Variables.
This will detach the tensors from the computation graph, so that no information will flow back to your model.
As Variables are deprecated since 0.4.0, you don’t need to warp tensors anymore.
However, even in older versions these lines would be problematic.
Just try to remove them or do you get any error, if you just use prediction and segmentation to calculate your dice loss?