non vectorized implementation of neural network's cost function