One guideline for nan in pytorch is that: Try exclude it in autograd.
loss_temp=(torch.abs(out-target))**potenz, in this step  target is stored as buffer for back prop, so it should not include nan value.