It looks like your custom loss function break the computation graph by using plain Python math functions.
Could you change them to PyTorch methods (e.g. torch.exp, torch.log) and try it again?
PS: You’ve used apostrophes to format your code, while backticks are needed (```), so I’ve formatted your post.