Yes. To the autograd machinery, any scalar function will do as a loss and it will just go over the computational graph regardless of how you arrived there (within the restrictions of needing differentiable things and only following nodes that have requires_grad, which e.g. the true labels usually do not have).
Best regards
Thomas