Loss function for training the model