-
Notifications
You must be signed in to change notification settings - Fork 32
/
composed.jl
233 lines (181 loc) · 7.12 KB
/
composed.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
###############
# Composition #
###############
"""
Composed(ts::A)
∘(b1::Bijector{N}, b2::Bijector{N})::Composed{<:Tuple}
composel(ts::Bijector{N}...)::Composed{<:Tuple}
composer(ts::Bijector{N}...)::Composed{<:Tuple}
where `A` refers to either
- `Tuple{Vararg{<:Bijector{N}}}`: a tuple of bijectors of dimensionality `N`
- `AbstractArray{<:Bijector{N}}`: an array of bijectors of dimensionality `N`
A `Bijector` representing composition of bijectors. `composel` and `composer` results in a
`Composed` for which application occurs from left-to-right and right-to-left, respectively.
Note that all the alternative ways of constructing a `Composed` returns a `Tuple` of bijectors.
This ensures type-stability of implementations of all relating methdos, e.g. `inv`.
If you want to use an `Array` as the container instead you can do
Composed([b1, b2, ...])
In general this is not advised since you lose type-stability, but there might be cases
where this is desired, e.g. if you have a insanely large number of bijectors to compose.
# Examples
## Simple example
Let's consider a simple example of `Exp`:
```julia-repl
julia> using Bijectors: Exp
julia> b = Exp()
Exp{0}()
julia> b ∘ b
Composed{Tuple{Exp{0},Exp{0}},0}((Exp{0}(), Exp{0}()))
julia> (b ∘ b)(1.0) == exp(exp(1.0)) # evaluation
true
julia> inv(b ∘ b)(exp(exp(1.0))) == 1.0 # inversion
true
julia> logabsdetjac(b ∘ b, 1.0) # determinant of jacobian
3.718281828459045
```
# Notes
## Order
It's important to note that `∘` does what is expected mathematically, which means that the
bijectors are applied to the input right-to-left, e.g. first applying `b2` and then `b1`:
```julia
(b1 ∘ b2)(x) == b1(b2(x)) # => true
```
But in the `Composed` struct itself, we store the bijectors left-to-right, so that
```julia
cb1 = b1 ∘ b2 # => Composed.ts == (b2, b1)
cb2 = composel(b2, b1) # => Composed.ts == (b2, b1)
cb1(x) == cb2(x) == b1(b2(x)) # => true
```
## Structure
`∘` will result in "flatten" the composition structure while `composel` and
`composer` preserve the compositional structure. This is most easily seen by an example:
```julia-repl
julia> b = Exp()
Exp{0}()
julia> cb1 = b ∘ b; cb2 = b ∘ b;
julia> (cb1 ∘ cb2).ts # <= different
(Exp{0}(), Exp{0}(), Exp{0}(), Exp{0}())
julia> (cb1 ∘ cb2).ts isa NTuple{4, Exp{0}}
true
julia> Bijectors.composer(cb1, cb2).ts
(Composed{Tuple{Exp{0},Exp{0}},0}((Exp{0}(), Exp{0}())), Composed{Tuple{Exp{0},Exp{0}},0}((Exp{0}(), Exp{0}())))
julia> Bijectors.composer(cb1, cb2).ts isa Tuple{Composed, Composed}
true
```
"""
struct Composed{A, N} <: Bijector{N}
ts::A
end
@functor Composed
Composed(bs::Tuple{Vararg{<:Bijector{N}}}) where N = Composed{typeof(bs),N}(bs)
Composed(bs::AbstractArray{<:Bijector{N}}) where N = Composed{typeof(bs),N}(bs)
isclosedform(b::Composed) = all(isclosedform, b.ts)
up1(b::Composed) = Composed(up1.(b.ts))
function Base.:(==)(b1::Composed{<:Any, N}, b2::Composed{<:Any, N}) where {N}
ts1, ts2 = b1.ts, b2.ts
return length(ts1) == length(ts2) && all(x == y for (x, y) in zip(ts1, ts2))
end
"""
composel(ts::Bijector...)::Composed{<:Tuple}
Constructs `Composed` such that `ts` are applied left-to-right.
"""
composel(ts::Bijector{N}...) where {N} = Composed(ts)
"""
composer(ts::Bijector...)::Composed{<:Tuple}
Constructs `Composed` such that `ts` are applied right-to-left.
"""
composer(ts::Bijector{N}...) where {N} = Composed(reverse(ts))
# The transformation of `Composed` applies functions left-to-right
# but in mathematics we usually go from right-to-left; this reversal ensures that
# when we use the mathematical composition ∘ we get the expected behavior.
# TODO: change behavior of `transform` of `Composed`?
@generated function ∘(b1::Bijector{N1}, b2::Bijector{N2}) where {N1, N2}
if N1 == N2
return :(composel(b2, b1))
else
return :(throw(DimensionMismatch("$(typeof(b1)) expects $(N1)-dim but $(typeof(b2)) expects $(N2)-dim")))
end
end
# type-stable composition rules
∘(b1::Composed{<:Tuple}, b2::Bijector) = composel(b2, b1.ts...)
∘(b1::Bijector, b2::Composed{<:Tuple}) = composel(b2.ts..., b1)
∘(b1::Composed{<:Tuple}, b2::Composed{<:Tuple}) = composel(b2.ts..., b1.ts...)
# type-unstable composition rules
∘(b1::Composed{<:AbstractArray}, b2::Bijector) = Composed(pushfirst!(copy(b1.ts), b2))
∘(b1::Bijector, b2::Composed{<:AbstractArray}) = Composed(push!(copy(b2.ts), b1))
function ∘(b1::Composed{<:AbstractArray}, b2::Composed{<:AbstractArray})
return Composed(append!(copy(b2.ts), copy(b1.ts)))
end
# if combining type-unstable and type-stable, return type-unstable
function ∘(b1::T1, b2::T2) where {T1<:Composed{<:Tuple}, T2<:Composed{<:AbstractArray}}
error("Cannot compose compositions of different container-types; ($T1, $T2)")
end
function ∘(b1::T1, b2::T2) where {T1<:Composed{<:AbstractArray}, T2<:Composed{<:Tuple}}
error("Cannot compose compositions of different container-types; ($T1, $T2)")
end
∘(::Identity{N}, ::Identity{N}) where {N} = Identity{N}()
∘(::Identity{N}, b::Bijector{N}) where {N} = b
∘(b::Bijector{N}, ::Identity{N}) where {N} = b
inv(ct::Composed) = Composed(reverse(map(inv, ct.ts)))
# # TODO: should arrays also be using recursive implementation instead?
function (cb::Composed{<:AbstractArray{<:Bijector}})(x)
@assert length(cb.ts) > 0
res = cb.ts[1](x)
for b ∈ Base.Iterators.drop(cb.ts, 1)
res = b(res)
end
return res
end
@generated function (cb::Composed{T})(x) where {T<:Tuple}
@assert length(T.parameters) > 0
expr = :(x)
for i in 1:length(T.parameters)
expr = :(cb.ts[$i]($expr))
end
return expr
end
function logabsdetjac(cb::Composed, x)
y, logjac = forward(cb.ts[1], x)
for i = 2:length(cb.ts)
res = forward(cb.ts[i], y)
y = res.rv
logjac += res.logabsdetjac
end
return logjac
end
@generated function logabsdetjac(cb::Composed{T}, x) where {T<:Tuple}
N = length(T.parameters)
expr = Expr(:block)
push!(expr.args, :((y, logjac) = forward(cb.ts[1], x)))
for i = 2:N - 1
temp = gensym(:res)
push!(expr.args, :($temp = forward(cb.ts[$i], y)))
push!(expr.args, :(y = $temp.rv))
push!(expr.args, :(logjac += $temp.logabsdetjac))
end
# don't need to evaluate the last bijector, only it's `logabsdetjac`
push!(expr.args, :(logjac += logabsdetjac(cb.ts[$N], y)))
push!(expr.args, :(return logjac))
return expr
end
function forward(cb::Composed, x)
rv, logjac = forward(cb.ts[1], x)
for t in cb.ts[2:end]
res = forward(t, rv)
rv = res.rv
logjac = res.logabsdetjac + logjac
end
return (rv=rv, logabsdetjac=logjac)
end
@generated function forward(cb::Composed{T}, x) where {T<:Tuple}
expr = Expr(:block)
push!(expr.args, :((y, logjac) = forward(cb.ts[1], x)))
for i = 2:length(T.parameters)
temp = gensym(:temp)
push!(expr.args, :($temp = forward(cb.ts[$i], y)))
push!(expr.args, :(y = $temp.rv))
push!(expr.args, :(logjac += $temp.logabsdetjac))
end
push!(expr.args, :(return (rv = y, logabsdetjac = logjac)))
return expr
end