liveBook · Manning

Top-k sampling

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

Top-k sampling draws the next token from a probability distribution restricted to the k highest-probability candidates.

Top-k sampling restricts the next-token choice to the k most probable candidates instead of the full vocabulary. Smaller k values favor highly likely tokens, producing more coherent but less creative output. section 12.3.2

During autoregressive image generation, the decoder predicts one image token at a time. The selected token updates the sequence and internal attention state before the next prediction. section 12.3.2

Configure top-k sampling alongside related generation controls:

  • Set temperature to adjust prediction randomness independently of the candidate restriction. section 12.3.2
  • Choose a small k for confidence and coherence, or a larger k to preserve more creative alternatives. section 12.3.2
  • Use the resulting token in the decoder loop, repeating the process for all 256 image tokens. section 12.3.2

A configured image-token loop

A min-DALL-E configuration uses temperature 0.5, top_k 128, and supercondition factor 4, then samples image tokens iteratively while caching attention state. section 12.3.2

token_indices=torch.arange(256,device=device)
temperature=0.5    #1
top_k=128    #2
supercondition_factor=4    #3
settings = torch.tensor(
    [temperature, top_k, supercondition_factor], 
    dtype=torch.float32,device=device)

Inspect the compact tensor of generation settings, including temperature, top-k, and supercondition values. section 12.3.2

Listing 12.7 Predicting image tokens iteratively
from copy import deepcopy

imgtokens=[]    #1
for i in range(256):    #2
    torch.cuda.empty_cache()                
    with torch.amp.autocast("cuda",dtype=getattr(torch, dtype)):
        image_tokens[:, i + 1], \    #3
        attention_state = decoder.sample_tokens(
            settings=settings,
            attention_mask=attention_mask,
            encoder_state=encoder_state,
            attention_state=attention_state,
            prev_tokens=image_tokens[:, [i]],
            token_index=token_indices[[i]])
        imgtokens.append(deepcopy(image_tokens))

Trace the iterative decoder call that samples each image token and updates the attention state. section 12.3.2

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

How deep it goes
Defines candidate restriction and trade-offs, then shows settings and decoder-loop integration without deriving the sampling algorithm.
Examples
Uses an underwater fantasy city prompt, top_k 128, and a 256-step image-token generation loop.
What it assumes
Assumes familiarity with VQGAN, transformer-based generation, decoder state, and image tokens.
What you can do after
Operate min-DALL-E generation by configuring top-k and tracing sampled image tokens through the decoder loop.
How it connects
Connects top-k sampling with temperature scaling, autoregressive decoding, and classifier-free guidance.

Also explained in

Compare how 8 books explain top-k sampling →

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.