Ok there is something wrong with my loss function, I used tensor.std() on a single element tensor with biased=False, it returns nan value, I changed it to biased=True when encountering single element list and it gives me 0 which is the expected result and solved my problem.