There might be different reasons for the divergence, such as an exploding loss.
I would also not recommend to reimplement already provided loss functions, if the custom ones don’t add any new functionality (e.g. soft labels etc.), as e.g. nn.CrossEntropyLoss uses internally the log-sum-exp trick for numerical stability, which is often missing in custom implementations.