liveBook · Manning

Cross entropy loss

As explained in Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI

CrossEntropyLoss is a PyTorch loss-function module that combines class-logit normalization with negative log-likelihood for classification.

Cross-entropy loss trains classification models by combining class-logit normalization with negative log-likelihood. It is appropriate when the target represents categories such as 0 or 1, rather than a continuous value. section 8.2.1

Diagnosing the wrong loss

Suppose a model predicts whether an Airbnb listing's price is above or below the median. If the target remains numeric, fastai may infer regression and select MSELoss(), producing disappointing accuracy. Converting the target with merged_data['target'] = merged_data.target.astype(str) lets fastai treat it as classification; inspecting learn.loss_func then shows CrossEntropyLoss(). section 8.2.1

Listing 8.8 Statement to show the loss function used in model training
learn.loss_func                                         #1
FlattenedLoss of CrossEntropyLoss()                     #2

Inspect the configured fastai loss function and verify that classification uses CrossEntropyLoss() rather than MSELoss(). section 8.2.1

How Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI covers it

How deep it goes
Defines the classification role and demonstrates loss-function diagnosis, but does not derive the calculation mathematically.
Examples
Uses binary Airbnb price classification, numeric-versus-string targets, MSELoss(), and CrossEntropyLoss() inspection.
What it assumes
Assumes basic classification and regression distinctions while demonstrating target typing from the example.
What you can do after
Diagnose incorrect fastai loss selection and configure target values so a binary model uses CrossEntropyLoss().
How it connects
Connects classification, regression, MSELoss, accuracy, target columns, PyTorch, and fastai.

Also explained in

Compare how 19 books explain cross entropy loss →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI. The section links point to the parts of the book it draws on.