You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/getting_started.md
+8-8
Original file line number
Diff line number
Diff line change
@@ -19,16 +19,16 @@ Let's write a short Gen program that does Bayesian linear regression: given a se
19
19
20
20
There are three main components to a typical Gen program.
21
21
22
-
First, we define a _generative model_: a Julia function, extended with some extra syntax, that, conceptually, simulates a fake dataset. The model below samples `slope` and `intercept` parameters, and then for each of the x-coordinates that it accepts as input, samples a corresponding y-coordinate. We name the random choices we make with `@addr`, so we can refer to them in our inference program.
22
+
First, we define a _generative model_: a Julia function, extended with some extra syntax, that, conceptually, simulates a fake dataset. The model below samples `slope` and `intercept` parameters, and then for each of the x-coordinates that it accepts as input, samples a corresponding y-coordinate. We name the random choices we make with `@trace`, so we can refer to them in our inference program.
23
23
24
24
```julia
25
25
using Gen
26
26
27
27
@genfunctionmy_model(xs::Vector{Float64})
28
-
slope =@addr(normal(0, 2), :slope)
29
-
intercept =@addr(normal(0, 10), :intercept)
28
+
slope =@trace(normal(0, 2), :slope)
29
+
intercept =@trace(normal(0, 10), :intercept)
30
30
for (i, x) inenumerate(xs)
31
-
@addr(normal(slope * x + intercept, 1), "y-$i")
31
+
@trace(normal(slope * x + intercept, 1), "y-$i")
32
32
end
33
33
end
34
34
```
@@ -42,14 +42,14 @@ The inference program below takes in a data set, and runs an iterative MCMC algo
Maps from the addresses of random choices to their values are stored in associative tree-structured data structures that have the following abstract type:
4
4
```@docs
5
-
Assignment
5
+
ChoiceMap
6
6
```
7
7
8
-
Assignments are constructed by users to express observations and/or constraints on the traces of generative functions.
9
-
Assignments are also returned by certain Gen inference methods, and are used internally by various Gen inference methods.
8
+
Choice maps are constructed by users to express observations and/or constraints on the traces of generative functions.
9
+
Choice maps are also returned by certain Gen inference methods, and are used internally by various Gen inference methods.
10
10
11
-
Assignments provide the following methods:
11
+
Choice maps provide the following methods:
12
12
```@docs
13
13
has_value
14
14
get_value
15
-
get_subassmt
15
+
get_submap
16
16
get_values_shallow
17
-
get_subassmts_shallow
17
+
get_submaps_shallow
18
18
to_array
19
19
from_array
20
20
address_set
21
21
```
22
22
Note that none of these methods mutate the assignment.
23
23
24
-
Assignments also provide `Base.isempty`, which tests of there are no random
24
+
Choice maps also provide `Base.isempty`, which tests of there are no random
25
25
choices in the assignment, and `Base.merge`, which takes two assignments, and
26
26
returns a new assignment containing all random choices in either assignment.
27
27
It is an error if the assignments both have values at the same address, or if
28
28
one assignment has a value at an address that is the prefix of the address of a
29
29
value in the other assignment.
30
30
31
31
32
-
## Dynamic Assignment
32
+
## Dynamic Choice Map
33
33
34
-
One concrete assignment type is `DynamicAssignment`, which is mutable.
35
-
Users construct `DynamicAssignments` and populate them for use as observations or constraints, e.g.:
34
+
One concrete assignment type is `DynamicChoiceMap`, which is mutable.
35
+
Users construct `DynamicChoiceMaps` and populate them for use as observations or constraints, e.g.:
36
36
```julia
37
-
assmt=DynamicAssignment()
38
-
assmt[:x] =true
39
-
assmt["foo"] =1.25
40
-
assmt[:y=>1=>:z] =-6.3
37
+
choices=choicemap()
38
+
choices[:x] =true
39
+
choices["foo"] =1.25
40
+
choices[:y=>1=>:z] =-6.3
41
41
```
42
42
43
-
There is also a constructor for `DynamicAssignment` that takes initial (address, value) pairs:
43
+
There is also a constructor for `DynamicChoiceMap` that takes initial (address, value) pairs:
This causes `foo` to be invoked twice, once with arguments `(0.0, 0.5)` in address namespace `1` and once with arguments `(0.5, 1.0)` in address namespace `2`.
37
37
If the resulting trace has random choices:
@@ -95,7 +95,7 @@ The initial state is denoted ``y_0``, the number of applications is ``n``, and t
95
95
For example, consider the following kernel, with state type `Bool`, which makes one random choice at address `:z`:
Copy file name to clipboardExpand all lines: docs/src/ref/gfi.md
+34-34
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ There are various kinds of generative functions, which are represented by concre
12
12
For example, the [Built-in Modeling Language](@ref) allows generative functions to be constructed using Julia function definition syntax:
13
13
```julia
14
14
@genfunctionfoo(a, b)
15
-
if@addr(bernoulli(0.5), :z)
15
+
if@trace(bernoulli(0.5), :z)
16
16
return a + b +1
17
17
else
18
18
return a + b
@@ -34,18 +34,18 @@ We represent the randomness used during an execution of a generative function as
34
34
In this section, we assume that random choices are discrete to simplify notation.
35
35
We say that two random choice maps ``t`` and ``s``**agree** if they assign the same value for any address that is in both of their domains.
36
36
37
-
Generative functions may also use **non-addressed randomness**, which is not included in the map ``t``.
38
-
However, the state of non-addressed random choices *is* maintained by the trace internally.
39
-
We denote non-addressed randomness by ``r``.
40
-
Non-addressed randomness is useful for example, when calling black box Julia code that implements a randomized algorithm.
37
+
Generative functions may also use **untraced randomness**, which is not included in the map ``t``.
38
+
However, the state of untraced random choices *is* maintained by the trace internally.
39
+
We denote untraced randomness by ``r``.
40
+
Untraced randomness is useful for example, when calling black box Julia code that implements a randomized algorithm.
41
41
42
42
The observable behavior of every generative function is defined by the following mathematical objects:
43
43
44
44
### 1. Input type
45
45
The set of valid argument tuples to the function, denoted ``X``.
46
46
47
47
### 2. Probability distribution family
48
-
A family of probability distributions ``p(t, r; x)`` on maps ``t`` from random choice addresses to their values, and non-addressed randomness ``r``, indexed by arguments ``x``, for all ``x \in X``.
48
+
A family of probability distributions ``p(t, r; x)`` on maps ``t`` from random choice addresses to their values, and untraced randomness ``r``, indexed by arguments ``x``, for all ``x \in X``.
49
49
Note that the distribution must be normalized:
50
50
```math
51
51
\sum_{t, r} p(t, r; x) = 1 \;\; \mbox{for all} \;\; x \in X
@@ -55,14 +55,14 @@ We use ``p(t; x)`` to denote the marginal distribution on the map ``t``:
55
55
```math
56
56
p(t; x) := \sum_{r} p(t, r; x)
57
57
```
58
-
And we denote the conditional distribution on non-addressed randomness ``r``, given the map ``t``, as:
58
+
And we denote the conditional distribution on untraced randomness ``r``, given the map ``t``, as:
59
59
```math
60
60
p(r; x, t) := p(t, r; x) / p(t; x)
61
61
```
62
62
63
63
### 3. Return value function
64
64
A (deterministic) function ``f`` that maps the tuple ``(x, t)`` of the arguments and the random choice map to the return value of the function (which we denote by ``y``).
65
-
Note that the return value cannot depend on the non-addressed randomness.
65
+
Note that the return value cannot depend on the untraced randomness.
66
66
67
67
### 4. Internal proposal distribution family
68
68
A family of probability distributions ``q(t; x, u)`` on maps ``t`` from random choice addresses to their values, indexed by tuples ``(x, u)`` where ``u`` is a map from random choice addresses to values, and where ``x`` are the arguments to the function.
@@ -76,7 +76,7 @@ p(t; x) > 0 \mbox{ if and only if } q(t; x, u) > 0 \mbox{ for all } u \mbox{ whe
76
76
```math
77
77
q(t; x, u) > 0 \mbox{ implies that } u \mbox{ and } t \mbox{ agree }.
78
78
```
79
-
There is also a family of probability distributions ``q(r; x, t)`` on non-addressed randomness, that satisfies:
79
+
There is also a family of probability distributions ``q(r; x, t)`` on untraced randomness, that satisfies:
80
80
```math
81
81
q(r; x, t) > 0 \mbox{ if and only if } p(r; x, t) > 0
82
82
```
@@ -107,7 +107,7 @@ get_retval
107
107
108
108
The map ``t`` from addresses of random choices to their values:
109
109
```@docs
110
-
get_assmt
110
+
get_choices
111
111
```
112
112
113
113
The log probability that the random choices took the values they did:
@@ -128,13 +128,13 @@ There are several methods that take a trace of a generative function as input an
128
128
We will illustrate these methods using the following generative function:
129
129
```julia
130
130
@genfunctionfoo()
131
-
val =@addr(bernoulli(0.3), :a)
132
-
if@addr(bernoulli(0.4), :b)
133
-
val =@addr(bernoulli(0.6), :c) && val
131
+
val =@trace(bernoulli(0.3), :a)
132
+
if@trace(bernoulli(0.4), :b)
133
+
val =@trace(bernoulli(0.6), :c) && val
134
134
else
135
-
val =@addr(bernoulli(0.1), :d) && val
135
+
val =@trace(bernoulli(0.1), :d) && val
136
136
end
137
-
val =@addr(bernoulli(0.7), :e) && val
137
+
val =@trace(bernoulli(0.7), :e) && val
138
138
return val
139
139
end
140
140
```
@@ -151,22 +151,22 @@ Suppose we have a trace (`trace`) with initial choices:
151
151
```
152
152
Note that address `:d` is not present because the branch in which `:d` is sampled was not taken because random choice `:b` had value `true`.
153
153
154
-
### Force Update
154
+
### Update
155
155
```@docs
156
-
force_update
156
+
update
157
157
```
158
-
Suppose we run [`force_update`](@ref) on the example `trace`, with the following constraints:
158
+
Suppose we run [`update`](@ref) on the example `trace`, with the following constraints:
Then, a new value for `:a` will be sampled from `bernoulli(0.3)`, and a new value for `:b` will be sampled from `bernoulli(0.4)`.
204
204
If the new value for `:b` is `true`, then the previous value for `:c` (`false`) will be retained.
205
205
If the new value for `:b` is `false`, then a new value for `:d` will be sampled from `bernoulli(0.7)`.
206
206
The previous value for `:c` will always be retained.
207
207
Suppose the new value for `:a` is `true`, and the new value for `:b` is `true`.
208
-
Then `get_assmt(new_trace)` will be:
208
+
Then `get_choices(new_trace)` will be:
209
209
```
210
210
│
211
211
├── :a : true
@@ -219,9 +219,9 @@ Then `get_assmt(new_trace)` will be:
219
219
The weight (`w`) is ``\log 1 = 0``.
220
220
221
221
222
-
### Extend
222
+
### Extend update
223
223
```@docs
224
-
extend
224
+
extend_update
225
225
```
226
226
227
227
### Argdiffs
@@ -270,8 +270,8 @@ This static property of the generative function is reported by `accepts_output_g
270
270
```@docs
271
271
has_argument_grads
272
272
accepts_output_grad
273
-
backprop_params
274
-
backprop_trace
273
+
accumulate_param_gradients!
274
+
choice_gradients
275
275
get_params
276
276
```
277
277
@@ -320,19 +320,19 @@ Then, the argument tuple passed to e.g. [`initialize`](@ref) will have two eleme
320
320
NOTE: Be careful to distinguish between arguments to the generative function itself, and arguments to the constructor of the generative function.
321
321
For example, if you have a generative function type that is parametrized by, for example, modeling DSL code, this DSL code would be a parameter of the generative function constructor.
322
322
323
-
### Decide what the addressed random choices (if any) will be
323
+
### Decide what the traced random choices (if any) will be
324
324
Remember that each random choice is assigned a unique address in (possibly) hierarchical address space.
325
325
You are free to design this address space as you wish, although you should document it for users of your generative function type.
326
326
327
327
### Implement the methods of the interface
328
328
329
329
- At minimum, you need to implement all methods under the [`Traces`](@ref) heading (e.g. [`initialize`](@ref), ..)
330
330
331
-
- To support [`metropolis_hastings`](@ref) or local optimization, or local iterative adjustments to traces, be sure to implement the [`force_update`](@ref) and [`free_update](@ref) methods.
331
+
- To support [`metropolis_hastings`](@ref) or local optimization, or local iterative adjustments to traces, be sure to implement the [`update`](@ref) and [`regenerate`](@ref) methods.
332
332
333
-
- To support gradients of the log probability density with respect to the arguments and/or random choices made by the function, implement the [`backprop_trace`](@ref) method.
333
+
- To support gradients of the log probability density with respect to the arguments and/or random choices made by the function, implement the [`choice_gradients`](@ref) method.
334
334
335
-
- Generative functions can also have trainable parameters (e.g. neural network weights). To support these, implement the [`backprop_params`](@ref) method.
335
+
- Generative functions can also have trainable parameters (e.g. neural network weights). To support these, implement the [`accumulate_param_gradients!`](@ref) method.
336
336
337
337
- To support use of your generative function in custom proposals (instead of just generative models), implement [`assess`](@ref) and [`propose`](@ref) methods.
0 commit comments