Refactor some code from SGD trainer into separate methods.