The cost function is primarily used to evaluate the current performance of the model by comparing the true class labels (y_true_cls) with the predicted class labels (y_pred_cls). Based on the current performance, the optimizer then fine-tunes the network parameters, such as weights and biases, to further improve its performance.