liveBook · Manning

CFG scale

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

CFG scale is a sampling parameter that controls the strength with which classifier-free guidance follows conditioning information.

CFG scale controls how strongly classifier-free guidance (CFG) steers a diffusion model toward a condition, such as a class label or text prompt. Raise it to favor images that match the condition; lower it to allow more diversity, with a greater risk of mismatch. It changes sampling, not the wording of the prompt. section 6.1.1

The scale has a practical role at each stage of guided generation:

  • Train for both cases: expose the model to conditioning information and to its absence, so it can make conditional and unconditional predictions. section 6.1.1
  • Prepare both inputs at inference. For text-to-image generation, encode the requested prompt and an empty unconditional prompt; for class-guided generation, use the label and no-label cases. section 9.3.1 section 6.1.2
  • At each denoising step, combine the two predictions before updating the noisy image or latent. The scale sets how much the conditional prediction steers that update. section 9.3.2

One latent-diffusion implementation writes the combination as `unconditional + cfg_scale × (conditional − unconditional)`. In this expression, a scale of 0 uses the unconditional prediction and 1 uses the conditional prediction; larger values push farther in the conditional direction. Check the implementation’s formula before interpreting a numeric setting: an earlier guidance-parameter blueprint uses a convention in which `w = 0` gives the conditional prediction. section 9.3.2 section 6.1.2

Tune against a visible target

For class-labeled grayscale clothing images, set `guide_w=5` and inspect a 4 × 10 grid: the generated items match their labels fairly closely. Repeat with `guide_w=1` to compare. For a text prompt, a latent-diffusion pipeline uses `cfg_scale=8` as a working setting; trying 5 favors diversity, while 12 favors prompt alignment but may reduce natural variation. Keep the prompt and random seed fixed when comparing settings, as in the text-to-image example. section 6.4.2 section 9.4

Keep CFG scale distinct from image-to-image `strength`: strength controls how far an edited output departs from its input image, whereas CFG scale controls its alignment with the condition. In the `diffusers` Stable Diffusion pipeline, the corresponding argument is `guidance_scale`; its cited default is 7.5. section 9.4 section 10.3

Listing 9.7 Conducting reverse diffusion in the latent space
from tqdm import tqdm
from copy import deepcopy

cfg_scale=8
latent_shape=(1,4,64,64)
noisy_latents=[]    
with torch.no_grad():
    latents=torch.randn(latent_shape,
        generator=generator,device=device)    #1
    timesteps=tqdm(sampler.timesteps)
    for i, timestep in enumerate(timesteps):    #2
        time_embedding=get_time_embedding(timestep).to(device)
        model_input=latents.to(device)
        model_input=model_input.repeat(2,1,1,1)
        model_output=diffusion(model_input,context,time_embedding)
        output_cond,output_uncond=model_output.chunk(2)
        model_output=cfg_scale*(output_cond-output_uncond)+\
            output_uncond    #3
        latents=sampler.step(timestep,latents,model_output)
        if i%10==9:
            noisy_latents.append(deepcopy(latents))    #4
diffusion.to("cpu")  

Follow the paired conditional and unconditional outputs through the scale formula, then into one reverse-diffusion step. section 9.3.2

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

How deep it goes
Traces dual training into conditional and unconditional inference, then implements their scaled combination inside a latent denoising loop.
Examples
Compares guidance settings on class-labeled clothing grids and prompted latent images, including image editing and Stable Diffusion outputs.
What it assumes
Builds on diffusion denoising and conditioning while introducing conditional and unconditional prediction as the basis of guidance.
What you can do after
Operate/configure it in a sampling loop or pipeline to trade prompt alignment against diversity.
How it connects
Connects classifier-free guidance to conditional diffusion, U-Nets, latent diffusion, text embeddings, and Stable Diffusion.

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.