liveBook · Manning

Cross entropy loss

As explained in Knowledge Graphs and LLMs in Action

Cross-entropy loss is a classification loss that penalizes incorrect probability assignments by comparing predicted probabilities with target labels.

Cross-entropy loss trains classifiers by penalizing incorrect probability assignments. It increases loss when a true relationship or class receives low probability, aligning optimization with label matching. section 11.4.1 section 12.1.4

In graph learning, the loss supports two complementary classification settings:

  • Link prediction uses negative sampling: positive graph edges receive high scores, while sampled false edges receive low scores; a balancing parameter controls negative-sample importance. section 11.4.1
  • Node classification applies CrossEntropyLoss to encoder outputs and actual labels, such as licit versus illicit transaction categories. section 12.1.4
  • A sigmoid converts relationship scores into probabilities, while log_softmax provides numerically stable class probabilities for the decoder. section 11.4.1 section 12.1.4
Listing 12.10 Encoder–decoder implementation for node classification
import torch
import torch.nn.functional as F

class NodeClassifier(torch.nn.Module):
    def __init__(self, gnn_model):
        super().__init__()
        self.gnn = gnn_model

    def forward(self, x, edge_index):
        x = self.gnn(x, edge_index)
        return F.log_softmax(x, dim=1)

A PyTorch node-classification decoder returns log_softmax outputs from a GNN encoder before CrossEntropyLoss evaluates the predictions. section 12.1.4

How Knowledge Graphs and LLMs in Action covers it

How deep it goes
Explains sigmoid, negative sampling, probability penalties, and log_softmax integration, but does not derive the full optimization mathematics.
Examples
Uses an aspirin-treats-headache knowledge-graph fact and licit-versus-illicit transaction classification.
What it assumes
Assumes graph data, GNN encoders, predicted scores, and target labels; teaches probability conversion and loss interpretation.
What you can do after
Apply PyTorch CrossEntropyLoss with log_softmax outputs to train a GNN classifier and evaluate label mismatches.
How it connects
Connects negative sampling, sigmoid, log_softmax, GNN encoders, link prediction, and node classification.

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 Knowledge Graphs and LLMs in Action. The section links point to the parts of the book it draws on.