Vectorised cost function for neural networks