liveBook · Manning

Label

As explained in Learn Generative AI with PyTorch

The reference target values used to supervise training, here the actual noise compared against the model’s prediction.

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

How Learn Generative AI with PyTorch covers it

How deep it goes
Works through numerical categories, binary target-tensor shapes, loss comparisons, and labels concatenated with generator inputs.
Examples
Uses Fashion-MNIST clothing categories, GAN real/fake targets, and eyeglasses labels for generated faces.
What it assumes
Introduces image tensors and numerical clothing categories before using tensor-shaped labels in training loops.
What you can do after
Apply it via PyTorch to compare predictions with target labels or condition a generator on an image attribute.
How it connects
Connects labels to cross-entropy loss, GAN discriminators, conditional generation, and noise vectors.

Also explained in

Compare how 20 books explain label →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.