TensorTonic
Study Plans
Study Plan
Forward Diffusion Sample
Diffusion ModelsGenerative Models
Easy

Produce the noisy sample xtx_txt​ for every item in a batch by jumping straight from the clean data to its timestep in one step.

xt=αˉt x0+1−αˉt ϵx_t = \sqrt{\bar\alpha_t}\, x_0 + \sqrt{1 - \bar\alpha_t}\, \epsilonxt​=αˉt​​x0​+1−αˉt​​ϵ

Here x0 holds the clean samples with the batch on the first axis, timesteps holds one integer timestep ttt per sample, alpha_bars is the cumulative schedule so that αˉt\bar\alpha_tαˉt​ is alpha_bars at index ttt, and noise holds the Gaussian noise ϵ\epsilonϵ with the same shape as x0. Each sample uses its own timestep, so the two scalar factors differ from one sample to the next.

Return a PyTorch tensor with the same shape, dtype and device as x0.

Examples

Input: x0 = [[[[1.0,-1.0],[0.5,0.0]]],[[[0.2,0.4],[-0.6,0.8]]]], timesteps = [0,3], alpha_bars = [0.9,0.7,0.4,0.1], noise = [[[[0.3,0.1],[-0.2,1.0]]],[[[1.0,-1.0],[0.5,0.0]]]]

Output: [[[[1.043552,-0.91706],[0.411096,0.316228]]],[[[1.011929,-0.822192],[0.284605,0.252982]]]]

Explanation: Here timesteps is an int64 tensor that gives each image its own timestep. The first image uses alpha_bars[0] = 0.9, so it keeps a signal factor of about 0.949 and a noise factor of about 0.316. The second image uses alpha_bars[3] = 0.1, so its noise dominates.

Input: x0 = [[2.0,0.0,-2.0],[1.0,1.0,1.0]], timesteps = [1,1], alpha_bars = [0.99,0.64], noise = [[0.5,0.5,0.5],[-1.0,0.0,1.0]]

Output: [[1.9,0.3,-1.3],[0.2,0.8,1.4]]

Input: x0 = [[3.0,-3.0]], timesteps = [2], alpha_bars = [0.5,0.25,0.0], noise = [[0.7,-0.2]]

Output: [[0.7,-0.2]]

Hint 1

Indexing alpha_bars[timesteps] with the int64 timestep tensor gives one value per sample, with shape (B,).

Hint 2

Calling .view(-1, *([1] * (x0.dim() - 1))) on a (B,) tensor reshapes it to (B, 1, 1, ...) so it broadcasts against x0 of any rank.

Hint 3

torch.sqrt takes the elementwise square root of a tensor.

Requirements

  • Use PyTorch tensor operations.
  • Broadcast the per-sample factors over every non-batch dimension, whatever the number of dimensions of x0.
  • Leave all input tensors unchanged.

Constraints

  • x0 is a floating-point PyTorch tensor of shape (B, ...) with B at least 1 and at least one non-batch dimension.
  • noise is a floating-point tensor with the same shape and dtype as x0.
  • timesteps is an int64 tensor of shape (B,) with every value between 0 and T - 1.
  • alpha_bars is a floating-point tensor of shape (T,) with T at least 1, values in [0, 1], and the same dtype as x0.
  • Supported dtypes are float32 and float64. All values are finite.
Try Similar Problems
Reparameterized Gaussian SampleEasyDiagonal Gaussian KL DivergenceEasyNoise Prediction LossEasyLinear Beta ScheduleEasyPosterior Mean and VarianceMediumPrediction ParameterizationMediumDDPM Ancestral StepMediumCosine Noise ScheduleMediumRescale to Zero Terminal SNRMediumAutoencoder Forward PassMediumVariational Autoencoder Forward PassMediumGAN Training StepHard

Accepts: array

Accepts: array

Accepts: array

Accepts: array

You must run your code first.

Forward Diffusion Sample

Diffusion ModelsGenerative Models
Easy

Produce the noisy sample xtx_txt​ for every item in a batch by jumping straight from the clean data to its timestep in one step.

xt=αˉt x0+1−αˉt ϵx_t = \sqrt{\bar\alpha_t}\, x_0 + \sqrt{1 - \bar\alpha_t}\, \epsilonxt​=αˉt​​x0​+1−αˉt​​ϵ

Here x0 holds the clean samples with the batch on the first axis, timesteps holds one integer timestep ttt per sample, alpha_bars is the cumulative schedule so that αˉt\bar\alpha_tαˉt​ is alpha_bars at index ttt, and noise holds the Gaussian noise ϵ\epsilonϵ with the same shape as x0. Each sample uses its own timestep, so the two scalar factors differ from one sample to the next.

Return a PyTorch tensor with the same shape, dtype and device as x0.

Examples

Input: x0 = [[[[1.0,-1.0],[0.5,0.0]]],[[[0.2,0.4],[-0.6,0.8]]]], timesteps = [0,3], alpha_bars = [0.9,0.7,0.4,0.1], noise = [[[[0.3,0.1],[-0.2,1.0]]],[[[1.0,-1.0],[0.5,0.0]]]]

Output: [[[[1.043552,-0.91706],[0.411096,0.316228]]],[[[1.011929,-0.822192],[0.284605,0.252982]]]]

Explanation: Here timesteps is an int64 tensor that gives each image its own timestep. The first image uses alpha_bars[0] = 0.9, so it keeps a signal factor of about 0.949 and a noise factor of about 0.316. The second image uses alpha_bars[3] = 0.1, so its noise dominates.

Input: x0 = [[2.0,0.0,-2.0],[1.0,1.0,1.0]], timesteps = [1,1], alpha_bars = [0.99,0.64], noise = [[0.5,0.5,0.5],[-1.0,0.0,1.0]]

Output: [[1.9,0.3,-1.3],[0.2,0.8,1.4]]

Input: x0 = [[3.0,-3.0]], timesteps = [2], alpha_bars = [0.5,0.25,0.0], noise = [[0.7,-0.2]]

Output: [[0.7,-0.2]]

Hint 1

Indexing alpha_bars[timesteps] with the int64 timestep tensor gives one value per sample, with shape (B,).

Hint 2

Calling .view(-1, *([1] * (x0.dim() - 1))) on a (B,) tensor reshapes it to (B, 1, 1, ...) so it broadcasts against x0 of any rank.

Hint 3

torch.sqrt takes the elementwise square root of a tensor.

Requirements

  • Use PyTorch tensor operations.
  • Broadcast the per-sample factors over every non-batch dimension, whatever the number of dimensions of x0.
  • Leave all input tensors unchanged.

Constraints

  • x0 is a floating-point PyTorch tensor of shape (B, ...) with B at least 1 and at least one non-batch dimension.
  • noise is a floating-point tensor with the same shape and dtype as x0.
  • timesteps is an int64 tensor of shape (B,) with every value between 0 and T - 1.
  • alpha_bars is a floating-point tensor of shape (T,) with T at least 1, values in [0, 1], and the same dtype as x0.
  • Supported dtypes are float32 and float64. All values are finite.
Try Similar Problems
Reparameterized Gaussian SampleEasyDiagonal Gaussian KL DivergenceEasyNoise Prediction LossEasyLinear Beta ScheduleEasyPosterior Mean and VarianceMediumPrediction ParameterizationMediumDDPM Ancestral StepMediumCosine Noise ScheduleMediumRescale to Zero Terminal SNRMediumAutoencoder Forward PassMediumVariational Autoencoder Forward PassMediumGAN Training StepHard

Accepts: array

Accepts: array

Accepts: array

Accepts: array

You must run your code first.