Compute cost and gradient for logistic regressio