how to minimize cost function in machine learning