Cross-entropy loss measures classification error from predicted probabilities and ground-truth labels. It is defined as the negative logarithm of the prediction assigned to the correct class, so less confident correct predictions produce larger losses. section 6.4.3 section 9.2.3
Use cross-entropy loss to:
- Train segmentation models that classify each receipt pixel as forged or genuine. section 6.3.2
- Account for class imbalance by assigning larger weights to mistakes on rare forged pixels. section 6.3.2
- Compare training and validation behavior, while interpreting larger losses cautiously when a task has more classes. section 6.4.3
- Train classifiers distinguishing illicit from licit Bitcoin transactions before evaluating accuracy and F1 score. section 9.2.3
Weighted receipt segmentation
For a two-class receipt segmentation model, class frequencies such as [1.2, 1] are inverted to create weights, then passed to PyTorch's CrossEntropyLoss. Raising the forged-class weight makes forged-pixel errors contribute more; making it excessively large can push the model toward predicting every pixel as forged. section 6.3.2
class_frequencies = [1.2, 1] #A weights = 1.0 / torch.tensor(class_frequencies, dtype=torch.float32) #B optimizer = torch.optim.Adam(model.parameters(), lr=1e-4) #C loss_function = torch.nn.CrossEntropyLoss(weight=weights) #C
Inspect the concise PyTorch setup for inverse-frequency class weights, the Adam optimizer, and weighted CrossEntropyLoss. section 6.3.2
def train():
model.train()
optimizer.zero_grad() #A
out = model(data.x) #B
loss = criterion(out[data.train_mask], data.y[data.train_mask]) #C
loss.backward() #D
optimizer.step() #E
return loss
Follow the training step from forward pass through loss calculation, backpropagation, and optimizer update. section 9.2.3

























