In case you landed here because of nan values in tensors but without using torch.empty():
In my case the nan got introduced in a torch.nn.functional.softmax(). The softmax will produce a nan if there is only one element and this has a value of -inf (in my case I mask out values by setting them to -float('inf'))
It happens only though if there is only one element in the dimension you apply the softmax. Example:
softmax = torch.nn.functional.softmax(torch.tensor([[0.7], [-float('inf')], [0.5]]), dim=-1)

>>>tensor([[1.],
        [nan],
        [1.]])

Solving it is rather simple; either take another value for masking or just don’t apply a Softmax if your sequence has a length of 1.