liveBook · Manning

Unconditional prompt

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

An unconditional prompt is an empty text input representing the absence of a specific conditioning description.

An unconditional prompt is an empty text input representing the absence of a specific condition or descriptive information. In classifier-free guidance, it supplies a baseline alongside the conditional prompt. section 9.3.1 section 10.3

Use the unconditional prompt through this sequence:

  • Set the prompt to an empty string, such as `uncond_prompt = ""`. section 9.3.1
  • Tokenize it with the same tokenizer and fixed sequence length as the conditional prompt; padding fills the remaining positions. section 9.3.1 section 12.2.1
  • Pass the tokens through the text encoder to create an unconditional text embedding. section 10.3
  • Combine the unconditional and conditional predictions or embeddings using the CFG parameter to steer generation toward the supplied description. section 9.3.1 section 10.3

Image-generation example

For the conditional prompt “a banana riding a motorcycle, wearing sunglasses and a straw hat,” use `uncond_prompt = ""`. Encode both inputs, then use classifier-free guidance to move generation toward the described image while retaining an unconditional baseline. section 9.3.1

Listing 9.5 Tokenizing and encoding prompts
prompt = '''A banana riding a motorcycle, wearing sunglasses and
a straw hat, highly detailed, ultra sharp, cinematic, 
100mm lens, 8k resolution.'''    

uncond_prompt=""    

seed=42
with torch.no_grad():
    generator=torch.Generator(device=device)
    generator.manual_seed(seed)
    cond_tokens=tokenizer.batch_encode_plus(
    [prompt], padding="max_length", max_length=77).input_ids    
    cond_tokens=torch.tensor(cond_tokens,
        dtype=torch.long,device=device)    #1
    print("conditional tokens are\n", cond_tokens)
    cond_context = clip(cond_tokens).to(device)    #2
    uncond_tokens=tokenizer.batch_encode_plus(
        [uncond_prompt], padding="max_length", 
        max_length=77).input_ids    
    uncond_tokens=torch.tensor(uncond_tokens,
       dtype=torch.long,device=device)
    print("unconditional tokens are\n", uncond_tokens)
    uncond_context = clip(uncond_tokens).to(device)    #3
    clip.to("cpu")
    context=torch.cat([cond_context,uncond_context])    #4

Inspect the tokenization and encoding flow for the conditional prompt and the empty unconditional prompt. section 9.3.1

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

How deep it goes
Explains the empty input, fixed-length tokenization, text embedding, and CFG combination, but does not derive the guidance formula.
Examples
Uses an empty string beside a banana-on-a-motorcycle prompt and demonstrates padded token sequences and unconditional embeddings.
What it assumes
Assumes familiarity with prompts and text encoders while demonstrating fixed-length tokenization and unconditional embedding from implementation.
What you can do after
Apply it via a tokenizer and text encoder, then combine its output with conditional output during classifier-free guidance.
How it connects
Connects unconditional prompts to classifier-free guidance, text embeddings, tokenization, and the CLIP text encoder.

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.