Implement gradient descent.