liveBook · Manning

Softmax

As explained in Hugging Face in Action

Softmax is a mathematical function that converts a vector of numbers into a probability distribution by exponentiating and normalizing its values.

Softmax converts a vector of model outputs into a probability distribution by exponentiating and normalizing its values. The resulting probabilities sum to one, with each value representing a class or category likelihood. section 3.1.5

In a transformer decoder, a final linear transformation followed by softmax produces probabilities for the next token. Neural-network classifiers likewise use it as a final activation. section 3.1 section 3.1.5

Classifying an image with CLIP

A CLIP workflow compares an image with the labels cat, dog, tiger, and train, converts the resulting logits with softmax(), and selects the label with the highest probability. For the demonstrated image, the result is dog. section 6.2.2

Listing 6.12 Using a multimodal model to simultaneously process visual and textual data
from transformers import CLIPProcessor, CLIPModel

model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")  #1
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")

labels = ["cat", "dog", "tiger", "train"]             
inputs = processor(text = labels,  #2
                   images = image,
                   return_tensors = "pt",
                   padding=True)

model.eval()

with torch.no_grad():
    outputs = model(**inputs)  #3

logits_per_image = outputs.logits_per_image  #4

probs = logits_per_image.softmax(dim=1)  #5
most_likely_index = torch.argmax(probs, dim=1).item()  #6
most_likely_object = labels[most_likely_index]

print(f"The most likely object is: {most_likely_object}")

The listing shows the complete CLIP path from processor inputs and model inference to softmax probabilities and the selected label. section 6.2.2

How Hugging Face in Action covers it

How deep it goes
Defines exponentiation and normalization, notes that probabilities sum to one, and demonstrates application without deriving implementation internals.
Examples
Uses neural-network output values, next-token prediction, and CLIP labels for cat, dog, tiger, and train.
What you can do after
Apply it via PyTorch's softmax() to rank candidate labels after a CLIP model produces logits.
How it connects
Connects logits, classification, transformer decoders, and multimodal image-text matching.

Also explained in

Compare how 17 books explain softmax →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Hugging Face in Action. The section links point to the parts of the book it draws on.