skills/cv/classifier-free-guidance-diffusion/SKILL.md
Manual stable diffusion inference loop with classifier-free guidance that interpolates between unconditional and conditional noise predictions for controllable image generation
npx skillsauth add wenmin-wu/ds-skills cv-classifier-free-guidance-diffusionInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Classifier-free guidance (CFG) steers diffusion model generation toward a text prompt without a separate classifier. At each denoising step, the model predicts noise twice — once conditioned on the prompt, once unconditionally — and the final noise is an extrapolation: noise = uncond + scale * (cond - uncond). Higher guidance scales produce images more faithful to the prompt at the cost of diversity.
import torch
from torch.cuda.amp import autocast
uncond_input = tokenizer([""], padding="max_length", max_length=77, return_tensors="pt")
text_input = tokenizer([prompt], padding="max_length", max_length=77, truncation=True, return_tensors="pt")
with torch.no_grad():
uncond_emb = text_encoder(uncond_input.input_ids.cuda())[0]
text_emb = text_encoder(text_input.input_ids.cuda())[0]
text_embeddings = torch.cat([uncond_emb, text_emb])
latents = torch.randn(1, 4, 64, 64).cuda() * scheduler.init_noise_sigma
for i, t in enumerate(scheduler.timesteps):
inp = torch.cat([latents] * 2)
inp = scheduler.scale_model_input(inp, t)
with torch.no_grad():
noise_pred = unet(inp, t, encoder_hidden_states=text_embeddings).sample
uncond, cond = noise_pred.chunk(2)
noise_pred = uncond + 7.5 * (cond - uncond)
latents = scheduler.step(noise_pred, t, latents).prev_sample
noise = uncond + guidance_scale * (cond - uncond)data-ai
Scaled Pinball Loss (SPL) metric for evaluating quantile forecasts, normalized by mean absolute successive differences of training data
data-ai
Walk backward through a time series and multiplicatively rescale segments when jumps exceed a fraction of the running mean to correct data collection anomalies
testing
Transform forecasting target to next/current ratio minus one so that optimizing MAE or squared error implicitly minimizes SMAPE
tools
Convert point forecasts to prediction intervals by scaling with logit-transformed quantile ratios passed through a Normal CDF