|
| 1 | +using MonotonicSplines |
| 2 | + |
| 3 | +struct NSC{T,A<:Flux.Chain} <: Bijectors.Bijector |
| 4 | + dim::Int # dimension of input |
| 5 | + K::Int # number of knots |
| 6 | + n_dims_transferred::Int # number of dimensions that are transformed |
| 7 | + B::T # bound of the knots |
| 8 | + nn::A # networks that parmaterize the knots and derivatives |
| 9 | + mask::Bijectors.PartitionMask |
| 10 | +end |
| 11 | + |
| 12 | +function NSC( |
| 13 | + dim::T1, # dimension of input |
| 14 | + hdims::AbstractVector{T1}, # dimension of hidden units for s and t |
| 15 | + K::T1, # number of knots |
| 16 | + B::T2, # bound of the knots |
| 17 | + mask_idx::AbstractVector{T1}, # index of dimensione that one wants to apply transformations on |
| 18 | + paramtype::Type{T2}, # type of the parameters, e.g., Float64 or Float32 |
| 19 | +) where {T1<:Int,T2<:AbstractFloat} |
| 20 | + num_of_transformed_dims = length(mask_idx) |
| 21 | + input_dims = dim - num_of_transformed_dims |
| 22 | + |
| 23 | + # output dim of the NN |
| 24 | + output_dims = (3K - 1)*num_of_transformed_dims |
| 25 | + # one big mlp that outputs all the knots and derivatives for all the transformed dimensions |
| 26 | + nn = fnn(input_dims, hdims, output_dims; output_activation=nothing, paramtype=paramtype) |
| 27 | + |
| 28 | + mask = Bijectors.PartitionMask(dim, mask_idx) |
| 29 | + return NSC{T2, typeof(nn)}(dim, K, num_of_transformed_dims, B, nn, mask) |
| 30 | +end |
| 31 | + |
| 32 | +@functor NSC (nn,) |
| 33 | + |
| 34 | +function get_nsl_params(nsl::NSC, x::AbstractVecOrMat) |
| 35 | + nnoutput = nsl.nn(x) |
| 36 | + px, py, dydx = MonotonicSplines.rqs_params_from_nn(nnoutput, nsl.n_dims_transferred, nsl.B) |
| 37 | + return px, py, dydx |
| 38 | +end |
| 39 | + |
| 40 | +function Bijectors.transform(nsl::NSC, x::AbstractVecOrMat) |
| 41 | + x1, x2, x3 = Bijectors.partition(nsl.mask, x) |
| 42 | + # instantiate rqs knots and derivatives |
| 43 | + px, py, dydx = get_nsl_params(nsl, x2) |
| 44 | + if x1 isa AbstractVector |
| 45 | + x1 = reshape(x1, 1, length(x1)) # ensure x1 is a matrix |
| 46 | + end |
| 47 | + y1, _ = MonotonicSplines.rqs_forward(x1, px, py, dydx) |
| 48 | + return Bijectors.combine(nsl.mask, y1, x2, x3) |
| 49 | +end |
| 50 | + |
| 51 | +function Bijectors.with_logabsdet_jacobian(nsl::NSC, x::AbstractVecOrMat) |
| 52 | + x1, x2, x3 = Bijectors.partition(nsl.mask, x) |
| 53 | + # instantiate rqs knots and derivatives |
| 54 | + px, py, dydx = get_nsl_params(nsl, x2) |
| 55 | + y1, logjac = MonotonicSplines.rqs_forward(x1, px, py, dydx) |
| 56 | + return Bijectors.combine(nsl.mask, y1, x2, x3), vec(logjac) |
| 57 | +end |
| 58 | + |
| 59 | +function Bijectors.transform(insl::Inverse{<:NSC}, y::AbstractVecOrMat) |
| 60 | + nsl = insl.orig |
| 61 | + y1, y2, y3 = partition(nsl.mask, y) |
| 62 | + px, py, dydx = get_nsl_params(nsl, y2) |
| 63 | + x1, _ = MonotonicSplines.rqs_inverse(y1, px, py, dydx) |
| 64 | + return Bijectors.combine(nsl.mask, x1, y2, y3) |
| 65 | +end |
| 66 | + |
| 67 | +function Bijectors.with_logabsdet_jacobian(insl::Inverse{<:NSC}, y::AbstractVecOrMat) |
| 68 | + nsl = insl.orig |
| 69 | + y1, y2, y3 = partition(nsl.mask, y) |
| 70 | + px, py, dydx = get_nsl_params(nsl, y2) |
| 71 | + x1, logjac = MonotonicSplines.rqs_inverse(y1, px, py, dydx) |
| 72 | + return Bijectors.combine(nsl.mask, x1, y2, y3), logjac isa Real ? logjac : vec(logjac) |
| 73 | +end |
| 74 | + |
| 75 | +function (nsl::NSC)(x::AbstractVecOrMat) |
| 76 | + return Bijectors.transform(nsl, x) |
| 77 | +end |
| 78 | + |
| 79 | + |
| 80 | +function new_NSF_layer( |
| 81 | + dims::T1, # dimension of problem |
| 82 | + hdims::AbstractVector{T1}, # dimension of hidden units for nn |
| 83 | + K::T1, # number of knots |
| 84 | + B::T2; # bound of the knots |
| 85 | + paramtype::Type{T2} = Float64, # type of the parameters |
| 86 | +) where {T1<:Int,T2<:AbstractFloat} |
| 87 | + |
| 88 | + mask_idx1 = 1:2:dims |
| 89 | + mask_idx2 = 2:2:dims |
| 90 | + |
| 91 | + # by default use the odd-even masking strategy |
| 92 | + nsf1 = NSC(dims, hdims, K, B, mask_idx1, paramtype) |
| 93 | + nsf2 = NSC(dims, hdims, K, B, mask_idx2, paramtype) |
| 94 | + return reduce(∘, (nsf1, nsf2)) |
| 95 | +end |
| 96 | + |
| 97 | +function new_nsf( |
| 98 | + q0::Distribution{Multivariate,Continuous}, |
| 99 | + hdims::AbstractVector{Int}, # dimension of hidden units for s and t |
| 100 | + K::Int, |
| 101 | + B::T, |
| 102 | + nlayers::Int; # number of RealNVP_layer |
| 103 | + paramtype::Type{T} = Float64, # type of the parameters |
| 104 | +) where {T<:AbstractFloat} |
| 105 | + |
| 106 | + dims = length(q0) # dimension of the reference distribution == dim of the problem |
| 107 | + Ls = [new_NSF_layer(dims, hdims, K, B; paramtype=paramtype) for _ in 1:nlayers] |
| 108 | + create_flow(Ls, q0) |
| 109 | +end |
| 110 | + |
| 111 | +new_nsf(q0; paramtype::Type{T} = Float64) where {T<:AbstractFloat} = new_nsf( |
| 112 | + q0, [32, 32], 10, 30*one(T), 10; paramtype=paramtype |
| 113 | +) |
0 commit comments