Why is the loss of Diffusion model calculated between "RANDOM noise" and "model predicted noise"? Not between "Actual added noise" and "model predicted noise"?
image1920×1208 121 KBWhy is the loss of Diffusion model calculated between“RANDOM noise” and “model predicted noise”? Not between “Actual added noise” and “model predicted noise”?When i trained the U-Net with loss between “the Actual added noise” and “model predicted noise”,it seems the model be optimized much much faster on my training dataset.May I use this loss ?Anybody has insight ?
2023-05-29T17:24:35Z
sorry I’m not sure I understand. what is the alternative noise you’re talking about?
2023-05-30T00:35:50Z
In the above picture,“noise” is randomly sampled noiseby“noise = torch.randn(sample_image.shape)”and is used for loss calculation.But I think “Actually added noise” should be used for loss calculation.the noise added between the “t-1” step and “t” step.Why we are using random noise for loss calculation?
2023-05-30T00:50:23Z
I trained U-Net using the loss with“the actual added noise”that is,the noise added between “t-1” step and “t” step, NOT “random noise”.then U-Net seems to be optimized faster.Whywe are using“random noise”for diffusion loss calculation andhow this be possible?
2023-05-30T05:23:52Z
The training process is like this:we generate a random noise vector with std=1we scale it down to have std<1we add the scaled down noise to the imagewe predict the noise with std=1If I understand correctly, you are asking why we are predicting the noise with std=1 instead of the one with std<1, right?There are many ways to formulate the output of the model:predict the noise with std=1 (normalized noise)predict the noise with std<1 (actual added noise)predict the original clean image itselfYou can actually try all these formulations. In fact, someone has tried it using the 3rd formulation on a toy project and it also works. Check the 2nd notebook in this repo here:diffusion-models-class/unit1 at main · huggingface/diffusion-models-class · GitHubBut I think the 1st formulation has advantage that it’s making the low noise prediction more important than 2nd formulation.Even if the actual added noise has std only 0.01, the 1st approach would still predict it as std=1, therefore making it 100x more important in the loss. This should result in a model that cares a lot about denoising correctly at the last few inference steps.Another advantange is probably about forcing the model to always predict noise that has std=1, this might help the model stabilize? I’m not sure about this reasoning though.In short, it’s all about the trade-offs on the model performance. Research is still going on about which formulation is the best.
2023-06-01T02:09:54Z
You may have no idea how much you’ve helped me.and I bet there will be many who wonder this one as well.Hope they find the repo you mentioned.Thanks a lot,@offchanGood luck !
2023-06-02T13:19:21Z
Another advantange is probably about forcing the model to always predict noise that has std=1, this might help the model stabilize? I’m not sure about this reasoning though.Intuitively, this makes a lot of sense. It makes the model output scaling independent of the timestep, right?
2023-06-17T16:19:54Z
Yeah, that’s what I think.
2023-11-22T02:51:20Z
Hi I have read the solutions and I am still confused about it. Why we want to use unet to predict the random noise, which is irrelevant with timesteps. If we need the prediction of random noise, why can’t we directly generate a random noise in the reverse process?
2023-11-23T07:35:56Z
Same question. Have you found the explanation yet?
2023-11-27T03:30:33Z
@offchanHi sorry to bother, I am appreciate your answer for this topic.  However,  I am still confused about it. Why we want to use unet to predict the random noise, which is irrelevant with timesteps. If we need the prediction of random noise, why can’t we directly generate a random noise in the reverse process?
2023-11-27T13:31:55Z
Each training step is like this:we have a clean image from the training data.we generate a random gaussian noise with std=1. let’s call itbig noisewe scale down the big noise based on timestep. For early timesteps, we’ll scale the big noise down just a little bit. For late timesteps, we’ll scale the big noise down a lot. Now the std will be less than 1. Let’s call the scaled noise assmall noise.we add small noise to the clean image, resulting in anoisy imagewe train the unet to predictbig noisegivennoisy imageandtimestepas input.Notice in the last step, we are asking the model to predict the big noise we have calculated in step 2. We are not asking it to predict a new random noise.And timestep is indeed relevant in step 3. One crucial detail here is that timestep is randomly sampled. It’s not sampled sequentially in a for loop like our intuition might suggest. You can read more about the logic behind random timestep in myother answer hereI urge you to look at the training code to gain deeper understanding.Here’s the code from the text-to-image training script by diffusers:github.comhuggingface/diffusers/blob/ebf581e85f3aad7faa30ceb4678148ee87375446/examples/text_to_image/train_text_to_image.py#L893-L912# Sample noise that we'll add to the latentsnoise = torch.randn_like(latents)if args.noise_offset:# https://www.crosslabs.org//blog/diffusion-with-offset-noisenoise += args.noise_offset * torch.randn((latents.shape[0], latents.shape[1], 1, 1), device=latents.device)if args.input_perturbation:new_noise = noise + args.input_perturbation * torch.randn_like(noise)bsz = latents.shape[0]# Sample a random timestep for each imagetimesteps = torch.randint(0, noise_scheduler.config.num_train_timesteps, (bsz,), device=latents.device)timesteps = timesteps.long()# Add noise to the latents according to the noise magnitude at each timestep# (this is the forward diffusion process)if args.input_perturbation:noisy_latents = noise_scheduler.add_noise(latents, new_noise, timesteps)else:noisy_latents = noise_scheduler.add_noise(latents, noise, timesteps)The clean image is namedlatentsand the big noise is namednoisein the code above. But the small noise is not shown here. The line that callsadd_noise()function is internally computing the small noise and produces noisy image which is namednoisy_latentsabove.
2023-11-27T13:39:55Z
Check the answer above.