(ooohhh, I think I finally figured out where the nans come from. Because I have some tensor where only some of the values I use, and the other values are undefined. Then I multiply this tensor by a binary mask, and add it to some other tensor. But … the undefined values can be nan, simply by random chance. and 0 * nan != 0, it still equals nan  )