How do I train such a network?

what are you exactly looking for? a pointer or a full blown example?
Generally, you can write your autograd expressions in free form and call backward on the final loss of the classifier.