Implementation of cost function for neural net