Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about the KL divergence loss #849

Open
marctimjen opened this issue May 8, 2024 · 1 comment
Open

Question about the KL divergence loss #849

marctimjen opened this issue May 8, 2024 · 1 comment

Comments

@marctimjen
Copy link

Hello

I hope someone can help me understand why the KL is calculated as:
0.5 * torch.sum(torch.pow(self.mean, 2) + self.var - 1.0 - self.logvar, dim=[1, 2, 3])

In the DiagonalGaussianDistribution listed here:

return 0.5 * torch.sum(torch.pow(self.mean, 2)

I am asking, because most loss functions for the VAE I can find use (-1 times this calculations) like this: 0.5 * torch.sum(-torch.pow(self.mean, 2) - self.var + 1.0 + self.logvar, dim=[1, 2, 3])

And I cannot see that we multiply by -1 in the contperceptual loss for instance:

https://github.com/CompVis/stable-diffusion/blob/21f890f9da3cfbeaba8e2ac3c425ee9e998d5229/ldm/modules/losses/contperceptual.py#L83C57-L83C65

Thank you very much in advance :)

@marctimjen
Copy link
Author

I found this material that does also have the loss on the form that is used here:

https://pyimagesearch.com/2023/10/02/a-deep-dive-into-variational-autoencoders-with-pytorch/

My confusion just happen because most papers write:

image

(From Bishops Deep learning)

And the original implementation of VAE:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant