liveBook · Manning

Logit

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

A logit is an unnormalized real-valued score assigned to a class before conversion into probabilities.

A logit is a raw, unnormalized real-valued score assigned to each class before scores become probabilities through softmax. A classifier therefore returns one logit per possible label rather than a probability distribution. section 3.3.5 section 3.4.3

A vision-transformer classifier produces logits through this pipeline:

  • Pass images through embeddings that combine token embeddings with positional encodings. section 3.3.5
  • Run the embeddings through the encoder-only vision transformer. section 3.3.5
  • Read the representation associated with the CLS token, which aggregates information across the image. section 3.3.5
  • Apply a linear classifier head to produce one logit for each of the 10 CIFAR-10 classes. section 3.3.5
  • Select the class with the highest logit for a prediction, or apply softmax when probability distributions are needed. section 3.4.3 section 3.3.5

Classifying a test batch

A trained ViT receives 32 CIFAR-10 test images and returns a logit tensor. Applying argmax across the class dimension produces 32 numerical predictions, which can be mapped to labels such as ship, plane, frog, or car. section 3.4.3

Listing 3.7 Building a classifier based on the ViT
class ViTForClassfication(nn.Module):
    def __init__(self, config):
        super().__init__()
        self.config = config
        self.image_size = config.image_size
        self.hidden_size = config.hidden_size
        self.num_classes = config.num_classes
        self.embedding = Embeddings(config)
        self.encoder = Encoder(config)
        self.classifier = nn.Linear(self.hidden_size,
                                    self.num_classes)  #1
        self.apply(self._init_weights)
    def forward(self, x, output_attentions=False):
        embedding_output = self.embedding(x)  #2
        encoder_output, all_attentions = self.encoder(\  #3
          embedding_output,output_attentions=output_attentions)
        logits = self.classifier(encoder_output[:, 0, :])  #4
        if not output_attentions:
            return (logits, None)
        else:
            return (logits, all_attentions)

Inspect the classifier implementation to see the linear head transform the CLS-token representation into class logits. section 3.3.5

Listing 3.10 Using the trained ViT to classify images
import math
import torch.nn.functional as F  

model.load_state_dict(torch.load('files/ViT.pth'))
model.eval()
with torch.no_grad():
    batch=next(iter(testloader))  #1
    batch = [t.to(device) for t in batch]
    images, labels = batch
    logits, attention_maps = model(images, 
        output_attentions=True)  #2
    predictions = torch.argmax(logits, dim=1)  #3

print(predictions)
print([names[i] for i in predictions.tolist()])  #4

Follow the inference code that loads trained weights, obtains logits, and selects predictions with argmax. section 3.4.3

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

How deep it goes
Traces logits from the CLS representation through a linear classifier head to argmax predictions and softmax probabilities.
Examples
Uses CIFAR-10 batches, ten class outputs, argmax predictions, and labels such as ship, plane, frog, and car.
What it assumes
Assumes a trained vision-transformer classifier and class labels while explaining how its output scores become predictions.
What you can do after
Apply it via PyTorch to obtain class predictions from logits and convert those scores into probabilities with softmax.
How it connects
Connects logits to softmax, classifier heads, vision transformers, CLS tokens, and image classification.

Also explained in

Compare how 17 books explain logit →

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.