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












