Compute the mean and variance of the forward process posterior q(xt−1∣xt,x0) for every sample in a batch.
μ=1−αˉtβtαˉt−1x0+1−αˉt(1−αˉt−1)αtxt,β~t=1−αˉt1−αˉt−1βtHere betas holds the per-step variances βt of length T, αt=1−βt, and αˉt is the product of α0 up to αt. The value αˉt−1 is the same product one step earlier, and at t=0 it is defined as 1, which makes β~0=0. The tensors x0 and x_t have shape (B, ...), and timesteps gives each sample its own t, so the two mean coefficients of a sample scale all of its elements.
The log variance must stay finite at t=0. For t≥1 it is logβ~t, and for t=0 it is logβ~1, the log of the variance at the next timestep.
Return a dict with three PyTorch tensors in the dtype and device of x0. The key "mean" holds μ with the shape of x0, "variance" holds β~t of shape (B,), and "log_variance" holds the clipped log variance of shape (B,).
Input: x0 = [[[[1.0,-1.0],[0.5,0.0]]],[[[0.2,0.4],[-0.6,0.8]]]], x_t = [[[[0.8,-0.5],[0.9,0.3]]],[[[1.0,-0.2],[0.1,0.6]]]], timesteps = [1,2], betas = [0.1,0.2,0.3,0.4]
Output: {"mean":[[[[0.933182,-0.83735],[0.62631,0.095831]]],[[[0.574953,0.110827],[-0.260703,0.693963]]]],"variance":[0.071429,0.169355],"log_variance":[-2.639057,-1.775759]}
Explanation: The first image uses t = 1, where alpha_bars is 0.9 at t = 0 and 0.72 at t = 1, so its variance is 0.2 * 0.1 / 0.28, about 0.0714. The second image uses t = 2 and gets its own coefficients, which are broadcast over all of its pixels.
Input: x0 = [[2.0,0.0,-2.0]], x_t = [[1.5,0.4,-1.0]], timesteps = [0], betas = [0.1,0.2,0.3]
Output: {"mean":[[2.0,0.0,-2.0]],"variance":[0.0],"log_variance":[-2.639057]}
Input: x0 = [[1.0,1.0],[-1.0,0.5]], x_t = [[0.0,2.0],[0.3,-0.7]], timesteps = [0,1], betas = [0.05,0.1]
Output: {"mean":[[1.0,1.0],[-0.574053,0.107104]],"variance":[0.0,0.034483],"log_variance":[-3.367296,-3.367296]}
torch.cumprod(1 - betas, dim=0) gives alpha_bar.
torch.cat([torch.ones_like(ab[:1]), ab[:-1]]) gives the previous values.
Indexing with timesteps then .view(-1, *[1] * (x0.dim() - 1)) broadcasts per sample.
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Compute the mean and variance of the forward process posterior q(xt−1∣xt,x0) for every sample in a batch.
μ=1−αˉtβtαˉt−1x0+1−αˉt(1−αˉt−1)αtxt,β~t=1−αˉt1−αˉt−1βtHere betas holds the per-step variances βt of length T, αt=1−βt, and αˉt is the product of α0 up to αt. The value αˉt−1 is the same product one step earlier, and at t=0 it is defined as 1, which makes β~0=0. The tensors x0 and x_t have shape (B, ...), and timesteps gives each sample its own t, so the two mean coefficients of a sample scale all of its elements.
The log variance must stay finite at t=0. For t≥1 it is logβ~t, and for t=0 it is logβ~1, the log of the variance at the next timestep.
Return a dict with three PyTorch tensors in the dtype and device of x0. The key "mean" holds μ with the shape of x0, "variance" holds β~t of shape (B,), and "log_variance" holds the clipped log variance of shape (B,).
Input: x0 = [[[[1.0,-1.0],[0.5,0.0]]],[[[0.2,0.4],[-0.6,0.8]]]], x_t = [[[[0.8,-0.5],[0.9,0.3]]],[[[1.0,-0.2],[0.1,0.6]]]], timesteps = [1,2], betas = [0.1,0.2,0.3,0.4]
Output: {"mean":[[[[0.933182,-0.83735],[0.62631,0.095831]]],[[[0.574953,0.110827],[-0.260703,0.693963]]]],"variance":[0.071429,0.169355],"log_variance":[-2.639057,-1.775759]}
Explanation: The first image uses t = 1, where alpha_bars is 0.9 at t = 0 and 0.72 at t = 1, so its variance is 0.2 * 0.1 / 0.28, about 0.0714. The second image uses t = 2 and gets its own coefficients, which are broadcast over all of its pixels.
Input: x0 = [[2.0,0.0,-2.0]], x_t = [[1.5,0.4,-1.0]], timesteps = [0], betas = [0.1,0.2,0.3]
Output: {"mean":[[2.0,0.0,-2.0]],"variance":[0.0],"log_variance":[-2.639057]}
Input: x0 = [[1.0,1.0],[-1.0,0.5]], x_t = [[0.0,2.0],[0.3,-0.7]], timesteps = [0,1], betas = [0.05,0.1]
Output: {"mean":[[1.0,1.0],[-0.574053,0.107104]],"variance":[0.0,0.034483],"log_variance":[-3.367296,-3.367296]}
torch.cumprod(1 - betas, dim=0) gives alpha_bar.
torch.cat([torch.ones_like(ab[:1]), ab[:-1]]) gives the previous values.
Indexing with timesteps then .view(-1, *[1] * (x0.dim() - 1)) broadcasts per sample.
Accepts: array
Accepts: array
Accepts: array
Accepts: array