liveBook · Manning

Categorical encoding

As explained in Math and Architectures of Deep Learning

A vector with exactly one element equal to 1 and all others equal to 0, used here to encode the correct class.

One-hot encoding represents one categorical outcome with a vector of m elements. Exactly one element is 1, identifying the observed class; every other element is 0. For a four-class image classifier, a cat label can be written as [1, 0, 0, 0], while a dog label is [0, 1, 0, 0]. section 5.9.6 section 9.1.3

The vector is also a compact representation of a categorical-distribution outcome. If the class probabilities are π₁, …, πₘ and the observed class is i, the one-hot vector has xᵢ = 1 and xⱼ = 0 for j ≠ i. In categorical probability expressions, the zero-valued powers disappear, leaving the probability πᵢ of the observed class. section 5.9.6

One-hot targets connect directly to classifier training:

  • Compare the one-hot ground truth with a prediction vector whose entries represent class probabilities. section 6.3
  • Compute cross-entropy as −Σᵢ yᵢ log(pᵢ). Because only the correct-label entry yᵢ equals 1, the expression reduces to −log(pcorrect). section 9.1.3
  • Use a confident correct prediction such as [0.003, 0.997, 0, 0] for a dog target [0, 1, 0, 0]; its loss is low. section 9.1.6

PyTorch cross-entropy

For prediction probabilities [0.8, 0.15, 0.04, 0.01] and cat target [1, 0, 0, 0], compute loss with `-torch.dot(y_gt, torch.log(y_pred))`. For logits, PyTorch can combine softmax and cross-entropy with `cross_entropy(scores, y_gt)`; the target may be supplied as the class index `1`. section 9.1.3 section 9.1.6

Listing 9.5 PyTorch code for softmax cross-entropy loss
from torch.nn.functional import cross_entropy

scores = torch.tensor([[9.99, 10, 0.01, -10]])

y_gt = torch.tensor([1])            #1

loss = cross_entropy(scores, y_gt)  #2

Inspect the PyTorch softmax cross-entropy listing to connect class scores, the target class, and the resulting loss. section 9.1.6

How Math and Architectures of Deep Learning covers it

How deep it goes
Defines the vector, derives its probability role, and connects it to cross-entropy and PyTorch code.
Examples
Uses cat, dog, airplane, and automobile labels, numeric prediction vectors, loss values, and PyTorch listings.
What it assumes
Assumes class labels and probability vectors while building from categorical outcomes and single-trial representations.
What you can do after
Apply it via PyTorch to represent class targets and compute categorical or softmax cross-entropy loss.
How it connects
Connects categorical distribution, Bernoulli distribution, cross-entropy loss, softmax, and classifier training.

Also explained in

Compare how 13 books explain categorical encoding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Math and Architectures of Deep Learning. The section links point to the parts of the book it draws on.