Skip to content

Commit f43bb30

Browse files
committed
Add troubleshooting page
1 parent dd621b2 commit f43bb30

File tree

3 files changed

+112
-11
lines changed

3 files changed

+112
-11
lines changed

_quarto.yml

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ website:
6666
- usage/sampler-visualisation/index.qmd
6767
- usage/dynamichmc/index.qmd
6868
- usage/external-samplers/index.qmd
69+
- usage/troubleshooting/index.qmd
6970

7071
- section: "Tutorials"
7172
contents:
@@ -181,17 +182,19 @@ probabilistic-pca: tutorials/11-probabilistic-pca
181182
gplvm: tutorials/12-gplvm
182183
seasonal-time-series: tutorials/13-seasonal-time-series
183184
using-turing-advanced: tutorials/docs-09-using-turing-advanced
184-
using-turing-autodiff: tutorials/docs-10-using-turing-autodiff
185-
using-turing-dynamichmc: tutorials/docs-11-using-turing-dynamichmc
186185
using-turing: tutorials/docs-12-using-turing-guide
187-
using-turing-performance-tips: tutorials/docs-13-using-turing-performance-tips
188-
using-turing-sampler-viz: tutorials/docs-15-using-turing-sampler-viz
189-
using-turing-external-samplers: tutorials/docs-16-using-turing-external-samplers
190-
using-turing-mode-estimation: tutorials/docs-17-mode-estimation
191-
usage-probability-interface: tutorials/usage-probability-interface
192-
usage-custom-distribution: tutorials/usage-custom-distribution
193-
usage-tracking-extra-quantities: tutorials/tracking-extra-quantities
194-
usage-modifying-logprob: tutorials/usage-modifying-logprob
186+
187+
usage-automatic-differentiation: usage/automatic-differentiation
188+
usage-custom-distribution: usage/custom-distribution
189+
usage-dynamichmc: usage/dynamichmc
190+
usage-external-samplers: usage/external-samplers
191+
usage-mode-estimation: usage/mode-estimation
192+
usage-modifying-logprob: usage/modifying-logprob
193+
usage-performance-tips: usage/performance-tips
194+
usage-probability-interface: usage/probability-interface
195+
usage-sampler-visualisation: usage/sampler-visualisation
196+
usage-tracking-extra-quantities: usage/tracking-extra-quantities
197+
usage-troubleshooting: usage/troubleshooting
195198

196199
contributing-guide: developers/contributing
197200
dev-model-manual: developers/compiler/model-manual

usage/performance-tips/index.qmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ supports several AD backends, including [ForwardDiff](https://github.com/JuliaDi
5252

5353
For many common types of models, the default ForwardDiff backend performs great, and there is no need to worry about changing it. However, if you need more speed, you can try
5454
different backends via the standard [ADTypes](https://github.com/SciML/ADTypes.jl) interface by passing an `AbstractADType` to the sampler with the optional `adtype` argument, e.g.
55-
`NUTS(adtype = AutoZygote())`. See [Automatic Differentiation]({{<meta using-turing-autodiff>}}) for details. Generally, `adtype = AutoForwardDiff()` is likely to be the fastest and most reliable for models with
55+
`NUTS(adtype = AutoZygote())`. See [Automatic Differentiation]({{<meta usage-automatic-differentiation>}}) for details. Generally, `adtype = AutoForwardDiff()` is likely to be the fastest and most reliable for models with
5656
few parameters (say, less than 20 or so), while reverse-mode backends such as `AutoZygote()` or `AutoReverseDiff()` will perform better for models with many parameters or linear algebra
5757
operations. If in doubt, it's easy to try a few different backends to see how they compare.
5858

usage/troubleshooting/index.qmd

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
---
2+
title: Troubleshooting
3+
engine: julia
4+
---
5+
6+
```{julia}
7+
#| echo: false
8+
#| output: false
9+
using Pkg;
10+
Pkg.instantiate();
11+
```
12+
13+
This page collects a number of common error messages observed when using Turing, along with suggestions on how to fix them.
14+
15+
If the suggestions here do not resolve your problem, please do feel free to [open an issue](https://github.com/TuringLang/Turing.jl/issues).
16+
17+
```{julia}
18+
using Turing
19+
```
20+
21+
## T0001
22+
23+
> failed to find valid initial parameters in {N} tries. This may indicate an error with the model or AD backend...
24+
25+
This error is seen when a Hamiltonian Monte Carlo sampler is unable to determine a valid set of initial parameters for the sampling.
26+
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`.
27+
28+
### `NaN` gradient
29+
30+
One of the most common causes of this error is having a `NaN` gradient.
31+
To find out whether this is happening, you can evaluate the gradient manually.
32+
Here is an example with a model that is known to be problematic:
33+
34+
```{julia}
35+
using Turing
36+
using DynamicPPL.TestUtils.AD: run_ad
37+
38+
@model function t0001_bad()
39+
a ~ Normal()
40+
x ~ truncated(Normal(a), 0, Inf)
41+
end
42+
43+
model = t0001_bad()
44+
adtype = AutoForwardDiff()
45+
result = run_ad(model, adtype; test=false, benchmark=false)
46+
result.grad_actual
47+
```
48+
49+
(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.)
50+
51+
In this case, the `NaN` gradient is caused by the `Inf` argument to `truncated`.
52+
(See, e.g., [this issue on Distributions.jl](https://github.com/JuliaStats/Distributions.jl/issues/1910).)
53+
Here, the upper bound of `Inf` is not needed, so it can be removed:
54+
55+
```{julia}
56+
@model function t0001_good()
57+
a ~ Normal()
58+
x ~ truncated(Normal(a); lower=0)
59+
end
60+
61+
model = t0001_good()
62+
adtype = AutoForwardDiff()
63+
run_ad(model, adtype; test=false, benchmark=false).grad_actual
64+
```
65+
66+
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.
67+
68+
### `-Inf` log density
69+
70+
Another cause of this error is having models with very extreme parameters.
71+
This example is taken from [this Turing.jl issue](https://github.com/TuringLang/Turing.jl/issues/2476):
72+
73+
```{julia}
74+
@model function t0001_bad2()
75+
x ~ Exponential(100)
76+
y ~ Uniform(0, x)
77+
end
78+
model = t0001_bad2() | (y = 50.0,)
79+
```
80+
81+
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.
82+
For a distribution of `Exponential(100)`, the appropriate transformation is `log(x)` (see the [variable transformation docs]({{< meta dev-transforms-distributions >}}) for more info).
83+
84+
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]`.
85+
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)`.
86+
Thus, the log density of the model is `-Inf`, as can be seen with `logjoint`:
87+
88+
```{julia}
89+
logjoint(model, (x = exp(-2),))
90+
```
91+
92+
```{julia}
93+
logjoint(model, (x = exp(2),))
94+
```
95+
96+
The most direct way of fixing this is to manually provide a set of initial parameters that are valid.
97+
For example, you can obtain a set of initial parameters with `rand(Dict, model)`, and then pass this as the `initial_params` keyword argument to `sample`.
98+
Otherwise, though, you may want to consider reparameterising the model to avoid such issues.

0 commit comments

Comments
 (0)