|
| 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