Hi Heng,
I am dealing with a similar problem and you post helps. Thanks!
There is one thing I would like to point out: You probably want to divide gradient by M, if you intended to average gradients for M iterations