To solve this problem, you must be know what lead to nan during the training process. I think the logvar.exp() in the following fomula lead to overflow in the running process

could also be this part

BCE = F.binary_cross_entropy(reconstructed_x, x, reduction='sum'

could you provide your exception?