Summary
I solved this problem by reducing num_coefs from 27 to 3. This parameter indicates the number of fully connected layers I have added to resnet. Each layer has 50 classes. And I calculated the loss of each fc separately.Each loss contains mse and cross entropy.
But who can explain why 27 fc layers will produce nan and 3 will not?