Skip to content

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 5 commits into from
Jun 2, 2025
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions usage/troubleshooting/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ If the suggestions here do not resolve your problem, please do feel free to [ope

```{julia}
using Turing
Turing.setprogress!(false)
```

## T0001
## Initial parameters

> failed to find valid initial parameters in {N} tries. This may indicate an error with the model or AD backend...

Expand All @@ -35,12 +36,12 @@ Here is an example with a model that is known to be problematic:
using Turing
using DynamicPPL.TestUtils.AD: run_ad

@model function t0001_bad()
@model function initial_bad()
a ~ Normal()
x ~ truncated(Normal(a), 0, Inf)
end

model = t0001_bad()
model = initial_bad()
adtype = AutoForwardDiff()
result = run_ad(model, adtype; test=false, benchmark=false)
result.grad_actual
Expand All @@ -53,12 +54,12 @@ In this case, the `NaN` gradient is caused by the `Inf` argument to `truncated`.
Here, the upper bound of `Inf` is not needed, so it can be removed:

```{julia}
@model function t0001_good()
@model function initial_good()
a ~ Normal()
x ~ truncated(Normal(a); lower=0)
end

model = t0001_good()
model = initial_good()
adtype = AutoForwardDiff()
run_ad(model, adtype; test=false, benchmark=false).grad_actual
```
Expand All @@ -71,11 +72,19 @@ 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 t0001_bad2()
@model function initial_bad2()
x ~ Exponential(100)
y ~ Uniform(0, x)
end
model = t0001_bad2() | (y = 50.0,)
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)
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.
Expand All @@ -94,5 +103,10 @@ 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(Dict, model)`, and then pass this as the `initial_params` keyword argument to `sample`.
Otherwise, though, you may want to consider reparameterising the model to avoid such issues.
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.
Loading