The dice implementation looks fine and a sanity check yields reasonable loss values:
target = torch.zeros(5, 100, 100)
target[:, 50:, 50:] = 1.

for idx in range(0, 51):
    input = torch.zeros(5, 100, 100)
    input[:, idx:(idx+50), idx:(idx+50)] = 1.

    loss = dice(input, target, reduction='mean')  
    print(loss)

Here I just push a rectangle of ones into the target.
Note that I’ve removed the sigmoid call in your dice loss implementation, as I already use probabilities for input.
Are you dealing with a multi-class classification use case?
If the gradient is exploding, you might push the parameters into really high values, which might thus yield a nan loss.
Which optimizer are you using?