ML Assignment 8 cost function and gradient