-
Notifications
You must be signed in to change notification settings - Fork 104
Add troubleshooting page #603
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
--- | ||
title: Troubleshooting | ||
engine: julia | ||
--- | ||
|
||
```{julia} | ||
#| echo: false | ||
#| output: false | ||
using Pkg; | ||
Pkg.instantiate(); | ||
``` | ||
|
||
This page collects a number of common error messages observed when using Turing, along with suggestions on how to fix them. | ||
|
||
If the suggestions here do not resolve your problem, please do feel free to [open an issue](https://github.com/TuringLang/Turing.jl/issues). | ||
|
||
```{julia} | ||
using Turing | ||
Turing.setprogress!(false) | ||
``` | ||
|
||
## Initial parameters | ||
|
||
> failed to find valid initial parameters in {N} tries. This may indicate an error with the model or AD backend... | ||
|
||
This error is seen when a Hamiltonian Monte Carlo sampler is unable to determine a valid set of initial parameters for the sampling. | ||
Here, 'valid' means that the log probability density of the model, as well as its gradient with respect to each parameter, is finite and not `NaN`. | ||
|
||
### `NaN` gradient | ||
|
||
One of the most common causes of this error is having a `NaN` gradient. | ||
To find out whether this is happening, you can evaluate the gradient manually. | ||
Here is an example with a model that is known to be problematic: | ||
|
||
```{julia} | ||
using Turing | ||
using DynamicPPL.TestUtils.AD: run_ad | ||
|
||
@model function initial_bad() | ||
a ~ Normal() | ||
x ~ truncated(Normal(a), 0, Inf) | ||
end | ||
|
||
model = initial_bad() | ||
adtype = AutoForwardDiff() | ||
result = run_ad(model, adtype; test=false, benchmark=false) | ||
result.grad_actual | ||
``` | ||
|
||
(See [the DynamicPPL docs](https://turinglang.org/DynamicPPL.jl/stable/api/#AD-testing-and-benchmarking-utilities) for more details on the `run_ad` function and its return type.) | ||
|
||
In this case, the `NaN` gradient is caused by the `Inf` argument to `truncated`. | ||
(See, e.g., [this issue on Distributions.jl](https://github.com/JuliaStats/Distributions.jl/issues/1910).) | ||
Here, the upper bound of `Inf` is not needed, so it can be removed: | ||
|
||
```{julia} | ||
@model function initial_good() | ||
a ~ Normal() | ||
x ~ truncated(Normal(a); lower=0) | ||
end | ||
|
||
model = initial_good() | ||
adtype = AutoForwardDiff() | ||
run_ad(model, adtype; test=false, benchmark=false).grad_actual | ||
``` | ||
|
||
More generally, you could try using a different AD backend; if you don't know why a model is returning `NaN` gradients, feel free to open an issue. | ||
|
||
### `-Inf` log density | ||
|
||
Another cause of this error is having models with very extreme parameters. | ||
This example is taken from [this Turing.jl issue](https://github.com/TuringLang/Turing.jl/issues/2476): | ||
|
||
```{julia} | ||
@model function initial_bad2() | ||
x ~ Exponential(100) | ||
y ~ Uniform(0, x) | ||
end | ||
model = initial_bad2() | (y = 50.0,) | ||
|
||
@model function initial_bad3() | ||
x_trf ~ Uniform(0, 1) | ||
x := -log(x_trf) / 100 | ||
@show x | ||
y ~ Uniform(0, x) | ||
penelopeysm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
model3 = initial_bad3() | (y = 50.0,) | ||
``` | ||
|
||
The problem here is that HMC attempts to find initial values for parameters inside the region of `[-2, 2]`, _after_ the parameters have been transformed to unconstrained space. | ||
For a distribution of `Exponential(100)`, the appropriate transformation is `log(x)` (see the [variable transformation docs]({{< meta dev-transforms-distributions >}}) for more info). | ||
|
||
Thus, HMC attempts to find initial values of `log(x)` in the region of `[-2, 2]`, which corresponds to `x` in the region of `[exp(-2), exp(2)]` = `[0.135, 7.39]`. | ||
However, all of these values of `x` will give rise to a zero probability density for `y` because the value of `y = 50.0` is outside the support of `Uniform(0, x)`. | ||
Thus, the log density of the model is `-Inf`, as can be seen with `logjoint`: | ||
|
||
```{julia} | ||
logjoint(model, (x = exp(-2),)) | ||
``` | ||
|
||
```{julia} | ||
logjoint(model, (x = exp(2),)) | ||
``` | ||
|
||
The most direct way of fixing this is to manually provide a set of initial parameters that are valid. | ||
For example, you can obtain a set of initial parameters with `rand(Vector, model)`, and then pass this as the `initial_params` keyword argument to `sample`: | ||
|
||
```{julia} | ||
sample(model, NUTS(), 1000; initial_params=rand(Vector, model)) | ||
``` | ||
|
||
More generally, you may also consider reparameterising the model to avoid such issues. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.