# Loss function in classification yolov8

**URL:** <https://community.ultralytics.com/t/loss-function-in-classification-yolov8/544>\
**Category:** YOLO\
**Tags:** yolov8, troubleshooting\
**Created:** [November 27, 2024, 10:54am UTC](https://community.ultralytics.com/t/loss-function-in-classification-yolov8/544 "2024-11-27T10:54:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aster\_Tournoy](https://sea1.discourse-cdn.com/flex001/user_avatar/community.ultralytics.com/aster_tournoy/32/435_2.png) [@Aster\_Tournoy](https://community.ultralytics.com/u/Aster_Tournoy)\
**Post date:** [November 27, 2024, 10:54am UTC](https://community.ultralytics.com/t/loss-function-in-classification-yolov8/544/1 "2024-11-27T10:54:20Z")

</div>

I tried to manually calculate the validation loss of my model, using the ‘probs’ and then taking the logarithmic function (CE loss). When I compared this I had different results, but when applying softmax it resulted the right answer. I was wondering if this softmax is not redundant as I’m using the probabilities (and not the logits)? So is the probs before applying softmax? Or how can this be the case?

I tried the same for the training loss and I achieved similar results when I did not add the extra softmax.

---

<div class="post-metadata">

**Author:** ![pderrenger](https://sea1.discourse-cdn.com/flex001/user_avatar/community.ultralytics.com/pderrenger/32/73_2.png) [@pderrenger](https://community.ultralytics.com/u/pderrenger)\
**Post date:** [November 27, 2024, 1:27pm UTC](https://community.ultralytics.com/t/loss-function-in-classification-yolov8/544/2 "2024-11-27T13:27:14Z")

</div>

Hello! 👋

Great question—let’s clarify a few things about how the loss function works in YOLO models like YOLOv8, especially for classification tasks. The key here lies in understanding the role of logits, softmax, and probabilities in the loss calculation.

### Validating CE Loss Calculation

1. **Logits vs. Probabilities:**

2. **Softmax in YOLOv8 Training:**

3. **Why Softmax Is Not Redundant:**

### Why Training vs. Validation Behavior Differs

For training, the softmax step is typically part of the framework’s internal implementation (e.g., PyTorch). That’s why you might not need to add an extra softmax manually when replicating training losses. Ensure alignment in how “probs” are defined for both cases during your manual validation.

### Additional Tips

Feel free to review Ultralytics YOLOv8 classification documentation for more insights: [YOLOv8 Classification Docs](https://docs.ultralytics.com/tasks/classify/). If you’d like, you can also look into the model implementation by downloading the source code to verify how the logits are processed during training and validation.

Hopefully, this clears things up! Let me know if you have more questions 😊

---

<div class="post-metadata">

**Author:** ![Aster\_Tournoy](https://sea1.discourse-cdn.com/flex001/user_avatar/community.ultralytics.com/aster_tournoy/32/435_2.png) [@Aster\_Tournoy](https://community.ultralytics.com/u/Aster_Tournoy)\
**Post date:** [November 27, 2024, 2:24pm UTC](https://community.ultralytics.com/t/loss-function-in-classification-yolov8/544/3 "2024-11-27T14:24:32Z")

</div>

> [@pderrenger](#):
>
> 1. 
> - using `probs`, double-check if they are pre-softmax or post-softmax. It seems YOLO outputs logits, so softmax isn’t redundant—it’s an integral part of the CE Loss computation.
> 
> ### Why Training vs. Validation B

Hey, thank you for the quick and helpful reply! This already clarified a lot. However, I used the training data as input (to find the losses the other way round), similarly I used the validation data and test data as input to evaluate the models behaviour. Originally I wanted to calculate the training accuracy, thus I used the training data as input to the model itself, to see which were labelled right and wrong. So, I used the probs the same way for the validation and train set and found that using the softmax is not necessary for the training loss but is necessary for the validation loss, hence my confusion. Below some numbers for clarification; keeping in mind that I extracted the probs exactly the same way for the two cases…  
train loss (by yolov8): 0.05  
train loss (probs): 1.5  
train loss (softmax(probs): 0.04

val loss (by yolov8): 1.4  
train loss (probs): 0.05  
train loss (softmax(probs): 1.4

---

<div class="post-metadata">

**Author:** ![Toxite](https://sea1.discourse-cdn.com/flex001/user_avatar/community.ultralytics.com/toxite/32/123_2.png) [@Toxite](https://community.ultralytics.com/u/Toxite)\
**Post date:** [November 27, 2024, 8:07pm UTC](https://community.ultralytics.com/t/loss-function-in-classification-yolov8/544/4 "2024-11-27T20:07:51Z")

</div>

It’s because when not in training mode, the output is softmaxed.

> <https://github.com/ultralytics/ultralytics/blob/386a3b7625b3263989650d69eec6ed3b1ce067cd/ultralytics/nn/modules/head.py#L299>

So training loss is not softmaxed, while validation loss is.
