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

Fix sigma_noise gradient propagation in BaseLaplace.log_likelihood #212

Merged
merged 1 commit into from
Jul 16, 2024

Conversation

kourbou
Copy link
Contributor

@kourbou kourbou commented Jul 16, 2024

Commit 1eea27c introduces type hints in baselaplace.py but breaks the gradient flow through BaseLaplace.log_likelihood(...) since sigma_noise loses its gradient via torch.tensor(...).

This is fixed by using torch.as_tensor(...) as it converts floats but preserves the gradient. (See highlighted note here.)

This change fixes the UserWarning below and passes type checking with Pylance.

laplace/baselaplace.py:226: UserWarning: To copy construct from a tensor, it is recommended to use
[...], rather than torch.tensor(sourceTensor).

Commit 1eea27c introduced type hints in baselaplace.py but broke the
gradient flow through `BaseLaplace.log_likelihood` since `sigma_noise`
loses its gradient via `torch.tensor(...)`. This is fixed by using
`torch.as_tensor(...)` as it converts floats but preserves the gradient.
@aleximmer
Copy link
Owner

Thanks a lot for bisecting the issue and fixing it as well.

@aleximmer aleximmer merged commit 508843d into aleximmer:main Jul 16, 2024
3 checks passed
@kourbou kourbou deleted the fix-sigma-noise-grad branch July 16, 2024 16:14
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

Successfully merging this pull request may close these issues.

2 participants