Extracted a SGDTrainer class from the code in the tutorials.