-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathBijectorsZygoteExt.jl
More file actions
198 lines (185 loc) · 5.97 KB
/
Copy pathBijectorsZygoteExt.jl
File metadata and controls
198 lines (185 loc) · 5.97 KB
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
module BijectorsZygoteExt
using Zygote: Zygote, @adjoint, pullback
using Bijectors:
Elementwise,
SimplexBijector,
simplex_link_jacobian,
simplex_invlink_jacobian,
simplex_logabsdetjac_gradient,
Inverse,
maphcat,
IrrationalConstants,
Distributions,
logabsdetjac,
_logabsdetjac_scale,
_simplex_bijector,
_simplex_inv_bijector,
replace_diag,
jacobian,
_transform_ordered,
_transform_inverse_ordered,
find_alpha,
pd_logpdf_with_trans,
istraining,
eachcolmaphcat,
sumeachcol,
pd_link,
pd_from_lower,
lower_triangular,
upper_triangular,
getlogp
using Bijectors.LinearAlgebra
using Bijectors.Distributions: LocationScale
@adjoint istraining() = true, _ -> nothing
@adjoint function eachcolmaphcat(f, x1, x2)
function g(f, x1, x2)
init = reshape(f(view(x1, :, 1), x2[1]), :, 1)
return reduce(hcat, [f(view(x1, :, i), x2[i]) for i in 2:size(x1, 2)]; init=init)
end
return pullback(g, f, x1, x2)
end
@adjoint function eachcolmaphcat(f, x)
function g(f, x)
init = reshape(f(view(x, :, 1)), :, 1)
return reduce(hcat, [f(view(x, :, i)) for i in 2:size(x, 2)]; init=init)
end
return pullback(g, f, x)
end
@adjoint function sumeachcol(f, x1, x2)
g(f, x1, x2) = sum([f(view(x1, :, i), x2[i]) for i in 1:size(x1, 2)])
return pullback(g, f, x1, x2)
end
@adjoint function logabsdetjac(b::Elementwise{typeof(log)}, x::AbstractVector)
return -sum(log, x), Δ -> (nothing, -Δ ./ x)
end
# AD implementations
@adjoint function _logabsdetjac_scale(a::Real, x::Real, ::Val{0})
return _logabsdetjac_scale(a, x, Val(0)), Δ -> (inv(a) .* Δ, nothing, nothing)
end
@adjoint function _logabsdetjac_scale(a::Real, x::AbstractVector, ::Val{0})
J = fill(inv.(a), length(x))
return _logabsdetjac_scale(a, x, Val(0)), Δ -> (transpose(J) * Δ, nothing, nothing)
end
@adjoint function _logabsdetjac_scale(a::Real, x::AbstractMatrix, ::Val{0})
J = fill(size(x, 1) / a, size(x, 2))
return _logabsdetjac_scale(a, x, Val(0)), Δ -> (transpose(J) * Δ, nothing, nothing)
end
@adjoint function _logabsdetjac_scale(a::AbstractVector, x::AbstractVector, ::Val{1})
# ∂ᵢ (∑ⱼ log|aⱼ|) = ∑ⱼ δᵢⱼ ∂ᵢ log|aⱼ|
# = ∂ᵢ log |aᵢ|
# = (1 / aᵢ) ∂ᵢ aᵢ
# = (1 / aᵢ)
J = inv.(a)
return _logabsdetjac_scale(a, x, Val(1)), Δ -> (J .* Δ, nothing, nothing)
end
@adjoint function _logabsdetjac_scale(a::AbstractVector, x::AbstractMatrix, ::Val{1})
Jᵀ = repeat(inv.(a), 1, size(x, 2))
return _logabsdetjac_scale(a, x, Val(1)), Δ -> (Jᵀ * Δ, nothing, nothing)
end
## Positive definite matrices
@adjoint function replace_diag(::typeof(log), X)
f(i, j) = i == j ? log(X[i, j]) : X[i, j]
out = f.(1:size(X, 1), (1:size(X, 2))')
return out, ∇ -> begin
g(i, j) = i == j ? ∇[i, j] / X[i, j] : ∇[i, j]
(nothing, g.(1:size(X, 1), (1:size(X, 2))'))
end
end
@adjoint function replace_diag(::typeof(exp), X)
f(i, j) = ifelse(i == j, exp(X[i, j]), X[i, j])
out = f.(1:size(X, 1), (1:size(X, 2))')
return out, ∇ -> begin
g(i, j) = ifelse(i == j, ∇[i, j] * exp(X[i, j]), ∇[i, j])
(nothing, g.(1:size(X, 1), (1:size(X, 2))'))
end
end
@adjoint function pd_logpdf_with_trans(d, X::AbstractMatrix{<:Real}, transform::Bool)
return pullback(pd_logpdf_with_trans_zygote, d, X, transform)
end
function pd_logpdf_with_trans_zygote(d, X::AbstractMatrix{<:Real}, transform::Bool)
T = eltype(X)
Xcf = cholesky(X; check=false)
if !issuccess(Xcf)
Xcf = cholesky(X + max(eps(T), eps(T) * norm(X)) * I; check=true)
end
lp = getlogp(d, Xcf, X)
if transform && isfinite(lp)
factors = Xcf.factors
n = size(d, 1)
k = n + 2
@inbounds for i in diagind(factors)
k -= 1
lp += k * log(factors[i])
end
lp += n * oftype(lp, IrrationalConstants.logtwo)
end
return lp
end
# Simplex adjoints
@adjoint function _simplex_bijector(X::AbstractVector, b::SimplexBijector)
return _simplex_bijector(X, b), Δ -> (simplex_link_jacobian(X)' * Δ, nothing)
end
@adjoint function _simplex_inv_bijector(Y::AbstractVector, b::SimplexBijector)
return _simplex_inv_bijector(Y, b), Δ -> (simplex_invlink_jacobian(Y)' * Δ, nothing)
end
@adjoint function _simplex_bijector(X::AbstractMatrix, b::SimplexBijector)
return _simplex_bijector(X, b),
Δ -> begin
maphcat(eachcol(X), eachcol(Δ)) do c1, c2
simplex_link_jacobian(c1)' * c2
end,
nothing
end
end
@adjoint function _simplex_inv_bijector(Y::AbstractMatrix, b::SimplexBijector)
return _simplex_inv_bijector(Y, b),
Δ -> begin
maphcat(eachcol(Y), eachcol(Δ)) do c1, c2
simplex_invlink_jacobian(c1)' * c2
end,
nothing
end
end
@adjoint function logabsdetjac(b::SimplexBijector, x::AbstractVector)
return logabsdetjac(b, x), Δ -> begin
(nothing, simplex_logabsdetjac_gradient(x) * Δ)
end
end
# LocationScale fix
# TODO: Remove this.
@adjoint function Base.minimum(d::Distributions.LocationScale)
function _minimum(d)
m = minimum(d.ρ)
if isfinite(m)
return d.μ + d.σ * m
else
return m
end
end
return pullback(_minimum, d)
end
@adjoint function Base.maximum(d::LocationScale)
function _maximum(d)
m = maximum(d.ρ)
if isfinite(m)
return d.μ + d.σ * m
else
return m
end
end
return pullback(_maximum, d)
end
@adjoint function pd_from_lower(X::AbstractMatrix)
return LowerTriangular(X) * LowerTriangular(X)',
Δ -> begin
Xl = LowerTriangular(X)
return (LowerTriangular(Δ' * Xl + Δ * Xl),)
end
end
@adjoint function pd_link(X::AbstractMatrix{<:Real})
return pullback(X) do X
Y = cholesky(X; check=true).L
return replace_diag(log, Y)
end
end
end