TensorTonic
Study Plans
Study Plan
Annealed Langevin Dynamics
Diffusion ModelsScore MatchingSampling
Medium

Run annealed Langevin dynamics on a batch of points, using the exact score of a Gaussian data distribution smoothed at each noise level.

si(x)=−x−μv+σi2,αi=η σi2σlast2,x←x+αi2 si(x)+αi zi,js_i(x) = -\frac{x - \mu}{v + \sigma_i^2}, \qquad \alpha_i = \eta\, \frac{\sigma_i^2}{\sigma_{\text{last}}^2}, \qquad x \leftarrow x + \frac{\alpha_i}{2}\, s_i(x) + \sqrt{\alpha_i}\, z_{i,j}si​(x)=−v+σi2​x−μ​,αi​=ησlast2​σi2​​,x←x+2αi​​si​(x)+αi​​zi,j​

Here x_init holds the starting points with shape (N, D), sigmas holds L decreasing noise levels in the order they are visited, σi\sigma_iσi​ is sigmas[i] for the 0-based level index i, and σlast\sigma_{\text{last}}σlast​ is the last entry sigmas[L - 1]. The data distribution is a Gaussian with mean μ\muμ given by data_mean (shape (D,)) and variance vvv given by data_var on every axis, so sis_isi​ is the score of that Gaussian after adding noise of level σi\sigma_iσi​. The base step size η\etaη is step_size. For each level i from 0 to L - 1, apply the update steps_per_sigma times with inner index j, where the step uses the fresh noise zi,jz_{i,j}zi,j​ = noise[i * steps_per_sigma + j], an (N, D) slice of noise, whose shape is (L * steps_per_sigma, N, D). The score is recomputed from the current x at every step.

Return the final points as a NumPy array of shape (N, D) with dtype float64.

Examples

Input: x_init = [[0.5,2.0],[-1.0,0.0]], sigmas = [1.0,0.5], steps_per_sigma = 2, step_size = 0.1, noise = [[[0.3,-0.2],[1.0,0.5]],[[-0.4,0.1],[0.2,-0.6]],[[0.0,0.8],[-0.3,0.3]],[[0.5,-0.5],[0.1,0.9]]], data_mean = [1.0,-1.0], data_var = 0.25

Output: [[0.796527,0.749342],[0.33595,-0.15069]]

Explanation: The first noise level uses alpha = 0.1 * 1.0^2 / 0.5^2 = 0.4 with noise rows 0 and 1, and the second level uses alpha = 0.1 with noise rows 2 and 3, so both points drift towards the mean (1, -1) while being perturbed.

Input: x_init = [[4.0],[-2.0],[0.0]], sigmas = [2.0,1.0,0.5], steps_per_sigma = 3, step_size = 0.05, noise = [[[0.0],[0.0],[0.0]],[[0.0],[0.0],[0.0]],[[0.0],[0.0],[0.0]],[[0.0],[0.0],[0.0]],[[0.0],[0.0],[0.0]],[[0.0],[0.0],[0.0]],[[0.0],[0.0],[0.0]],[[0.0],[0.0],[0.0]],[[0.0],[0.0],[0.0]]], data_mean = [1.0], data_var = 1.0

Output: [[2.885097],[-0.885097],[0.371634]]

Input: x_init = [[1.0,-1.0,0.5]], sigmas = [0.3], steps_per_sigma = 1, step_size = 0.04, noise = [[[0.5,1.0,-1.5]]], data_mean = [0.0,0.0,0.0], data_var = 0.5

Output: [[1.066102,-0.766102,0.183051]]

Hint 1

for i, sigma in enumerate(sigmas) gives each level with its index.

Hint 2

A (D,) mean broadcasts over (N, D) points.

Hint 3

np.array(x_init, dtype=np.float64) makes a float64 copy, so x_init stays unchanged.

Requirements

  • Use NumPy and compute in float64.
  • Leave all input arrays unchanged.

Constraints

  • x_init is a float64 array of shape (N, D) with N between 1 and 4 and D between 1 and 3.
  • sigmas is a float64 array of shape (L,) with L between 1 and 5, strictly decreasing, with every value positive.
  • steps_per_sigma is an integer between 1 and 4.
  • noise is a float64 array of shape (L * steps_per_sigma, N, D).
  • data_mean is a float64 array of shape (D,) and data_var is a positive Python float.
  • step_size is a positive Python float no larger than twice the square of the last sigma, so every update is stable.
  • All values are finite.
Try Similar Problems
Denoising Score Matching LossMediumGaussian Mixture ScoreMediumDDPM Ancestral StepMediumClassifier-Free GuidanceMediumConditional Flow Matching LossMediumDDIM StepHardKarras Euler SamplerHardToy DDPM SamplerHardCosine Noise ScheduleMediumRescale to Zero Terminal SNRMediumTime-Conditioned Residual BlockMediumSpatial Self-Attention BlockMedium

Accepts: array

Accepts: array

Accepts: integer

Accepts: number

Accepts: array

Accepts: array

Accepts: number

You must run your code first.

Annealed Langevin Dynamics

Diffusion ModelsScore MatchingSampling
Medium

Run annealed Langevin dynamics on a batch of points, using the exact score of a Gaussian data distribution smoothed at each noise level.

si(x)=−x−μv+σi2,αi=η σi2σlast2,x←x+αi2 si(x)+αi zi,js_i(x) = -\frac{x - \mu}{v + \sigma_i^2}, \qquad \alpha_i = \eta\, \frac{\sigma_i^2}{\sigma_{\text{last}}^2}, \qquad x \leftarrow x + \frac{\alpha_i}{2}\, s_i(x) + \sqrt{\alpha_i}\, z_{i,j}si​(x)=−v+σi2​x−μ​,αi​=ησlast2​σi2​​,x←x+2αi​​si​(x)+αi​​zi,j​

Here x_init holds the starting points with shape (N, D), sigmas holds L decreasing noise levels in the order they are visited, σi\sigma_iσi​ is sigmas[i] for the 0-based level index i, and σlast\sigma_{\text{last}}σlast​ is the last entry sigmas[L - 1]. The data distribution is a Gaussian with mean μ\muμ given by data_mean (shape (D,)) and variance vvv given by data_var on every axis, so sis_isi​ is the score of that Gaussian after adding noise of level σi\sigma_iσi​. The base step size η\etaη is step_size. For each level i from 0 to L - 1, apply the update steps_per_sigma times with inner index j, where the step uses the fresh noise zi,jz_{i,j}zi,j​ = noise[i * steps_per_sigma + j], an (N, D) slice of noise, whose shape is (L * steps_per_sigma, N, D). The score is recomputed from the current x at every step.

Return the final points as a NumPy array of shape (N, D) with dtype float64.

Examples

Input: x_init = [[0.5,2.0],[-1.0,0.0]], sigmas = [1.0,0.5], steps_per_sigma = 2, step_size = 0.1, noise = [[[0.3,-0.2],[1.0,0.5]],[[-0.4,0.1],[0.2,-0.6]],[[0.0,0.8],[-0.3,0.3]],[[0.5,-0.5],[0.1,0.9]]], data_mean = [1.0,-1.0], data_var = 0.25

Output: [[0.796527,0.749342],[0.33595,-0.15069]]

Explanation: The first noise level uses alpha = 0.1 * 1.0^2 / 0.5^2 = 0.4 with noise rows 0 and 1, and the second level uses alpha = 0.1 with noise rows 2 and 3, so both points drift towards the mean (1, -1) while being perturbed.

Input: x_init = [[4.0],[-2.0],[0.0]], sigmas = [2.0,1.0,0.5], steps_per_sigma = 3, step_size = 0.05, noise = [[[0.0],[0.0],[0.0]],[[0.0],[0.0],[0.0]],[[0.0],[0.0],[0.0]],[[0.0],[0.0],[0.0]],[[0.0],[0.0],[0.0]],[[0.0],[0.0],[0.0]],[[0.0],[0.0],[0.0]],[[0.0],[0.0],[0.0]],[[0.0],[0.0],[0.0]]], data_mean = [1.0], data_var = 1.0

Output: [[2.885097],[-0.885097],[0.371634]]

Input: x_init = [[1.0,-1.0,0.5]], sigmas = [0.3], steps_per_sigma = 1, step_size = 0.04, noise = [[[0.5,1.0,-1.5]]], data_mean = [0.0,0.0,0.0], data_var = 0.5

Output: [[1.066102,-0.766102,0.183051]]

Hint 1

for i, sigma in enumerate(sigmas) gives each level with its index.

Hint 2

A (D,) mean broadcasts over (N, D) points.

Hint 3

np.array(x_init, dtype=np.float64) makes a float64 copy, so x_init stays unchanged.

Requirements

  • Use NumPy and compute in float64.
  • Leave all input arrays unchanged.

Constraints

  • x_init is a float64 array of shape (N, D) with N between 1 and 4 and D between 1 and 3.
  • sigmas is a float64 array of shape (L,) with L between 1 and 5, strictly decreasing, with every value positive.
  • steps_per_sigma is an integer between 1 and 4.
  • noise is a float64 array of shape (L * steps_per_sigma, N, D).
  • data_mean is a float64 array of shape (D,) and data_var is a positive Python float.
  • step_size is a positive Python float no larger than twice the square of the last sigma, so every update is stable.
  • All values are finite.
Try Similar Problems
Denoising Score Matching LossMediumGaussian Mixture ScoreMediumDDPM Ancestral StepMediumClassifier-Free GuidanceMediumConditional Flow Matching LossMediumDDIM StepHardKarras Euler SamplerHardToy DDPM SamplerHardCosine Noise ScheduleMediumRescale to Zero Terminal SNRMediumTime-Conditioned Residual BlockMediumSpatial Self-Attention BlockMedium

Accepts: array

Accepts: array

Accepts: integer

Accepts: number

Accepts: array

Accepts: array

Accepts: number

You must run your code first.