Check gradient for each dimension.
fixed some bugs in gradient