is loss nan?
I don’t know why, but you may try

logits = model_ft(inputs)
out=torch.sigmoid(logits)
mask = 1- torch.isnan(target)
out = out[mask]
target = target[mask]
loss_temp=(torch.abs(out-target))**potenz
loss=torch.mean(loss_temp)
loss.backward()