A label is a value associated with a sample. In supervised classification, it identifies the correct category against which a model’s prediction is compared. In a generative model, a label can instead specify a characteristic to produce. The role depends on where the label enters the model. section 2.2.1 section 5.5.3
Three uses make the distinction concrete:
- For clothing classification, each grayscale image has a numerical category label. Label 9 means “ankle boot”; cross-entropy loss compares predictions with the true labels. section 2.2.2 section 2.2.1
- For a GAN discriminator, real samples receive labels of 1 and generated samples labels of 0. Its predictions are compared with these targets to calculate loss. When training the generator, the discriminator’s output is compared with real labels: the aim is to make generated samples appear real, not to assert that they are real. section 3.4.1
- For a conditioned image generator, an eyeglasses label is joined with a noise vector before generation. Changing the label selects whether the generated face has glasses; changing the noise vector can select a different face. section 5.5.3
real_labels=torch.ones((batch_size,1)) real_labels=real_labels.to(device) fake_labels=torch.zeros((batch_size,1)) fake_labels=fake_labels.to(device)
The discriminator’s target tensors each have shape (batch_size, 1): ones for real samples and zeros for fake samples. section 3.4.1
Listing 5.12 Selecting two characteristics simultaneously
plt.figure(figsize=(20,5),dpi=50)
for i in range(4): #1
ax = plt.subplot(1, 4, i + 1)
p=i//2
q=i%2
z=z_female_g*p+z_male_g*(1-p) #2
label=labels_ng[0]*q+labels_g[0]*(1-q) #3
noise_and_labels=torch.cat(
[z.reshape(1, z_dim, 1, 1),
label.reshape(1, 2, 1, 1)],dim=1).to(device) #4
fake=generator(noise_and_labels)
img=(fake.cpu().detach()[0]/2+0.5).permute(1,2,0)
plt.imshow(img.numpy())
plt.xticks([])
plt.yticks([])
plt.subplots_adjust(wspace=-0.08,hspace=-0.01)
plt.show()
See how a selected eyeglasses label is concatenated with a reshaped noise vector and passed to the generator. section 5.5.3
Keep the label’s role clear when reading a training step: it may be a ground-truth target for a loss calculation or an input that controls what a generator makes. section 3.4.1 section 5.5.3



















