Moved cost function from trainer