Skip to content

Commit 45c8e45

Browse files
committed
Minor text fixes
1 parent 7f5017d commit 45c8e45

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

developers/models/varinfo-overview/index.qmd

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ latents = rand(Dict, model)
4949
Simply calling `rand(model)`, by default, returns a NamedTuple.
5050
This is fine for simple models where all variables on the left-hand side of tilde statements are standalone variables like `x`.
5151
However, if you have indices or fields such as `x[1]` or `x.a` on the left-hand side, then the NamedTuple will not be able to represent these variables properly.
52-
Feeding such a NamedTuple back into the model, as shown in the next section, will lead to errors.
52+
Feeding such a NamedTuple back into the model will lead to errors.
5353

5454
In general, `Dict{VarName}` will always avoid such correctness issues.
5555
:::
@@ -84,10 +84,10 @@ returned(model, latents)
8484

8585
The above functions are convenient, but for many 'serious' applications they might not be flexible enough.
8686
For example, if you wanted to obtain the return value _and_ the log joint, you would have to execute the model twice: once with `returned` and once with `logjoint`.
87-
If you want to avoid this duplicate work, you need to use a lower-level interface, which is `DynamicPPL.evaluate!!`.
8887

88+
If you want to avoid this duplicate work, you need to use a lower-level interface, which is `DynamicPPL.evaluate!!`.
8989
At its core, `evaluate!!` takes a model and a VarInfo object, and returns a tuple of the return value and the new VarInfo.
90-
So before we get to `evaluate!!`, we need to understand what a VarInfo is.
90+
So, before we even get to `evaluate!!`, we need to understand what a VarInfo is.
9191

9292
A VarInfo is a container that tracks the state of model execution, as well as any outputs related to its latent variables, such as log probabilities.
9393
DynamicPPL's source code contains many different kinds of VarInfos, each with different trade-offs.
@@ -115,7 +115,7 @@ DynamicPPL.getlogprior(v)
115115

116116
What about the return value?
117117
Well, the VarInfo does not store this directly: recall that `evaluate!!` gives us back the return value separately from the updated VarInfo.
118-
So, let's call it.
118+
So, let's try calling it to see what happens.
119119
The default behaviour of `evaluate!!` is to use the parameter values stored in the VarInfo during model execution.
120120
That is, when it sees `x ~ Normal()`, it will use the value of `x` stored in `v`.
121121
We will see later how to change this behaviour.
@@ -133,8 +133,8 @@ vout[@varname(x)] == v[@varname(x)]
133133

134134
which is in line with the statement above that by default `evaluate!!` uses the values stored in the VarInfo.
135135

136-
At this point, the sharp reader will notice that we have not really solved the problem here.
137-
Although the call to `DynamicPPL.evaluate!!` does indeed only execute the model once, we also had to do this once at the beginning when constructing the VarInfo.
136+
At this point, the keen reader will notice that we have not really solved the problem here.
137+
Although the call to `DynamicPPL.evaluate!!` does indeed only execute the model once, we also had to do this once more at the beginning when constructing the VarInfo.
138138

139139
Besides, we don't know how to control the parameter values used during model execution: they were simply whatever we got in the original VarInfo.
140140

@@ -158,7 +158,7 @@ You can also provide an `AbstractRNG` as the first argument to `init!!` to contr
158158
:::
159159

160160
Alternatively, to provide specific sets of values, we can use `InitFromParams(...)` to specify them.
161-
`InitFromParams` can wrap either a `NamedTuple` or an `AbstractDict{<:VarName}`, but for reasons explained above, `Dict` is generally much preferred.
161+
`InitFromParams` can wrap either a `NamedTuple` or an `AbstractDict{<:VarName}`, but `Dict` is generally much preferred as this guarantees correct behaviour even for complex variable names.
162162

163163
```{julia}
164164
retval, v_new = DynamicPPL.init!!(
@@ -181,11 +181,15 @@ If you have a loop in which you want to repeatedly evaluate a model with differe
181181
- First generate a VarInfo using `VarInfo(model)`;
182182
- Then call `DynamicPPL.init!!(model, v, InitFromParams(...))` to evaluate the model using those parameters.
183183

184-
This costs an extra model evaluation at the very beginning to generate the VarInfo, but subsequent evaluations will be efficient.
184+
This requires you to pay a one-time cost at the very beginning to generate the VarInfo, but subsequent evaluations will be efficient.
185+
DynamicPPL uses this approach when implementing functions such as `predict(model, chain)`.
185186

186-
For example, this is how functions like `predict(model, chain)` are implemented.
187+
::: {.callout-tip}
188+
If you want to avoid even the first model evaluation, you will need to read on to the 'Advanced' section below.
189+
However, for most applications this should not necessary.
190+
:::
187191

188-
## `unflatten`
192+
## Parameters in the form of Vectors
189193

190194
In general, one problem with `init!!` is that it is often slower than `evaluate!!`.
191195
This is primarily because it does more work: it has to not only read from the provided parameters, but also overwrite existing values in the VarInfo.
@@ -237,7 +241,7 @@ The inverse operation of `unflatten` is `DynamicPPL.getindex_internal(v, :)`:
237241
DynamicPPL.getindex_internal(v_unflattened, :)
238242
```
239243

240-
## LogDensityFunction
244+
## `LogDensityFunction`
241245

242246
There is one place where `unflatten` is (unfortunately) quite indispensable, namely, the implementation of the LogDensityProblems.jl interface for Turing models.
243247

0 commit comments

Comments
 (0)