Classifier Gradient Guidance Step
Problem Statement
Before classifier-free guidance, Dhariwal & Nichol used an external classifier's gradient to nudge the noise prediction. Implement that guided-noise update.
Background
Classifier guidance shifts the predicted noise by the gradient of the classifier's log-probability of the target class, scaled by the noise level and a strength s:
Ξ΅^=Ξ΅βs1βΞ±ΛtβββxtββlogpΟβ(yβ£xtβ)
The sqrt(1 - alpha_bar_t) factor converts a score shift into a noise shift. The gradient is supplied.
Your Task
Implement:
def classifier_guidance(eps, grad_log_p, alpha_bar_t, s):
Return the guided noise as a list rounded to 4 decimals.
Input Format
- eps, grad_log_p: lists of equal length D.
- alpha_bar_t (float) in (0, 1), s (float): guidance strength.
Output Format
- A list of D floats rounded to 4 decimals.
Sample
print(classifier_guidance([0.0, 0.0], [1.0, -1.0], 0.75, 2.0))
Output:
[-1.0, 1.0]
Example:
print(classifier_guidance([0.0, 0.0], [1.0, -1.0], 0.75, 2.0))
[-1.0, 1.0]
- Calculate the noise scaling factor derived from the time step, which converts the score shift into a noise shift: \sqrt{1 - \alpha_{\text{bar}}_t} = \sqrt{1 - 0.75} = \sqrt{0.25} = 0.5.
- Determine the effective guidance magnitude by multiplying the strength s by the scaling factor: 2.0Γ0.5=1.0.
- Compute the guided noise for the first dimension by subtracting the scaled gradient from the predicted noise: 0.0β(1.0Γ1.0)=β1.0.
- Compute the guided noise for the second dimension by subtracting the scaled gradient from the predicted noise: 0.0β(1.0Γβ1.0)=1.0.
- The final output is [-1.0, 1.0]
Constraints:
len(eps) == len(grad_log_p),0 < alpha_bar_t < 1.eps_guided = eps - s*sqrt(1-alpha_bar_t)*grad.- Round to 4 decimals; avoid
-0.0.
1. Background Knowledge
In classifier-guided diffusion, a separate neural network (the classifier) is trained to predict the probability of a class label y given a noisy latent xtβ. During sampling, we want the diffusion process to move toward regions of high pΟβ(yβ£xtβ). The direction of this movement is given by the gradient βxtββlogpΟβ(yβ£xtβ), which points toward higher log-probability for the target class.
The raw diffusion model predicts noise Ξ΅ at timestep t. To incorporate classifier guidance, we shift this prediction by a scaled gradient term. The scaling factor 1βΞ±Λtββ is critical: it converts a score-space shift (in the xtβ domain) into an equivalent noise-space shift. This arises from the relationship between the score function βxtββlogptβ(xtβ) and the noise prediction Ξ΅ in the forward diffusion process, where Ξ±Λtβ is the cumulative product of per-step signal retention factors.
The guidance strength s controls how aggressively the classifier steers the generation. When s=0, the classifier has no effect and the output equals the original noise prediction. As s increases, the generated samples become more class-conditional but may lose diversity or fidelity.
2. Algorithm Approach
This is a pointwise element-wise transformation problem. For each dimension i of the input vectors, you compute:
Ξ΅^iβ=Ξ΅iββsβ 1βΞ±Λtβββ βiβ
where βiβ is the i-th component of the gradient vector. The entire operation reduces to:
- Compute the scalar coefficient c=sβ 1βΞ±Λtββ.
- For each index i, subtract cβ grad_log_p[i] from eps[i].
- Round each result to 4 decimal places.
No loops over timesteps or batch dimensions are neededβthis is a single-step, single-sample update.
3. Step-by-Step Strategy
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.