|
| 1 | +################################## |
| 2 | +# define affine coupling layer using Bijectors.jl interface |
| 3 | +################################# |
| 4 | +struct AffineCoupling <: Bijectors.Bijector |
| 5 | + dim::Int |
| 6 | + mask::Bijectors.PartitionMask |
| 7 | + s::Flux.Chain |
| 8 | + t::Flux.Chain |
| 9 | +end |
| 10 | + |
| 11 | +# let params track field s and t |
| 12 | +@functor AffineCoupling (s, t) |
| 13 | + |
| 14 | +function AffineCoupling( |
| 15 | + dim::Int, # dimension of input |
| 16 | + hdims::Int, # dimension of hidden units for s and t |
| 17 | + mask_idx::AbstractVector, # index of dimensione that one wants to apply transformations on |
| 18 | +) |
| 19 | + cdims = length(mask_idx) # dimension of parts used to construct coupling law |
| 20 | + s = mlp3(cdims, hdims, cdims) |
| 21 | + t = mlp3(cdims, hdims, cdims) |
| 22 | + mask = PartitionMask(dim, mask_idx) |
| 23 | + return AffineCoupling(dim, mask, s, t) |
| 24 | +end |
| 25 | + |
| 26 | +function Bijectors.transform(af::AffineCoupling, x::AbstractVecOrMat) |
| 27 | + # partition vector using 'af.mask::PartitionMask` |
| 28 | + x₁, x₂, x₃ = partition(af.mask, x) |
| 29 | + y₁ = x₁ .* af.s(x₂) .+ af.t(x₂) |
| 30 | + return combine(af.mask, y₁, x₂, x₃) |
| 31 | +end |
| 32 | + |
| 33 | +function (af::AffineCoupling)(x::AbstractArray) |
| 34 | + return transform(af, x) |
| 35 | +end |
| 36 | + |
| 37 | +function Bijectors.with_logabsdet_jacobian(af::AffineCoupling, x::AbstractVector) |
| 38 | + x_1, x_2, x_3 = Bijectors.partition(af.mask, x) |
| 39 | + y_1 = af.s(x_2) .* x_1 .+ af.t(x_2) |
| 40 | + logjac = sum(log ∘ abs, af.s(x_2)) # this is a scalar |
| 41 | + return combine(af.mask, y_1, x_2, x_3), logjac |
| 42 | +end |
| 43 | + |
| 44 | +function Bijectors.with_logabsdet_jacobian(af::AffineCoupling, x::AbstractMatrix) |
| 45 | + x_1, x_2, x_3 = Bijectors.partition(af.mask, x) |
| 46 | + y_1 = af.s(x_2) .* x_1 .+ af.t(x_2) |
| 47 | + logjac = sum(log ∘ abs, af.s(x_2); dims = 1) # 1 × size(x, 2) |
| 48 | + return combine(af.mask, y_1, x_2, x_3), vec(logjac) |
| 49 | +end |
| 50 | + |
| 51 | + |
| 52 | +function Bijectors.with_logabsdet_jacobian( |
| 53 | + iaf::Inverse{<:AffineCoupling}, y::AbstractVector |
| 54 | +) |
| 55 | + af = iaf.orig |
| 56 | + # partition vector using `af.mask::PartitionMask` |
| 57 | + y_1, y_2, y_3 = partition(af.mask, y) |
| 58 | + # inverse transformation |
| 59 | + x_1 = (y_1 .- af.t(y_2)) ./ af.s(y_2) |
| 60 | + logjac = -sum(log ∘ abs, af.s(y_2)) |
| 61 | + return combine(af.mask, x_1, y_2, y_3), logjac |
| 62 | +end |
| 63 | + |
| 64 | +function Bijectors.with_logabsdet_jacobian( |
| 65 | + iaf::Inverse{<:AffineCoupling}, y::AbstractMatrix |
| 66 | +) |
| 67 | + af = iaf.orig |
| 68 | + # partition vector using `af.mask::PartitionMask` |
| 69 | + y_1, y_2, y_3 = partition(af.mask, y) |
| 70 | + # inverse transformation |
| 71 | + x_1 = (y_1 .- af.t(y_2)) ./ af.s(y_2) |
| 72 | + logjac = -sum(log ∘ abs, af.s(y_2); dims = 1) |
| 73 | + return combine(af.mask, x_1, y_2, y_3), vec(logjac) |
| 74 | +end |
| 75 | + |
| 76 | +################### |
| 77 | +# an equivalent definition of AffineCoupling using Bijectors.Coupling |
| 78 | +# (see https://github.com/TuringLang/Bijectors.jl/blob/74d52d4eda72a6149b1a89b72524545525419b3f/src/bijectors/coupling.jl#L188C1-L188C1) |
| 79 | +################### |
| 80 | + |
| 81 | +# struct AffineCoupling <: Bijectors.Bijector |
| 82 | +# dim::Int |
| 83 | +# mask::Bijectors.PartitionMask |
| 84 | +# s::Flux.Chain |
| 85 | +# t::Flux.Chain |
| 86 | +# end |
| 87 | + |
| 88 | +# # let params track field s and t |
| 89 | +# @functor AffineCoupling (s, t) |
| 90 | + |
| 91 | +# function AffineCoupling(dim, mask, s, t) |
| 92 | +# return Bijectors.Coupling(θ -> Bijectors.Shift(t(θ)) ∘ Bijectors.Scale(s(θ)), mask) |
| 93 | +# end |
| 94 | + |
| 95 | +# function AffineCoupling( |
| 96 | +# dim::Int, # dimension of input |
| 97 | +# hdims::Int, # dimension of hidden units for s and t |
| 98 | +# mask_idx::AbstractVector, # index of dimensione that one wants to apply transformations on |
| 99 | +# ) |
| 100 | +# cdims = length(mask_idx) # dimension of parts used to construct coupling law |
| 101 | +# s = mlp3(cdims, hdims, cdims) |
| 102 | +# t = mlp3(cdims, hdims, cdims) |
| 103 | +# mask = PartitionMask(dim, mask_idx) |
| 104 | +# return AffineCoupling(dim, mask, s, t) |
| 105 | +# end |
| 106 | + |
0 commit comments