# Why target is noise data when calculating loss?

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhy-target-is-noise-data-when-calculating-loss%2F64608%3E%5C
**Category:** 🧨 Diffusers\
**Created:** [December 5, 2023, 5:54am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhy-target-is-noise-data-when-calculating-loss%2F64608 "2023-12-05T05:54:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon92153564](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon92153564](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fanon92153564)%5C
**Post date:** [December 5, 2023, 5:54am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhy-target-is-noise-data-when-calculating-loss%2F64608%2F1 "2023-12-05T05:54:59Z")

</div>

I’m studying the learning process. It looks like model\_pred is the result of dinoizing latent with unet.

I don’t know why loss compare this result to noise. Shouldn’t we compare it with the latent encoded by VAE encoder?

And what role is elif’s noise\_scheduler.get\_velocity?

```auto
# Get the target for loss depending on the prediction type
if noise_scheduler.config.prediction_type == "epsilon":
    target = noise
elif noise_scheduler.config.prediction_type == "v_prediction":
    target = noise_scheduler.get_velocity(latents, noise, timesteps)
else:
    raise ValueError(f"Unknown prediction type {noise_scheduler.config.prediction_type}")

# Predict the noise residual and compute loss
'''이게 디노이징이면 타겟은 노이즈 먹이기 전 edit_image를 vae로 인코딩한 값이어야 하지 않는가'''
model_pred = unet(noisy_latents, timesteps, encoder_hidden_states).sample
loss = F.mse_loss(model_pred.float(), target.float(), reduction="mean")

```

---

<div class="post-metadata">

**Author:** ![mrmiller](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/mrmiller/32/23676_2.png) [@mrmiller](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmrmiller)%5C
**Post date:** [February 8, 2024, 1:41am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhy-target-is-noise-data-when-calculating-loss%2F64608%2F2 "2024-02-08T01:41:28Z")

</div>

That noise is based on the latent image.

---

<div class="post-metadata">

**Author:** ![sh-lee](https://avatars.discourse-cdn.com/v4/letter/s/67e7ee/32.png) [@sh-lee](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsh-lee)%5C
**Post date:** [April 18, 2024, 4:30am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhy-target-is-noise-data-when-calculating-loss%2F64608%2F3 "2024-04-18T04:30:09Z")

</div>

Is noise based on the latent image, right?  
Becase above some lines, noise is set to random noise.  
`noise = torch.randn_like(latents)`

---

<div class="post-metadata">

**Author:** ![mrmiller](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/mrmiller/32/23676_2.png) [@mrmiller](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmrmiller)%5C
**Post date:** [April 18, 2024, 5:10am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fwhy-target-is-noise-data-when-calculating-loss%2F64608%2F4 "2024-04-18T05:10:57Z")

</div>

The random noise is added to the image in img2img.
