liveBook · Manning

Categorical encoding

As explained in Build a Text-to-Image Generator (from Scratch)

A one-hot tensor represents each categorical value with a vector containing one active entry and zeros elsewhere.

One-hot encoding represents each categorical value as a vector with one active entry and zeros elsewhere. For ten clothing classes, each label becomes a ten-value tensor before entering a label-embedding block. section 6.2.1

Build the representation and connect it to conditional image generation:

  • Convert integer labels to one-hot vectors with num_classes=10. section 6.2.1
  • Optionally mask the vectors with 10% probability, jointly training unconditional and conditional behavior for classifier-free guidance. section 6.2.1
  • Pass the vectors through embedding blocks to produce tensors shaped for the denoising U-Net. section 6.2.1

Batch of clothing labels

Labels [2, 9, 0] become three ten-value one-hot vectors. Embedding them at dimensions 512 and 256 produces tensors shaped [3, 512, 1, 1] and [3, 256, 1, 1]. section 6.2.1

Listing 6.3 Generating label embeddings
n_classes=10
contextembed1=EmbedLayer(n_classes, 512)  #1
contextembed2=EmbedLayer(n_classes, 256)   #1
label=torch.tensor([2, 9, 0]).long()  #2
context_mask = torch.bernoulli(
    torch.zeros_like(label)+0.1)  #3
onehot = torch.nn.functional.one_hot(label, 
            num_classes=n_classes).type(torch.float)  #4
context_mask = context_mask[:, None]
context_mask = context_mask.repeat(1,n_classes)
context_mask = (-1*(1-context_mask)) 
c = onehot * context_mask    
cemb1 = contextembed1(c).view(-1, 512, 1, 1) 
cemb2 = contextembed2(c).view(-1, 256, 1, 1) 
print("the shape of the first label embedding is", cemb1.shape)
print("the shape of the second label embedding is", cemb2.shape)

Inspect the PyTorch implementation for one-hot conversion, masking, and label embedding. section 6.2.1

How Build a Text-to-Image Generator (from Scratch) covers it

How deep it goes
Implements conversion, masking, embedding, and output shapes, stopping short of encoding theory.
Examples
Uses clothing labels 2, 9, and 0 across ten classes, producing 512- and 256-channel embedding tensors.
What you can do after
Builds one-hot label conditioning with PyTorch embedding blocks and configurable context masking.
How it connects
Connects one-hot encoding to label embeddings, conditional diffusion, and classifier-free guidance.

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 Build a Text-to-Image Generator (from Scratch). The section links point to the parts of the book it draws on.