Fixes to sgd.py, courtesy of Sina